51 lines
1.3 KiB
PHP
51 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace wchat\wx\V3;
|
|
|
|
use Exception;
|
|
use Kiri\Client;
|
|
use Kiri\Message\Stream;
|
|
use wchat\wx\SmallProgram;
|
|
|
|
class WxV3Payment extends SmallProgram
|
|
{
|
|
|
|
|
|
use WxV3PaymentTait;
|
|
|
|
|
|
/**
|
|
* @param $orderNo
|
|
* @param int $total
|
|
* @param string|null $openId
|
|
* @param string $payer_client_ip
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
public function payment($orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
|
{
|
|
$body = $this->getInitCore($orderNo, $total);
|
|
$body['payer'] = ['openid' => $openId];
|
|
$body['scene_info'] = ['payer_client_ip' => $payer_client_ip];
|
|
|
|
$sign = $this->signature('POST', '/v3/pay/transactions/jsapi', $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/jsapi');
|
|
$client->close();
|
|
|
|
$json = json_decode($client->getBody(), TRUE);
|
|
if (!isset($json['prepay_id'])) {
|
|
throw new Exception('微信支付调用失败');
|
|
}
|
|
|
|
return $this->createResponse($json, $body);
|
|
}
|
|
|
|
|
|
}
|