2019-10-25 15:20:09 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/3/26 0026
|
|
|
|
|
* Time: 10:22
|
|
|
|
|
*/
|
|
|
|
|
|
2020-03-05 12:41:49 +08:00
|
|
|
namespace wchat\qq;
|
2019-10-25 15:20:09 +08:00
|
|
|
|
2020-05-21 18:16:48 +08:00
|
|
|
use Exception;
|
2022-09-09 16:42:55 +08:00
|
|
|
use Kiri\Client;
|
2020-03-05 12:41:49 +08:00
|
|
|
use wchat\common\HttpClient;
|
|
|
|
|
use wchat\common\Result;
|
|
|
|
|
use wchat\common\Help;
|
2019-10-25 15:20:09 +08:00
|
|
|
|
2019-11-11 18:14:47 +08:00
|
|
|
class Recharge extends SmallProgram
|
2019-10-25 15:20:09 +08:00
|
|
|
{
|
|
|
|
|
private $money = 0;
|
|
|
|
|
|
|
|
|
|
private $orderNo;
|
|
|
|
|
|
|
|
|
|
private $data = [];
|
|
|
|
|
|
2020-05-21 18:18:36 +08:00
|
|
|
private $spill_create_ip = '';
|
2020-05-21 18:17:51 +08:00
|
|
|
|
2020-05-21 18:18:36 +08:00
|
|
|
private $uniformer = 'https://qpay.qq.com/cgi-bin/pay/qpay_unified_order.cgi';
|
2019-10-25 15:20:09 +08:00
|
|
|
|
2020-05-21 18:17:51 +08:00
|
|
|
/**
|
2021-04-06 15:20:39 +08:00
|
|
|
* @param $value
|
2020-05-21 18:17:51 +08:00
|
|
|
*/
|
2021-04-06 15:20:39 +08:00
|
|
|
public function setSpillCreateIp($value)
|
2020-05-21 18:17:51 +08:00
|
|
|
{
|
2020-05-21 18:18:36 +08:00
|
|
|
$this->spill_create_ip = $value;
|
2020-05-21 18:17:51 +08:00
|
|
|
}
|
|
|
|
|
|
2019-10-25 15:20:09 +08:00
|
|
|
/**
|
|
|
|
|
* @param int $money
|
2022-09-09 16:42:55 +08:00
|
|
|
* @param string $orderNo
|
|
|
|
|
* @param string $openId
|
|
|
|
|
* @return Result
|
2019-10-25 15:20:09 +08:00
|
|
|
* @throws
|
|
|
|
|
*/
|
2022-09-09 16:42:55 +08:00
|
|
|
public function recharge(int $money, string $orderNo, string $openId = ''): Result
|
2019-10-25 15:20:09 +08:00
|
|
|
{
|
|
|
|
|
if ($money < 0) {
|
2022-09-09 16:42:55 +08:00
|
|
|
return new Result(code: 500, message: '充值金额不能小于0.');
|
2019-10-25 15:20:09 +08:00
|
|
|
}
|
|
|
|
|
$this->money = $money;
|
|
|
|
|
$this->orderNo = $orderNo;
|
|
|
|
|
$this->data['openid'] = $openId;
|
|
|
|
|
|
2022-09-09 16:42:55 +08:00
|
|
|
$client = new Client('qpay.qq.com', 443, true);
|
|
|
|
|
$client->withHeader(['Content-Type' => 'application/xml']);
|
|
|
|
|
$client->withBody($this->builder());
|
|
|
|
|
$client->post($this->uniformer);
|
|
|
|
|
$client->close();
|
|
|
|
|
|
|
|
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
|
|
|
|
return new Result(code: 505, message: 'network error.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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 Help::sign([
|
|
|
|
|
'signType' => $this->config->getSignType(),
|
|
|
|
|
'package' => 'prepay_id=' . $prepay_id,
|
|
|
|
|
'nonceStr' => Help::random(32),
|
|
|
|
|
'timestamp' => time()
|
|
|
|
|
], $this->getConfig()->getKey(), $this->getConfig()->getSignType());
|
2019-10-25 15:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2022-09-09 16:42:55 +08:00
|
|
|
protected function builder(): string
|
2019-10-25 15:20:09 +08:00
|
|
|
{
|
|
|
|
|
$data = [
|
|
|
|
|
'appid' => $this->config->getAppid(),
|
|
|
|
|
'mch_id' => $this->config->getMchId(),
|
|
|
|
|
'nonce_str' => Help::random(32),
|
|
|
|
|
'body' => $this->config->getBody(),
|
|
|
|
|
'out_trade_no' => $this->orderNo,
|
|
|
|
|
'total_fee' => $this->money,
|
2020-05-21 18:18:36 +08:00
|
|
|
'spbill_create_ip' => $this->spill_create_ip,
|
2019-10-25 15:20:09 +08:00
|
|
|
'notify_url' => $this->config->getNotifyUrl(),
|
|
|
|
|
'trade_type' => $this->config->getTradeType(),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->data = array_merge($data, $this->data);
|
|
|
|
|
|
|
|
|
|
$key = $this->config->getKey();
|
|
|
|
|
$sign_type = $this->config->getSignType();
|
|
|
|
|
|
|
|
|
|
$this->data['sign_type'] = $this->config->getSignType();
|
|
|
|
|
$this->data['sign'] = Help::sign($this->data, $key, $sign_type);
|
|
|
|
|
|
|
|
|
|
return Help::toXml($this->data);
|
|
|
|
|
}
|
|
|
|
|
}
|