From 88d1a9ae8125c36bb1fc8acf3eb28754fe7da103 Mon Sep 17 00:00:00 2001 From: hitsword Date: Mon, 20 May 2019 16:30:38 +0800 Subject: [PATCH] debug Signed-off-by: hitsword --- class-wc-huayi-score.php | 11 ++++++++- includes/huayi-score-sdk.php | 46 +++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/class-wc-huayi-score.php b/class-wc-huayi-score.php index d5cc717..85ef58f 100644 --- a/class-wc-huayi-score.php +++ b/class-wc-huayi-score.php @@ -136,7 +136,16 @@ class WC_Huayi_Score extends WC_Payment_Gateway { if ( $huayi_uid > 0 ) { $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 ) {//积分够扣 $payResult = $huayiScoreSdk->pay($huayi_uid,$order->order_total,'订单号:'.$order_id);//执行扣款 TODO diff --git a/includes/huayi-score-sdk.php b/includes/huayi-score-sdk.php index 35806b7..c908886 100644 --- a/includes/huayi-score-sdk.php +++ b/includes/huayi-score-sdk.php @@ -11,19 +11,39 @@ class HuayiScoreSdk //接口Token protected $token = null; // 错误信息 - protected $error; + protected $error = null; + + //查询积分接口 + protected $get_balance_url = '/api/scoreshopv1/balance/index'; public function __construct($option) { $this->apiurl = isset($option['apiurl']) ? $option['apiurl'] : ''; $this->token = isset($option['token']) ? $option['token'] : ''; } + /** * 获取积分余额 + * @param $huayi_uid + * @return bool */ 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 */ - protected function getError() + public function getError() { 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; + } + } } \ No newline at end of file