Files
kiri-wchat/qq/Recharge.php
T

95 lines
2.6 KiB
PHP
Raw Normal View History

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-03-05 12:41:49 +08:00
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
{
2023-08-18 15:31:47 +08:00
private float $money = 0;
private string $orderNo;
private array $data = [];
private string $spill_create_ip = '';
private string $uniformer = '/cgi-bin/pay/qpay_unified_order.cgi';
2023-11-14 01:08:06 +08:00
protected string $host = 'qpay.qq.com';
2023-08-18 15:31:47 +08:00
/**
2023-11-14 14:14:42 +08:00
* @param string $value
2023-08-18 15:31:47 +08:00
*/
2023-12-12 15:35:37 +08:00
public function setSpillCreateIp(string $value): void
2023-08-18 15:31:47 +08:00
{
$this->spill_create_ip = $value;
}
/**
* @param int $money
* @param string $orderNo
* @param string $openId
* @return Result
* @throws
*/
public function recharge(int $money, string $orderNo, string $openId = ''): Result
{
if ($money < 0) {
return new Result(code: 500, message: '充值金额不能小于0.');
}
2023-11-14 01:08:06 +08:00
$this->money = $money;
$this->orderNo = $orderNo;
2023-08-18 15:31:47 +08:00
$this->data['openid'] = $openId;
2023-11-14 12:39:28 +08:00
return $this->post('api.q.qq.com', $this->uniformer, $this->builder(), 'application/xml');
2023-08-18 15:31:47 +08:00
}
/**
* @param string $prepay_id
* @return string
*/
public function reception(string $prepay_id): string
{
return Help::sign([
2023-11-14 01:08:06 +08:00
'signType' => $this->payConfig->getSignType(),
2023-08-18 15:31:47 +08:00
'package' => 'prepay_id=' . $prepay_id,
'nonceStr' => Help::random(32),
'timestamp' => time()
2023-11-14 01:08:06 +08:00
], $this->payConfig->pay->qq->mchSecret, $this->payConfig->getSignType());
2023-08-18 15:31:47 +08:00
}
/**
* @return string
*/
protected function builder(): string
{
2023-11-14 01:08:06 +08:00
$data = [
'appid' => $this->payConfig->appId,
'mch_id' => $this->payConfig->pay->qq->mchId,
2023-08-18 15:31:47 +08:00
'nonce_str' => Help::random(32),
2023-11-14 01:08:06 +08:00
'body' => $this->payConfig->getBody(),
2023-08-18 15:31:47 +08:00
'out_trade_no' => $this->orderNo,
'total_fee' => $this->money,
'spbill_create_ip' => $this->spill_create_ip,
2023-11-14 01:08:06 +08:00
'notify_url' => $this->payConfig->getNotifyUrl(),
'trade_type' => $this->payConfig->getTradeType(),
2023-08-18 15:31:47 +08:00
];
2023-11-14 01:08:06 +08:00
$this->data = array_merge($data, $this->data);
$key = $this->payConfig->pay->qq->mchSecret;
$sign_type = $this->payConfig->getSignType();
$this->data['sign_type'] = $this->payConfig->getSignType();
$this->data['sign'] = Help::sign($this->data, $key, $sign_type);
2023-08-18 15:31:47 +08:00
return Help::toXml($this->data);
}
2019-10-25 15:20:09 +08:00
}