Files
kiri-wchat/wx/V3/WxV3Withdrawal.php
T

100 lines
3.1 KiB
PHP
Raw Normal View History

2022-09-09 16:42:55 +08:00
<?php
namespace wchat\wx\V3;
use Exception;
use JetBrains\PhpStorm\ArrayShape;
use wchat\wx\SmallProgram;
2025-02-24 17:10:45 +08:00
use wchat\wx\V3\Libs\TransferDetail;
2022-09-09 16:42:55 +08:00
class WxV3Withdrawal extends SmallProgram
{
2023-11-07 15:13:11 +08:00
use WxV3PaymentTait;
2022-09-09 16:42:55 +08:00
2023-11-07 15:13:11 +08:00
/**
2023-11-14 14:10:36 +08:00
* @param string $orderNo
2023-11-07 15:13:11 +08:00
* @param string $batch_name
* @param string $batch_remark
* @param TransferDetail[] $details
* @return array
2023-12-12 15:35:37 +08:00
* @throws
2023-11-07 15:13:11 +08:00
*/
2023-11-14 14:10:36 +08:00
public function payment(string $orderNo, string $batch_name, string $batch_remark, array $details): array
2023-11-07 15:13:11 +08:00
{
$body = $this->create($orderNo, $batch_name, $batch_remark, $details);
2022-09-09 16:42:55 +08:00
2023-11-07 15:13:11 +08:00
$sign = $this->signature('POST', '/v3/pay/transactions/batches', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
2022-09-09 16:42:55 +08:00
2023-11-13 21:20:10 +08:00
$client = $this->createClient($sign, $json);
2023-11-07 15:13:11 +08:00
$client->post('/v3/pay/transactions/batches');
$client->close();
2022-09-09 16:42:55 +08:00
2023-11-07 15:13:11 +08:00
$json = json_decode($client->getBody(), TRUE);
if (!isset($json['prepay_id'])) {
throw new Exception('微信支付调用失败');
}
2022-09-09 16:42:55 +08:00
2023-11-07 15:13:11 +08:00
return $this->createResponse($json, $body);
}
2022-09-09 16:42:55 +08:00
2023-11-14 14:10:36 +08:00
/**
* @param string $orderNo
* @param string $batch_name
* @param string $batch_remark
* @param array $details
* @return array[]
*/
2023-11-07 15:13:11 +08:00
#[ArrayShape(['transfer_detail_list' => "array", 'total_amount' => "int", 'total_num' => "int", 'batch_remark' => "string", 'batch_name' => "string", 'out_batch_no' => ""])]
2023-11-14 14:10:36 +08:00
private function create(string $orderNo, string $batch_name, string $batch_remark, array $details): array
2023-11-07 15:13:11 +08:00
{
$total = 0;
$body = ['transfer_detail_list' => []];
$body['out_batch_no'] = $orderNo;
$body['batch_name'] = $batch_name;
$body['batch_remark'] = $batch_remark;
$body['total_num'] = count($details);
foreach ($details as $detail) {
$total += $detail->transfer_amount;
$body['transfer_detail_list'][] = $detail->toArray();
}
$body['total_amount'] = $total;
return $body;
}
2022-09-09 16:42:55 +08:00
2025-07-08 11:43:14 +08:00
/**
* @param TransferDetail $detail
* @return array<'out_bill_no', 'transfer_bill_no', 'create_time', 'state'>
* @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();
Kiri::getLogger()->println($client->getBody());
if ($client->getStatusCode() == 200) {
return json_decode($client->getBody(), TRUE);
}
throw new Exception('转账申请发起失败');
}
2022-09-09 16:42:55 +08:00
}