合单支付基本在加盟店或是分公司或是营销系统里面常见。他的出现,打破了传统提现支付或是转账支付。他的业务原理其实很简单,就是需要优先申请非普通商户,其次是每个入驻的商户都需要申请普通商户。在这之前一定要申请好对应的场景服务,比如公众号支付就需要优先申请公众号,小程序支付就需要优先申请小程序,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);
}
标签:arr,timestamp,url,微信,合单,combine,支付,data
From: https://blog.csdn.net/m0_63603104/article/details/142591978