2017 © Pedro Peláez
 

project pay-sdk

image

winwin/pay-sdk

  • Thursday, May 10, 2018
  • by wenbinye
  • Repository
  • 4 Watchers
  • 3 Stars
  • 1,411 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 37 % Grown

The README.md

码上赢支付SDK

安装

使用 composer:, (*1)

$ composer require winwin/pay-sdk

配置

调用接口需要先创建 payment 对象:, (*2)

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

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

appid 和 secret 需要申请获取。, (*3)

创建订单

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 需要申请获取。, (*4)

openid 需要网页授权获取,参考微信官方文档。, (*5)

处理支付异步通知

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

handleNotify 接收一个回调函数,该回调函数接收两个参数,这两个参数分别为: - $notify 为封装了通知信息的数组对象,可以使用对象或者数组形式来读取通知内容,比如:$notify->total_fee 或者 $notify['total_fee']。 - $successful 用于判断用户是否付款成功了, (*6)

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

handleNotify 返回值 $response 是一个 PSR-7 Response 对象。, (*8)

订单查询

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 :, (*9)

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

$payment->setLogger($logger);

The Versions

10/05 2018

dev-master

9999999-dev

  Sources   Download

The Requires

 

The Development Requires

10/05 2018

0.2.0

0.2.0.0

  Sources   Download

The Requires

 

The Development Requires

08/05 2017

0.1.1

0.1.1.0

  Sources   Download

The Requires

 

The Development Requires

24/04 2017

0.1.0

0.1.0.0

  Sources   Download

The Requires

 

The Development Requires