thinkphp6调用微信商户支付-合单支付工具代码开发

35 篇文章 0 订阅
27 篇文章 0 订阅

合单支付基本在加盟店或是分公司或是营销系统里面常见。他的出现,打破了传统提现支付或是转账支付。他的业务原理其实很简单,就是需要优先申请非普通商户,其次是每个入驻的商户都需要申请普通商户。在这之前一定要申请好对应的场景服务,比如公众号支付就需要优先申请公众号,小程序支付就需要优先申请小程序,APP支付就需要提供APP应用ID(在开放平台申请),PC网站支付就需要优先提供网站授权函,如果是企业微信支付,就需要提供企业微信。

一、支付产品流程图

二、微信支付工具集代码开发

<?php
/**
 * 微信合单支付工具
 * User: 龙哥·三年风水
 * Date: 2024/9/27
 * Time: 14:13
 */
namespace app\service;
use app\BaseError;
class WechatCooperatePayment
{
    protected $combineMchid = null;// 合单商户号
    protected $merchantSerialNumber = null;// 商户API证书序列号
    protected $combineAppid = null;// 合单商户Appid
    protected $merchantPrivateKey = null;// 商户私钥

    /**
     * 初始化参数
     * WechatPayment constructor.
     */

    public function __construct(){
        $this->combineMchid = config('pay.wechat.combine_appid');
        $this->combineAppid = config('pay.wechat.app_id');
        $this->merchantSerialNumber = config('pay.wechat.merchant_serial_number');
        $file = file_get_contents('./wechatpay/apiclient_key.pem');
        $this->merchantPrivateKey = openssl_get_privatekey($file);// 读取商户秘钥
    }

    /**
     * 合单支付
     * User: 龙哥·三年风水
     * Date: 2024/9/27
     * Time: 14:21
     * @ param $url
     * @ param $data
     * @ return mixed
     */

    public function combinePay($url,$data){
        $data['combine_mchid'] = $this->combineMchid;
        $data['combine_appid'] = $this->combineAppid;
        $authorization = $this->getSign($url,$data);
        $res = http_post($url, $authorization, $data);
        $arr = json_decode($res, true);
        if(isset($arr['code'])) {
            $error['code'] = $arr['code'];
            $error['message'] = $arr['message'];
            $error['timestamp'] = time();
            $error['ip'] = get_client_ip();
            file_put_contents('wechat_payment.txt', json_encode($error) . PHP_EOL, FILE_APPEND);
            throw new BaseError($arr['message'], 50000, 200);
        }
        return $arr;
    }

    /**
     * 获取签名
     * User: 龙哥·三年风水
     * Date: 2024/9/27
     * Time: 9:23
     * @ param $url 地址
     * @ param $data 数据
     * @ return string 返回
     */

    private function getSign($url,$data){
        $timestamp = time();
        $nonce = alnum(12).date('YmdHis', $timestamp) . rand(1000, 9999);
        $url_parts = parse_url($url);
        $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
        $data = json_encode($data);
        $message = 'POST' . "\n" .
            $canonical_url . "\n" .
            $timestamp . "\n" .
            $nonce . "\n" .
            $data . "\n";
        openssl_sign($message, $signature, $this->merchantPrivateKey, "sha256WithRSAEncryption");
        $sign = base64_encode($signature);
        $schema = 'WECHATPAY2-SHA256-RSA2048';
        $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',  $this->combineMchid, $nonce, $timestamp, $this->merchantSerialNumber, $sign);
        return "Authorization: " . $schema . " " . $token;
    }

    /**
     * 销毁参数
     */

    public function __destruct(){
        $this->combineMchid = null;
        $this->combineAppid = null;
        $this->merchantSerialNumber = null;
        $this->merchantPrivateKey = null;
    }
}

三、测试JSAPI支付订单调用

public function index(){
        //定义参数
        $data['combine_out_trade_no'] = create_order();
        $data['sub_orders'] =   [
            [
                'mchid'     =>  '1230000109',
                'attach'    =>  '深圳分店',
                'amount'    =>  [
                    'total_amount'  =>  10,
                    'currency'      =>  'CNY'
                ],
                'out_trade_no'  =>  create_order(),
                'description'   =>  '运费钱'
            ]
        ];
        $data['combine_payer_info'] =   [
            'combine_payer_info'    =>  'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o'
        ];
        $data['notify_url'] =   'http://www.baidu.com';
        $wechatCooperatePayment = new WechatCooperatePayment();
        $res = $wechatCooperatePayment->combinePay('https://api.mch.weixin.qq.com/v3/combine-transactions/jsapi',$data);
        var_dump($res);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙哥·三年风水

感觉写得好,给创作人一点赏赐呗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值