add
This commit is contained in:
@@ -100,6 +100,9 @@ abstract class Base
|
||||
*/
|
||||
public $app_secret = '';
|
||||
|
||||
public $ssl_cert = '';
|
||||
public $ssl_key = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
||||
+9
-5
@@ -15,7 +15,7 @@ class Http
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
private function request($url, $pushType = 'get', $data = [], callable $callback = NULL)
|
||||
private function request($url, $pushType = 'get', $data = [], callable $callback = NULL, $isSSL = FALSE)
|
||||
{
|
||||
|
||||
if (
|
||||
@@ -25,7 +25,7 @@ class Http
|
||||
$url = $this->url . '/' . $url;
|
||||
}
|
||||
|
||||
return $this->curl_push($url, $pushType, $data, $callback);
|
||||
return $this->curl_push($url, $pushType, $data, $callback, $isSSL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,7 +36,7 @@ class Http
|
||||
* @return Result
|
||||
* curl请求
|
||||
*/
|
||||
public function curl_push($url, $type = 'get', $data = [], callable $callback = NULL)
|
||||
public function curl_push($url, $type = 'get', $data = [], callable $callback = NULL, $isSSL = FALSE)
|
||||
{
|
||||
$_data = $this->paramEncode($data);
|
||||
if ($type == 'get' && is_array($_data)) {
|
||||
@@ -50,6 +50,10 @@ class Http
|
||||
if (!empty($this->header)) {
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
|
||||
}
|
||||
if ($isSSL && is_array($isSSL)) {
|
||||
curl_setopt($ch, CURLOPT_SSLCERT, $isSSL[0]);
|
||||
curl_setopt($ch, CURLOPT_SSLKEY, $isSSL[1]);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);// 超时设置
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
|
||||
@@ -133,14 +137,14 @@ class Http
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
public static function post($url, $data = [], callable $callback = NULL, array $header = NULL)
|
||||
public static function post($url, $data = [], callable $callback = NULL, array $header = NULL, $isSSl = FALSE)
|
||||
{
|
||||
static $_class = NULL;
|
||||
if ($_class == NULL) $_class = new Http();
|
||||
if (!empty($header)) {
|
||||
$_class->setHeaders($header);
|
||||
}
|
||||
return $_class->request($url, 'post', $data, $callback);
|
||||
return $_class->request($url, 'post', $data, $callback, $isSSl);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+115
-82
@@ -5,90 +5,123 @@
|
||||
* Date: 2018/3/26 0026
|
||||
* Time: 10:22
|
||||
*/
|
||||
|
||||
namespace wchat;
|
||||
|
||||
class Recharge extends Base
|
||||
{
|
||||
|
||||
/** @var Recharge */
|
||||
private static $recharge;
|
||||
|
||||
private $money = 0;
|
||||
|
||||
private $orderNo;
|
||||
|
||||
private $data = [];
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @return bool|Result
|
||||
*/
|
||||
public function payment(int $money, string $orderNo, $openId = null)
|
||||
{
|
||||
$_this = $this;
|
||||
if ($money < 0) {
|
||||
return new Result(['code' => 500, 'message' => '充值金额不能小于0.']);
|
||||
}
|
||||
$this->money = $money;
|
||||
$this->orderNo = $orderNo;
|
||||
$this->data['openid'] = $openId;
|
||||
return Http::post($this->createPayUrl(), $this->builder(),
|
||||
function ($result, $body) use ($_this) {
|
||||
$data = $_this->toArray($result);
|
||||
if(isset($data['sign'])){
|
||||
$sign = $data['sign'];
|
||||
unset($data['sign']);
|
||||
$_sign = $_this->sign($data);
|
||||
}
|
||||
$return = [];
|
||||
if (!isset($sign) || $sign != $_sign) {
|
||||
$return['code'] = -1;
|
||||
$return['message'] = $data['return_msg'] ?? '返回数据签名验证失败';
|
||||
} else {
|
||||
if ($data['return_code'] == 'FAIL') {
|
||||
$return['code'] = -1;
|
||||
$return['message'] = $data['return_msg'];
|
||||
} else {
|
||||
$return['code'] = 0;
|
||||
$return['data'] = $data;
|
||||
$return['data']['postBody'] = $body;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}, ['Content-Type' => 'text/xml']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function builder()
|
||||
{
|
||||
$data = [
|
||||
'appid' => $this->app_id,
|
||||
'mch_id' => $this->mch_id,
|
||||
'nonce_str' => $this->random(32),
|
||||
'body' => $this->body,
|
||||
'out_trade_no' => $this->orderNo,
|
||||
'total_fee' => $this->money,
|
||||
'sign_type' => $this->sign_type,
|
||||
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
|
||||
'notify_url' => $this->notify_url,
|
||||
'trade_type' => $this->trade_type,
|
||||
];
|
||||
|
||||
$data = array_merge($data, $this->data);
|
||||
|
||||
$data['sign'] = $this->sign($data);
|
||||
|
||||
return $this->toXml($data);
|
||||
}
|
||||
|
||||
private function createPayUrl()
|
||||
{
|
||||
return $this->mch_host . '/pay/unifiedorder';
|
||||
}
|
||||
|
||||
/** @var Recharge */
|
||||
private static $recharge;
|
||||
|
||||
private $money = 0;
|
||||
|
||||
private $orderNo;
|
||||
|
||||
private $data = [];
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @return bool|Result
|
||||
*/
|
||||
public function payment(int $money, string $orderNo, $openId = NULL)
|
||||
{
|
||||
$_this = $this;
|
||||
if ($money < 0) {
|
||||
return new Result(['code' => 500, 'message' => '充值金额不能小于0.']);
|
||||
}
|
||||
$this->money = $money;
|
||||
$this->orderNo = $orderNo;
|
||||
$this->data['openid'] = $openId;
|
||||
return Http::post($this->createPayUrl(), $this->builder(),
|
||||
function ($result, $body) use ($_this) {
|
||||
$data = $_this->toArray($result);
|
||||
if (isset($data['sign'])) {
|
||||
$sign = $data['sign'];
|
||||
unset($data['sign']);
|
||||
$_sign = $_this->sign($data);
|
||||
}
|
||||
$return = [];
|
||||
if (!isset($sign) || $sign != $_sign) {
|
||||
$return['code'] = -1;
|
||||
$return['message'] = $data['return_msg'] ?? '返回数据签名验证失败';
|
||||
} else {
|
||||
if ($data['return_code'] == 'FAIL') {
|
||||
$return['code'] = -1;
|
||||
$return['message'] = $data['return_msg'];
|
||||
} else {
|
||||
$return['code'] = 0;
|
||||
$return['data'] = $data;
|
||||
$return['data']['postBody'] = $body;
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}, ['Content-Type' => 'text/xml']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function builder()
|
||||
{
|
||||
$data = [
|
||||
'appid' => $this->app_id,
|
||||
'mch_id' => $this->mch_id,
|
||||
'nonce_str' => $this->random(32),
|
||||
'body' => $this->body,
|
||||
'out_trade_no' => $this->orderNo,
|
||||
'total_fee' => $this->money,
|
||||
'sign_type' => $this->sign_type,
|
||||
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
|
||||
'notify_url' => $this->notify_url,
|
||||
'trade_type' => $this->trade_type,
|
||||
];
|
||||
|
||||
$data = array_merge($data, $this->data);
|
||||
|
||||
$data['sign'] = $this->sign($data);
|
||||
|
||||
return $this->toXml($data);
|
||||
}
|
||||
|
||||
private function createPayUrl()
|
||||
{
|
||||
return $this->mch_host . '/pay/unifiedorder';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $money
|
||||
* @param $openid
|
||||
* @param $order
|
||||
* @param $REMOTE_ADDR
|
||||
* @return Result
|
||||
*
|
||||
* 提现
|
||||
*/
|
||||
public function tx($money, $openid, $order, $REMOTE_ADDR)
|
||||
{
|
||||
$array = [
|
||||
'nonce_str' => $this->random(32),
|
||||
'partner_trade_no' => $order,
|
||||
'mchid' => $this->mch_id,
|
||||
'mch_appid' => $this->app_id,
|
||||
'openid' => $openid,
|
||||
'check_name' => 'NO_CHECK',
|
||||
'amount' => $money * 100,
|
||||
'spbill_create_ip' => $REMOTE_ADDR,
|
||||
'desc' => '看世界杯集球星卡,一起瓜分现金红包。',
|
||||
];
|
||||
|
||||
$transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
|
||||
|
||||
$array['sign'] = $this->sign($array);
|
||||
|
||||
return Http::post($transfers, $this->toXml($array), function ($data) {
|
||||
return $this->toArray($data);
|
||||
}, NULL, [$this->ssl_cert, $this->ssl_key]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user