Files
kiri-wchat/wx/Recharge.php
T

163 lines
3.7 KiB
PHP
Raw Normal View History

2018-07-19 12:10:22 +08:00
<?php
2019-05-21 16:25:00 +08:00
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/26 0026
* Time: 10:22
*/
2018-07-19 19:18:22 +08:00
2018-07-19 12:10:22 +08:00
namespace wchat;
2019-07-17 17:17:37 +08:00
class Recharge extends Miniprogarampage
2018-07-19 12:10:22 +08:00
{
2019-07-12 19:25:47 +08:00
private $money = 0;
private $orderNo;
private $data = [];
2019-07-17 17:17:37 +08:00
private $transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
private $unifiedorder = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
2019-07-12 19:25:47 +08:00
/**
* @param int $money
* @param string $orderNo
2019-07-17 17:17:37 +08:00
* @param string $openId
* @return array|mixed|Result
* @throws
2019-07-12 19:25:47 +08:00
*/
2019-07-17 17:17:37 +08:00
public function recharge(int $money, string $orderNo, $openId = '')
2019-07-12 19:25:47 +08:00
{
if ($money < 0) {
return new Result(['code' => 500, 'message' => '充值金额不能小于0.']);
}
$this->money = $money;
$this->orderNo = $orderNo;
$this->data['openid'] = $openId;
2019-07-17 17:17:37 +08:00
$this->request->setCallback([$this, 'payCallback']);
return $this->send($this->unifiedorder, $this->builder());
2019-07-12 19:25:47 +08:00
}
/**
* @param $result
* @param $body
* @return array
*/
public function payCallback($result, $body)
{
$data = Help::toArray($result);
if (isset($data['sign'])) {
$sign = $data['sign'];
unset($data['sign']);
}
$return = [];
2019-07-17 17:17:37 +08:00
$_sign = Help::sign($data, $this->config->getKey(), $this->config->getSignType());
2019-07-12 19:25:47 +08:00
if (!isset($sign) || $sign != $_sign) {
$return['code'] = -1;
$return['message'] = $data['return_msg'] ?? '返回数据签名验证失败';
} else {
2019-07-17 17:17:37 +08:00
$return['code'] = 0;
$return['data'] = $data;
$return['data']['postBody'] = $body;
2019-07-12 19:25:47 +08:00
if ($data['return_code'] == 'FAIL') {
$return['code'] = -1;
$return['message'] = $data['return_msg'];
}
}
return $return;
}
/**
* @return string
*/
protected function builder()
{
$data = [
2019-07-17 17:17:37 +08:00
'appid' => $this->config->getAppid(),
'mch_id' => $this->config->getMchId(),
'nonce_str' => Help::random(32),
'body' => $this->config->getBody(),
2019-07-12 19:25:47 +08:00
'out_trade_no' => $this->orderNo,
'total_fee' => $this->money,
2019-07-17 17:17:37 +08:00
'sign_type' => $this->config->getSignType(),
2019-07-12 19:25:47 +08:00
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
2019-07-17 17:17:37 +08:00
'notify_url' => $this->config->getNotifyUrl(),
'trade_type' => $this->config->getTradeType(),
2019-07-12 19:25:47 +08:00
];
$data = array_merge($data, $this->data);
2019-07-17 17:17:37 +08:00
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
2019-07-12 19:25:47 +08:00
2019-07-17 17:17:37 +08:00
$data['sign'] = Help::sign($data, $key, $sign_type);
2019-07-12 19:25:47 +08:00
return Help::toXml($data);
}
/**
* @param $money
* @param $openid
* @param $order
2019-07-17 17:17:37 +08:00
* @param $desc
2019-07-12 19:25:47 +08:00
* @return Result
* @throws
*
* 提现
*/
2019-07-17 17:17:37 +08:00
public function cashWithdrawal($money, $openid, $order, $desc = '零钱提现')
2019-07-12 19:25:47 +08:00
{
$array = [
2019-07-17 17:17:37 +08:00
'nonce_str' => Help::random(32),
2019-07-12 19:25:47 +08:00
'partner_trade_no' => $order,
2019-07-17 17:17:37 +08:00
'mchid' => $this->config->getMchId(),
'mch_appid' => $this->config->getAppid(),
2019-07-12 19:25:47 +08:00
'openid' => $openid,
'check_name' => 'NO_CHECK',
'amount' => $money * 100,
2019-07-17 17:17:37 +08:00
'spbill_create_ip' => $this->config->getRemoteAddr(),
'desc' => $desc,
2019-07-12 19:25:47 +08:00
];
2019-07-17 17:17:37 +08:00
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$array['sign'] = Help::sign($array, $key, $sign_type);
2019-07-12 19:25:47 +08:00
2019-07-17 17:17:37 +08:00
$this->request->setCallback([$this, 'txCallback']);
return $this->send($this->transfers, Help::toXml($array));
2019-07-12 19:25:47 +08:00
}
2019-07-17 17:17:37 +08:00
/**
* @param $url
* @param $data
* @return array|mixed|Result
* @throws \Exception
*/
private function send($url, $data)
{
$this->request->setIsSSL(true);
$this->request->setMethod(WxClient::POST);
$this->request->addHeader('Content-Type', 'text/xml');
return $this->request->send($url, $data);
}
2019-07-12 19:25:47 +08:00
/**
* @param $data
* @return Result
* 提现回调
*/
public function txCallback($data)
{
$array = Help::toArray($data);
if ($array['result_code'] != 'SUCCESS') {
$data = ['code' => $array['err_code'], 'message' => $array['err_code_des']];
} else {
$data = ['code' => 0, 'message' => '支付成功'];
}
return new Result($data);
}
2019-05-21 16:25:00 +08:00
2019-05-21 16:02:17 +08:00
}