Home > WeChat Applet > WeChat Development > WeChat public platform develops WeChat payment

WeChat public platform develops WeChat payment

高洛峰
Release: 2017-03-01 09:59:09
Original
1854 people have browsed it

1. JS-SDK

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>
Copy after login

2) Inject the permission verification configuration through the config interface

<script>
    wx.config({
    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
    appId: &#39;&#39;, // 必填,公众号的唯一标识
    timestamp: , // 必填,生成签名的时间戳
    nonceStr: &#39;&#39;, // 必填,生成签名的随机串
    signature: &#39;&#39;,// 必填,签名
});
</script>
Copy after login

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 = [
            &#39;appId&#39; => $weixin->getAppId(),
            &#39;noncestr&#39; => uniqid(),
            &#39;jsapi_ticket&#39; => $ticketMongo->getJsapiTicket(),
            &#39;timestamp&#39; => time()
        ];
        //拼装原始待签名串
        $src = [
            &#39;noncestr=&#39; . $data[&#39;noncestr&#39;],
            &#39;jsapi_ticket=&#39; . $data[&#39;jsapi_ticket&#39;],
            &#39;timestamp=&#39; . $data[&#39;timestamp&#39;]
        ];
        sort($src);
        $data[&#39;signature&#39;] = sha1(implode(&#39;&&#39;, $src));
        return $data;
    }
Copy after login

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 = &#39;https://api.weixin.qq.com/cgi-bin/ticket/getticket&#39;;
        $param = [
        &#39;access_token&#39; => $access_token,
        &#39;type&#39; => &#39;jsapi&#39;
                ];
        $res = $this->request($url, $param);
        $result = json_decode($res, true);
        if (isset($result[&#39;errcode&#39;]) && $result[&#39;errcode&#39;] == 0 && isset($result[&#39;ticket&#39;])) {
            return $result;
        }
        return null;
    }
Copy after login

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: &#39;&#39;, // 支付签名随机串,不长于 32 位
    package: &#39;&#39;, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
    signType: &#39;&#39;, // 签名方式,默认为&#39;SHA1&#39;,使用新版支付需传入&#39;MD5&#39;
    paySign: &#39;&#39;, // 支付签名
    success: function (res) {
        // 支付成功后的回调函数
    }
});
Copy after login

/**
     * 生成jsapi需要调用的参数
     */
    public function getJsapiParam(){
        $param = [
        &#39;appId&#39; => $this->APPID,
        &#39;timeStamp&#39; => time(),
        &#39;nonceStr&#39; => md5(uniqid(&#39;baiaimama&#39;)),
        &#39;package&#39; => &#39;prepay_id=&#39;.$this->param[&#39;prepay_id&#39;],
        &#39;signType&#39; => &#39;MD5&#39;
                ];
    
        $str = [];
        foreach($param as $k=>$v){
            if(!empty($v)){
                $str[] = "{$k}={$v}";
            }
        }
        sort($str);
        $unsignKey = join(&#39;&&#39;, $str).&#39;&key=&#39;.$this->KEY;
        $sign = strtoupper(md5($unsignKey));
        $param[&#39;paySign&#39;] = $sign;
        return $param;
    }
Copy after login

2. Asynchronous callback

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(&#39;php://input&#39;);
        $msg = $weixin->parseMsg($xml);
    
        //记录微信推送日志
        $notifyMongo = new WeixinPayNotify();
        $notifyMongo->logPayNotify($xml);

        if(!$msg || !is_object($msg)){
            $weixinPay->notifyXml(&#39;FAIL&#39;, &#39;通知不合法&#39;);
        }
    
        if(!isset($msg->return_code) || $msg->return_code != &#39;SUCCESS&#39;){
            $weixinPay->notifyXml(&#39;FAIL&#39;, &#39;通信失败&#39;);
        }
    
        if(!isset($msg->result_code) || $msg->result_code != "SUCCESS"){
            $weixinPay->notifyXml(&#39;FAIL&#39;, &#39;交易失败&#39;);
        }
    
        //签名验证失败
        if(!$weixinPay->checkSign($msg)){
            $weixinPay->notifyXml(&#39;FAIL&#39;, &#39;签名验证失败&#39;);
        }
        //$notifyMongo->add($msg);
        //流程走到这里说明已经支付成功了,这里无需更新订单逻辑
        $userOrder = new UserOrder();
        //记录微信订单号
        $userOrder->pay($msg->out_trade_no, $msg->transaction_id);
    }
Copy after login

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!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template