diff --git a/wx/V3/TransferBatches.php b/wx/V3/TransferBatches.php index 3d28265..cf4bbad 100644 --- a/wx/V3/TransferBatches.php +++ b/wx/V3/TransferBatches.php @@ -17,7 +17,7 @@ class TransferBatches extends SmallProgram * @return array */ #[ArrayShape([])] - public function transfer(string $transfer_scene_id ,array $transfer_scene_report_infos ,TransferDetail $detail): array + public function transfer(string $transfer_scene_id, array $transfer_scene_report_infos, TransferDetail $detail): array { $payConfig = $this->getPayConfig(); $body = []; @@ -26,25 +26,22 @@ class TransferBatches extends SmallProgram } else { $body['appid'] = $payConfig->appId; } - $body['out_batch_no'] = $detail->out_detail_no; - $body["batch_name"] = $payConfig->getBody(); - $body["body"] = $payConfig->getBody(); - $body["batch_remark"] = $payConfig->getBody(); - $body["total_amount"] = $detail->transfer_amount; - $body["total_num"] = 1; + $body['out_bill_no'] = $detail->out_detail_no; $body["transfer_scene_id"] = $transfer_scene_id; + $body['openid'] = $detail->openid; + $body["transfer_amount"] = $detail->transfer_amount; + $body["transfer_remark"] = $detail->transfer_remark; $body["transfer_scene_report_infos"] = $transfer_scene_report_infos; - $body["transfer_detail_list"] = [$detail->toArray()]; - $sign = $this->signature('POST' ,'/v3/fund-app/mch-transfer/transfer-bills' ,$json = json_encode($body ,JSON_UNESCAPED_UNICODE)); + $sign = $this->signature('POST', '/v3/fund-app/mch-transfer/transfer-bills', $json = json_encode($body, JSON_UNESCAPED_UNICODE)); - $client = $this->createClient($sign ,$json); + $client = $this->createClient($sign, $json); $client->post('/v3/fund-app/mch-transfer/transfer-bills'); $client->close(); - $data = json_decode($client->getBody() ,TRUE); + $data = json_decode($client->getBody(), TRUE); if (json_last_error() != JSON_ERROR_NONE) { - return ['code' => $client->getStatusCode() ,'message' => $client->getBody()]; + return ['code' => $client->getStatusCode(), 'message' => $client->getBody()]; } else { return $data; } diff --git a/wx/V3/TransferDetail.php b/wx/V3/TransferDetail.php index 765e5d6..8c70a05 100644 --- a/wx/V3/TransferDetail.php +++ b/wx/V3/TransferDetail.php @@ -2,51 +2,223 @@ namespace wchat\wx\V3; -use JetBrains\PhpStorm\ArrayShape; +use Exception; class TransferDetail { + private string $out_bill_no; + private string $transfer_scene_id; + private string $openid; + private string $user_name = ''; + private int $transfer_amount; + private string $transfer_remark; + private string $notify_url = ''; + private string $user_recv_perception = ''; + private array $transfer_scene_report_infos; + + /** - * @param string $out_detail_no - * @param int|float $transfer_amount - * @param string $transfer_remark - * @param string $openid - * @param string $user_name + * @return string */ - public function __construct( - public string $out_detail_no, - public int|float $transfer_amount, - public string $transfer_remark, - public string $openid, - public string $user_name = '' - ) + public function getOutBillNo(): string { + return $this->out_bill_no; + } + + + /** + * @param string $out_bill_no + * @return void + */ + public function setOutBillNo(string $out_bill_no): void + { + $this->out_bill_no = $out_bill_no; + } + + + /** + * @return string + */ + public function getTransferSceneId(): string + { + return $this->transfer_scene_id; + } + + + /** + * @param string $transfer_scene_id + * @return void + */ + public function setTransferSceneId(string $transfer_scene_id): void + { + $this->transfer_scene_id = $transfer_scene_id; + } + + + /** + * @return string + */ + public function getOpenid(): string + { + return $this->openid; + } + + + /** + * @param string $openid + * @return void + */ + public function setOpenid(string $openid): void + { + $this->openid = $openid; + } + + + /** + * @return string + */ + public function getUserName(): string + { + return $this->user_name; + } + + + /** + * @param string $user_name + * @return void + */ + public function setUserName(string $user_name): void + { + $this->user_name = $user_name; + } + + + /** + * @return int + */ + public function getTransferAmount(): int + { + return $this->transfer_amount; + } + + + /** + * @param int $transfer_amount + * @return void + */ + public function setTransferAmount(int $transfer_amount): void + { + $this->transfer_amount = $transfer_amount; + } + + + /** + * @return string + */ + public function getTransferRemark(): string + { + return $this->transfer_remark; + } + + + /** + * @param string $transfer_remark + * @return void + */ + public function setTransferRemark(string $transfer_remark): void + { + $this->transfer_remark = $transfer_remark; + } + + + /** + * @return string + */ + public function getNotifyUrl(): string + { + return $this->notify_url; + } + + + /** + * @param string $notify_url + * @return void + */ + public function setNotifyUrl(string $notify_url): void + { + $this->notify_url = $notify_url; + } + + + /** + * @return string + */ + public function getUserRecvPerception(): string + { + return $this->user_recv_perception; + } + + + /** + * @param string $user_recv_perception + * @return void + */ + public function setUserRecvPerception(string $user_recv_perception): void + { + $this->user_recv_perception = $user_recv_perception; } /** * @return array */ - #[ArrayShape(['out_detail_no' => "string", 'transfer_amount' => "float|int", 'transfer_remark' => "string", 'openid' => "string", 'user_name' => "string"])] + public function getTransferSceneReportInfos(): array + { + return $this->transfer_scene_report_infos; + } + + + /** + * @param array $transfer_scene_report_infos + * @return void + */ + public function setTransferSceneReportInfos(array $transfer_scene_report_infos): void + { + $this->transfer_scene_report_infos = $transfer_scene_report_infos; + } + + + /** + * @return array + * @throws Exception + */ public function toArray(): array { - if (empty($this->user_name)) { - return [ - 'out_detail_no' => $this->out_detail_no, - 'transfer_amount' => $this->transfer_amount, - 'transfer_remark' => $this->transfer_remark, - 'openid' => $this->openid, - ]; - } - return [ - 'out_detail_no' => $this->out_detail_no, - 'transfer_amount' => $this->transfer_amount, - 'transfer_remark' => $this->transfer_remark, - 'openid' => $this->openid, - 'user_name' => $this->user_name, + $array = [ + 'out_bill_no' => $this->out_bill_no, + 'transfer_scene_id' => $this->transfer_scene_id, + 'openid' => $this->openid, + 'transfer_amount' => $this->transfer_amount, + 'transfer_remark' => $this->transfer_remark, + 'transfer_scene_report_infos' => $this->transfer_scene_report_infos, ]; + foreach ($array as $key => $value) { + if (empty($value)) { + throw new Exception('必填项' . $key . '不能为空.'); + } + } + if (!empty($this->user_name)) { + $array['user_name'] = $this->user_name; + } + if (!empty($this->notify_url)) { + $array['notify_url'] = $this->notify_url; + } + if (!empty($this->user_recv_perception)) { + $array['user_recv_perception'] = $this->user_recv_perception; + } + return $array; } } diff --git a/wx/V3/WxV3Transfer.php b/wx/V3/WxV3Transfer.php new file mode 100644 index 0000000..3dc07c6 --- /dev/null +++ b/wx/V3/WxV3Transfer.php @@ -0,0 +1,54 @@ + + * @throws Exception + */ + #[ArrayShape([])] + public function transfer(TransferDetail $detail): array + { + $payConfig = $this->getPayConfig(); + $body = $detail->toArray(); + if ($payConfig->typeIsApp()) { + $body['appid'] = $payConfig->pay->wx->appId; + } else { + $body['appid'] = $payConfig->appId; + } + $sign = $this->signature('POST', '/v3/fund-app/mch-transfer/transfer-bills', $json = json_encode($body, JSON_UNESCAPED_UNICODE)); + + $client = $this->createClient($sign, $json); + $client->post('/v3/fund-app/mch-transfer/transfer-bills'); + $client->close(); + + if ($client->getStatusCode() == 200) { + return json_decode($client->getBody(), TRUE); + } + throw new Exception('转账申请发起失败'); + } + + +}