Browse Source

对接hitpay

lizhen_gitee 1 year ago
parent
commit
82ffa98cb2

+ 59 - 46
application/api/controller/Demo.php

@@ -15,9 +15,9 @@ class Demo extends Api
     //如果接口已经设置无需登录,那也就无需鉴权了
     //
     // 无需登录的接口,*表示全部
-    protected $noNeedLogin = ['test', 'test1'];
+    protected $noNeedLogin = ['*'];
     // 无需鉴权的接口,*表示全部
-    protected $noNeedRight = ['test2'];
+    protected $noNeedRight = ['*'];
 
     /**
      * 测试方法
@@ -38,73 +38,86 @@ class Demo extends Api
          'msg':'返回成功'
         })
      */
-    public function test()
+    public function test(){
+        $out_trade_no = createUniqueNo('Test');
+        $money = '0.3';
+        $notifyurl = config('notify_cdnurl');
+        $rs = $this->hitpay_payment($out_trade_no,$money,$notifyurl);
+        dump($rs);
+
+        //payorder 保存 $rs['id'] 为 payment_request_id
+    }
+
+    public function hitpay_payment($out_trade_no,$money,$notifyurl)
     {
-        $apiKey = '096a06e94fb1e0a8b015485bb76ef5dd63c167679297e07331a687327bd8b12e';
-        $salt   = 'LNrFYxOiGhtdHAuJiqqy3wOl1nVwyJn5dfOYkLquFx8j2J6bm2nsplTnpkYuKyo5';
+        $return = [
+            'status' => false,
+            'msg'  => '',
+            'url' => '',
+            'id' => 0,
+        ];
 
-        $hitPayClient = new \HitPay\Client($apiKey, false);
+        $apiKey = config('hitpay.apikey');
 
         try {
+            $hitPayClient = new \HitPay\Client($apiKey, true);
+
             $request = new \HitPay\Request\CreatePayment();
 
-            $request->setAmount(66)
-                ->setCurrency('SGD');
+            $request->setAmount($money)
+                    ->setCurrency('SGD')
+                    ->setWebhook($notifyurl)
+                    ->setReferenceNumber($out_trade_no);
+
             $result = $hitPayClient->createPayment($request);
 
-            print_r($result);
+            $return['status'] = true;
+            $return['url'] = $result->getUrl();
+            $return['id']  = $result->getId();
+
+
+            //print_r($result);
 
             $data = $hitPayClient->getPaymentStatus($result->getId());
-            print_r($data);
+            dump($data);
+            dump($data->status);
+
+            /*$data = $hitPayClient->deletePaymentRequest($data->getId());
+            print_r($data);*/
 
-            $data = $hitPayClient->deletePaymentRequest($data->getId());
-            print_r($data);
 
         } catch (\Exception $e) {
-            print_r($e->getMessage());
+            $return['msg'] = $e->getMessage();
         }
-    }
 
-    /**
-     * 无需登录的接口
-     *
-     */
-    public function test1()
-    {
-        $curl = curl_init();
-
-        curl_setopt_array($curl, [
-            CURLOPT_URL => "https://api.sandbox.hit-pay.com/v1/orders",
-            CURLOPT_RETURNTRANSFER => true,
-            CURLOPT_ENCODING => "",
-            CURLOPT_MAXREDIRS => 10,
-            CURLOPT_TIMEOUT => 30,
-            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
-            CURLOPT_CUSTOMREQUEST => "GET",
-            CURLOPT_HTTPHEADER => [
-                "X-BUSINESS-API-KEY: <096a06e94fb1e0a8b015485bb76ef5dd63c167679297e07331a687327bd8b12e>"
-            ],
-        ]);
-
-        $response = curl_exec($curl);
-        $err = curl_error($curl);
-
-        curl_close($curl);
-
-        if ($err) {
-            echo "cURL Error #:" . $err;
-        } else {
-            echo $response;
-        }
+        return $return;
     }
 
+
+
+
     /**
      * 需要登录的接口
      *
      */
     public function test2()
     {
-        $this->success('返回成功', ['action' => 'test2']);
+        $url = 'https://api.hit-pay.com/v1/payment-requests';
+        $data = [
+            'amount' => 1,
+            //'payment_method' => ['paynow_online', 'card', 'wechat', 'alipay', 'grabpay', 'fave_duit', 'shopback', 'atome'],
+            'currency' => 'SGD',
+            //'webhook' => '',
+        ];
+
+        $header = array(
+            'X-BUSINESS-API-KEY' => '096a06e94fb1e0a8b015485bb76ef5dd63c167679297e07331a687327bd8b12e',
+            'Content-Type' => 'application/x-www-form-urlencoded',
+            'X-Requested-With' => 'XMLHttpRequest'
+        );
+
+        $rs = curl_post($url,$data,$header);
+        dump($rs);
     }
 
     /**

+ 56 - 10
application/api/controller/Notify.php

@@ -12,48 +12,94 @@ class Notify extends Api
     protected $noNeedLogin = ['*'];
     protected $noNeedRight = ['*'];
 
+    public function checkNotify($args = []){
+
+        $secret = config('hitpay.salt');
+
+        $input_hmac = $args['hmac'];
+        unset($args['hmac']);
+
+        //hitpay/client/generateSignatureArray
+        $hmacSource = [];
+        foreach ($args as $key => $val) {
+            $hmacSource[$key] = "{$key}{$val}";
+        }
+        ksort($hmacSource);
+        $sig = implode("", array_values($hmacSource));
+        $new_hmac = hash_hmac('sha256', $sig, $secret);
+
+        //判断相等
+        if($new_hmac == $input_hmac){
+            return true;
+        }else{
+            return false;
+        }
+    }
+
+    //主动获取一次
+    private function getPaymentStatus($payment_request_id){
+        $apiKey = config('hitpay.apikey');
+        $hitPayClient = new \HitPay\Client($apiKey, true);
+        $data = $hitPayClient->getPaymentStatus($payment_request_id);
+        return $data->status;
+    }
+
     //充值金币 异步回调对外方法
     public function recharge_notify_base(){
 
-        //验签
+        //日志
         $paytype = input('paytype','hitpay');
         $notify_file = $this->notify_log_start($paytype);
-        $pay = Service::checkNotify($paytype);
-        if (!$pay) {
+
+        //接参
+        $field = ['payment_id','payment_request_id','phone','amount','currency','status','reference_number','hmac'];
+        $notify_data = request_post_hub($field);
+
+        //验签
+        $checkNotify = $this->checkNotify($notify_data);
+        if ($checkNotify !== true) {
             echo '签名错误';
             exit;
         }
 
+        //检查支付完成 completed / failed
+        if($notify_data['status'] != 'completed'){
+            $now_status = $this->getPaymentStatus($notify_data['payment_request_id']);
+            if($now_status != 'completed'){
+                echo '没有支付完成';
+                exit;
+            }
+        }
+
         //验证,拿订单号等信息
-        $data = $pay->verify();
-        $out_trade_no = $data['out_trade_no'];
+        $out_trade_no = $notify_data['reference_number'];
         //订单查询
         $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
 
         if(empty($info)){
-            echo $pay->success();
+            echo 'success';
             exit;
         }
 
         if($info['order_status'] != 0)
         {
-            echo $pay->success();
+            echo 'success';
             exit;
         }
         //你可以在此编写订单逻辑
         $rs = $this->recharge_notify_do($out_trade_no);
         if($rs === false){
             //不论结果都应返回success
-            echo $pay->success();
+            echo 'success';
             exit;
         }else{
             //不论结果都应返回success
-            echo $pay->success();
+            echo 'success';
             exit;
         }
 
         //默认
-        echo $pay->success();
+        echo 'success';
         exit;
     }
 

+ 1 - 0
application/config.php

@@ -322,6 +322,7 @@ return [
 
     //图片地址
     'domain_cdnurl' => 'http://www.yueke.com',
+    'notify_cdnurl' => 'http://yueke.huxiukeji.cn/api/notify/recharge_notify_base',
 
     'hitpay' => [
         'apikey' => '096a06e94fb1e0a8b015485bb76ef5dd63c167679297e07331a687327bd8b12e',

+ 4 - 0
application/index/controller/Index.php

@@ -7,6 +7,10 @@ use think\Db;
 class Index extends Controller
 {
 
+    public function index(){
+
+    }
+
 
     //基础文章网页
     public function basedata(){