|
|
|
@ -63,6 +63,15 @@ class V3 |
|
|
|
|
$this->config = $this->getConfig($options); |
|
|
|
|
return $this; |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* 微信分账-添加分账人 |
|
|
|
|
* [addReceiver description] |
|
|
|
|
* @param string $type [description] |
|
|
|
|
* @param string $account [description] |
|
|
|
|
* @param string $name [description] |
|
|
|
|
* @param string $relation_type [description] |
|
|
|
|
* @param string $custom_relation [description] |
|
|
|
|
*/ |
|
|
|
|
public function addReceiver(string $type, string $account, string $name, string $relation_type = "HEADQUARTER", string $custom_relation = ""): bool{ |
|
|
|
|
//是否解冻剩余未分资金,暂时请求分账即解冻,后续可看需求调整 |
|
|
|
|
$unfreeze_unsplit = true; |
|
|
|
@ -70,7 +79,7 @@ class V3 |
|
|
|
|
$params = [ |
|
|
|
|
'type' => $type, |
|
|
|
|
'account' => $account, |
|
|
|
|
'name' => $name, |
|
|
|
|
'name' => $this->getEncrypt($name), |
|
|
|
|
'relation_type' => $relation_type, |
|
|
|
|
//'custom_relation' => $custom_relation, |
|
|
|
|
]; |
|
|
|
@ -83,7 +92,7 @@ class V3 |
|
|
|
|
$params['sub_mchid'] = $this->config['sub_mchid']; |
|
|
|
|
} else { |
|
|
|
|
$params['appid'] = $this->config['app_id']; |
|
|
|
|
$params['mchid'] = $this->config['mch_id']; |
|
|
|
|
//$params['mchid'] = $this->config['mch_id']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
@ -91,10 +100,10 @@ class V3 |
|
|
|
|
// Doc: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml |
|
|
|
|
$resp = $this->getApp() |
|
|
|
|
->chain($this->getAddReceiverUrl()) |
|
|
|
|
->post(['json' => $params]); |
|
|
|
|
->post(['json' => $params,'headers' => ['Accept' => 'application/json','Wechatpay-Serial' => $this->platformCertificateSerial]]); |
|
|
|
|
// 记录api返回的数据 |
|
|
|
|
$unifyResult = helper::jsonDecode((string)$resp->getBody()); |
|
|
|
|
var_dump($unifyResult);exit; |
|
|
|
|
//var_dump($unifyResult);exit; |
|
|
|
|
return true; |
|
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
// 异常处理 |
|
|
|
@ -103,15 +112,21 @@ class V3 |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* 微信发起分账 |
|
|
|
|
* [profitsharing description] |
|
|
|
|
* @param string $transaction_id [description] |
|
|
|
|
* @param string $out_order_no [description] |
|
|
|
|
* @param array $receivers [description] |
|
|
|
|
* @return [type] [description] |
|
|
|
|
*/ |
|
|
|
|
public function profitsharing(string $transaction_id, string $out_order_no, array $receivers): bool{ |
|
|
|
|
//是否解冻剩余未分资金,暂时请求分账即解冻,后续可看需求调整 |
|
|
|
|
$unfreeze_unsplit = true; |
|
|
|
|
// 下单的参数 |
|
|
|
|
$params = [ |
|
|
|
|
'out_order_no' => $out_order_no, |
|
|
|
|
'transaction_id' => $transaction_id, |
|
|
|
|
'receivers' => $receivers, |
|
|
|
|
'unfreeze_unsplit' => $unfreeze_unsplit, |
|
|
|
|
'unfreeze_unsplit' => true, |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
// 普通商户参数和服务商支付参数 |
|
|
|
@ -133,7 +148,8 @@ class V3 |
|
|
|
|
->post(['json' => $params]); |
|
|
|
|
// 记录api返回的数据 |
|
|
|
|
$unifyResult = helper::jsonDecode((string)$resp->getBody()); |
|
|
|
|
var_dump($unifyResult);exit; |
|
|
|
|
var_dump($unifyResult); |
|
|
|
|
//exit; |
|
|
|
|
return true; |
|
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
// 异常处理 |
|
|
|
@ -142,6 +158,34 @@ class V3 |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* 分账查询 |
|
|
|
|
* [profitsharingQuery description] |
|
|
|
|
* @param string $out_order_no [description] |
|
|
|
|
* @param string $transaction_id [description] |
|
|
|
|
* @return [type] [description] |
|
|
|
|
*/ |
|
|
|
|
public function profitsharingQuery(string $out_order_no, string $transaction_id): ?array |
|
|
|
|
{ |
|
|
|
|
// 下单的参数 |
|
|
|
|
$params = ['transaction_id' => $transaction_id]; |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
$resp = $this->getApp() |
|
|
|
|
->chain($this->getProfitUrl()."/".$out_order_no) |
|
|
|
|
->get(['query' => $params]); |
|
|
|
|
// 记录api返回的数据 |
|
|
|
|
$result = helper::jsonDecode((string)$resp->getBody()); |
|
|
|
|
var_dump($result); |
|
|
|
|
// 判断订单支付成功 |
|
|
|
|
return $result; |
|
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
// 异常处理 |
|
|
|
|
$message = $this->getThrowMessage($e); |
|
|
|
|
$this->throwError('tradeQuery', "微信支付交易查询失败:{$message}"); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* 统一下单API |
|
|
|
|
* @param string $outTradeNo 交易订单号 |
|
|
|
@ -158,8 +202,11 @@ class V3 |
|
|
|
|
'description' => '线上商城商品', |
|
|
|
|
'notify_url' => $this->notifyUrl(), // 支付结果异步通知地址 |
|
|
|
|
'amount' => ['total' => (int)helper::bcmul($totalFee, 100), 'currency' => 'CNY'], |
|
|
|
|
'scene_info' => ['payer_client_ip' => \request()->ip()] |
|
|
|
|
'scene_info' => ['payer_client_ip' => \request()->ip()], |
|
|
|
|
'settle_info' => ['profit_sharing' => $extra['profit_sharing'] ?? false],//是否指定分账 |
|
|
|
|
]; |
|
|
|
|
// var_dump($params); |
|
|
|
|
// exit(); |
|
|
|
|
// 普通商户参数和服务商支付参数 |
|
|
|
|
if ($this->isProvider()) { |
|
|
|
|
$params['sp_appid'] = $this->config['app_id']; |
|
|
|
@ -511,6 +558,8 @@ class V3 |
|
|
|
|
|
|
|
|
|
// 从「微信支付平台证书」中获取「证书序列号」 |
|
|
|
|
$platformCertificateSerial = PemUtil::parseCertificateSerialNo($platformCertificateFilePath); |
|
|
|
|
//设置类属性 |
|
|
|
|
$this->platformCertificateSerial = $platformCertificateSerial; |
|
|
|
|
|
|
|
|
|
// 构造一个 APIv3 客户端实例 |
|
|
|
|
return Builder::factory([ |
|
|
|
@ -585,6 +634,25 @@ class V3 |
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* 平台私密信息加密算法 |
|
|
|
|
* [getEncrypt description] |
|
|
|
|
* @param [type] $str [description] |
|
|
|
|
* @return [type] [description] |
|
|
|
|
*/ |
|
|
|
|
private function getEncrypt($str) { |
|
|
|
|
//$str是待加密字符串 |
|
|
|
|
$public_key_path = $this->config['platform_cert_path']; |
|
|
|
|
$public_key = file_get_contents($public_key_path); |
|
|
|
|
$encrypted = ''; |
|
|
|
|
if (openssl_public_encrypt($str, $encrypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING)) { |
|
|
|
|
//base64编码 |
|
|
|
|
$sign = base64_encode($encrypted); |
|
|
|
|
} else { |
|
|
|
|
throw new Exception('encrypt failed'); |
|
|
|
|
} |
|
|
|
|
return $sign; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 异步回调地址 |
|
|
|
|