微信电商收付通功能v3版JSAPI调起微信合单支付PHP详解

上一篇文章我们说了小微商户的进件功能,这篇文章来看一看 微信JSAPI合单支付
要在微信中通过JSAPI调起支付,需要在服务端与微信通信,服务端把订单信息发送给微信服务器,然后微信服务器会生成一个预支付订单的标识返回给我们,我们拿到这个预支付订单的标识,再去通过js调起微信支付。
php服务器端代码:

//下单请求微信 微信返回 prepay_id
public function goPayJs(){
    $ip=$this->getIP();//获取IP方法,见下文
    $order_sn=date("YmdHis").rand(10000,99999);//自定义一个支付订单号
    $info=array(//订单信息
        "combine_out_trade_no"  => $order_sn,//合单支付总订单号
        "combine_mchid"         => "123456789",//服务商的商户号
        "combine_appid"         => "wx1234567890",//合单发起方的appid
        "scene_info"            => array(
                                        "payer_client_ip"   => $ip,//用户端实际ip 
                                    ),
        "sub_orders"            => array(//子订单,最多支持50个
                                        array(
                                            "mchid"             => "123456789",//服务商的商户号,
                                            "attach"            => "日用杂货店",//附加数据,在查询API和支付通知中原样返回,可作为自定义参数使用。 
                                            "amount"            => array(//支付信息
                                                                    "total_amount"  =>1,//支付金额 单位为分 int类型
                                                                    "currency"      =>"CNY"//币种  CNY人民币
                                                                   ),
                                            "out_trade_no"      => "2020".time(),//自定义子单订单号
                                            "sub_mchid"         => "123123123",//二级商户的商户号
                                            "description"       => "府周店-日用杂货店"//描述
                                            )
                                    ),
        "combine_payer_info"    =>array(//支付用户的信息
                                        "openid"=>"FWEFWE324234G3443G34G" //支付用户的 微信openid
                                    ),
        "notify_url"            => "https://www.baidu.com/" //支付回调地址 支付成功后 微信通知你服务器的地址
    );
    $json=json_encode($info);
    $url="https://api.mch.weixin.qq.com/v3/combine-transactions/jsapi";
        
    $timeStamp=time();//时间戳
    $nonce=$this->nonce_str();//获取一个随机字符串
        
    $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
    $merchant_id='123456789';//服务商商户号
    $serial_no='123123SJDFOWJEO23J32I23J2J3LJ';//商户证书序列号
    $sign=$this->sign($url,'POST',$timeStamp,$nonce,$json,$mch_private_key,$merchant_id,$serial_no);//签名
    $header=[
            'Authorization:WECHATPAY2-SHA256-RSA2048 '.$sign,
            'Accept:application/json',
            'User-Agent:'.$merchant_id,
            'Content-Type:application/json',
    ];
    $result=$this->curl($url,$json,$header,'POST');
    $result=json_decode($result,true);

    //$result['prepay_id']微信返回的 预支付订单的标识
    $prepayID="prepay_id=".$result['prepay_id'];//字符串拼接
    $key="微信证书v3  32位密钥字符串";   //微信证书v3密钥 在微信商户平台 API安全中设置的
    $qianming=$this->signJS("wx1234567890",$timeStamp,$nonce,$prepayID,$mch_private_key);//二次签名
    //一下几个参数需要传到前端 js中使用
    $data['time']       = $timeStamp;//上文生成的时间戳
    $data['nonce']      = $nonce;//上文生成的随机字符串
    $data['prepay_id']  = $result['prepay_id'];//微信返回的预支付订单标识
    $data['sign']       = $qianming;//二次签名
    $data['appid']      = "wx1234567890";//商户的appid
}
//获取IP
private function getIP(){ 
    if (@$_SERVER["HTTP_X_FORWARDED_FOR"]) 
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
    else if (@$_SERVER["HTTP_CLIENT_IP"]) 
        $ip = $_SERVER["HTTP_CLIENT_IP"]; 
    else if (@$_SERVER["REMOTE_ADDR"]) 
        $ip = $_SERVER["REMOTE_ADDR"]; 
    else if (@getenv("HTTP_X_FORWARDED_FOR")) 
        $ip = getenv("HTTP_X_FORWARDED_FOR"); 
    else if (@getenv("HTTP_CLIENT_IP")) 
        $ip = getenv("HTTP_CLIENT_IP"); 
    else if (@getenv("REMOTE_ADDR")) 
        $ip = getenv("REMOTE_ADDR"); 
    else 
        $ip = "Unknown"; 
    return $ip; 
}
//生成随机字符串
public function nonce_str($length=32){
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; 
        $str ="";
        for ( $i = 0; $i < $length; $i++ )  { 
            $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1); 
        } 
        return $str;
 }
//读取商户api证书公钥
public function getPublicKey(){
        return openssl_get_privatekey(file_get_contents('E:\项目\cert\apiclient_key.pem'));//商户平台API安全证书中下载,保存到服务器 直接读取就可以
}
//签名
private function sign($url,$http_method,$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no){
        $url_parts=parse_url($url);
        $canonical_url=($url_parts['path'].(!empty($url_parts['query'])?"?${url_parts['query']}":""));
        $message=
            $http_method . "\n".
            $canonical_url . "\n".
            $timestamp . "\n".
            $nonce . "\n".
            $body . "\n";
        openssl_sign($message,$raw_sign,$mch_private_key,'sha256WithRSAEncryption');
        $sign=base64_encode($raw_sign);
        $schema='WECHATPAY2-SHA256-RSA2048';
        $token=sprintf(
            'mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',
            $merchant_id,
            $nonce,
            $sign,
            $timestamp,
            $serial_no
        );
        return $token;
}
//curl提交
private function curl($url,$data=[],$header,$method='POST'){
        $curl=curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
        curl_setopt($curl,CURLOPT_HEADER,false);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        if($method=="POST"){
            curl_setopt($curl,CURLOPT_POST,TRUE);
            curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        }
        $result=curl_exec($curl);
        curl_close($curl);
        return $result;
}
//二次签名
public function signJS($wxid,$timeStamp,$nonce,$package,$mch_private_key){
        $message=
            $wxid . "\n".
            $timeStamp . "\n".
            $nonce . "\n".
            $package . "\n";
        openssl_sign($message,$raw_sign,$mch_private_key,'sha256');
        $sign=base64_encode($raw_sign);
        return $sign;
}

JS代码:直接写在网页即可,即可调用微信浏览器内部的js方法 调起支付

function onBridgeReady(){
        //alert();
       WeixinJSBridge.invoke(
            'getBrandWCPayRequest', {
                "appId":"",                     //商户appid
                "timeStamp":"",                  //时间戳,自1970年以来的秒数     
                "nonceStr":"",                  //随机串     
                "package":"prepay_id=",     //预订单标识,注意:要拼上 “prepay_id”
                "signType":"RSA",                                   //微信签名方式:     
                "paySign":""                     //微信签名 
            },
            function(res){
                if(res.err_msg == "get_brand_wcpay_request:ok" ){
                    window.location.href="支付成功后的跳转链接";
                }else if(res.err_msg =="get_brand_wcpay_request:cancel"){
                    window.location.href="取消支付后的跳转链接";
                }else{// get_brand_wcpay_request:fail 支付失败
                    alert('支付失败');
                    window.location.href="支付失败后的跳转链接";
                }
            }); 
    }
    if (typeof WeixinJSBridge == "undefined"){
       if( document.addEventListener ){
           document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
       }else if (document.attachEvent){
           document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
           document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
       }
    }else{
       onBridgeReady();
    }

微信JSAPI合单发起支付到这里就完事啦,下篇说一下,支付回调

你可能感兴趣的:(微信电商收付通功能v3版JSAPI调起微信合单支付PHP详解)