parent
3b57f314ea
commit
88d1a9ae81
|
|
@ -136,7 +136,16 @@ class WC_Huayi_Score extends WC_Payment_Gateway {
|
||||||
|
|
||||||
if ( $huayi_uid > 0 ) {
|
if ( $huayi_uid > 0 ) {
|
||||||
$huayiScoreSdk = new HuayiScoreSdk(array('apiurl'=>$this->apiurl,'token'=>$this->token));
|
$huayiScoreSdk = new HuayiScoreSdk(array('apiurl'=>$this->apiurl,'token'=>$this->token));
|
||||||
$balanceResult = $huayiScoreSdk->getBalance($huayi_uid);//获取积分余额 TODO
|
$balanceResult = $huayiScoreSdk->getBalance($huayi_uid);//获取积分余额
|
||||||
|
if (!$balanceResult) {
|
||||||
|
wc_add_notice( __('查询积分失败:', 'huayi_score') . $huayiScoreSdk->getError(), 'error' );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//debug
|
||||||
|
wc_add_notice( __('查询成功:', 'huayi_score') . json_encode($balanceResult), 'error' );
|
||||||
|
return;
|
||||||
|
//debug
|
||||||
|
|
||||||
if ($balanceResult >= $order->order_total ) {//积分够扣
|
if ($balanceResult >= $order->order_total ) {//积分够扣
|
||||||
$payResult = $huayiScoreSdk->pay($huayi_uid,$order->order_total,'订单号:'.$order_id);//执行扣款 TODO
|
$payResult = $huayiScoreSdk->pay($huayi_uid,$order->order_total,'订单号:'.$order_id);//执行扣款 TODO
|
||||||
|
|
|
||||||
|
|
@ -11,19 +11,39 @@ class HuayiScoreSdk
|
||||||
//接口Token
|
//接口Token
|
||||||
protected $token = null;
|
protected $token = null;
|
||||||
// 错误信息
|
// 错误信息
|
||||||
protected $error;
|
protected $error = null;
|
||||||
|
|
||||||
|
//查询积分接口
|
||||||
|
protected $get_balance_url = '/api/scoreshopv1/balance/index';
|
||||||
|
|
||||||
public function __construct($option)
|
public function __construct($option)
|
||||||
{
|
{
|
||||||
$this->apiurl = isset($option['apiurl']) ? $option['apiurl'] : '';
|
$this->apiurl = isset($option['apiurl']) ? $option['apiurl'] : '';
|
||||||
$this->token = isset($option['token']) ? $option['token'] : '';
|
$this->token = isset($option['token']) ? $option['token'] : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取积分余额
|
* 获取积分余额
|
||||||
|
* @param $huayi_uid
|
||||||
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getBalance($huayi_uid)
|
public function getBalance($huayi_uid)
|
||||||
{
|
{
|
||||||
return $huayi_uid;
|
$url = $this->apiurl.$this->get_balance_url;
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'uid'=>$huayi_uid,
|
||||||
|
'timestamp'=>time()
|
||||||
|
];
|
||||||
|
$response = $this->postRequest($url, $data);
|
||||||
|
if (!$response) {//请求失败
|
||||||
|
return false;
|
||||||
|
} elseif ($response['code'] == 1) {//请求成功
|
||||||
|
return $response;
|
||||||
|
} else {//请求成功但是返回错误
|
||||||
|
$this->error = $response['msg'];
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -98,10 +118,30 @@ class HuayiScoreSdk
|
||||||
* 获取错误信息
|
* 获取错误信息
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function getError()
|
public function getError()
|
||||||
{
|
{
|
||||||
return $this->error;
|
return $this->error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 封装Post请求
|
||||||
|
* @param $url
|
||||||
|
* @param $data
|
||||||
|
* @return bool / array
|
||||||
|
*/
|
||||||
|
protected function postRequest($url, $data)
|
||||||
|
{
|
||||||
|
$response = wp_remote_post($url, array(
|
||||||
|
'method' => 'POST',
|
||||||
|
'body' => array_merge($data, $this->getSignature($this->token, $data)),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( is_wp_error($response) ) {
|
||||||
|
$this->error = $response->get_error_message();
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue