浏览代码

<feat>增加回调签名验证,调整部分参数

alexzy 2 年之前
父节点
当前提交
0e23a4696f
共有 6 个文件被更改,包括 129 次插入44 次删除
  1. 2 1
      composer.json
  2. 5 0
      src/CreateOrderBase.php
  3. 25 32
      src/JuFu.php
  4. 59 0
      src/PayNotify.php
  5. 38 0
      src/Sign.php
  6. 0 11
      src/alipayH5/param/CreateOrder.php

+ 2 - 1
composer.json

@@ -23,7 +23,8 @@
     "require": {
         "php": ">=7.2",
         "hyperf/framework": "^2.0.0",
-        "hyperf/guzzle": "^2.0.0"
+        "hyperf/guzzle": "^2.0.0",
+        "ext-json": "*"
     },
     "require-dev": {
         "mockery/mockery": "^1.0",

+ 5 - 0
src/CreateOrderBase.php

@@ -88,6 +88,7 @@ class CreateOrderBase extends RequestBase
     public function __construct()
     {
         parent::__construct();
+        $this->aging = 1;
     }
 
     public function getRequestBody(): array
@@ -105,6 +106,10 @@ class CreateOrderBase extends RequestBase
             'syscode'    => $this->sys_code,
             'account'    => $this->account,
             'trans_time' => date("YmdHis"),
+            'pay_mode'   => $this->pay_mode,
+            'amount'     => $this->amount,
+            'app_id'     => $this->app_id,
+            'notify_url' => $this->notify_url,
         ];
     }
 

+ 25 - 32
src/JuFu.php

@@ -4,6 +4,7 @@ namespace Ycbl\YinlianPay;
 
 use GuzzleHttp\Exception\GuzzleException;
 use Hyperf\Guzzle\ClientFactory;
+use phpDocumentor\Reflection\Types\This;
 
 class JuFu
 {
@@ -17,10 +18,15 @@ class JuFu
      */
     protected $result = "";
 
+    /**
+     * @var bool
+     */
+    protected $error = false;
+
     /**
      * @var string
      */
-    protected $error = "";
+    protected $errorMessage = "";
 
     /**
      * @var array
@@ -35,40 +41,12 @@ class JuFu
     public function execute(RequestBase $requestBase)
     {
         $data = $requestBase->getRequestSignBody();
-        $sign = $this->parseSignData($data, $requestBase->getMd5Key());
+        $sign = Sign::parseSignData($data, $requestBase->getMd5Key());
         $data = $requestBase->getRequestBody();
         $data['signature'] = $sign;
         $this->postGbk($requestBase->getUrl(), $data);
     }
 
-    /**
-     * 获取签名加密key
-     * @param $data
-     * @param string $key
-     * @return string
-     */
-    private function parseSignData($data, string $key = ""): string
-    {
-        if (is_array($data)) {
-            $md5Str = urldecode(http_build_query($this->arr_sort($data)));
-        } else {
-            $md5Str = $data;
-        }
-        $md5Str = $md5Str . "&key=" . $key;
-        return strtoupper(md5($md5Str));
-    }
-
-    /**
-     * 数组排序
-     * @param $arr
-     * @return mixed
-     */
-    private function arr_sort($arr)
-    {
-        ksort($arr);
-        reset($arr);
-        return $arr;
-    }
 
     /**
      * 使用guzzle发送GBK编码 post请求
@@ -94,11 +72,11 @@ class JuFu
     {
         $data = json_decode($this->result, true);
         if (!$data) {
-            $this->error = "失败";
+            $this->setError("失败");
             return;
         }
         if (isset($data['errorcode']) && $data['errorcode'] != "0000") {
-            $this->error = $data['errormessage'];
+            $this->setError($data['errormessage']);
             return;
         }
 
@@ -115,6 +93,15 @@ class JuFu
         return $this->result;
     }
 
+    /**
+     * @param string $message
+     */
+    private function setError(string $message = "")
+    {
+        $this->errorMessage = $message;
+        $this->error = true;
+    }
+
     /**
      * @return string
      */
@@ -123,6 +110,11 @@ class JuFu
         return $this->error;
     }
 
+    public function getErrorMessage(): string
+    {
+        return $this->errorMessage;
+    }
+
     /**
      * @return array
      */
@@ -130,4 +122,5 @@ class JuFu
     {
         return $this->body;
     }
+
 }

+ 59 - 0
src/PayNotify.php

@@ -0,0 +1,59 @@
+<?php
+
+
+namespace Ycbl\YinlianPay;
+
+
+use Hyperf\Contract\ConfigInterface;
+
+class PayNotify
+{
+    protected $data;
+
+    protected $sys_code;
+
+    protected $account;
+
+    protected $md5_key;
+
+    protected $error;
+
+    public function __construct(array $data)
+    {
+        /** @var $config ConfigInterface */
+        $config = make(ConfigInterface::class);
+
+        $this->sys_code = $config->get('jufu_pay.sys_code');
+        $this->account = $config->get('jufu_pay.account');
+        $this->md5_key = $config->get('jufu_pay.md5_key');
+        $this->data = $data;
+    }
+
+    public function verifySign(): bool
+    {
+        $params = $this->data;
+        if (!isset($params['account']) && $params['account'] != $this->account) {
+            $this->error = "account error";
+            return false;
+        }
+
+        $verify_params = [
+            'account'  => $params['account'],
+            'trans_id' => $params['trans_id'],
+            'result'   => $params['result'],
+            'amount'   => $params['amount'],
+            'app_id'   => $params['app_id'],
+        ];
+        $sign = Sign::parseSignData($verify_params, $this->md5_key);
+        if (!isset($params['signature']) || $params['signature'] != $sign) {
+            $this->error = "signature error";
+            return false;
+        }
+        return true;
+    }
+
+    public function getError()
+    {
+        return $this->error;
+    }
+}

+ 38 - 0
src/Sign.php

@@ -0,0 +1,38 @@
+<?php
+
+
+namespace Ycbl\YinlianPay;
+
+
+class Sign
+{
+    /**
+     * 获取签名加密key
+     * @param $data
+     * @param string $key
+     * @return string
+     */
+    public static function parseSignData($data, string $key = ""): string
+    {
+        if (is_array($data)) {
+            $md5Str = urldecode(http_build_query(self::arr_sort($data)));
+        } else {
+            $md5Str = $data;
+        }
+        $md5Str = $md5Str . "&key=" . $key;
+        return strtoupper(md5($md5Str));
+    }
+
+    /**
+     * 数组排序
+     * @param $arr
+     * @return mixed
+     */
+    public static function arr_sort($arr)
+    {
+        ksort($arr);
+        reset($arr);
+        return $arr;
+    }
+
+}

+ 0 - 11
src/alipayH5/param/CreateOrder.php

@@ -9,15 +9,4 @@ use Ycbl\YinlianPay\CreateOrderBase;
 class CreateOrder extends CreateOrderBase
 {
     public $pay_mode = "H5_ZFBWEB";
-
-    public function getRequestSignBody(): array
-    {
-        $parent = parent::getRequestSignBody();
-        return array_merge($parent, [
-            'pay_mode'   => $this->pay_mode,
-            'amount'     => $this->amount,
-            'app_id'     => $this->app_id,
-            'notify_url' => $this->notify_url,
-        ]);
-    }
 }