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

48 lines
1.2 KiB
PHP
Raw Normal View History

2022-09-09 16:42:55 +08:00
<?php
namespace wchat\wx\V3;
use Exception;
use Kiri\Client;
use Kiri\Message\Stream;
use JetBrains\PhpStorm\ArrayShape;
use wchat\wx\SmallProgram;
class WxV3NativePayment extends SmallProgram
{
use WxV3PaymentTait;
/**
* @param $orderNo
* @param int $total
* @param string|null $openId
* @param string $payer_client_ip
* @return array
* @throws Exception
*/
#[ArrayShape(['code_url' => "string"])]
public function payment($orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
{
$body = $this->getInitCore($orderNo, $total);
$sign = $this->signature('POST', '/v3/pay/transactions/native', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
$client = new Client('api.mch.weixin.qq.com', 443, TRUE);
$client->withAddedHeader('Authorization', $sign)->withContentType('application/json')
->withAgent('application/json')->withBody(new Stream($json))
->post('/v3/pay/transactions/native');
$client->close();
$json = json_decode($client->getBody(), TRUE);
if (!isset($json['code_url'])) {
throw new Exception('微信支付调用失败');
}
return ['code_url' => $json['code_url']];
}
}