PHP如何接入微信支付分(代码示例)

 4458

一、微信支付分介绍及开通

产品介绍:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_0.shtml

接入前准备:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_1.shtml

测试号配置:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_5.shtml


二、免确认模式开发

参考网址:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_3.shtml

步骤1 用户在商户侧下单购买产品或服务,此时,我们需要先对用户的授权状态进行查询

步骤2 引导用户开启授权服务

步骤3 创建支付分订单

步骤4 商户为用户提供服务,待服务结束后,商户调用完结订单接口完结当前订单。

步骤5 收到用户扣款成功通知,业务流程结束


三、SDK相关

官方文档:https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay6_0.shtml

wechatpay-php(推荐):https://github.com/wechatpay-apiv3/wechatpay-php


四、代码示例

  1. /**
  2.  * Notes: 步骤1 用户在商户侧下单购买产品或服务,此时,我们需要先对用户的授权状态进行查询
  3.  * User: XXX
  4.  * DateTime: 2021/7/27 9:59
  5.  */
  6. public function getAuthStatus(string $cid)
  7. {
  8.     $openid = $this->getOpenid($cid);
  9.     if (!$openid) {
  10.         return false;
  11.     }
  12.     try {
  13.         $resp = $this->instance->v3->payscore->permissions->openid->{'{openid}'}
  14.             ->get(
  15.                 [
  16.                     'query'  => [
  17.                         'appid'      => $this->appid,
  18.                         'service_id' => $this->serviceId,
  19.                     ],
  20.                     // uri_template 字面量参数
  21.                     'openid' => $openid,
  22.                 ]
  23.             );
  24.         $res = json_decode($resp->getBody()->getContents(), true);
  25.         if ($res['authorization_state'] == 'AVAILABLE') {
  26.             return true;
  27.         } else {
  28.             return false;
  29.         }
  30.     } catch (\Exception $e) {
  31.         return false;
  32.         /*echo($e->getResponse()->getStatusCode());
  33.         // 进行错误处理
  34.         echo $e->getMessage()->getReasonPhrase(), PHP_EOL;
  35.         if ($e instanceof \Psr\Http\Message\ResponseInterface && $e->hasResponse()) {
  36.             echo $e->getResponse()->getStatusCode() . ' ' . $e->getResponse()->getReasonPhrase(), PHP_EOL;
  37.             echo $e->getResponse()->getBody();
  38.         }*/
  39.     }
  40. }
  1. /**
  2.  * Notes:步骤2 引导用户开启授权服务-获取预授权码
  3.  * User: XXX
  4.  * DateTime: 2021/7/27 18:37
  5.  */
  6. public function openAuthStatus()
  7. {
  8.     try {
  9.         $resp = $this->instance->v3->payscore->permissions->post(
  10.             [
  11.                 'json' => [
  12.                     'service_id'         => $this->serviceId,
  13.                     'appid'              => $this->appid,
  14.                     'authorization_code' => $this->getRandStr(12), // 授权协议号,类似订单号
  15.                     //'notify_url'         => 'https://weixin.qq.com/',
  16.                 ]
  17.             ]
  18.         );
  19.         $res = json_decode($resp->getBody(), true);
  20.         return $res['apply_permissions_token'];
  21.     } catch (\Exception $e) {
  22.         // 进行错误处理
  23.         /*if ($e->hasResponse()) {
  24.             echo $e->getResponse()->getBody();
  25.         }*/
  26.         return false;
  27.     }
  28. }
  1. /**
  2.  * Notes: 步骤3 创建支付分订单
  3.  * User: xxx
  4.  * DateTime: 2021/7/27 19:21
  5.  * @param string $cid     用户ID
  6.  * @param string $orderSn 订单号
  7.  */
  8. public function makeOrder(string $cid, string $orderSn)
  9. {
  10.     // 订单信息
  11.     ....
  12.     $openid = $this->getOpenid($cid);
  13.     if (!$openid) {
  14.         return [
  15.             'code' => -1,
  16.             'msg'  => 'openid不可以为空',
  17.         ];
  18.     }
  19.     // 异步通知地址,有时候发现莫名的变成了localhost,这里先固定
  20.     $notiryUrl = route('api.v1.wxpayPointsNotify');
  21.     $json = [
  22.         'out_order_no'         => $orderSn,                                                        // 商户服务订单号
  23.         'appid'                => $this->appid,                                                    // 应用ID
  24.         'service_id'           => $this->serviceId,                                                // 服务ID
  25.         'service_introduction' => '换电费用',                                                          // 服务信息,用于介绍本订单所提供的服务 ,当参数长度超过20个字符时,报错处理
  26.         'time_range'           => [
  27.             'start_time' => $startTime, //'20210729160710',
  28.         ],
  29.         'risk_fund'            => [
  30.             'name'   => 'ESTIMATE_ORDER_COST',         // 风险金名称
  31.             'amount' => 300,                           // 风险金额 数字,必须>0(单位分)
  32.         ],
  33.         'attach'               => $orderSn,// 商户数据包
  34.         'notify_url'           => $notiryUrl,
  35.         'openid'               => $openid,// 用户标识
  36.         'need_user_confirm'    => false,// 是否需要用户确认
  37.     ];
  38.     try {
  39.         $resp = $this->instance->v3->payscore->serviceorder->post(
  40.             [
  41.                 'json' => $json
  42.             ]
  43.         );
  44.         $res = json_decode($resp->getBody(), true);
  45.         // 入库支付分订单
  46.         ...
  47.         return [
  48.             'code' => 0,
  49.             'msg'  => '支付分订单创建完成',
  50.         ];
  51.     } catch (\Exception $e) {
  52.         // 进行错误处理
  53.         if ($e->hasResponse()) {
  54.             $body = $e->getResponse()->getBody();
  55.             if ($body) {
  56.                 return [
  57.                     'code' => -1,
  58.                     'msg'  => (string)$body,
  59.                 ];
  60.             }
  61.         }
  62.         return '';
  63.     }
  64. }

完结支付分订单、取消支付分订单、查询支付分订单 类似,这里不再写出来。

  1. /**
  2.  * Notes: 异步通知
  3.  * User: XXX
  4.  * DateTime: 2021/8/3 14:20
  5.  */
  6. public function notify()
  7. {
  8.     // 获取返回的信息
  9.     $responseBody = file_get_contents("php://input");
  10.     $responseArr = json_decode($responseBody, true);
  11.     if ($responseArr) {
  12.         $res = AesGcm::decrypt($responseArr['resource']['ciphertext'], 'xxxapi密钥', $responseArr['resource']['nonce'], $responseArr['resource']['associated_data']);
  13.         $resArr = json_decode($res, true);
  14.         if ($resArr) {
  15.             // 记录日志
  16.             ...
  17.             // 业务逻辑处理
  18.             ...
  19.             // 订单日志记录
  20.            ...
  21.         } else {
  22.             return [
  23.                 'code' => -1,
  24.                 'msg'  => '解析有误',
  25.             ];
  26.         }
  27.     } else {
  28.         return [
  29.             'code' => -1,
  30.             'msg'  => 'nothing post',
  31.         ];
  32.     }
  33. }


五、注意事项

严格遵循文档中的参数要求,出现问题第一时间比较传入参数和官方示例的区别

支付分订单必须取消,或完结


TAG标签:
本文网址:https://www.zztuku.com/detail-9250.html
站长图库 - PHP如何接入微信支付分(代码示例)
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐