前面文章介绍了服务端和小程序端实现微信支付API v3的整个流程,现在介绍下微信支付API v3-分账功能的实现。
首先,我们可以自行参考下: 微信支付官方API文档-分账
上代码:
// 微信支付分账方法封装
public function profitSendV3($transaction_id,$merchant=[]){
$out_order_no = mt_rand(100000,999999).time().mt_rand(100,999);
//是否解冻剩余未分资金,暂时请求分账即解冻,后续可看需求调整
$unfreeze_unsplit = true;
//组合分账数据
list($receivers,$moneyUseTotal) = $this->getReceivers($order,$product);
$newPara["receivers"] = json_encode($receivers);
$newPara["appid"] = $this->config['app_id'];
// 省略部分取数据代码
// ...
try {
$json = [
'out_order_no' => $out_order_no,
'appid' => $newPara["appid"],
'transaction_id' => $transaction_id,
'receivers' => $receivers,
'unfreeze_unsplit' => $unfreeze_unsplit,
];
$resp = $this->instance
->chain('v3/profitsharing/orders')
->post(
['headers'=> [ //这里加这个字段
'Wechatpay-Serial'=>$this->platformCertificateSerial // 这里加上Wechatpay-Serial
],
'json' => $json
]);
$result = json_decode($resp->getBody(), true);
ProcessReceivers::dispatch($this->mer_id,$transaction_id,$out_order_no)->delay(now()->addSecond(3));
return 'SUCCESS';
} catch (\Exception $e) {
// 进行错误处理
$this->err_msg = $e->getMessage();
if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
$r = $e->getResponse();
$result = $r->getBody();
$result = json_decode($result,true);
$this->err_code = $result['code'];
$this->err_msg = $result['message'];
}
return false;
}
}
微信支付-分账的应用场景还是很多的,比如普通商家的收入分账给平台、分销员来进行分佣操作。