@@ -61,7 +61,8 @@ if ($msg->MsgType == 'text' && $msg->Content == '你好') {
6161
6262 ``` shell
6363 # 安装composer依赖
64- composer require " gaoming13/wechat-php-sdk:1.0.*"
64+ composer require " gaoming13/wechat-php-sdk:1.5.*"
65+ composer dump-autoload --optimize
6566 ```
6667
6768 ``` php
@@ -910,106 +911,106 @@ demo见项目内 `demo/snsapi/`
910911
911912eg: api
912913
913- ```php
914- /* @var $cache \yii\redis\cache */
915- $cache = \Yii::$app->cache;
916-
917- $api = new Api([
918- 'appId' => 'wx312273d5za28s525', //AppID
919- 'appSecret' => '7d8e268465d6ec7ch2f4ed2364h5ef32', //AppSecret
920- 'mchId' => '1633246021', //微信支付商户号
921- 'key' => '613A60282Aa87E2B1A22E3A3DFD9AE32A', //微信商户API密钥
922- 'get_access_token' => function() use ($cache) {
923- return $cache->get('WE_CHAT_SDK_ACCESS_TOKEN');
924- },
925- 'save_access_token' => function($token) use ($cache) {
926- $cache->set('WE_CHAT_SDK_ACCESS_TOKEN', $token);
927- }
928- ]);;
929- ```
914+ ```php
915+ /* @var $cache \yii\redis\cache */
916+ $cache = \Yii::$app->cache;
917+
918+ $api = new Api([
919+ 'appId' => 'wx312273d5za28s525', //AppID
920+ 'appSecret' => '7d8e268465d6ec7ch2f4ed2364h5ef32', //AppSecret
921+ 'mchId' => '1633246021', //微信支付商户号
922+ 'key' => '613A60282Aa87E2B1A22E3A3DFD9AE32A', //微信商户API密钥
923+ 'get_access_token' => function() use ($cache) {
924+ return $cache->get('WE_CHAT_SDK_ACCESS_TOKEN');
925+ },
926+ 'save_access_token' => function($token) use ($cache) {
927+ $cache->set('WE_CHAT_SDK_ACCESS_TOKEN', $token);
928+ }
929+ ]);;
930+ ```
930931
931932eg: 页面内调起微信支付
932933
933- ```php
934- // 获取用户的openid
935- list($err, $user_info) = $api->get_userinfo_by_authorize('snsapi_base');
936- if ($user_info == null) {
937- $url = $api->get_authorize_url('snsapi_base', Yii::$app->request->absoluteUrl);
938- return $this->controller->redirect($url);
939- }
934+ ``` php
935+ // 获取用户的openid
936+ list($err, $user_info) = $api->get_userinfo_by_authorize('snsapi_base');
937+ if ($user_info == null) {
938+ $url = $api->get_authorize_url('snsapi_base', Yii::$app->request->absoluteUrl);
939+ return $this->controller->redirect($url);
940+ }
940941
941- // 生成预订单
942- $wxOrder = $api->wxPayUnifiedOrder($user_info->openid, [
943- 'out_trade_no' => JiariOrder::getOutTradeNo($order['id']),
944- 'body' => '[NO.'.$order['batch_id'].']'.$order['name'],
945- 'total_fee' => (double)$order['total_pay_need'] * 100,
946- //'time_expire' => date('YmdHis', (int)$order['created_at'] + (int)$order['timeout']),
947- 'notify_url' => HTTP.'://my.'.IDN.'/we-chat-pay/asyn-notify',
948- ]);
949- // 判断预订单是否生成成功
950- if ($wxOrder['return_code'] != 'SUCCESS') {
951- Yii::error(['微信支付预订单生成失败', $wxOrder, $order], __METHOD__);
952-
953- throw new NotFoundHttpException('使用微信支付失败,请改用其它支付方式!');
954- }
942+ // 生成预订单
943+ $wxOrder = $api->wxPayUnifiedOrder($user_info->openid, [
944+ 'out_trade_no' => JiariOrder::getOutTradeNo($order['id']),
945+ 'body' => '[NO.'.$order['batch_id'].']'.$order['name'],
946+ 'total_fee' => (double)$order['total_pay_need'] * 100,
947+ //'time_expire' => date('YmdHis', (int)$order['created_at'] + (int)$order['timeout']),
948+ 'notify_url' => HTTP.'://my.'.IDN.'/we-chat-pay/asyn-notify',
949+ ]);
950+ // 判断预订单是否生成成功
951+ if ($wxOrder['return_code'] != 'SUCCESS') {
952+ Yii::error(['微信支付预订单生成失败', $wxOrder, $order], __METHOD__);
953+
954+ throw new NotFoundHttpException('使用微信支付失败,请改用其它支付方式!');
955+ }
955956
956- // 生成微信支付JSAPI参数
957- if (! array_key_exists('prepay_id', $wxOrder)) {
958- Yii::error(['微信支付预订单生成失败', $wxOrder, $order], __METHOD__);
959- throw new NotFoundHttpException('使用微信支付失败,请改用其它支付方式!');
960- }
961- $jsApiParams = $api->getWxPayJsApiParameters($wxOrder['prepay_id']);
962- ```
963-
964- ```html
965- <html >
966- <head >
967- <meta http-equiv =" content-type" content =" text/html;charset=utf-8" />
968- <meta name =" viewport" content =" width=device-width, initial-scale=1" />
969- <title >微信支付</title >
970- <script type =" text/javascript" >
971- function jsApiCall () {
972- WeixinJSBridge .invoke (
973- ' getBrandWCPayRequest' ,
974- < ? = $jsApiParams ? > ,
975- function (res ){
976- alert (JSON .stringfy (res));
977- WeixinJSBridge .log (res .err_msg );
978- alert (res .err_code + res .err_desc + res .err_msg );
979- }
980- );
981- }
982- if (typeof WeixinJSBridge == ' undefined' ){
983- if ( document .addEventListener ){
984- document .addEventListener (' WeixinJSBridgeReady' , jsApiCall, false );
985- }else if (document .attachEvent ){
986- document .attachEvent (' WeixinJSBridgeReady' , jsApiCall);
987- document .attachEvent (' onWeixinJSBridgeReady' , jsApiCall);
988- }
989- }else {
990- jsApiCall ();
957+ // 生成微信支付JSAPI参数
958+ if (! array_key_exists('prepay_id', $wxOrder)) {
959+ Yii::error(['微信支付预订单生成失败', $wxOrder, $order], __METHOD__);
960+ throw new NotFoundHttpException('使用微信支付失败,请改用其它支付方式!');
961+ }
962+ $jsApiParams = $api->getWxPayJsApiParameters($wxOrder['prepay_id']);
963+ ```
964+
965+ ``` html
966+ <html >
967+ <head >
968+ <meta http-equiv =" content-type" content =" text/html;charset=utf-8" />
969+ <meta name =" viewport" content =" width=device-width, initial-scale=1" />
970+ <title >微信支付</title >
971+ <script type =" text/javascript" >
972+ function jsApiCall () {
973+ WeixinJSBridge .invoke (
974+ ' getBrandWCPayRequest' ,
975+ < ? = $jsApiParams ? > ,
976+ function (res ){
977+ alert (JSON .stringfy (res));
978+ WeixinJSBridge .log (res .err_msg );
979+ alert (res .err_code + res .err_desc + res .err_msg );
991980 }
992- < / script>
993- < / head>
994- < / html>
995- ` ` `
981+ );
982+ }
983+ if (typeof WeixinJSBridge == ' undefined' ){
984+ if ( document .addEventListener ){
985+ document .addEventListener (' WeixinJSBridgeReady' , jsApiCall, false );
986+ }else if (document .attachEvent ){
987+ document .attachEvent (' WeixinJSBridgeReady' , jsApiCall);
988+ document .attachEvent (' onWeixinJSBridgeReady' , jsApiCall);
989+ }
990+ }else {
991+ jsApiCall ();
992+ }
993+ < / script>
994+ < / head>
995+ < / html>
996+ ` ` `
996997
997998eg: 处理微信支付结果异步回调
998999
999- ` ` ` php
1000- // 处理微信支付异步通知
1001- // $res: 是否支付成功
1002- // $notifyData: 异步通知的原始数据
1003- // $replyData: 回复微信异步通知的数据
1004- list ($res, $notifyData, $replyData) = $api- > progressWxPayNotify ();
1005-
1006- // 处理业务逻辑
1007- // ...
1008-
1009- // 回复微信
1010- $api- > replyWxPayNotify ($replyData);
1011- exit ();
1012- ` ` `
1000+ ` ` ` php
1001+ // 处理微信支付异步通知
1002+ // $res: 是否支付成功
1003+ // $notifyData: 异步通知的原始数据
1004+ // $replyData: 回复微信异步通知的数据
1005+ list ($res, $notifyData, $replyData) = $api- > progressWxPayNotify ();
1006+
1007+ // 处理业务逻辑
1008+ // ...
1009+
1010+ // 回复微信
1011+ $api- > replyWxPayNotify ($replyData);
1012+ exit ();
1013+ ` ` `
10131014
10141015### 常见问题
10151016
0 commit comments