协程支付网关(微信支付)

组件安装方法和说明

注意:请务必检查你的 php 环境有没有安装 php-bcmath 扩展,没有安装 php-bcmath 扩展时安装的 pay 组件的版本是 1.2.17 之前的版本(不是最新)。想要使用最新稳定版 pay 组件的功能,请先安装 php-bcmath 扩展,php 安装此扩展的方法请自行查询。

  1. composer require easyswoole/pay

微信支付方法

微信支付目前支持 6 种支付方法,对应的支付 method 如下:

method说明参数返回值
wap手机网站支付RequestResponse
officialAccount公众号支付RequestResponse
scan扫码支付RequestResponse
miniProgram小程序支付RequestResponse
barCode条码当面支付RequestResponse
AppApp支付RequestResponse

微信参数配置

  1. $wechatConfig = new Config();
  2. $wechatConfig->setAppId('xxxxxx'); // 除了小程序以外使用该APPID
  3. $wechatConfig->setMiniAppId('xxxxxx'); // 小程序使用该APPID
  4. $wechatConfig->setMchId('xxxxxx');
  5. $wechatConfig->setKey('xxxxxx');
  6. $wechatConfig->setNotifyUrl('xxxxx');
  7. $wechatConfig->setApiClientCert('xxxxxxx');//客户端证书
  8. $wechatConfig->setApiClientKey('xxxxxxx'); //客户端证书秘钥

由于小程序拥有独立的APPID,只需要在配置里同时配置上AppId和MiniAppId两个配置项,在支付的时候会自动选择对应的APPID来发起支付

公众号支付

  1. $officialAccount = new OfficialAccount();
  2. $officialAccount->setOpenid('xxxxxxx');
  3. $officialAccount->setOutTradeNo('CN' . date('YmdHis') . rand(1000, 9999));
  4. $officialAccount->setBody('xxxxx-测试' . $outTradeNo);
  5. $officialAccount->setTotalFee(1);
  6. $officialAccount->setSpbillCreateIp('xxxxx');
  7. $pay = new \EasySwoole\Pay\Pay();
  8. $params = $pay->weChat($wechatConfig)->officialAccount($officialAccount);

可以参考demo/wechat/index.php

在使用微信支付时,商品名称中如果出现特殊字符,如 & 请自行使用urlencode在生成签名传参时进行编码

H5支付

  1. $wap = new \EasySwoole\Pay\WeChat\RequestBean\Wap();
  2. $wap->setOutTradeNo('CN' . date('YmdHis') . rand(1000, 9999));
  3. $wap->setBody('xxxxx-WAP测试' . $outTradeNo);
  4. $wap->setTotalFee(1);
  5. $wap->setSpbillCreateIp('xxxxx');
  6. $pay = new \EasySwoole\Pay\Pay();
  7. $params = $pay->weChat($wechatConfig)->wap($wap);

小程序支付

  1. $bean = new \EasySwoole\Pay\WeChat\RequestBean\MiniProgram();
  2. $bean->setOpenid('xxxxxxxxx');
  3. $bean->setOutTradeNo('CN' . date('YmdHis') . rand(1000, 9999));
  4. $bean->setBody('xxxx-测试' . $outTradeNo);
  5. $bean->setTotalFee(1);
  6. $bean->setSpbillCreateIp($this->request()->getHeader('x-real-ip')[0]);
  7. $pay = new \EasySwoole\Pay\Pay();
  8. $params = $pay->weChat($this->wechatConfig)->miniProgram($bean);

App支付

  1. $app = new App();
  2. $app->setNonceStr('xxxxx');
  3. $app->setSign('xxxxx');
  4. $app->setBody('app支付测试');
  5. $app->setOutTradeNo(date(YmdHis).rand(1000,999));
  6. $app->setTotalFee(1);
  7. $app->setSpbillCreateIp('xxxx');
  8. $pay = new \EasySwoole\Pay\Pay();
  9. $result = $pay->weChat($wechatConfig)->app($app);

扫码支付

模式一

生成扫码链接 然后生成二维码 具体请查看demo/wechat/index.php

  1. $biz = new Biz();
  2. $biz->setProductId('123456789');
  3. $biz->setTimeStamp(time());
  4. $biz->setAppId($wechatConfig->getAppId());
  5. $biz->setMchId($wechatConfig->getMchid());
  6. $data = $biz->toArray();
  7. $u = new Utility($wechatConfig);
  8. $sign = $u->generateSign($data);
  9. $biz->setSign($sign);
  10. $url1 = "weixin://wxpay/bizpayurl?" . $this->ToUrlParams($biz->toArray());

扫码回调地址(ps:公众号平台设置)

  1. $xml = $this->request()->getBody()->__toString();
  2. $pay = new Pay();
  3. $data = $pay->weChat($this->wechatConfig)->verify($xml);
  4. $bean = new \EasySwoole\Pay\WeChat\RequestBean\Scan();
  5. $bean->setOutTradeNo('CN' . date('YmdHis') . rand(1000, 9999));
  6. $bean->setOpenid('xxxxxx');
  7. $bean->setProductId($data['product_id']);
  8. $bean->setBody('xxxxxx-SCAN测试' . $outTradeNo);
  9. $bean->setTotalFee(1);
  10. $bean->setSpbillCreateIp($this->request()->getHeader('x-real-ip')[0]);
  11. $response = $pay->weChat($this->wechatConfig)->scan($bean);
  12. $nativeResponse = new NativeResponse([
  13. 'appid' => $this->wechatConfig->getAppId(),
  14. 'mch_id' => $this->wechatConfig->getMchId(),
  15. 'prepay_id' => $response->getPrepayId(),
  16. 'nonce_str' => $response->getNonceStr()]);
  17. $u = new Utility($this->wechatConfig);
  18. $nativeResponse->setSign($u->generateSign($nativeResponse->toArray()));
  19. $xml = (new SplArray($nativeResponse->toArray()))->toXML();
  20. $this->response()->write($xml);

模式二

  1. $bean = new \EasySwoole\Pay\WeChat\RequestBean\Scan();
  2. $bean->setOutTradeNo($outTradeNo);
  3. $bean->setProductId('123456789');
  4. $bean->setBody('xxxx-SCAN2测试' . $outTradeNo);
  5. $bean->setTotalFee(1);
  6. $bean->setSpbillCreateIp($this->request()->getHeader('x-real-ip')[0]);
  7. $pay = new Pay();
  8. $data = $pay->weChat($this->wechatConfig)->scan($bean);
  9. $url2 = $data->getCodeUrl();

订单查询

  1. go(function () use ($wechatConfig) {
  2. $orderFind = new \EasySwoole\Pay\WeChat\RequestBean\OrderFind();
  3. $orderFind->setOutTradeNo('CN201903181044383609');
  4. $pay = new \EasySwoole\Pay\Pay();
  5. $info = $pay->weChat($wechatConfig)->orderFind($orderFind);
  6. print_r((array)$info);
  7. });

申请退款

  1. go(function () use ($wechatConfig) {
  2. $refund = new \EasySwoole\Pay\WeChat\RequestBean\Refund();
  3. $refund->setOutTradeNo('CN201903181111275823');
  4. $refund->setOutRefundNo('TK' . date('YmdHis') . rand(1000, 9999));
  5. $refund->setTotalFee(1);
  6. $refund->setRefundFee(1);
  7. $refund->setNotifyUrl('xxxxx');
  8. $pay = new \EasySwoole\Pay\Pay();
  9. $info = $pay->weChat($wechatConfig)->refund($refund);
  10. print_r($info);
  11. });

退款查询

  1. go(function () use ($wechatConfig) {
  2. $refundFind = new \EasySwoole\Pay\WeChat\RequestBean\RefundFind();
  3. $refundFind->setOutTradeNo('CN201903181044383609');
  4. $pay = new \EasySwoole\Pay\Pay();
  5. $info = $pay->weChat($wechatConfig)->refundFind($refundFind);
  6. print_r((array)$info);
  7. });

关闭订单

  1. go(function () use ($wechatConfig) {
  2. $close = new \EasySwoole\Pay\WeChat\RequestBean\Close();
  3. $close->setOutTradeNo('CN201903151343107239');
  4. $pay = new \EasySwoole\Pay\Pay();
  5. $info = $pay->weChat($wechatConfig)->close($close);
  6. print_r((array)$info);
  7. });

下载对账单

  1. go(function () use ($wechatConfig) {
  2. $download = new \EasySwoole\Pay\WeChat\RequestBean\Download();
  3. $download->setBillDate('20190312');
  4. $download->setBillType('ALL');//这个参数必传
  5. $pay = new \EasySwoole\Pay\Pay();
  6. $info = $pay->weChat($wechatConfig)->download($download);
  7. echo htmlspecialchars($info, ENT_QUOTES);
  8. });

下载资金对账单

  1. go(function () use ($wechatConfig) {
  2. $download = new \EasySwoole\Pay\WeChat\RequestBean\DownloadFundFlow();
  3. $download->setBillDate('20190312');
  4. $download->setAccountType('Basic');
  5. $pay = new \EasySwoole\Pay\Pay();
  6. $info = $pay->weChat($wechatConfig)->downloadFundFlow($download);
  7. echo htmlspecialchars($info, ENT_QUOTES);
  8. });

验证签名

  1. $pay = new \EasySwoole\Pay\Pay();
  2. $content = '';//content为xml原始数据,在easyswoole中可通过$this->request()->getBody()->__toString()取出
  3. $data = $pay->weChat($wechatConfig)->verify($content )

服务器确认收到异步通知字符串获取

  1. \EasySwoole\Pay\WeChat\WeChat::success();//成功响应
  2. \EasySwoole\Pay\WeChat\WeChat::fail();//失败响应