WeChat payment in the official account needs to be implemented through JS. WeChat JS-SDK is a web development toolkit based on WeChat provided by WeChat public platform for web developers.
1) Introduce JS script file
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
2) Inject the permission verification configuration through the config interface
<script> wx.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: '', // 必填,公众号的唯一标识 timestamp: , // 必填,生成签名的时间戳 nonceStr: '', // 必填,生成签名的随机串 signature: '',// 必填,签名 }); </script>
appId is the application ID, the string of characters starting with wx, timestampIn php, use time() to obtain it, while nonceStr uses uniqid() to obtain it, and signature is obtained according to a specific algorithm.
protected function getJsapiConfig() { $weixin = new Weixin(); $ticketMongo = new WeixinJsapiTicket(); $data = [ 'appId' => $weixin->getAppId(), 'noncestr' => uniqid(), 'jsapi_ticket' => $ticketMongo->getJsapiTicket(), 'timestamp' => time() ]; //拼装原始待签名串 $src = [ 'noncestr=' . $data['noncestr'], 'jsapi_ticket=' . $data['jsapi_ticket'], 'timestamp=' . $data['timestamp'] ]; sort($src); $data['signature'] = sha1(implode('&', $src)); return $data; }
Here is a description of "jsapi_ticket". jsapi_ticket is a temporary ticket used by public accounts to call the WeChat JS interface. Under normal circumstances, the validity period of jsapi_ticket is 7200 seconds, which is obtained through access_token. Since there is a time limit and the number of api calls to obtain jsapi_ticket is very limited, I will save the obtained jsapi_ticket to MongoDB.
/** * 通过access_token获取jsapi_ticket * @param $access_token * @return string | null */ public function getJsapiTicket($access_token) { $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; $param = [ 'access_token' => $access_token, 'type' => 'jsapi' ]; $res = $this->request($url, $param); $result = json_decode($res, true); if (isset($result['errcode']) && $result['errcode'] == 0 && isset($result['ticket'])) { return $result; } return null; }
3) Process successful verification through ready interface
1) prepay_id is based on The locally generated order number is obtained. The order number is different every time you request it, otherwise an error will be reported.
2) Use md5(uniqid('baiaimama')) to obtain the nonceStr.
3) signType uses MD5
4) paySign is obtained by sorting and concatenating according to the parameters of the code.
wx.chooseWXPay({ timestamp: 0, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符 nonceStr: '', // 支付签名随机串,不长于 32 位 package: '', // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***) signType: '', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5' paySign: '', // 支付签名 success: function (res) { // 支付成功后的回调函数 } });
/** * 生成jsapi需要调用的参数 */ public function getJsapiParam(){ $param = [ 'appId' => $this->APPID, 'timeStamp' => time(), 'nonceStr' => md5(uniqid('baiaimama')), 'package' => 'prepay_id='.$this->param['prepay_id'], 'signType' => 'MD5' ]; $str = []; foreach($param as $k=>$v){ if(!empty($v)){ $str[] = "{$k}={$v}"; } } sort($str); $unsignKey = join('&', $str).'&key='.$this->KEY; $sign = strtoupper(md5($unsignKey)); $param['paySign'] = $sign; return $param; }
In the asynchronous callback, do some operations such as modifying the order status, sending text messages, and pushing messages.
/** * 微信支付异步回调API * 微信支付成功,会收到异步回调 */ public function actionWxpay() { $weixinPay = new WeixinPay(); $weixin = new Weixin(); $xml = file_get_contents('php://input'); $msg = $weixin->parseMsg($xml); //记录微信推送日志 $notifyMongo = new WeixinPayNotify(); $notifyMongo->logPayNotify($xml); if(!$msg || !is_object($msg)){ $weixinPay->notifyXml('FAIL', '通知不合法'); } if(!isset($msg->return_code) || $msg->return_code != 'SUCCESS'){ $weixinPay->notifyXml('FAIL', '通信失败'); } if(!isset($msg->result_code) || $msg->result_code != "SUCCESS"){ $weixinPay->notifyXml('FAIL', '交易失败'); } //签名验证失败 if(!$weixinPay->checkSign($msg)){ $weixinPay->notifyXml('FAIL', '签名验证失败'); } //$notifyMongo->add($msg); //流程走到这里说明已经支付成功了,这里无需更新订单逻辑 $userOrder = new UserOrder(); //记录微信订单号 $userOrder->pay($msg->out_trade_no, $msg->transaction_id); }
demo download:
github address: https://github.com/pwstrick/weixin_demo
CSDN address: http://download.csdn.net/detail/loneleaf1 /9045731
For more WeChat public platform development and WeChat payment related articles, please pay attention to the PHP Chinese website!