refactor: 重构规范部分代码、邮件队列增加失败重试、去除多个支付方式、更新依赖

This commit is contained in:
xboard
2024-04-10 00:51:03 +08:00
parent ec63e05575
commit 4c6c7182e2
50 changed files with 421 additions and 1005 deletions
+1
View File
@@ -7,6 +7,7 @@ namespace App\Payments;
use App\Exceptions\ApiException;
class AlipayF2F {
protected $config;
public function __construct($config)
{
$this->config = $config;
+18 -12
View File
@@ -1,11 +1,15 @@
<?php
namespace App\Payments;
use App\Exceptions\ApiException;
class BTCPay {
public function __construct($config) {
class BTCPay
{
protected $config;
public function __construct($config)
{
$this->config = $config;
}
@@ -35,7 +39,8 @@ class BTCPay {
];
}
public function pay($order) {
public function pay($order)
{
$params = [
'jsonResponse' => true,
@@ -52,7 +57,7 @@ class BTCPay {
$ret = @json_decode($ret_raw, true);
if(empty($ret['checkoutLink'])) {
if (empty($ret['checkoutLink'])) {
throw new ApiException("error!");
}
return [
@@ -61,7 +66,8 @@ class BTCPay {
];
}
public function notify($params) {
public function notify($params)
{
$payload = trim(get_request_content());
$headers = getallheaders();
@@ -76,7 +82,7 @@ class BTCPay {
$computedSignature = "sha256=" . \hash_hmac('sha256', $payload, $this->config['btcpay_webhook_key']);
if (!self::hashEqual($signraturHeader, $computedSignature)) {
throw new ApiException('HMAC signature does not match',400);
throw new ApiException('HMAC signature does not match', 400);
return false;
}
@@ -93,16 +99,16 @@ class BTCPay {
$out_trade_no = $invoiceDetail['metadata']["orderId"];
$pay_trade_no=$json_param['invoiceId'];
$pay_trade_no = $json_param['invoiceId'];
return [
'trade_no' => $out_trade_no,
'callback_no' => $pay_trade_no
];
}
private function _curlPost($url,$params=false){
private function _curlPost($url, $params = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
@@ -111,7 +117,9 @@ class BTCPay {
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt(
$ch, CURLOPT_HTTPHEADER, array('Authorization:' .'token '.$this->config['btcpay_api_key'], 'Content-Type: application/json')
$ch,
CURLOPT_HTTPHEADER,
array('Authorization:' . 'token ' . $this->config['btcpay_api_key'], 'Content-Type: application/json')
);
$result = curl_exec($ch);
curl_close($ch);
@@ -143,6 +151,4 @@ class BTCPay {
return !$ret;
}
}
}
+7 -3
View File
@@ -1,10 +1,14 @@
<?php
namespace App\Payments;
use App\Exceptions\ApiException;
class CoinPayments {
public function __construct($config) {
class CoinPayments
{
protected $config;
public function __construct($config)
{
$this->config = $config;
}
@@ -99,7 +103,7 @@ class CoinPayments {
throw new ApiException('Payment Timed Out or Error');
} else {
//payment is pending, you can optionally add a note to the order page
return('IPN OK: pending');
return ('IPN OK: pending');
}
}
}
+18 -12
View File
@@ -1,10 +1,14 @@
<?php
namespace App\Payments;
use App\Exceptions\ApiException;
class Coinbase {
public function __construct($config) {
class Coinbase
{
protected $config;
public function __construct($config)
{
$this->config = $config;
}
@@ -29,7 +33,8 @@ class Coinbase {
];
}
public function pay($order) {
public function pay($order)
{
$params = [
'name' => '订阅套餐',
@@ -50,7 +55,7 @@ class Coinbase {
$ret = @json_decode($ret_raw, true);
if(empty($ret['data']['hosted_url'])) {
if (empty($ret['data']['hosted_url'])) {
throw new ApiException("error!");
}
return [
@@ -59,7 +64,8 @@ class Coinbase {
];
}
public function notify($params) {
public function notify($params)
{
$payload = trim(get_request_content());
$json_param = json_decode($payload, true);
@@ -71,20 +77,20 @@ class Coinbase {
$computedSignature = \hash_hmac('sha256', $payload, $this->config['coinbase_webhook_key']);
if (!self::hashEqual($signatureHeader, $computedSignature)) {
throw new ApiException( 'HMAC signature does not match', 400);
throw new ApiException('HMAC signature does not match', 400);
}
$out_trade_no = $json_param['event']['data']['metadata']['outTradeNo'];
$pay_trade_no=$json_param['event']['id'];
$pay_trade_no = $json_param['event']['id'];
return [
'trade_no' => $out_trade_no,
'callback_no' => $pay_trade_no
];
}
private function _curlPost($url,$params=false){
private function _curlPost($url, $params = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
@@ -93,7 +99,9 @@ class Coinbase {
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt(
$ch, CURLOPT_HTTPHEADER, array('X-CC-Api-Key:' .$this->config['coinbase_api_key'], 'X-CC-Version: 2018-03-22')
$ch,
CURLOPT_HTTPHEADER,
array('X-CC-Api-Key:' . $this->config['coinbase_api_key'], 'X-CC-Version: 2018-03-22')
);
$result = curl_exec($ch);
curl_close($ch);
@@ -124,6 +132,4 @@ class Coinbase {
return !$ret;
}
}
}
+3 -1
View File
@@ -2,7 +2,9 @@
namespace App\Payments;
class EPay {
class EPay
{
protected $config;
public function __construct($config)
{
$this->config = $config;
-118
View File
@@ -1,118 +0,0 @@
<?php
/**
* 自己写别抄,抄NMB抄
*/
namespace App\Payments;
use App\Exceptions\ApiException;
use Stripe\Source;
use Stripe\Stripe;
class StripeAlipay {
public function __construct($config)
{
$this->config = $config;
}
public function form()
{
return [
'currency' => [
'label' => '货币单位',
'description' => '',
'type' => 'input',
],
'stripe_sk_live' => [
'label' => 'SK_LIVE',
'description' => '',
'type' => 'input',
],
'stripe_webhook_key' => [
'label' => 'WebHook密钥签名',
'description' => '',
'type' => 'input',
]
];
}
public function pay($order)
{
$currency = $this->config['currency'];
$exchange = $this->exchange('CNY', strtoupper($currency));
if (!$exchange) {
throw new ApiException(__('Currency conversion has timed out, please try again later'));
}
Stripe::setApiKey($this->config['stripe_sk_live']);
$source = Source::create([
'amount' => floor($order['total_amount'] * $exchange),
'currency' => $currency,
'type' => 'alipay',
'statement_descriptor' => $order['trade_no'],
'metadata' => [
'user_id' => $order['user_id'],
'out_trade_no' => $order['trade_no'],
'identifier' => ''
],
'redirect' => [
'return_url' => $order['return_url']
]
]);
if (!$source['redirect']['url']) {
throw new ApiException(__('Payment gateway request failed'));
}
return [
'type' => 1,
'data' => $source['redirect']['url']
];
}
public function notify($params)
{
\Stripe\Stripe::setApiKey($this->config['stripe_sk_live']);
try {
$event = \Stripe\Webhook::constructEvent(
get_request_content(),
request()->header('HTTP_STRIPE_SIGNATURE'),
$this->config['stripe_webhook_key']
);
} catch (\Stripe\Error\SignatureVerification $e) {
abort(400);
}
switch ($event->type) {
case 'source.chargeable':
$object = $event->data->object;
\Stripe\Charge::create([
'amount' => $object->amount,
'currency' => $object->currency,
'source' => $object->id,
'metadata' => json_decode($object->metadata, true)
]);
break;
case 'charge.succeeded':
$object = $event->data->object;
if ($object->status === 'succeeded') {
if (!isset($object->metadata->out_trade_no) && !isset($object->source->metadata)) {
return('order error');
}
$metaData = isset($object->metadata->out_trade_no) ? $object->metadata : $object->source->metadata;
$tradeNo = $metaData->out_trade_no;
return [
'trade_no' => $tradeNo,
'callback_no' => $object->id
];
}
break;
default:
throw new ApiException('event is not support');
}
return('success');
}
private function exchange($from, $to)
{
$result = file_get_contents('https://api.exchangerate.host/latest?symbols=' . $to . '&base=' . $from);
$result = json_decode($result, true);
return $result['rates'][$to];
}
}
-140
View File
@@ -1,140 +0,0 @@
<?php
namespace App\Payments;
use App\Exceptions\ApiException;
use Stripe\Stripe;
use Stripe\Checkout\Session;
class StripeCheckout {
public function __construct($config)
{
$this->config = $config;
}
public function form()
{
return [
'currency' => [
'label' => '货币单位',
'description' => '',
'type' => 'input',
],
'stripe_sk_live' => [
'label' => 'SK_LIVE',
'description' => 'API 密钥',
'type' => 'input',
],
'stripe_pk_live' => [
'label' => 'PK_LIVE',
'description' => 'API 公钥',
'type' => 'input',
],
'stripe_webhook_key' => [
'label' => 'WebHook 密钥签名',
'description' => '',
'type' => 'input',
],
'stripe_custom_field_name' => [
'label' => '自定义字段名称',
'description' => '例如可设置为“联系方式”,以便及时与客户取得联系',
'type' => 'input',
]
];
}
public function pay($order)
{
$currency = $this->config['currency'];
$exchange = $this->exchange('CNY', strtoupper($currency));
if (!$exchange) {
throw new ApiException(__('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';
$params = [
'success_url' => $order['return_url'],
'cancel_url' => $order['return_url'],
'client_reference_id' => $order['trade_no'],
'line_items' => [
[
'price_data' => [
'currency' => $currency,
'product_data' => [
'name' => $order['trade_no']
],
'unit_amount' => floor($order['total_amount'] * $exchange)
],
'quantity' => 1
]
],
'mode' => 'payment',
'invoice_creation' => ['enabled' => true],
'phone_number_collection' => ['enabled' => true],
'custom_fields' => [
[
'key' => 'contactinfo',
'label' => ['type' => 'custom', 'custom' => $customFieldName],
'type' => 'text',
],
],
// 'customer_email' => $user['email'] not support
];
Stripe::setApiKey($this->config['stripe_sk_live']);
try {
$session = Session::create($params);
} catch (\Exception $e) {
info($e);
throw new ApiException("Failed to create order. Error: {$e->getMessage}");
}
return [
'type' => 1, // 0:qrcode 1:url
'data' => $session->url
];
}
public function notify($params)
{
\Stripe\Stripe::setApiKey($this->config['stripe_sk_live']);
try {
$event = \Stripe\Webhook::constructEvent(
get_request_content(),
request()->header('HTTP_STRIPE_SIGNATURE'),
$this->config['stripe_webhook_key']
);
} catch (\Stripe\Error\SignatureVerification $e) {
abort(400);
}
switch ($event->type) {
case 'checkout.session.completed':
$object = $event->data->object;
if ($object->payment_status === 'paid') {
return [
'trade_no' => $object->client_reference_id,
'callback_no' => $object->payment_intent
];
}
break;
case 'checkout.session.async_payment_succeeded':
$object = $event->data->object;
return [
'trade_no' => $object->client_reference_id,
'callback_no' => $object->payment_intent
];
break;
default:
throw new ApiException('event is not support');
}
return('success');
}
private function exchange($from, $to)
{
$result = file_get_contents('https://api.exchangerate.host/latest?symbols=' . $to . '&base=' . $from);
$result = json_decode($result, true);
return $result['rates'][$to];
}
}
-126
View File
@@ -1,126 +0,0 @@
<?php
/**
* 自己写别抄,抄NMB抄
*/
namespace App\Payments;
use App\Exceptions\ApiException;
use Stripe\Source;
use Stripe\Stripe;
class StripeCredit {
public function __construct($config)
{
$this->config = $config;
}
public function form()
{
return [
'currency' => [
'label' => '货币单位',
'description' => '',
'type' => 'input',
],
'stripe_sk_live' => [
'label' => 'SK_LIVE',
'description' => '',
'type' => 'input',
],
'stripe_pk_live' => [
'label' => 'PK_LIVE',
'description' => '',
'type' => 'input',
],
'stripe_webhook_key' => [
'label' => 'WebHook密钥签名',
'description' => '',
'type' => 'input',
]
];
}
public function pay($order)
{
info($order);
$currency = $this->config['currency'];
$exchange = $this->exchange('CNY', strtoupper($currency));
if (!$exchange) {
throw new ApiException(__('Currency conversion has timed out, please try again later'));
}
Stripe::setApiKey($this->config['stripe_sk_live']);
try {
$charge = \Stripe\Charge::create([
'amount' => floor($order['total_amount'] * $exchange),
'currency' => $currency,
'source' => $order['stripe_token'],
'metadata' => [
'user_id' => $order['user_id'],
'out_trade_no' => $order['trade_no'],
'identifier' => ''
]
]);
} catch (\Exception $e) {
info($e);
throw new ApiException(__('Payment failed. Please check your credit card information'));
}
if (!$charge->paid) {
throw new ApiException(__('Payment failed. Please check your credit card information'));
}
return [
'type' => 2,
'data' => $charge->paid
];
}
public function notify($params)
{
\Stripe\Stripe::setApiKey($this->config['stripe_sk_live']);
try {
$event = \Stripe\Webhook::constructEvent(
get_request_content(),
request()->header('HTTP_STRIPE_SIGNATURE'),
$this->config['stripe_webhook_key']
);
} catch (\Stripe\Error\SignatureVerification $e) {
\Log::error($e);
abort(400);
}
switch ($event->type) {
case 'source.chargeable':
$object = $event->data->object;
\Stripe\Charge::create([
'amount' => $object->amount,
'currency' => $object->currency,
'source' => $object->id,
'metadata' => json_decode($object->metadata, true)
]);
break;
case 'charge.succeeded':
$object = $event->data->object;
if ($object->status === 'succeeded') {
if (!isset($object->metadata->out_trade_no) && !isset($object->source->metadata)) {
return('order error');
}
$metaData = isset($object->metadata->out_trade_no) ? $object->metadata : $object->source->metadata;
$tradeNo = $metaData->out_trade_no;
return [
'trade_no' => $tradeNo,
'callback_no' => $object->id
];
}
break;
default:
throw new ApiException('event is not support');
}
return('success');
}
private function exchange($from, $to)
{
$result = file_get_contents('https://api.exchangerate.host/latest?symbols=' . $to . '&base=' . $from);
$result = json_decode($result, true);
return $result['rates'][$to];
}
}
-118
View File
@@ -1,118 +0,0 @@
<?php
/**
* 自己写别抄,抄NMB抄
*/
namespace App\Payments;
use App\Exceptions\ApiException;
use Stripe\Source;
use Stripe\Stripe;
class StripeWepay {
public function __construct($config)
{
$this->config = $config;
}
public function form()
{
return [
'currency' => [
'label' => '货币单位',
'description' => '',
'type' => 'input',
],
'stripe_sk_live' => [
'label' => 'SK_LIVE',
'description' => '',
'type' => 'input',
],
'stripe_webhook_key' => [
'label' => 'WebHook密钥签名',
'description' => '',
'type' => 'input',
]
];
}
public function pay($order)
{
$currency = $this->config['currency'];
$exchange = $this->exchange('CNY', strtoupper($currency));
if (!$exchange) {
throw new ApiException(__('Currency conversion has timed out, please try again later'));
}
Stripe::setApiKey($this->config['stripe_sk_live']);
$source = Source::create([
'amount' => floor($order['total_amount'] * $exchange),
'currency' => $currency,
'type' => 'wechat',
'statement_descriptor' => $order['trade_no'],
'metadata' => [
'user_id' => $order['user_id'],
'out_trade_no' => $order['trade_no'],
'identifier' => ''
],
'redirect' => [
'return_url' => $order['return_url']
]
]);
if (!$source['wechat']['qr_code_url']) {
throw new ApiException(__('Payment gateway request failed'));
}
return [
'type' => 0,
'data' => $source['wechat']['qr_code_url']
];
}
public function notify($params)
{
\Stripe\Stripe::setApiKey($this->config['stripe_sk_live']);
try {
$event = \Stripe\Webhook::constructEvent(
get_request_content(),
request()->header('HTTP_STRIPE_SIGNATURE'),
$this->config['stripe_webhook_key']
);
} catch (\Stripe\Error\SignatureVerification $e) {
abort(400);
}
switch ($event->type) {
case 'source.chargeable':
$object = $event->data->object;
\Stripe\Charge::create([
'amount' => $object->amount,
'currency' => $object->currency,
'source' => $object->id,
'metadata' => json_decode($object->metadata, true)
]);
break;
case 'charge.succeeded':
$object = $event->data->object;
if ($object->status === 'succeeded') {
if (!isset($object->metadata->out_trade_no) && !isset($object->source->metadata)) {
return('order error');
}
$metaData = isset($object->metadata->out_trade_no) ? $object->metadata : $object->source->metadata;
$tradeNo = $metaData->out_trade_no;
return [
'trade_no' => $tradeNo,
'callback_no' => $object->id
];
}
break;
default:
throw new ApiException('event is not support');
}
return('success');
}
private function exchange($from, $to)
{
$result = file_get_contents('https://api.exchangerate.host/latest?symbols=' . $to . '&base=' . $from);
$result = json_decode($result, true);
return $result['rates'][$to];
}
}
-85
View File
@@ -1,85 +0,0 @@
<?php
namespace App\Payments;
use App\Exceptions\ApiException;
use Omnipay\Omnipay;
use Omnipay\WechatPay\Helper;
class WechatPayNative {
public function __construct($config)
{
$this->config = $config;
}
public function form()
{
return [
'app_id' => [
'label' => 'APPID',
'description' => '绑定微信支付商户的APPID',
'type' => 'input',
],
'mch_id' => [
'label' => '商户号',
'description' => '微信支付商户号',
'type' => 'input',
],
'api_key' => [
'label' => 'APIKEY(v1)',
'description' => '',
'type' => 'input',
]
];
}
public function pay($order)
{
$gateway = Omnipay::create('WechatPay_Native');
$gateway->setAppId($this->config['app_id']);
$gateway->setMchId($this->config['mch_id']);
$gateway->setApiKey($this->config['api_key']);
$gateway->setNotifyUrl($order['notify_url']);
$params = [
'body' => $order['trade_no'],
'out_trade_no' => $order['trade_no'],
'total_fee' => $order['total_amount'],
'spbill_create_ip' => '0.0.0.0',
'fee_type' => 'CNY'
];
$request = $gateway->purchase($params);
$response = $request->send();
$response = $response->getData();
if ($response['return_code'] !== 'SUCCESS') {
throw new ApiException($response['return_msg']);
}
return [
'type' => 0,
'data' => $response['code_url'],
'custom_result' => '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>'
];
}
public function notify($params)
{
$data = Helper::xml2array(get_request_content());
$gateway = Omnipay::create('WechatPay');
$gateway->setAppId($this->config['app_id']);
$gateway->setMchId($this->config['mch_id']);
$gateway->setApiKey($this->config['api_key']);
$response = $gateway->completePurchase([
'request_params' => get_request_content()
])->send();
if (!$response->isPaid()) {
return('FAIL');
}
return [
'trade_no' => $data['out_trade_no'],
'callback_no' => $data['transaction_id']
];
}
}