ソースを参照

PayStack支付webhook调试

kevin_zhangl 3 年 前
コミット
5fe3a57108

+ 8 - 0
common/config/params.php

@@ -321,6 +321,14 @@ return [
             'value' => 7,
             'label' => '订单退款'
         ],
+        '-1' => [
+            'value' => -1,
+            'label' => '支付失败'
+        ],
+        'failPaid' => [
+            'value' => -1,
+            'label' => '支付失败'
+        ],
     ],
     'exportModule' => [
         'shop' => ['label' => '商城管理', 'value'=>'shop'],

+ 5 - 5
common/models/forms/ApproachOrderForm.php

@@ -281,7 +281,7 @@ class ApproachOrderForm extends Model
         }
 
         // 调用PayStack支付校验
-        $payload = PayStack::transactionVerify($this->remark['reference']);
+        $payload = PayStack::transactionVerify($this->note['reference']);
         if ($payload['status'] !== true) {
             throw new Exception(Form::formatErrorsForApi($payload['message']));
         }
@@ -293,10 +293,10 @@ class ApproachOrderForm extends Model
         $transaction = $db->beginTransaction();
         try {
             // 更新准订单状态为已支付
-            $this->_model->STATUS = \Yii::$app->params['orderStatus']['paid']['value'];
-            $this->_model->NOTE = json_encode($this->remark);
-            $this->_model->PAY_AT = Date::nowTime();
-            $this->_model->EMAIL = $this->email;
+            $this->_model->STATUS = $this->status;
+            $this->_model->NOTE = json_encode($this->note);
+            $this->_model->PAY_AT = $this->note['paid_at'];
+            $this->_model->EMAIL = $this->note['email'];
             if (!$this->_model->save()) {
                 throw new Exception(Form::formatErrorsForApi($this->_model->getErrors()));
             }

+ 35 - 22
frontendApi/modules/v1/controllers/ShopController.php

@@ -37,22 +37,6 @@ use Yii;
 class ShopController extends BaseController {
     public $modelClass = DecOrder::class;
 
-//    public function beforeAction($action)
-//    {
-//        parent::beforeAction($action);
-//
-//        // 指定接口不进行校验
-//        $currentAction = $action->id;
-//        LoggerTool::notice($currentAction);
-//        if (in_array($currentAction, $this->_method_not_check)) {
-//            $action->controller->enableCsrfValidation = false;
-//            LoggerTool::notice($action->controller->enableCsrfValidation);
-//        }
-//        LoggerTool::notice($action->controller->enableCsrfValidation);
-//
-//        return true;
-//    }
-
     /**
      * 商品列表
      * @return mixed
@@ -194,13 +178,42 @@ class ShopController extends BaseController {
      * @throws \yii\web\HttpException
      */
     public function actionVerifyApproachOrder() {
-        LoggerTool::info('isPost => ' . \Yii::$app->request->isPost);
         if (\Yii::$app->request->isPost) {
-            $params = \Yii::$app->request->post();
-            LoggerTool::info($params);
-//        if (parent::edit(ApproachOrderForm::class, 'PayStack pay Success', 'verifyPayStack', ['verifyPayStack'])) {
-//            return http_response_code(200);
-//        }
+            $request = json_decode(\Yii::$app->request->post(), true);
+            LoggerTool::notice($request);
+            // TODO:支付webhook回调日志
+
+
+            $data = $request['data'];
+            $status = $request['data']['status'] ?? 'false';
+            // 订单状态
+            $orderStatus = ($status == 'success') ? \Yii::$app->params['orderStatus']['paid']['value'] : \Yii::$app->params['orderStatus']['failPaid']['value'];
+
+            $customFields = $request['data']['metadata']['custom_fields'] ?? [];
+            $oderSn = $customFields[0]['value'] ?? '';
+
+            $formModel = new ApproachOrderForm();
+            $formModel['sn'] = $oderSn;
+            $formModel['status'] = $orderStatus;
+            $formModel['note'] = [
+                'id' => $data['id'],
+                'domain' => $data['domain'],
+                'status' => $data['status'],
+                'reference' => $data['reference'],
+                'amount' => $data['amount'],
+                'created_at' => $data['created_at'],
+                'paid_at' => $data['paid_at'],
+                'channel' => $data['channel'],
+                'currency' => $data['currency'],
+                'ip_address' => $data['ip_address'],
+                'metadata' => $data['metadata'],
+                'customer' => $data['customer'],
+                'email' => $data['customer']['email'],
+            ];
+
+            if (parent::edit(ApproachOrderForm::class, 'PayStack pay Success', 'verifyPayStack', ['verifyPayStack'])) {
+                return http_response_code(200);
+            }
         }
 
         return http_response_code(500);