瀏覽代碼

Add payment failed handle

BrettonYe 2 年之前
父節點
當前提交
977b999f81

+ 5 - 0
app/Models/Payment.php

@@ -34,6 +34,11 @@ class Payment extends Model
         return $this->update(['status' => -1]);
     }
 
+    public function failed() // 关闭支付单
+    {
+        return $this->close() && $this->order()->close();
+    }
+
     public function complete() // 完成支付单
     {
         return $this->update(['status' => 1]);

+ 1 - 1
app/Payments/F2Fpay.php

@@ -39,7 +39,7 @@ class F2Fpay extends Gateway
             $result = $gateWay->qrCharge($data);
             $payment->update(['qr_code' => 1, 'url' => $result['qr_code']]);
         } catch (Exception $e) {
-            $payment->delete();
+            $payment->failed();
             Log::alert('【支付宝当面付】支付错误: '.$e->getMessage());
             exit;
         }

+ 4 - 4
app/Payments/Library/AlipayF2F.php

@@ -95,7 +95,7 @@ class AlipayF2F
             throw new RuntimeException('支付宝RSA公钥错误。请检查公钥文件格式是否正确');
         }
 
-        $result = ! openssl_verify(json_encode($data, JSON_UNESCAPED_UNICODE), base64_decode($sign), $publicKey, OPENSSL_ALGO_SHA256);
+        $result = (bool) openssl_verify(json_encode($data), base64_decode($sign), $publicKey, OPENSSL_ALGO_SHA256);
         if (PHP_VERSION_ID < 80000) {
             openssl_free_key($publicKey);
         }
@@ -128,16 +128,16 @@ class AlipayF2F
         $response = Http::get(self::$gatewayUrl, $this->buildParams())->json();
         $resKey = str_replace('.', '_', $this->config['method']).'_response';
         if (! isset($response[$resKey])) {
-            throw new RuntimeException('【支付宝当面付】请求错误:看起来是请求失败');
+            throw new RuntimeException('请求错误-看起来是请求失败');
         }
 
         if (! $this->rsaVerify($response[$resKey], $response['sign'])) {
-            throw new RuntimeException('【支付宝当面付】请求错误:'.$response[$resKey]['msg'].' | '.$response[$resKey]['sub_msg']);
+            throw new RuntimeException('验签错误-'.$response[$resKey]['msg'].' | '.($response[$resKey]['sub_msg'] ?? var_export($response, true)));
         }
 
         $response = $response[$resKey];
         if ($response['msg'] !== 'Success') {
-            throw new RuntimeException($response['sub_msg']);
+            throw new RuntimeException($response[$resKey]['sub_msg'] ?? var_export($response, true));
         }
 
         return $response;

+ 21 - 18
app/Payments/PayBeaver.php

@@ -38,6 +38,13 @@ class PayBeaver extends Gateway
             'return_url'        => route('invoice'),
         ]);
 
+        if (! isset($result['message']) && isset($result['data']['pay_url'])) {
+            $payment->update(['url' => $result['data']['pay_url']]);
+
+            return Response::json(['status' => 'success', 'url' => $result['data']['pay_url'], 'message' => '创建订单成功!']);
+        }
+
+        $payment->failed();
         if (isset($result['message'])) {
             Log::alert('【海狸支付】创建订单错误:'.$result['message']);
 
@@ -46,13 +53,24 @@ class PayBeaver extends Gateway
 
         if (! isset($result['data']['pay_url'])) {
             Log::alert('【海狸支付】创建订单错误:未获取到支付链接'.var_export($result, true));
+        }
+
+        return Response::json(['status' => 'fail', 'message' => '创建订单失败:未知错误']);
+    }
+
+    public function notify($request): void
+    {
+        if (! $this->paybeaverVerify($request->post())) {
+            exit(json_encode(['status' => 400]));
+        }
 
-            return Response::json(['status' => 'fail', 'message' => '创建订单失败:未知错误']);
+        if ($request->has(['merchant_order_id']) && $this->paymentReceived($request->input(['merchant_order_id']))) {
+            exit(json_encode(['status' => 200]));
         }
 
-        $payment->update(['url' => $result['data']['pay_url']]);
+        Log::error('【海狸支付】交易失败:'.var_export($request->all(), true));
 
-        return Response::json(['status' => 'success', 'url' => $result['data']['pay_url'], 'message' => '创建订单成功!']);
+        exit(json_encode(['status' => 500]));
     }
 
     private function createOrder($params)
@@ -80,21 +98,6 @@ class PayBeaver extends Gateway
         return strtolower(md5(http_build_query($params).$this->appSecret));
     }
 
-    public function notify($request): void
-    {
-        if (! $this->paybeaverVerify($request->post())) {
-            exit(json_encode(['status' => 400]));
-        }
-
-        if ($request->has(['merchant_order_id']) && $this->paymentReceived($request->input(['merchant_order_id']))) {
-            exit(json_encode(['status' => 200]));
-        }
-
-        Log::error('【海狸支付】交易失败:'.var_export($request->all(), true));
-
-        exit(json_encode(['status' => 500]));
-    }
-
     private function paybeaverVerify($params)
     {
         return hash_equals($params['sign'], $this->sign($params));

+ 10 - 9
app/Payments/PayPal.php

@@ -24,19 +24,19 @@ class PayPal extends Gateway
         $config = [
             'mode' => 'live',
             'live' => [
-                'username' => sysConfig('paypal_username'),
-                'password' => sysConfig('paypal_password'),
-                'secret' => sysConfig('paypal_secret'),
+                'username'    => sysConfig('paypal_username'),
+                'password'    => sysConfig('paypal_password'),
+                'secret'      => sysConfig('paypal_secret'),
                 'certificate' => sysConfig('paypal_certificate'),
-                'app_id' => sysConfig('paypal_app_id'),
+                'app_id'      => sysConfig('paypal_app_id'),
             ],
 
             'payment_action' => 'Sale',
-            'currency' => 'USD',
-            'billing_type' => 'MerchantInitiatedBilling',
-            'notify_url' => route('payment.notify', ['method' => 'paypal']),
-            'locale' => 'zh_CN',
-            'validate_ssl' => true,
+            'currency'       => 'USD',
+            'billing_type'   => 'MerchantInitiatedBilling',
+            'notify_url'     => route('payment.notify', ['method' => 'paypal']),
+            'locale'         => 'zh_CN',
+            'validate_ssl'   => true,
         ];
         $this->provider->setApiCredentials($config);
         $response = Http::timeout(15)->get('http://api.k780.com/?app=finance.rate&scur=USD&tcur=CNY&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4');
@@ -60,6 +60,7 @@ class PayPal extends Gateway
 
             return Response::json(['status' => 'success', 'url' => $response['paypal_link'], 'message' => '创建订单成功!']);
         } catch (Exception $e) {
+            $payment->failed();
             Log::error('【PayPal】错误: '.$e->getMessage());
             exit;
         }

+ 2 - 2
app/Payments/Stripe.php

@@ -56,7 +56,7 @@ class Stripe extends Gateway
             if ($type == 3) {
                 if (! $source['wechat']['qr_code_url']) {
                     Log::warning('创建订单错误:未知错误');
-                    $payment->delete();
+                    $payment->failed();
 
                     return Response::json(['status' => 'fail', 'message' => '创建订单失败:未知错误']);
                 }
@@ -66,7 +66,7 @@ class Stripe extends Gateway
             } else {
                 if (! $source['redirect']['url']) {
                     Log::warning('创建订单错误:未知错误');
-                    $payment->delete();
+                    $payment->failed();
 
                     return response()->json(['code' => 0, 'msg' => '创建订单失败:未知错误']);
                 }

+ 10 - 10
app/Payments/THeadPay.php

@@ -31,7 +31,7 @@ class THeadPay extends Gateway
 
                 return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => '创建订单成功!']);
             }
-
+            $payment->failed();
             Log::error('【平头哥支付】 返回错误信息:'.$result['message']);
         }
 
@@ -40,15 +40,6 @@ class THeadPay extends Gateway
         return Response::json(['status' => 'fail', 'message' => '创建在线订单失败,请工单通知管理员!']);
     }
 
-    private function sign($params)
-    {
-        unset($params['sign']);
-        ksort($params, SORT_STRING);
-        $params['key'] = sysConfig('theadpay_key');
-
-        return strtoupper(md5(http_build_query($params)));
-    }
-
     public function notify($request): void
     {
         if ($this->verify_notify($request->post())) {
@@ -65,6 +56,15 @@ class THeadPay extends Gateway
         exit('fail');
     }
 
+    private function sign($params)
+    {
+        unset($params['sign']);
+        ksort($params, SORT_STRING);
+        $params['key'] = sysConfig('theadpay_key');
+
+        return strtoupper(md5(http_build_query($params)));
+    }
+
     private function verify_notify($params)
     {
         return $params['sign'] === $this->sign($params);