Files
kiri-wchat/wchat/wx/V3/WxV3Withdrawal.php
T
2022-09-09 16:42:55 +08:00

68 lines
1.8 KiB
PHP

<?php
namespace wchat\wx\V3;
use Exception;
use JetBrains\PhpStorm\ArrayShape;
use Kiri\Client;
use Kiri\Message\Stream;
use wchat\wx\SmallProgram;
class WxV3Withdrawal extends SmallProgram
{
use WxV3PaymentTait;
/**
* @param $orderNo
* @param string $batch_name
* @param string $batch_remark
* @param TransferDetail[] $details
* @return array
* @throws Exception
*/
public function payment($orderNo, string $batch_name, string $batch_remark, array $details): array
{
$body = $this->create($orderNo, $batch_name, $batch_remark, $details);
$sign = $this->signature('POST', '/v3/pay/transactions/batches', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
$client = new Client('api.mch.weixin.qq.com', 443, TRUE);
$client->withAddedHeader('Authorization', $sign)
->withContentType('application/json')
->withAddedHeader('User-Agent', 'application/json')
->withBody(new Stream($json))
->post('/v3/pay/transactions/batches');
$client->close();
$json = json_decode($client->getBody(), TRUE);
if (!isset($json['prepay_id'])) {
throw new Exception('微信支付调用失败');
}
return $this->createResponse($json, $body);
}
#[ArrayShape(['transfer_detail_list' => "array", 'total_amount' => "int", 'total_num' => "int", 'batch_remark' => "string", 'batch_name' => "string", 'out_batch_no' => ""])]
private function create($orderNo, string $batch_name, string $batch_remark, array $details): array
{
$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;
}
}