winwin/pay-sdk

There is no license information available for the latest version (0.2.0) of this package.

Installs: 5 786

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 5

Forks: 0

Open Issues: 0

Type:project

0.2.0 2018-05-10 06:38 UTC

This package is auto-updated.

Last update: 2022-05-15 12:43:31 UTC


README

安装

使用 composer:

$ composer require winwin/pay-sdk

配置

调用接口需要先创建 payment 对象:

use winwin\pay\sdk\Config;
use winwin\pay\sdk\payment\Payment;

$payment = new Payment(new Config([
    'appid' => $appid,
    'secret' => $secret,
]));

appid 和 secret 需要申请获取。

创建订单

use winwin\pay\sdk\payment\Order;

$result = $payment->prepare(new Order([
    'mch_id' => $merchant_id,
    'method' => 'pay.weixin.jsapi',
    'body' => '支付1分',
    'total_fee' => 1,
    'out_trade_no' => date('ymdHis') . mt_rand(1000, 9999),
    'notify_url' => 'http://example.org/notify',
    'openid' => $openid,
]));

mch_id 需要申请获取。

openid 需要网页授权获取,参考微信官方文档

处理支付异步通知

$response = $payment->handleNotify(function($notify, $successful) {
    // 处理逻辑
    return true;
});
echo $response->getBody();

handleNotify 接收一个回调函数,该回调函数接收两个参数,这两个参数分别为:

  • $notify 为封装了通知信息的数组对象,可以使用对象或者数组形式来读取通知内容,比如:$notify->total_fee 或者 $notify['total_fee']
  • $successful 用于判断用户是否付款成功了

回调函数返回 false 或者一个具体的错误消息,那么系统会在稍后再次继续通知你,直到你明确的告诉它:“我已经处理完成了”,在函数里 return true; 代表处理完成。

handleNotify 返回值 $response 是一个 PSR-7 Response 对象。

订单查询

use winwin\pay\sdk\payment\OrderQuery;

$result = $payment->query(new OrderQuery([
    'mch_id' => $merchant_id,
    'method' => 'trade.query',
    'transaction_id' => '平台订单号',
    'out_trade_no' => '商户订单号',
]));

关闭订单

use winwin\pay\sdk\payment\CloseOrder;

$result = $payment->close(new CloseOrder([
    'mch_id' => $merchant_id,
    'method' => 'trade.close',
    'out_trade_no' => '商户订单号',
]));

退款

use winwin\pay\sdk\payment\Refund;

$result = $payment->refund(new Refund([
    'mch_id' => $merchant_id,
    'method' => 'trade.refund',
    'transaction_id' => '平台订单号',
    'out_trade_no' => '商户订单号',
    'out_refund_no' => '商户退款单号',
    'total_fee' => 1,
    'refund_fee' => 1,
    'op_user_id' => $merchant_id,
]));

退款查询

use winwin\pay\sdk\payment\RefundQuery;

$result = $payment->queryRefund(new RefundQuery([
    'mch_id' => $merchant_id,
    'method' => 'trade.refund.query',
    'transaction_id' => '平台订单号',
    'out_trade_no' => '商户订单号',
    'out_refund_no' => '商户退款单号',
    'refund_id' => '平台退款单号',
]));

调试

如果需要打印 http 请求日志,可使用 PSR-3 实现库,例如 Monolog

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$logger = new Logger('WinwinPay');
$logger->pushHandler(new StreamHandler('php://stderr', Logger::DEBUG));

$payment->setLogger($logger);