{
"appid": "wxc5af960e4dd79f8f",
"transaction_id": "4200001419202205139167858506",
"out_order_no": "P202205131103762364215",
"receivers": [{
"type": "MERCHANT_ID",
"account": "1614276273",
"name": "",
"amount": 3,
"description": "分给商户爱尔福"
}],
"unfreeze_unsplit": true
}
以下是我的分账代码
public static function profitSharing($transaction_id,$amount) { // 商户相关配置, $merchantId = config('wechat.wechat_mch_id'); // 商户号 $merchantSerialNumber = '2278A992C067DCE19C8328B027B6B41D90DC2349'; // 商户API证书序列号 $merchantPrivateKey = PemUtil::loadPrivateKey(config_path().'/cert/apiclient_key.pem'); // 商户私钥文件路径 // 微信支付平台配置 $wechatpayCertificate = PemUtil::loadCertificate(config_path().'/cert/wechatpay_platform.pem'); // 微信支付平台证书文件路径 // 构造一个WechatPayMiddleware $wechatpayMiddleware = WechatPayMiddleware::builder() ->withMerchant($merchantId, $merchantSerialNumber, $merchantPrivateKey) // 传入商户相关配置 ->withWechatPay([ $wechatpayCertificate ]) // 可传入多个微信支付平台证书,参数类型为array ->build(); // 将WechatPayMiddleware添加到Guzzle的HandlerStack中 $stack = \GuzzleHttp\HandlerStack::create(); $stack->push($wechatpayMiddleware, 'wechatpay'); // 创建Guzzle HTTP Client时,将HandlerStack传入,接下来,正常使用Guzzle发起API请求,WechatPayMiddleware会自动地处理签名和验签 $client = new \GuzzleHttp\Client(['handler' => $stack]); //$pay_time_expire_minutes = env('TIME_EXPIRE'); //$time_expire = Carbon::parse('+'.$pay_time_expire_minutes.'minutes')->toW3cString(); $header = array( "Content-Type: application/json", "Accept: application/json", 'Content-Type: application/json; charset=utf-8', 'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36', 'Expect:', ); $out_order_no = 'P'.orderNO(mt_rand(0,99)); $receiver = array( 'type' => 'MERCHANT_ID', 'account' => '1614276273', // 爱尔福文化创意咨询商户号 'name' => '', 'amount' => $amount, // 分账金额,单位为分,只能为整数,不能超过原订单支付金额及最大分账比例金额 'description' => '分给商户爱尔福', // 分账描述 ); $param = array( 'appid' => config('wechat.wechat_small_appid'), // appid 'transaction_id' => $transaction_id, // 微信订单号 'out_order_no' => $out_order_no, // 商户分账单号 'receivers' => [$receiver], // 分账接收方列表 'unfreeze_unsplit'=> true, // 是否解冻剩余未分资金 ); Log::info('请求分账:'); Log::info(json_encode($param)); try { $resp = $client->request( 'POST', 'https://api.mch.weixin.qq.com/v3/profitsharing/orders', //请求URL [ // JSON请求体 'json' => $param, 'headers' => $header ] ); $statusCode = $resp->getStatusCode(); Log::info(json_encode($resp)); } catch (RequestException $e) { // 进行错误处理 $msg = $e->getMessage()."\n"; $code = 10001; Log::info(json_encode($e)); if ($e->hasResponse()) { $code = $e->getResponse()->getStatusCode(); $msg = $e->getResponse()->getBody(); } $ret = [ 'code' => $code, 'msg' => $msg, ]; Log::info(json_encode($ret)); } }