Files
kiri-wchat/wchat/qq/Recharge.php
T

117 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-05-21 18:16:48 +08:00
use Exception;
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
2021-04-06 15:20:39 +08:00
* @param $orderNo
* @param $openId
2019-10-25 15:20:09 +08:00
* @return array|mixed|Result
* @throws
*/
2021-04-06 15:20:39 +08:00
public function recharge(int $money, $orderNo, $openId = '')
2019-10-25 15:20:09 +08:00
{
if ($money < 0) {
return new Result(['code' => 500, 'message' => '充值金额不能小于0.']);
}
$this->money = $money;
$this->orderNo = $orderNo;
$this->data['openid'] = $openId;
2020-05-21 18:16:48 +08:00
$this->request->setCallback(function ($result, $body) {
2019-10-25 15:20:09 +08:00
$data = Help::toArray($result);
if (isset($data['sign'])) {
$sign = $data['sign'];
unset($data['sign']);
}
2020-03-09 16:20:25 +08:00
if (!isset($sign)) {
$return['code'] = -1;
$return['message'] = '返回数据签名验证失败';
} else {
$return['code'] = 0;
$return['data'] = $data;
2020-05-21 18:18:36 +08:00
$return['data']['postBody'] = $body;
2020-03-09 16:20:25 +08:00
if ($data['return_code'] == 'FAIL') {
$return['code'] = -1;
$return['message'] = $data['return_msg'];
}
2020-03-09 15:52:20 +08:00
}
2020-01-03 18:32:41 +08:00
return new Result($return);
2019-10-25 15:20:09 +08:00
});
2020-05-21 18:18:36 +08:00
return $this->send($this->uniformer, $this->builder());
2019-10-25 15:20:09 +08:00
}
/**
* @return string
*/
protected function builder()
{
$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);
}
/**
* @param $url
* @param $data
* @return array|mixed|Result
2020-05-21 18:16:48 +08:00
* @throws Exception
2019-10-25 15:20:09 +08:00
*/
private function send($url, $data)
{
$this->request->setIsSSL(true);
2019-11-11 18:14:47 +08:00
$this->request->setMethod(HttpClient::POST);
2019-10-25 15:20:09 +08:00
$this->request->addHeader('Content-Type', 'text/xml');
return $this->request->send($url, $data);
}
}