mirror of
https://gitee.com/yansongda/pay.git
synced 2024-12-02 04:08:21 +08:00
v3
This commit is contained in:
parent
fc309c0bb1
commit
5c6234e316
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017 yansongda <me@yansongda.cn>
|
||||
Copyright (c) yansongda <me@yansongda.cn>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
|
307
README.md
307
README.md
@ -1,307 +0,0 @@
|
||||
<h1 align="center">Pay</h1>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://styleci.io/repos/100355112"><img src="https://styleci.io/repos/100355112/shield?branch=master" alt="StyleCI"></a>
|
||||
<a href="https://scrutinizer-ci.com/g/yansongda/pay/?branch=master"><img src="https://scrutinizer-ci.com/g/yansongda/pay/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality"></a>
|
||||
<a href="https://scrutinizer-ci.com/g/yansongda/pay/build-status/master"><img src="https://scrutinizer-ci.com/g/yansongda/pay/badges/build.png?b=master" alt="Build Status"></a>
|
||||
<a href="https://packagist.org/packages/yansongda/pay"><img src="https://poser.pugx.org/yansongda/pay/v/stable" alt="Latest Stable Version"></a>
|
||||
<a href="https://packagist.org/packages/yansongda/pay"><img src="https://poser.pugx.org/yansongda/pay/downloads" alt="Total Downloads"></a>
|
||||
<a href="https://packagist.org/packages/yansongda/pay"><img src="https://poser.pugx.org/yansongda/pay/v/unstable" alt="Latest Unstable Version"></a>
|
||||
<a href="https://packagist.org/packages/yansongda/pay"><img src="https://poser.pugx.org/yansongda/pay/license" alt="License"></a>
|
||||
</p>
|
||||
|
||||
该文档为 v2.x 版本,如果您想找 v1.x 版本文档,请点击[https://github.com/yansongda/pay/tree/v1.x](https://github.com/yansongda/pay/tree/v1.x)
|
||||
|
||||
**注意:v1.x 与 v2.x 版本不兼容**
|
||||
|
||||
|
||||
开发了多次支付宝与微信支付后,很自然产生一种反感,惰性又来了,想在网上找相关的轮子,可是一直没有找到一款自己觉得逞心如意的,要么使用起来太难理解,要么文件结构太杂乱,只有自己撸起袖子干了。
|
||||
|
||||
**!!请先熟悉 支付宝/微信 说明文档!!请具有基本的 debug 能力!!**
|
||||
|
||||
欢迎 Star,欢迎 PR!
|
||||
|
||||
laravel 扩展包请 [传送至这里](https://github.com/yansongda/laravel-pay)
|
||||
|
||||
QQ交流群:690027516
|
||||
|
||||
## 特点
|
||||
- 丰富的事件系统
|
||||
- 命名不那么乱七八糟
|
||||
- 隐藏开发者不需要关注的细节
|
||||
- 根据支付宝、微信最新 API 开发而成
|
||||
- 高度抽象的类,免去各种拼json与xml的痛苦
|
||||
- 符合 PSR 标准,你可以各种方便的与你的框架集成
|
||||
- 文件结构清晰易理解,可以随心所欲添加本项目中没有的支付网关
|
||||
- 方法使用更优雅,不必再去研究那些奇怪的的方法名或者类名是做啥用的
|
||||
|
||||
|
||||
## 运行环境
|
||||
- PHP 7.0+ (v2.8.0 开始 >= 7.1.3)
|
||||
- composer
|
||||
|
||||
> php5 请使用 v1.x 版本[https://github.com/yansongda/pay/tree/v1.x](https://github.com/yansongda/pay/tree/v1.x)
|
||||
|
||||
|
||||
## 支持的支付方法
|
||||
### 1、支付宝
|
||||
- 电脑支付
|
||||
- 手机网站支付
|
||||
- APP 支付
|
||||
- 刷卡支付
|
||||
- 扫码支付
|
||||
- 账户转账
|
||||
- 小程序支付
|
||||
|
||||
| method | 描述 |
|
||||
| :-------: | :-------: |
|
||||
| web | 电脑支付 |
|
||||
| wap | 手机网站支付 |
|
||||
| app | APP 支付 |
|
||||
| pos | 刷卡支付 |
|
||||
| scan | 扫码支付 |
|
||||
| transfer | 帐户转账 |
|
||||
| mini | 小程序支付 |
|
||||
|
||||
### 2、微信
|
||||
- 公众号支付
|
||||
- 小程序支付
|
||||
- H5 支付
|
||||
- 扫码支付
|
||||
- 刷卡支付
|
||||
- APP 支付
|
||||
- 企业付款
|
||||
- 普通红包
|
||||
- 分裂红包
|
||||
|
||||
| method | 描述 |
|
||||
| :-----: | :-------: |
|
||||
| mp | 公众号支付 |
|
||||
| miniapp | 小程序支付 |
|
||||
| wap | H5 支付 |
|
||||
| scan | 扫码支付 |
|
||||
| pos | 刷卡支付 |
|
||||
| app | APP 支付 |
|
||||
| transfer | 企业付款 |
|
||||
| redpack | 普通红包 |
|
||||
| groupRedpack | 分裂红包 |
|
||||
|
||||
## 支持的方法
|
||||
所有网关均支持以下方法
|
||||
|
||||
- find(array/string $order)
|
||||
说明:查找订单接口
|
||||
参数:`$order` 为 `string` 类型时,请传入系统订单号,对应支付宝或微信中的 `out_trade_no`; `array` 类型时,参数请参考支付宝或微信官方文档。
|
||||
返回:查询成功,返回 `Yansongda\Supports\Collection` 实例,可以通过 `$colletion->xxx` 或 `$collection['xxx']` 访问服务器返回的数据。
|
||||
异常:`GatewayException` 或 `InvalidSignException`
|
||||
|
||||
- refund(array $order)
|
||||
说明:退款接口
|
||||
参数:`$order` 数组格式,退款参数。
|
||||
返回:退款成功,返回 `Yansongda\Supports\Collection` 实例,可以通过 `$colletion->xxx` 或 `$collection['xxx']` 访问服务器返回的数据。
|
||||
异常:`GatewayException` 或 `InvalidSignException`
|
||||
|
||||
- cancel(array/string $order)
|
||||
说明:取消订单接口
|
||||
参数:`$order` 为 `string` 类型时,请传入系统订单号,对应支付宝或微信中的 `out_trade_no`; `array` 类型时,参数请参考支付宝或微信官方文档。
|
||||
返回:取消成功,返回 `Yansongda\Supports\Collection` 实例,可以通过 `$colletion->xxx` 或 `$collection['xxx']` 访问服务器返回的数据。
|
||||
异常:`GatewayException` 或 `InvalidSignException`
|
||||
|
||||
- close(array/string $order)
|
||||
说明:关闭订单接口
|
||||
参数:`$order` 为 `string` 类型时,请传入系统订单号,对应支付宝或微信中的 `out_trade_no`; `array` 类型时,参数请参考支付宝或微信官方文档。
|
||||
返回:关闭成功,返回 `Yansongda\Supports\Collection` 实例,可以通过 `$colletion->xxx` 或 `$collection['xxx']` 访问服务器返回的数据。
|
||||
异常:`GatewayException` 或 `InvalidSignException`
|
||||
|
||||
- verify()
|
||||
说明:验证服务器返回消息是否合法
|
||||
返回:验证成功,返回 `Yansongda\Supports\Collection` 实例,可以通过 `$colletion->xxx` 或 `$collection['xxx']` 访问服务器返回的数据。
|
||||
异常:`GatewayException` 或 `InvalidSignException`
|
||||
|
||||
- PAYMETHOD(array $order)
|
||||
说明:进行支付;具体支付方法名称请参考「支持的支付方法」一栏
|
||||
返回:成功,返回 `Yansongda\Supports\Collection` 实例,可以通过 `$colletion->xxx` 或 `$collection['xxx']` 访问服务器返回的数据或 `Symfony\Component\HttpFoundation\Response` 实例,可通过 `return $response->send()`(laravel 框架中直接 `return $response`) 返回,具体请参考文档。
|
||||
异常:`GatewayException` 或 `InvalidSignException`
|
||||
|
||||
## 安装
|
||||
```shell
|
||||
composer require yansongda/pay -vvv
|
||||
```
|
||||
|
||||
## 使用说明
|
||||
|
||||
### 支付宝
|
||||
```php
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Yansongda\Pay\Pay;
|
||||
use Yansongda\Pay\Log;
|
||||
|
||||
class PayController
|
||||
{
|
||||
protected $config = [
|
||||
'app_id' => '2016082000295641',
|
||||
'notify_url' => 'http://yansongda.cn/notify.php',
|
||||
'return_url' => 'http://yansongda.cn/return.php',
|
||||
'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuWJKrQ6SWvS6niI+4vEVZiYfjkCfLQfoFI2nCp9ZLDS42QtiL4Ccyx8scgc3nhVwmVRte8f57TFvGhvJD0upT4O5O/lRxmTjechXAorirVdAODpOu0mFfQV9y/T9o9hHnU+VmO5spoVb3umqpq6D/Pt8p25Yk852/w01VTIczrXC4QlrbOEe3sr1E9auoC7rgYjjCO6lZUIDjX/oBmNXZxhRDrYx4Yf5X7y8FRBFvygIE2FgxV4Yw+SL3QAa2m5MLcbusJpxOml9YVQfP8iSurx41PvvX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u2Om+aTzhK2PI6WTDVTKAJBYegXaahBCqVbSxieR62IWtmOMjggTtAKWZ1P5LQcRwdkaB2rAoGAWnAPT318Kp7YcDx8whOzMGnxqtCc24jvk2iSUZgb2Dqv+3zCOTF6JUsV0Guxu5bISoZ8GdfSFKf5gBAo97sGFeuUBMsHYPkcLehM1FmLZk1Q+ljcx3P1A/ds3kWXLolTXCrlpvNMBSN5NwOKAyhdPK/qkvnUrfX8sJ5XK2H4J8ECgYAGIZ0HIiE0Y+g9eJnpUFelXvsCEUW9YNK4065SD/BBGedmPHRC3OLgbo8X5A9BNEf6vP7fwpIiRfKhcjqqzOuk6fueA/yvYD04v+Da2MzzoS8+hkcqF3T3pta4I4tORRdRfCUzD80zTSZlRc/h286Y2eTETd+By1onnFFe2X01mwKBgQDaxo4PBcLL2OyVT5DoXiIdTCJ8KNZL9+kV1aiBuOWxnRgkDjPngslzNa1bK+klGgJNYDbQqohKNn1HeFX3mYNfCUpuSnD2Yag53Dd/1DLO+NxzwvTu4D6DCUnMMMBVaF42ig31Bs0jI3JQZVqeeFzSET8fkoFopJf3G6UXlrIEAQ==',
|
||||
'log' => [ // optional
|
||||
'file' => './logs/alipay.log',
|
||||
'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
|
||||
'type' => 'single', // optional, 可选 daily.
|
||||
'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
|
||||
],
|
||||
'http' => [ // optional
|
||||
'timeout' => 5.0,
|
||||
'connect_timeout' => 5.0,
|
||||
// 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
|
||||
],
|
||||
'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
|
||||
];
|
||||
|
||||
public function index()
|
||||
{
|
||||
$order = [
|
||||
'out_trade_no' => time(),
|
||||
'total_amount' => '1',
|
||||
'subject' => 'test subject - 测试',
|
||||
];
|
||||
|
||||
$alipay = Pay::alipay($this->config)->web($order);
|
||||
|
||||
return $alipay->send();// laravel 框架中请直接 `return $alipay`
|
||||
}
|
||||
|
||||
public function return()
|
||||
{
|
||||
$data = Pay::alipay($this->config)->verify(); // 是的,验签就这么简单!
|
||||
|
||||
// 订单号:$data->out_trade_no
|
||||
// 支付宝交易号:$data->trade_no
|
||||
// 订单总金额:$data->total_amount
|
||||
}
|
||||
|
||||
public function notify()
|
||||
{
|
||||
$alipay = Pay::alipay($this->config);
|
||||
|
||||
try{
|
||||
$data = $alipay->verify(); // 是的,验签就这么简单!
|
||||
|
||||
// 请自行对 trade_status 进行判断及其它逻辑进行判断,在支付宝的业务通知中,只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时,支付宝才会认定为买家付款成功。
|
||||
// 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号;
|
||||
// 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额);
|
||||
// 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email);
|
||||
// 4、验证app_id是否为该商户本身。
|
||||
// 5、其它业务逻辑情况
|
||||
|
||||
Log::debug('Alipay notify', $data->all());
|
||||
} catch (\Exception $e) {
|
||||
// $e->getMessage();
|
||||
}
|
||||
|
||||
return $alipay->success()->send();// laravel 框架中请直接 `return $alipay->success()`
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 微信
|
||||
```php
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Yansongda\Pay\Pay;
|
||||
use Yansongda\Pay\Log;
|
||||
|
||||
class PayController
|
||||
{
|
||||
protected $config = [
|
||||
'appid' => 'wxb3fxxxxxxxxxxx', // APP APPID
|
||||
'app_id' => 'wxb3fxxxxxxxxxxx', // 公众号 APPID
|
||||
'miniapp_id' => 'wxb3fxxxxxxxxxxx', // 小程序 APPID
|
||||
'mch_id' => '14577xxxx',
|
||||
'key' => 'mF2suE9sU6Mk1Cxxxxxxxxxxx',
|
||||
'notify_url' => 'http://yanda.net.cn/notify.php',
|
||||
'cert_client' => './cert/apiclient_cert.pem', // optional,退款等情况时用到
|
||||
'cert_key' => './cert/apiclient_key.pem',// optional,退款等情况时用到
|
||||
'log' => [ // optional
|
||||
'file' => './logs/wechat.log',
|
||||
'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
|
||||
'type' => 'single', // optional, 可选 daily.
|
||||
'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
|
||||
],
|
||||
'http' => [ // optional
|
||||
'timeout' => 5.0,
|
||||
'connect_timeout' => 5.0,
|
||||
// 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
|
||||
],
|
||||
'mode' => 'dev', // optional, dev/hk;当为 `hk` 时,为香港 gateway。
|
||||
];
|
||||
|
||||
public function index()
|
||||
{
|
||||
$order = [
|
||||
'out_trade_no' => time(),
|
||||
'total_fee' => '1', // **单位:分**
|
||||
'body' => 'test body - 测试',
|
||||
'openid' => 'onkVf1FjWS5SBIixxxxxxx',
|
||||
];
|
||||
|
||||
$pay = Pay::wechat($this->config)->mp($order);
|
||||
|
||||
// $pay->appId
|
||||
// $pay->timeStamp
|
||||
// $pay->nonceStr
|
||||
// $pay->package
|
||||
// $pay->signType
|
||||
}
|
||||
|
||||
public function notify()
|
||||
{
|
||||
$pay = Pay::wechat($this->config);
|
||||
|
||||
try{
|
||||
$data = $pay->verify(); // 是的,验签就这么简单!
|
||||
|
||||
Log::debug('Wechat notify', $data->all());
|
||||
} catch (\Exception $e) {
|
||||
// $e->getMessage();
|
||||
}
|
||||
|
||||
return $pay->success()->send();// laravel 框架中请直接 `return $pay->success()`
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 事件系统
|
||||
[请见详细文档](http://pay.yansongda.cn)
|
||||
|
||||
## 详细文档
|
||||
[详细说明文档](http://pay.yansongda.cn)
|
||||
|
||||
## 错误
|
||||
如果在调用相关支付网关 API 时有错误产生,会抛出 `GatewayException`,`InvalidSignException` 错误,可以通过 `$e->getMessage()` 查看,同时,也可通过 `$e->raw` 查看调用 API 后返回的原始数据,该值为数组格式。
|
||||
|
||||
### 所有异常
|
||||
|
||||
* Yansongda\Pay\Exceptions\InvalidGatewayException ,表示使用了除本 SDK 支持的支付网关。
|
||||
* Yansongda\Pay\Exceptions\InvalidSignException ,表示验签失败。
|
||||
* Yansongda\Pay\Exceptions\InvalidConfigException ,表示缺少配置参数,如,`ali_public_key`, `private_key` 等。
|
||||
* Yansongda\Pay\Exceptions\GatewayException ,表示支付宝/微信服务器返回的数据非正常结果,例如,参数错误,对账单不存在等。
|
||||
|
||||
|
||||
## 代码贡献
|
||||
由于测试及使用环境的限制,本项目中只开发了「支付宝」和「微信支付」的相关支付网关。
|
||||
|
||||
如果您有其它支付网关的需求,或者发现本项目中需要改进的代码,**_欢迎 Fork 并提交 PR!_**
|
||||
|
||||
## 赏一杯咖啡吧
|
||||
|
||||
![pay](https://pay.yanda.net.cn/images/pay.jpg)
|
||||
|
||||
## LICENSE
|
||||
MIT
|
@ -21,7 +21,8 @@
|
||||
"ext-json": "*",
|
||||
"yansongda/supports": "^1.8",
|
||||
"symfony/http-foundation": "^4.0",
|
||||
"symfony/event-dispatcher": "^4.0"
|
||||
"symfony/event-dispatcher": "^4.0",
|
||||
"pimple/pimple": "^3.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^7.5",
|
||||
|
@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Tests\Gateways;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Yansongda\Pay\Pay;
|
||||
use Yansongda\Pay\Tests\TestCase;
|
||||
|
||||
class AlipayTest extends TestCase
|
||||
{
|
||||
public function testSuccess()
|
||||
{
|
||||
$success = Pay::alipay([])->success();
|
||||
|
||||
$this->assertInstanceOf(Response::class, $success);
|
||||
$this->assertEquals('success', $success->getContent());
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Tests;
|
||||
|
||||
use Yansongda\Pay\Contracts\GatewayApplicationInterface;
|
||||
use Yansongda\Pay\Exceptions\InvalidGatewayException;
|
||||
use Yansongda\Pay\Gateways\Alipay;
|
||||
use Yansongda\Pay\Gateways\Wechat;
|
||||
use Yansongda\Pay\Pay;
|
||||
|
||||
class PayTest extends TestCase
|
||||
{
|
||||
public function testAlipayGateway()
|
||||
{
|
||||
$alipay = Pay::alipay(['foo' => 'bar']);
|
||||
|
||||
$this->assertInstanceOf(Alipay::class, $alipay);
|
||||
$this->assertInstanceOf(GatewayApplicationInterface::class, $alipay);
|
||||
}
|
||||
|
||||
public function testWechatGateway()
|
||||
{
|
||||
$wechat = Pay::wechat(['foo' => 'bar']);
|
||||
|
||||
$this->assertInstanceOf(Wechat::class, $wechat);
|
||||
$this->assertInstanceOf(GatewayApplicationInterface::class, $wechat);
|
||||
}
|
||||
|
||||
public function testFooGateway()
|
||||
{
|
||||
$this->expectException(InvalidGatewayException::class);
|
||||
$this->expectExceptionMessage('INVALID_GATEWAY: Gateway [foo] Not Exists');
|
||||
|
||||
Pay::foo([]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user