2022-09-09 16:42:55 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace wchat\wx\V2;
|
|
|
|
|
|
|
|
|
|
use Kiri\Client;
|
|
|
|
|
use Phalcon\Cache\Frontend\Json;
|
|
|
|
|
use wchat\common\Help;
|
|
|
|
|
use wchat\common\Result;
|
|
|
|
|
use wchat\wx\SmallProgram;
|
|
|
|
|
|
|
|
|
|
class WxV2PayJsApi extends SmallProgram
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private string $uniformed = '/pay/unifiedorder';
|
|
|
|
|
|
|
|
|
|
use WxV2PayTait;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $money
|
|
|
|
|
* @param string $orderNo
|
|
|
|
|
* @param string $openId
|
|
|
|
|
* @param string $spbill_create_ip
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
|
|
|
|
public function applet(int $money, string $orderNo, string $openId, 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'] = 'JSAPI';
|
|
|
|
|
$body['spbill_create_ip'] = $spbill_create_ip;
|
|
|
|
|
$body['openid'] = $openId;
|
|
|
|
|
|
|
|
|
|
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
|
|
|
|
$client->withHeader(['Content-Type' => 'application/json']);
|
|
|
|
|
$client->withBody($this->sign($body));
|
2023-11-07 15:13:11 +08:00
|
|
|
$proxyHost = $this->getConfig()->getProxyHost();
|
|
|
|
|
$proxyPort = $this->getConfig()->getProxyPort();
|
|
|
|
|
if (!empty($proxyHost) && $proxyPort > 0) {
|
|
|
|
|
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
|
|
|
|
}
|
2022-09-09 16:42:55 +08:00
|
|
|
$client->post($this->uniformed);
|
|
|
|
|
$client->close();
|
|
|
|
|
|
|
|
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
2023-08-18 19:59:46 +08:00
|
|
|
return new Result(code: 505, message: $client->getBody());
|
2022-09-09 16:42:55 +08:00
|
|
|
}
|
|
|
|
|
$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 int $money
|
|
|
|
|
* @param string $orderNo
|
|
|
|
|
* @param string $app_name
|
|
|
|
|
* @param string $package_name
|
|
|
|
|
* @param string $spbill_create_ip
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
|
|
|
|
public function h5Android(int $money, string $orderNo, string $app_name, string $package_name, 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'] = 'MWEB';
|
|
|
|
|
$body['spbill_create_ip'] = $spbill_create_ip;
|
|
|
|
|
$body['scene_info'] = json_encode([
|
|
|
|
|
'h5_info' => [
|
|
|
|
|
'type' => 'Android',
|
|
|
|
|
'app_name' => $app_name,
|
|
|
|
|
'package_name' => $package_name
|
|
|
|
|
]
|
|
|
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
|
|
|
|
|
|
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
|
|
|
|
$client->withHeader(['Content-Type' => 'application/json']);
|
|
|
|
|
$client->withBody($this->sign($body));
|
2023-11-07 15:13:11 +08:00
|
|
|
$proxyHost = $this->getConfig()->getProxyHost();
|
|
|
|
|
$proxyPort = $this->getConfig()->getProxyPort();
|
|
|
|
|
if (!empty($proxyHost) && $proxyPort > 0) {
|
|
|
|
|
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
|
|
|
|
}
|
2022-09-09 16:42:55 +08:00
|
|
|
$client->post($this->uniformed);
|
|
|
|
|
$client->close();
|
|
|
|
|
|
|
|
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
2023-08-18 19:59:46 +08:00
|
|
|
return new Result(code: 505, message: $client->getBody());
|
2022-09-09 16:42:55 +08:00
|
|
|
}
|
|
|
|
|
$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 int $money
|
|
|
|
|
* @param string $orderNo
|
|
|
|
|
* @param string $app_name
|
|
|
|
|
* @param string $bundle_id
|
|
|
|
|
* @param string $spbill_create_ip
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
|
|
|
|
public function h5Ios(int $money, string $orderNo, string $app_name, string $bundle_id, 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'] = 'MWEB';
|
|
|
|
|
$body['spbill_create_ip'] = $spbill_create_ip;
|
|
|
|
|
$body['scene_info'] = json_encode([
|
|
|
|
|
'h5_info' => [
|
|
|
|
|
'type' => 'IOS',
|
|
|
|
|
'app_name' => $app_name,
|
|
|
|
|
'bundle_id' => $bundle_id
|
|
|
|
|
]
|
|
|
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
|
|
|
|
|
|
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
|
|
|
|
$client->withHeader(['Content-Type' => 'application/json']);
|
|
|
|
|
$client->withBody($this->sign($body));
|
2023-11-07 15:13:11 +08:00
|
|
|
$proxyHost = $this->getConfig()->getProxyHost();
|
|
|
|
|
$proxyPort = $this->getConfig()->getProxyPort();
|
|
|
|
|
if (!empty($proxyHost) && $proxyPort > 0) {
|
|
|
|
|
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
|
|
|
|
}
|
2022-09-09 16:42:55 +08:00
|
|
|
$client->post($this->uniformed);
|
|
|
|
|
$client->close();
|
|
|
|
|
|
|
|
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
2023-08-18 19:59:46 +08:00
|
|
|
return new Result(code: 505, message: $client->getBody());
|
2022-09-09 16:42:55 +08:00
|
|
|
}
|
|
|
|
|
$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 int $money
|
|
|
|
|
* @param string $orderNo
|
|
|
|
|
* @param string $wap_url
|
|
|
|
|
* @param string $wap_name
|
|
|
|
|
* @param string $spbill_create_ip
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
|
|
|
|
public function h5(int $money, string $orderNo, string $wap_url, string $wap_name, 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'] = 'MWEB';
|
|
|
|
|
$body['spbill_create_ip'] = $spbill_create_ip;
|
|
|
|
|
$body['scene_info'] = json_encode([
|
|
|
|
|
'h5_info' => [
|
|
|
|
|
'type' => 'IOS',
|
|
|
|
|
'wap_url' => $wap_url,
|
|
|
|
|
'wap_name' => $wap_name
|
|
|
|
|
]
|
|
|
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
|
|
|
|
|
|
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
|
|
|
|
$client->withHeader(['Content-Type' => 'application/json']);
|
|
|
|
|
$client->withBody($this->sign($body));
|
2023-11-07 15:13:11 +08:00
|
|
|
$proxyHost = $this->getConfig()->getProxyHost();
|
|
|
|
|
$proxyPort = $this->getConfig()->getProxyPort();
|
|
|
|
|
if (!empty($proxyHost) && $proxyPort > 0) {
|
|
|
|
|
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
|
|
|
|
}
|
2022-09-09 16:42:55 +08:00
|
|
|
$client->post($this->uniformed);
|
|
|
|
|
$client->close();
|
|
|
|
|
|
|
|
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
2023-08-18 19:59:46 +08:00
|
|
|
return new Result(code: 505, message: $client->getBody());
|
2022-09-09 16:42:55 +08:00
|
|
|
}
|
|
|
|
|
$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([
|
|
|
|
|
'signType' => $this->config->getSignType(),
|
|
|
|
|
'package' => 'prepay_id=' . $prepay_id,
|
|
|
|
|
'nonceStr' => Help::random(32),
|
|
|
|
|
'timestamp' => time()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|