How to access WeChat Pay points with PHP
This article mainly introduces how to access WeChat Pay points in PHP, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.
WeChat Pay's introduction and opening
Product introduction: https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_0.shtml
Prepare before access: https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_1.shtml
Test number configuration: https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_5.shtml
Second, the development of confirmation-free mode
Reference website: https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter3_1_3.shtml
Step 1 the user places an order to purchase products or services at the merchant side. At this point, we need to query the authorization status of the user first.
Step 2 guide the user to activate the authorization service
Step 3 create a payment sub-order
Step 4 the merchant provides services for the user, and after the service is over, the merchant calls the API to terminate the order to finish the current order.
Step 5 receive the notification of successful deduction from the user, and the business process ends
III. Related to SDK
Official document: https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay6_0.shtml
Wechatpay-php (recommended): https://github.com/wechatpay-apiv3/wechatpay-php
Fourth, code example
/ * Notes: step 1 A user issues an order to purchase products or services at the merchant side. At this point, we need to query the authorization status of the user * User: XXX * DateTime: 9:59 / public function getAuthStatus (string $cid) {$openid = $this- > getOpenid ($cid); if (! $openid) {return false } try {$resp = $this- > instance- > v3-> payscore- > permissions- > openid- > {'{openid}'}-> get (['query' = > [' appid' = > $this- > appid, 'service_id' = > $this- > serviceId ], / / uri_template literal parameter 'openid' = > $openid,]) Res = json_decode ($resp- > getBody ()-> getContents (), true); if ($res ['authorization_state'] = =' AVAILABLE') {return true;} else {return false;}} catch (\ Exception $e) {return false; / * echo ($e-> getResponse ()-> getStatusCode ()) / / handle echo $e-> getMessage ()-> getReasonPhrase (), PHP_EOL; if ($e instanceof\ Psr\ Http\ Message\ ResponseInterface & & $e-> hasResponse ()) {echo $e-> getResponse ()-> getStatusCode (). ''. $e-> getResponse ()-> getReasonPhrase (), PHP_EOL; echo $e-> getResponse ()-> getBody () } * /}} / * Notes: step 2 guide the user to activate the authorization service-obtain the pre-authorization code * User: XXX * DateTime: 18:37 on 2021-7-27 * / public function openAuthStatus () {try {$resp = $this- > instance- > v3-> payscore- > permissions- > post ([ 'json' = > [' service_id' = > $this- > serviceId 'appid' = > $this- > appid,' authorization_code' = > $this- > getRandStr (12), / / Authorization Agreement number, similar to order number / / 'notify_url' = >' https://weixin.qq.com/',]]) $res = json_decode ($resp- > getBody (), true); return $res ['apply_permissions_token'];} catch (\ Exception $e) {/ / error handling / * if ($e-> hasResponse ()) {echo $e-> getResponse ()-> getBody ();} * / return false }} / * Notes: step 3 create a payment sub-order * User: xxx * DateTime: 19:21 on 2021-7-27 * @ param string $cid user ID * @ param string $orderSn order number * / public function makeOrder (string $cid, string $orderSn) {/ / order information. $openid = $this- > getOpenid ($cid); if (! $openid) {return ['code' = >-1,' msg' = > 'openid cannot be empty',];} / / Asynchronous notification address. Sometimes the inexplicable address becomes localhost. Fix $notiryUrl = route ('api.v1.wxpayPointsNotify') here. $json = ['out_order_no' = > $orderSn, / / merchant service order number' appid' = > $this- > appid / / Application ID 'service_id' = > $this- > serviceId, / / Service ID' service_introduction' = > 'Power replacement charge' / / Service information Used to describe the services provided by this order when the parameter length is more than 20 characters Error handling 'time_range' = > [' start_time' = > $startTime, / / '20210729160710,],' risk_fund' = > ['name' = >' ESTIMATE_ORDER_COST', / / risk name 'amount' = > 300 / / risk amount figure Must be > 0 (unit score)], 'attach' = > $orderSn,// merchant packet' notify_url' = > $notiryUrl, 'openid' = > $openid,// user ID' need_user_confirm' = > whether false,// requires user confirmation] Try {$resp = $this- > instance- > v3-> payscore- > serviceorder- > post (['json' = > $json]); $res = json_decode ($resp- > getBody (), true); / / pay sub-order in storage. Return ['code' = > 0,' msg' = > 'payment sub-order creation',];} catch (\ Exception $e) {/ / error handling if ($e-> hasResponse ()) {$body = $e-> getResponse ()-> getBody () If ($body) {return ['code' = >-1,' msg' = > (string) $body,];}} return';}}
It is similar to finish the payment sub-order, cancel the payment sub-order and inquire about the payment sub-order, which is no longer written here.
/ * Notes: asynchronous Notification * User: XXX * DateTime: 14:20 on 2021-8-3 * / public function notify () {/ / get the returned information $responseBody = file_get_contents ("php://input"); $responseArr = json_decode ($responseBody, true) If ($responseArr) {$res = AesGcm::decrypt ($responseArr ['resource'] [' ciphertext'], 'xxxapi key', $responseArr ['resource'] [' nonce'], $responseArr ['resource'] [' associated_data']); $resArr = json_decode ($res, true) If ($resArr) {/ / logging. / / Business logic processing. / / order logging.} else {return ['code' = >-1 'msg' = >' parsing error',] }} else {return ['code' = >-1,' msg' = > 'nothing post',];}} 5. Precautions
Strictly follow the parameter requirements in the document, and compare the differences between the passed parameters and the official examples as soon as there is a problem.
The payment sub-order must be cancelled or terminated.
Thank you for reading this article carefully. I hope the article "how to access WeChat Pay points with PHP" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!