Files
kiri-wchat/wx/V2/WxV2AppPayment.php
T
2023-11-14 00:15:27 +08:00

78 lines
2.0 KiB
PHP

<?php
namespace wchat\wx\V2;
use Kiri\Client;
use wchat\common\Help;
use wchat\common\Result;
use wchat\wx\SmallProgram;
class WxV2AppPayment extends SmallProgram
{
private string $uniformed = '/pay/unifiedorder';
use WxV2PayTait;
/**
* @param int $money
* @param string $orderNo
* @param string $spbill_create_ip
* @return Result
*/
public function payment(int $money, string $orderNo, string $spbill_create_ip = '127.0.0.1'): Result
{
if ($money < 0) {
return new Result(code: 400, message: '充值金额不能小于0.');
}
$body = $this->getInitCore($orderNo, $money);
$body['trade_type'] = 'APP';
$body['spbill_create_ip'] = $spbill_create_ip;
$client = new Client('api.mch.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
$client->withBody($this->sign($body));
$client->post($this->uniformed);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$data = Help::toArray($client->getBody());
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
return new Result(code: 503, message: $data['return_msg']);
}
if ($data['result_code'] != 'SUCCESS') {
return new Result(code: 504, message: $data['err_code_des']);
}
return new Result(code: 0, data: $this->reception($data['prepayid']));
}
/**
* @param string $prepay_id
* @return string
*/
public function reception(string $prepay_id): string
{
return $this->sign([
'appId' => $this->config->getAppid(),
'partnerid' => $this->config->getMchId(),
'prepayid' => $prepay_id,
'package' => 'Sign=WXPay',
'noncestr' => Help::random(32),
'timestamp' => time()
]);
}
}