微信小程序支付V3总结

yang
2021-06-01 / 0 评论 / 32 阅读 / 正在检测是否收录...

微信小程序支付 需要先下单

https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml

数据根据文档拼凑下就好了,主要是签名

注意这只是下单的签名

    /**
     * 获得下单支付签名 sign
     */
    public function getSign($config,$request,$url){
        $url_parts = parse_url($url);
        $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
        $timestamp = time();
        $nonce = makeOrderNo();
        $merchant_id = $config['mchid'];
        $serial_no = $config['serial_no'];

        $message = 'POST'."\n".
            $canonical_url."\n".
            $timestamp."\n".
            $nonce."\n".
            json_encode($request )."\n";
        $mch_private_key = ($config['mch_key']);

        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",timestamp="%d",serial_no="%s",signature="%s"',
            $merchant_id, $nonce, $timestamp, $serial_no, $sign);
        return [
            'header'=>'Authorization: '.$schema.'  '.$token,
            'sign'=>$sign,
            'timeStamp'=> $timestamp,
            'nonceStr' => $nonce,
            ];
    }

创建订单之后 还需要小程序唤起支付

https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_4.shtml

注意最大的坑 这个支付的签名和下单的签名不一样,需要重新生成

   /**
     * 微信小程序支付sign 验签生成
     */
    function getWXSign($config,$prepay_id){
        $timestamp = time();
        $nonce = makeOrderNo();
        $merchant_id = $config['mchid'];
        $serial_no = $config['serial_no'];

        $message = $config['appid']."\n".
            $timestamp."\n".
            $nonce."\n".
            'prepay_id='.$prepay_id."\n";
        $mch_private_key = ($config['mch_key']);

        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",timestamp="%d",serial_no="%s",signature="%s"',
            $merchant_id, $nonce, $timestamp, $serial_no, $sign);
        return [
            'header'=>'Authorization: '.$schema.'  '.$token,
            'sign'=>$sign,
            'timeStamp'=> $timestamp,
            'nonceStr' => $nonce,
        ];
    }

余下的自己拼凑下就好了 ,然后去小程序支付

0

评论 (0)

取消