refactor: 规范Expection处理

This commit is contained in:
xboard
2023-12-04 20:40:49 +08:00
parent aa0fe64afe
commit 0ab7dee52d
65 changed files with 625 additions and 362 deletions
+2 -1
View File
@@ -4,6 +4,7 @@
* 自己写别抄,抄NMB抄
*/
namespace App\Payments;
use App\Exceptions\ApiException;
class AlipayF2F {
public function __construct($config)
@@ -57,7 +58,7 @@ class AlipayF2F {
'data' => $gateway->getQrCodeUrl()
];
} catch (\Exception $e) {
abort(500, $e->getMessage());
throw new ApiException(500, $e->getMessage());
}
}
+3 -2
View File
@@ -1,6 +1,7 @@
<?php
namespace App\Payments;
use App\Exceptions\ApiException;
class BTCPay {
@@ -52,7 +53,7 @@ class BTCPay {
$ret = @json_decode($ret_raw, true);
if(empty($ret['checkoutLink'])) {
abort(500, "error!");
throw new ApiException(500, "error!");
}
return [
'type' => 1, // Redirect to url
@@ -75,7 +76,7 @@ class BTCPay {
$computedSignature = "sha256=" . \hash_hmac('sha256', $payload, $this->config['btcpay_webhook_key']);
if (!self::hashEqual($signraturHeader, $computedSignature)) {
abort(400, 'HMAC signature does not match');
throw new ApiException(400, 'HMAC signature does not match');
return false;
}
+5 -4
View File
@@ -1,6 +1,7 @@
<?php
namespace App\Payments;
use App\Exceptions\ApiException;
class CoinPayments {
public function __construct($config) {
@@ -62,7 +63,7 @@ class CoinPayments {
{
if (!isset($params['merchant']) || $params['merchant'] != trim($this->config['coinpayments_merchant_id'])) {
abort(500, 'No or incorrect Merchant ID passed');
throw new ApiException(500, 'No or incorrect Merchant ID passed');
}
$headers = getallheaders();
@@ -77,11 +78,11 @@ class CoinPayments {
$hmac = hash_hmac("sha512", $request, trim($this->config['coinpayments_ipn_secret']));
// if ($hmac != $signHeader) { <-- Use this if you are running a version of PHP below 5.6.0 without the hash_equals function
// abort(400, 'HMAC signature does not match');
// throw new ApiException(400, 'HMAC signature does not match');
// }
if (!hash_equals($hmac, $signHeader)) {
abort(400, 'HMAC signature does not match');
throw new ApiException(400, 'HMAC signature does not match');
}
// HMAC Signature verified at this point, load some variables.
@@ -95,7 +96,7 @@ class CoinPayments {
];
} else if ($status < 0) {
//payment error, this is usually final but payments will sometimes be reopened if there was no exchange rate conversion or with seller consent
abort(500, 'Payment Timed Out or Error');
throw new ApiException(500, 'Payment Timed Out or Error');
} else {
//payment is pending, you can optionally add a note to the order page
return('IPN OK: pending');
+3 -2
View File
@@ -1,6 +1,7 @@
<?php
namespace App\Payments;
use App\Exceptions\ApiException;
class Coinbase {
public function __construct($config) {
@@ -50,7 +51,7 @@ class Coinbase {
$ret = @json_decode($ret_raw, true);
if(empty($ret['data']['hosted_url'])) {
abort(500, "error!");
throw new ApiException(500, "error!");
}
return [
'type' => 1,
@@ -70,7 +71,7 @@ class Coinbase {
$computedSignature = \hash_hmac('sha256', $payload, $this->config['coinbase_webhook_key']);
if (!self::hashEqual($signatureHeader, $computedSignature)) {
abort(400, 'HMAC signature does not match');
throw new ApiException(400, 'HMAC signature does not match');
}
$out_trade_no = $json_param['event']['data']['metadata']['outTradeNo'];
+6 -5
View File
@@ -5,6 +5,7 @@
*/
namespace App\Payments;
use App\Exceptions\ApiException;
use \Curl\Curl;
class MGate {
@@ -62,21 +63,21 @@ class MGate {
$curl->post($this->config['mgate_url'] . '/v1/gateway/fetch', http_build_query($params));
$result = $curl->response;
if (!$result) {
abort(500, '网络异常');
throw new ApiException(500, '网络异常');
}
if ($curl->error) {
if (isset($result->errors)) {
$errors = (array)$result->errors;
abort(500, $errors[array_keys($errors)[0]][0]);
throw new ApiException(500, $errors[array_keys($errors)[0]][0]);
}
if (isset($result->message)) {
abort(500, $result->message);
throw new ApiException(500, $result->message);
}
abort(500, '未知错误');
throw new ApiException(500, '未知错误');
}
$curl->close();
if (!isset($result->data->trade_no)) {
abort(500, '接口请求失败');
throw new ApiException(500, '接口请求失败');
}
return [
'type' => 1, // 0:qrcode 1:url
+5 -4
View File
@@ -5,6 +5,7 @@
*/
namespace App\Payments;
use App\Exceptions\ApiException;
use Stripe\Source;
use Stripe\Stripe;
@@ -40,7 +41,7 @@ class StripeAlipay {
$currency = $this->config['currency'];
$exchange = $this->exchange('CNY', strtoupper($currency));
if (!$exchange) {
abort(500, __('Currency conversion has timed out, please try again later'));
throw new ApiException(500, __('Currency conversion has timed out, please try again later'));
}
Stripe::setApiKey($this->config['stripe_sk_live']);
$source = Source::create([
@@ -58,7 +59,7 @@ class StripeAlipay {
]
]);
if (!$source['redirect']['url']) {
abort(500, __('Payment gateway request failed'));
throw new ApiException(500, __('Payment gateway request failed'));
}
return [
'type' => 1,
@@ -76,7 +77,7 @@ class StripeAlipay {
$this->config['stripe_webhook_key']
);
} catch (\Stripe\Error\SignatureVerification $e) {
abort(400);
throw new ApiException(400);
}
switch ($event->type) {
case 'source.chargeable':
@@ -103,7 +104,7 @@ class StripeAlipay {
}
break;
default:
abort(500, 'event is not support');
throw new ApiException(500, 'event is not support');
}
return('success');
}
+5 -4
View File
@@ -2,6 +2,7 @@
namespace App\Payments;
use App\Exceptions\ApiException;
use Stripe\Stripe;
use Stripe\Checkout\Session;
@@ -47,7 +48,7 @@ class StripeCheckout {
$currency = $this->config['currency'];
$exchange = $this->exchange('CNY', strtoupper($currency));
if (!$exchange) {
abort(500, __('Currency conversion has timed out, please try again later'));
throw new ApiException(500, __('Currency conversion has timed out, please try again later'));
}
$customFieldName = isset($this->config['stripe_custom_field_name']) ? $this->config['stripe_custom_field_name'] : 'Contact Infomation';
@@ -86,7 +87,7 @@ class StripeCheckout {
$session = Session::create($params);
} catch (\Exception $e) {
info($e);
abort(500, "Failed to create order. Error: {$e->getMessage}");
throw new ApiException(500, "Failed to create order. Error: {$e->getMessage}");
}
return [
'type' => 1, // 0:qrcode 1:url
@@ -104,7 +105,7 @@ class StripeCheckout {
$this->config['stripe_webhook_key']
);
} catch (\Stripe\Error\SignatureVerification $e) {
abort(400);
throw new ApiException(400);
}
switch ($event->type) {
@@ -125,7 +126,7 @@ class StripeCheckout {
];
break;
default:
abort(500, 'event is not support');
throw new ApiException(500, 'event is not support');
}
return('success');
}
+6 -5
View File
@@ -5,6 +5,7 @@
*/
namespace App\Payments;
use App\Exceptions\ApiException;
use Stripe\Source;
use Stripe\Stripe;
@@ -46,7 +47,7 @@ class StripeCredit {
$currency = $this->config['currency'];
$exchange = $this->exchange('CNY', strtoupper($currency));
if (!$exchange) {
abort(500, __('Currency conversion has timed out, please try again later'));
throw new ApiException(500, __('Currency conversion has timed out, please try again later'));
}
Stripe::setApiKey($this->config['stripe_sk_live']);
try {
@@ -62,10 +63,10 @@ class StripeCredit {
]);
} catch (\Exception $e) {
info($e);
abort(500, __('Payment failed. Please check your credit card information'));
throw new ApiException(500, __('Payment failed. Please check your credit card information'));
}
if (!$charge->paid) {
abort(500, __('Payment failed. Please check your credit card information'));
throw new ApiException(500, __('Payment failed. Please check your credit card information'));
}
return [
'type' => 2,
@@ -83,7 +84,7 @@ class StripeCredit {
$this->config['stripe_webhook_key']
);
} catch (\Stripe\Error\SignatureVerification $e) {
abort(400);
throw new ApiException(400);
}
switch ($event->type) {
case 'source.chargeable':
@@ -110,7 +111,7 @@ class StripeCredit {
}
break;
default:
abort(500, 'event is not support');
throw new ApiException(500, 'event is not support');
}
return('success');
}
+5 -4
View File
@@ -5,6 +5,7 @@
*/
namespace App\Payments;
use App\Exceptions\ApiException;
use Stripe\Source;
use Stripe\Stripe;
@@ -40,7 +41,7 @@ class StripeWepay {
$currency = $this->config['currency'];
$exchange = $this->exchange('CNY', strtoupper($currency));
if (!$exchange) {
abort(500, __('Currency conversion has timed out, please try again later'));
throw new ApiException(500, __('Currency conversion has timed out, please try again later'));
}
Stripe::setApiKey($this->config['stripe_sk_live']);
$source = Source::create([
@@ -58,7 +59,7 @@ class StripeWepay {
]
]);
if (!$source['wechat']['qr_code_url']) {
abort(500, __('Payment gateway request failed'));
throw new ApiException(500, __('Payment gateway request failed'));
}
return [
'type' => 0,
@@ -76,7 +77,7 @@ class StripeWepay {
$this->config['stripe_webhook_key']
);
} catch (\Stripe\Error\SignatureVerification $e) {
abort(400);
throw new ApiException(400);
}
switch ($event->type) {
case 'source.chargeable':
@@ -103,7 +104,7 @@ class StripeWepay {
}
break;
default:
abort(500, 'event is not support');
throw new ApiException(500, 'event is not support');
}
return('success');
}
+2 -1
View File
@@ -2,6 +2,7 @@
namespace App\Payments;
use App\Exceptions\ApiException;
use Omnipay\Omnipay;
use Omnipay\WechatPay\Helper;
@@ -52,7 +53,7 @@ class WechatPayNative {
$response = $request->send();
$response = $response->getData();
if ($response['return_code'] !== 'SUCCESS') {
abort(500, $response['return_msg']);
throw new ApiException(500, $response['return_msg']);
}
return [
'type' => 0,