微信app支付

536 查看

文档
注:开放平台的微信支付和公众号的微信支付是不一样的,公众平台和开放平台的支付申请下来之后会有各自对应的商户平台账号

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' 支付成功
    }
    }

附:
https://youqingkui.me/note/e57abb2a-3997-47f1-b9fe-ac94740130ce
python版微信支付
http://bblove.me/2015/10/25/weixin-app-pay-v3-0/
微信APP支付服务端php sdk开发教程
https://github.com/fanhefan/wechat_app_pay
微信红包API接口
http://jeffchen.sinaapp.com/
http://tao.logdown.com/posts/195357-micro-payments-app-integration