Home > Backend Development > PHP Tutorial > WeChat app payment

WeChat app payment

WBOY
Release: 2016-07-29 09:11:03
Original
1197 people have browsed it

Document
Note: The WeChat payment of the open platform is different from the WeChat payment of the official account. After the payment application for the public platform and the open platform, there will be their corresponding merchant platform accounts

<code>function wechat($appid,$mchid,$appkey,$cert_path,$key_path,$order_id,$openid,$amount,$desc){
    $arr = [
            'mch_appid'=>$appid,//注意区分公众号和app商户号不同
            'mchid'=>$mchid,
            'nonce_str'=>str_random(32),//随机数
            'partner_trade_no'=>$order_id,//自己定义一个不重复订单号
            'openid'=>$openid,//微信openid 通过微信授权登录获取
            'check_name'=>'NO_CHECK',
            'amount'=>$amount*100,//注意这里传给微信的单位是分
            'desc'=>$desc,
            'spbill_create_ip'=>\Request::getClientIp(),
            'sign'=>'',
        ];
        ksort($arr);
        $sign="";
        foreach ($arr as $key => $value) {
            if($value && $key!="sign" && $key!="key"){
                $sign.=$key."=".$value."&";
            }
        }
        $sign.="key=".$appkey;//商户后台自定义的
        $arr['sign'] = strtoupper(md5($sign));
        $xml = "<xml>";
        foreach ($arr as $key=>$val)
        {
                if (is_numeric($val))
             {
                $xml.="<".$key.">".$val."</".$key.">"; 

             }
             else
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";  
        }
        $xml.="</xml>";
       
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,60);
        curl_setopt($ch,CURLOPT_URL,'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers');
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        //默认格式为PEM
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLCERT,$cert_path);//注意区分公众号和app商户号的证书不同,需要到pay.weixin.qq.com后台下载
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY,$key_path);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
        $data = curl_exec($ch);
        $data = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        curl_close($ch);
        return $data;//$data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS' 支付成功
    }
    }</code>
Copy after login

Attachment:
https://youqingkui .me/note/e57abb2a-3997-47f1-b9fe-ac94740130ce
Python version of WeChat payment
http://bblove.me/2015/10/25/weixin-app-pay-v3-0/
WeChat APP payment server PHP sdk development tutorial
https://github.com/fanhefan/wechat_app_pay
WeChat red envelope API interface
http://jeffchen.sinaapp.com/
http://tao.logdown.com/posts/195357-micro -payments-app-integration

The above has introduced WeChat app payment, including github content. I hope it will be helpful to friends who are interested in PHP tutorials.

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