mirror of
https://github.com/ProxyPanel/ProxyPanel.git
synced 2026-04-11 15:10:54 +00:00
➕ Modularize Payment System
This commit is contained in:
@@ -42,19 +42,19 @@ class SystemController extends Controller
|
||||
|
||||
private function getPayments(): array
|
||||
{
|
||||
$paymentConfigs = [ // 支付渠道及其所需配置项映射
|
||||
'f2fpay' => ['f2fpay_app_id', 'f2fpay_private_key', 'f2fpay_public_key'],
|
||||
'codepay' => ['codepay_url', 'codepay_id', 'codepay_key'],
|
||||
'epay' => ['epay_url', 'epay_mch_id', 'epay_key'],
|
||||
'payjs' => ['payjs_mch_id', 'payjs_key'],
|
||||
'bitpayx' => ['bitpay_secret'],
|
||||
'paypal' => ['paypal_client_id', 'paypal_client_secret', 'paypal_app_id'],
|
||||
'stripe' => ['stripe_public_key', 'stripe_secret_key'],
|
||||
'paybeaver' => ['paybeaver_app_id', 'paybeaver_app_secret'],
|
||||
'theadpay' => ['theadpay_mchid', 'theadpay_key'],
|
||||
];
|
||||
$paymentConfigs = cache()->rememberForever('payment_configs', function () { // 支付渠道及其所需配置项映射
|
||||
foreach (glob(app_path('Utils/Payments/*.php')) as $file) {
|
||||
$className = 'App\\Utils\\Payments\\'.basename($file, '.php');
|
||||
if (class_exists($className)) {
|
||||
$methodDetails = $className::$methodDetails ?? null;
|
||||
if ($methodDetails && ! empty($methodDetails['settings'])) {
|
||||
$configs[$methodDetails['key']] = $methodDetails['settings'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$payment = [];
|
||||
return $configs ?? [];
|
||||
});
|
||||
|
||||
// 遍历映射,检查配置项是否存在
|
||||
foreach ($paymentConfigs as $paymentName => $configKeys) {
|
||||
|
||||
@@ -9,17 +9,8 @@ use App\Models\Payment;
|
||||
use App\Services\CouponService;
|
||||
use App\Utils\Helpers;
|
||||
use App\Utils\Library\Templates\Gateway;
|
||||
use App\Utils\Payments\CodePay;
|
||||
use App\Utils\Payments\EPay;
|
||||
use App\Utils\Payments\F2Fpay;
|
||||
use App\Utils\Payments\Local;
|
||||
use App\Utils\Payments\Manual;
|
||||
use App\Utils\Payments\PayBeaver;
|
||||
use App\Utils\Payments\PayJs;
|
||||
use App\Utils\Payments\PayPal;
|
||||
use App\Utils\Payments\Stripe;
|
||||
use App\Utils\Payments\THeadPay;
|
||||
use Exception;
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -40,29 +31,37 @@ class PaymentController extends Controller
|
||||
|
||||
public static function getClient(): Gateway
|
||||
{
|
||||
// Mapping of payment methods to their respective classes
|
||||
$paymentMethods = [
|
||||
'credit' => Local::class,
|
||||
'f2fpay' => F2Fpay::class,
|
||||
'codepay' => CodePay::class,
|
||||
'payjs' => PayJs::class,
|
||||
'paypal' => PayPal::class,
|
||||
'epay' => EPay::class,
|
||||
'stripe' => Stripe::class,
|
||||
'paybeaver' => PayBeaver::class,
|
||||
'theadpay' => THeadPay::class,
|
||||
'manual' => Manual::class,
|
||||
];
|
||||
$method = self::$method;
|
||||
$paymentClasses = self::getPaymentClasses();
|
||||
|
||||
// Check if the method exists in the mapping
|
||||
if (isset($paymentMethods[self::$method])) {
|
||||
// Instantiate and return the corresponding class
|
||||
return new $paymentMethods[self::$method];
|
||||
if (isset($paymentClasses[$method])) {
|
||||
try {
|
||||
return Container::getInstance()->make($paymentClasses[$method]);
|
||||
} catch (Exception $e) {
|
||||
Log::emergency('Failed to instantiate payment class: '.$e->getMessage());
|
||||
abort(500);
|
||||
}
|
||||
}
|
||||
|
||||
// Log an emergency message and exit if the method is unknown
|
||||
Log::emergency(trans('user.payment.order_creation.unknown_payment').': '.self::$method);
|
||||
exit(404);
|
||||
Log::emergency(trans('user.payment.order_creation.unknown_payment').': '.$method);
|
||||
abort(404);
|
||||
}
|
||||
|
||||
private static function getPaymentClasses(): array
|
||||
{
|
||||
return cache()->rememberForever('payment_classes', function () {
|
||||
foreach (glob(app_path('Utils/Payments/*.php')) as $file) {
|
||||
$className = 'App\\Utils\\Payments\\'.basename($file, '.php');
|
||||
if (class_exists($className)) {
|
||||
$methodDetails = $className::$methodDetails ?? null;
|
||||
if ($methodDetails) {
|
||||
$classes[$methodDetails['key']] = $className;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $classes ?? [];
|
||||
});
|
||||
}
|
||||
|
||||
public static function getStatus(Request $request): JsonResponse
|
||||
|
||||
@@ -140,7 +140,7 @@ class Order extends Model
|
||||
break;
|
||||
case 0:
|
||||
$tag = 1;
|
||||
$label = trans('common.payment.status.wait');
|
||||
$label = trans('common.status.payment_pending');
|
||||
break;
|
||||
case 1:
|
||||
$tag = 2;
|
||||
|
||||
@@ -64,7 +64,7 @@ class Payment extends Model
|
||||
get: fn () => match ($this->status) {
|
||||
-1 => trans('common.failed_item', ['attribute' => trans('user.pay')]),
|
||||
1 => trans('common.success_item', ['attribute' => trans('user.pay')]),
|
||||
default => trans('common.payment.status.wait'),
|
||||
default => trans('common.status.payment_pending'),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Payment;
|
||||
use App\Models\PaymentCallback;
|
||||
use App\Notifications\PaymentReceived;
|
||||
use Str;
|
||||
|
||||
class PaymentService
|
||||
{
|
||||
final public function createPayment(int $uid, int $oid, float|int $amount): Payment
|
||||
{
|
||||
$payment = new Payment;
|
||||
$payment->trade_no = Str::random(8);
|
||||
$payment->user_id = $uid;
|
||||
$payment->order_id = $oid;
|
||||
$payment->amount = $amount;
|
||||
$payment->save();
|
||||
|
||||
return $payment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $trade_no 本地订单号
|
||||
* @param string $out_trade_no 外部订单号
|
||||
* @param float|int $amount 交易金额
|
||||
*/
|
||||
final protected function createPaymentCallback(string $trade_no, string $out_trade_no, float|int $amount): int
|
||||
{
|
||||
$log = new PaymentCallback;
|
||||
$log->trade_no = $trade_no;
|
||||
$log->out_trade_no = $out_trade_no;
|
||||
$log->amount = $amount;
|
||||
|
||||
return $log->save();
|
||||
}
|
||||
|
||||
protected function paymentReceived(string $tradeNo): bool
|
||||
{
|
||||
$payment = Payment::whereTradeNo($tradeNo)->with('order')->first();
|
||||
if ($payment) {
|
||||
$ret = $payment->order->complete();
|
||||
if ($ret) {
|
||||
$payment->user->notify(new PaymentReceived($payment->order->sn, $payment->amount_tag));
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
namespace App\Utils\Library;
|
||||
|
||||
use App\Models\Payment;
|
||||
use App\Models\PaymentCallback;
|
||||
use App\Notifications\PaymentReceived;
|
||||
use Str;
|
||||
|
||||
class PaymentHelper
|
||||
{
|
||||
/**
|
||||
@@ -36,4 +41,54 @@ class PaymentHelper
|
||||
|
||||
return md5(urldecode(http_build_query($data)).$key); // 拼接
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $uid 用户ID
|
||||
* @param int $oid 订单ID
|
||||
* @param float|int $amount 交易金额
|
||||
*/
|
||||
public static function createPayment(int $uid, int $oid, float|int $amount): Payment
|
||||
{
|
||||
$payment = new Payment;
|
||||
$payment->trade_no = Str::random(8);
|
||||
$payment->user_id = $uid;
|
||||
$payment->order_id = $oid;
|
||||
$payment->amount = $amount;
|
||||
$payment->save();
|
||||
|
||||
return $payment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $trade_no 本地订单号
|
||||
* @param string $out_trade_no 外部订单号
|
||||
* @param float|int $amount 交易金额
|
||||
*/
|
||||
public static function createPaymentCallback(string $trade_no, string $out_trade_no, float|int $amount): bool
|
||||
{
|
||||
$log = new PaymentCallback;
|
||||
$log->trade_no = $trade_no;
|
||||
$log->out_trade_no = $out_trade_no;
|
||||
$log->amount = $amount;
|
||||
|
||||
return $log->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $tradeNo 本地订单号
|
||||
*/
|
||||
public static function paymentReceived(string $tradeNo): bool
|
||||
{
|
||||
$payment = Payment::whereTradeNo($tradeNo)->with('order')->first();
|
||||
if ($payment) {
|
||||
$ret = $payment->order->complete();
|
||||
if ($ret) {
|
||||
$payment->user->notify(new PaymentReceived($payment->order->sn, $payment->amount_tag));
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,20 +2,22 @@
|
||||
|
||||
namespace App\Utils\Payments;
|
||||
|
||||
use App\Services\PaymentService;
|
||||
use App\Utils\Library\PaymentHelper;
|
||||
use App\Utils\Library\Templates\Gateway;
|
||||
use Auth;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
use Response;
|
||||
|
||||
class CodePay extends PaymentService implements Gateway
|
||||
class CodePay implements Gateway
|
||||
{
|
||||
public static array $methodDetails = [
|
||||
'key' => 'codepay',
|
||||
'settings' => ['codepay_url', 'codepay_id', 'codepay_key'],
|
||||
];
|
||||
|
||||
public function purchase(Request $request): JsonResponse
|
||||
{
|
||||
$payment = $this->createPayment(Auth::id(), $request->input('id'), $request->input('amount'));
|
||||
$payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
|
||||
|
||||
$data = [
|
||||
'id' => sysConfig('codepay_id'),
|
||||
@@ -32,7 +34,7 @@ class CodePay extends PaymentService implements Gateway
|
||||
$url = sysConfig('codepay_url').http_build_query($data);
|
||||
$payment->update(['url' => $url]);
|
||||
|
||||
return Response::json(['status' => 'success', 'url' => $url, 'message' => trans('user.payment.order_creation.success')]);
|
||||
return response()->json(['status' => 'success', 'url' => $url, 'message' => trans('user.payment.order_creation.success')]);
|
||||
}
|
||||
|
||||
public function notify(Request $request): void
|
||||
@@ -40,7 +42,7 @@ class CodePay extends PaymentService implements Gateway
|
||||
$tradeNo = $request->input('pay_id');
|
||||
if ($tradeNo && $request->input('pay_no')
|
||||
&& PaymentHelper::verify($request->except('method'), sysConfig('codepay_key'), $request->input('sign'), false)) {
|
||||
if ($this->paymentReceived($tradeNo)) {
|
||||
if (PaymentHelper::paymentReceived($tradeNo)) {
|
||||
exit('success');
|
||||
}
|
||||
|
||||
|
||||
@@ -4,15 +4,17 @@ namespace App\Utils\Payments;
|
||||
|
||||
use App\Models\Goods;
|
||||
use App\Models\Order;
|
||||
use App\Services\PaymentService;
|
||||
use App\Utils\Helpers;
|
||||
use App\Utils\Library\Templates\Gateway;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Response;
|
||||
|
||||
class Local extends PaymentService implements Gateway
|
||||
class Credit implements Gateway
|
||||
{
|
||||
public static array $methodDetails = [
|
||||
'key' => 'credit',
|
||||
];
|
||||
|
||||
public function purchase(Request $request): JsonResponse
|
||||
{
|
||||
$order = Order::find($request->input('id'));
|
||||
@@ -27,7 +29,7 @@ class Local extends PaymentService implements Gateway
|
||||
|
||||
$order->complete();
|
||||
|
||||
return Response::json(['status' => 'success', 'message' => trans('user.purchase.completed')]);
|
||||
return response()->json(['status' => 'success', 'message' => trans('user.purchase.completed')]);
|
||||
}
|
||||
|
||||
public function notify(Request $request): void
|
||||
@@ -2,21 +2,23 @@
|
||||
|
||||
namespace App\Utils\Payments;
|
||||
|
||||
use App\Services\PaymentService;
|
||||
use App\Utils\Library\PaymentHelper;
|
||||
use App\Utils\Library\Templates\Gateway;
|
||||
use Auth;
|
||||
use Http;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
use Response;
|
||||
|
||||
class EPay extends PaymentService implements Gateway
|
||||
class EPay implements Gateway
|
||||
{
|
||||
public static array $methodDetails = [
|
||||
'key' => 'epay',
|
||||
'settings' => ['epay_url', 'epay_mch_id', 'epay_key'],
|
||||
];
|
||||
|
||||
public function purchase(Request $request): JsonResponse
|
||||
{
|
||||
$payment = $this->createPayment(Auth::id(), $request->input('id'), $request->input('amount'));
|
||||
$payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
|
||||
|
||||
$data = [
|
||||
'pid' => sysConfig('epay_mch_id'),
|
||||
@@ -33,14 +35,14 @@ class EPay extends PaymentService implements Gateway
|
||||
$url = sysConfig('epay_url').'submit.php?'.http_build_query($data);
|
||||
$payment->update(['url' => $url]);
|
||||
|
||||
return Response::json(['status' => 'success', 'url' => $url, 'message' => trans('user.payment.order_creation.success')]);
|
||||
return response()->json(['status' => 'success', 'url' => $url, 'message' => trans('user.payment.order_creation.success')]);
|
||||
}
|
||||
|
||||
public function notify(Request $request): void
|
||||
{
|
||||
if ($request->input('trade_status') === 'TRADE_SUCCESS' && $request->has('out_trade_no')
|
||||
&& PaymentHelper::verify($request->except('method'), sysConfig('epay_key'), $request->input('sign'))) {
|
||||
if ($this->paymentReceived($request->input('out_trade_no'))) {
|
||||
if (PaymentHelper::paymentReceived($request->input('out_trade_no'))) {
|
||||
exit('SUCCESS');
|
||||
}
|
||||
|
||||
@@ -61,9 +63,9 @@ class EPay extends PaymentService implements Gateway
|
||||
]);
|
||||
|
||||
if ($response->ok()) {
|
||||
return Response::json(['status' => 'success', 'data' => $response->json()]);
|
||||
return response()->json(['status' => 'success', 'data' => $response->json()]);
|
||||
}
|
||||
|
||||
return Response::json(['status' => 'fail', 'message' => '获取失败!请检查配置信息']);
|
||||
return response()->json(['status' => 'fail', 'message' => '获取失败!请检查配置信息']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,18 +3,21 @@
|
||||
namespace App\Utils\Payments;
|
||||
|
||||
use App\Models\Payment;
|
||||
use App\Services\PaymentService;
|
||||
use App\Utils\Library\AlipayF2F;
|
||||
use App\Utils\Library\PaymentHelper;
|
||||
use App\Utils\Library\Templates\Gateway;
|
||||
use Auth;
|
||||
use Exception;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
use Response;
|
||||
|
||||
class F2Fpay extends PaymentService implements Gateway
|
||||
class F2FPay implements Gateway
|
||||
{
|
||||
public static array $methodDetails = [
|
||||
'key' => 'f2fpay',
|
||||
'settings' => ['f2fpay_app_id', 'f2fpay_private_key', 'f2fpay_public_key'],
|
||||
];
|
||||
|
||||
private static AlipayF2F $aliClient;
|
||||
|
||||
public function __construct()
|
||||
@@ -29,7 +32,7 @@ class F2Fpay extends PaymentService implements Gateway
|
||||
|
||||
public function purchase(Request $request): JsonResponse
|
||||
{
|
||||
$payment = $this->createPayment(Auth::id(), $request->input('id'), $request->input('amount'));
|
||||
$payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
|
||||
|
||||
$data = [
|
||||
'subject' => sysConfig('subject_name') ?: sysConfig('website_name'),
|
||||
@@ -46,7 +49,7 @@ class F2Fpay extends PaymentService implements Gateway
|
||||
exit;
|
||||
}
|
||||
|
||||
return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => trans('user.payment.order_creation.success')]);
|
||||
return response()->json(['status' => 'success', 'data' => $payment->trade_no, 'message' => trans('user.payment.order_creation.success')]);
|
||||
}
|
||||
|
||||
public function notify(Request $request): void
|
||||
@@ -54,7 +57,7 @@ class F2Fpay extends PaymentService implements Gateway
|
||||
try {
|
||||
if (sysConfig('f2fpay_app_id') === $request->input('app_id') && self::$aliClient->validate_notification_sign($request->except('method'), $request->input('sign'))) {
|
||||
$payment = Payment::whereTradeNo($request->input('out_trade_no'))->with('order')->first();
|
||||
if ($payment && abs($payment->amount - $request->input('total_amount')) < 0.01 && in_array($request->input('trade_status'), ['TRADE_FINISHED', 'TRADE_SUCCESS']) && $this->paymentReceived($request->input('out_trade_no'))) {
|
||||
if ($payment && abs($payment->amount - $request->input('total_amount')) < 0.01 && in_array($request->input('trade_status'), ['TRADE_FINISHED', 'TRADE_SUCCESS']) && PaymentHelper::paymentReceived($request->input('out_trade_no'))) {
|
||||
exit('success');
|
||||
}
|
||||
}
|
||||
@@ -82,7 +85,7 @@ class F2Fpay extends PaymentService implements Gateway
|
||||
|
||||
if ($result['code'] === '10000' && $result['msg'] === 'Success') {
|
||||
if ($result['out_trade_no'] && in_array($result['trade_status'], ['TRADE_FINISHED', 'TRADE_SUCCESS'])) {
|
||||
if ($this->paymentReceived($result['out_trade_no'])) {
|
||||
if (PaymentHelper::paymentReceived($result['out_trade_no'])) {
|
||||
return true;
|
||||
}
|
||||
Log::error('【支付宝当面付】收单交易订单结算失败:'.var_export($result, true));
|
||||
|
||||
@@ -3,25 +3,27 @@
|
||||
namespace App\Utils\Payments;
|
||||
|
||||
use App\Models\Payment;
|
||||
use App\Services\PaymentService;
|
||||
use App\Utils\Library\PaymentHelper;
|
||||
use App\Utils\Library\Templates\Gateway;
|
||||
use Auth;
|
||||
use Hashids\Hashids;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Response;
|
||||
|
||||
class Manual extends PaymentService implements Gateway
|
||||
class Manual implements Gateway
|
||||
{
|
||||
public static array $methodDetails = [
|
||||
'key' => 'manual',
|
||||
];
|
||||
|
||||
public function purchase(Request $request): JsonResponse
|
||||
{
|
||||
$payment = $this->createPayment(Auth::id(), $request->input('id'), $request->input('amount'));
|
||||
$payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
|
||||
|
||||
$url = route('manual.checkout', ['payment' => $payment->trade_no]);
|
||||
$payment->update(['url' => $url]);
|
||||
|
||||
return Response::json(['status' => 'success', 'url' => $url, 'message' => trans('user.payment.order_creation.success')]);
|
||||
return response()->json(['status' => 'success', 'url' => $url, 'message' => trans('user.payment.order_creation.success')]);
|
||||
}
|
||||
|
||||
public function redirectPage(string $trade_no): View
|
||||
@@ -43,10 +45,10 @@ class Manual extends PaymentService implements Gateway
|
||||
$payment = Payment::uid()->with(['order'])->whereTradeNo($trade_no)->firstOrFail();
|
||||
$payment->order->update(['status' => 1]);
|
||||
|
||||
return Response::json(['status' => 'success', 'message' => trans('user.payment.order_creation.info')]);
|
||||
return response()->json(['status' => 'success', 'message' => trans('user.payment.order_creation.info')]);
|
||||
}
|
||||
|
||||
public function notify(Request $request)
|
||||
public function notify(Request $request): View
|
||||
{
|
||||
$code = $request->input('sign');
|
||||
$status = $request->input('status');
|
||||
@@ -56,7 +58,7 @@ class Manual extends PaymentService implements Gateway
|
||||
$payment = Payment::findOrFail($payment_info[0]);
|
||||
if ($payment && $payment->order && $payment->order->status === 1) {
|
||||
if ($status) {
|
||||
$this->paymentReceived($payment->trade_no);
|
||||
PaymentHelper::paymentReceived($payment->trade_no);
|
||||
} else {
|
||||
$payment->order->close();
|
||||
}
|
||||
|
||||
@@ -7,19 +7,23 @@
|
||||
|
||||
namespace App\Utils\Payments;
|
||||
|
||||
use App\Services\PaymentService;
|
||||
use App\Utils\Library\PaymentHelper;
|
||||
use App\Utils\Library\Templates\Gateway;
|
||||
use Auth;
|
||||
use Http;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
use Response;
|
||||
|
||||
class PayBeaver extends PaymentService implements Gateway
|
||||
class PayBeaver implements Gateway
|
||||
{
|
||||
private const API_URL = 'https://api.paybeaver.com/api/v1/developer';
|
||||
|
||||
public static array $methodDetails = [
|
||||
'key' => 'paybeaver',
|
||||
'settings' => ['paybeaver_app_id', 'paybeaver_app_secret'],
|
||||
];
|
||||
|
||||
private string $appId;
|
||||
|
||||
private string $appSecret;
|
||||
@@ -32,7 +36,7 @@ class PayBeaver extends PaymentService implements Gateway
|
||||
|
||||
public function purchase(Request $request): JsonResponse
|
||||
{
|
||||
$payment = $this->createPayment(Auth::id(), $request->input('id'), $request->input('amount'));
|
||||
$payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
|
||||
|
||||
$result = $this->createOrder([
|
||||
'app_id' => $this->appId,
|
||||
@@ -93,7 +97,7 @@ class PayBeaver extends PaymentService implements Gateway
|
||||
exit(json_encode(['status' => 400]));
|
||||
}
|
||||
|
||||
if ($request->has(['merchant_order_id']) && $this->paymentReceived($request->input(['merchant_order_id']))) {
|
||||
if ($request->has(['merchant_order_id']) && PaymentHelper::paymentReceived($request->input(['merchant_order_id']))) {
|
||||
exit(json_encode(['status' => 200]));
|
||||
}
|
||||
|
||||
|
||||
@@ -2,17 +2,20 @@
|
||||
|
||||
namespace App\Utils\Payments;
|
||||
|
||||
use App\Services\PaymentService;
|
||||
use App\Utils\Library\PaymentHelper;
|
||||
use App\Utils\Library\Templates\Gateway;
|
||||
use Auth;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
use Response;
|
||||
use Xhat\Payjs\Payjs as Pay;
|
||||
|
||||
class PayJs extends PaymentService implements Gateway
|
||||
class PayJs implements Gateway
|
||||
{
|
||||
public static array $methodDetails = [
|
||||
'key' => 'payjs',
|
||||
'settings' => ['payjs_mch_id', 'payjs_key'],
|
||||
];
|
||||
|
||||
private static array $config;
|
||||
|
||||
public function __construct()
|
||||
@@ -25,7 +28,7 @@ class PayJs extends PaymentService implements Gateway
|
||||
|
||||
public function purchase(Request $request): JsonResponse
|
||||
{
|
||||
$payment = $this->createPayment(Auth::id(), $request->input('id'), $request->input('amount'));
|
||||
$payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
|
||||
|
||||
$result = (new Pay($this::$config))->cashier([
|
||||
'body' => sysConfig('subject_name') ?: sysConfig('website_name'),
|
||||
@@ -38,7 +41,7 @@ class PayJs extends PaymentService implements Gateway
|
||||
$payment->update(['qr_code' => 1, 'url' => $result]);
|
||||
|
||||
//$this->addPamentCallback($payment->trade_no, null, $payment->amount * 100);
|
||||
return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => trans('user.payment.order_creation.success')]);
|
||||
return response()->json(['status' => 'success', 'data' => $payment->trade_no, 'message' => trans('user.payment.order_creation.success')]);
|
||||
}
|
||||
|
||||
public function notify(Request $request): void
|
||||
@@ -46,7 +49,7 @@ class PayJs extends PaymentService implements Gateway
|
||||
$data = (new Pay($this::$config))->notify();
|
||||
|
||||
if ($data['return_code'] == 1) {
|
||||
if ($this->paymentReceived($data['out_trade_no'])) {
|
||||
if (PaymentHelper::paymentReceived($data['out_trade_no'])) {
|
||||
exit('success');
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -2,17 +2,20 @@
|
||||
|
||||
namespace App\Utils\Payments;
|
||||
|
||||
use App\Services\PaymentService;
|
||||
use App\Utils\CurrencyExchange;
|
||||
use App\Utils\Library\PaymentHelper;
|
||||
use App\Utils\Library\Templates\Gateway;
|
||||
use Auth;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
use Response;
|
||||
|
||||
class PayPal extends PaymentService implements Gateway
|
||||
class PayPal implements Gateway
|
||||
{
|
||||
public static array $methodDetails = [
|
||||
'key' => 'paypal',
|
||||
'settings' => ['paypal_client_id', 'paypal_client_secret', 'paypal_app_id'],
|
||||
];
|
||||
|
||||
protected static \Srmklive\PayPal\Services\PayPal $provider;
|
||||
|
||||
public function __construct()
|
||||
@@ -38,9 +41,9 @@ class PayPal extends PaymentService implements Gateway
|
||||
self::$provider->getAccessToken();
|
||||
}
|
||||
|
||||
public function purchase($request): JsonResponse
|
||||
public function purchase(Request $request): JsonResponse
|
||||
{
|
||||
$payment = $this->createPayment(Auth::id(), $request->input('id'), $request->input('amount'));
|
||||
$payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
|
||||
|
||||
$data = $this->getCheckoutData($payment->trade_no, $payment->amount);
|
||||
|
||||
@@ -62,13 +65,13 @@ class PayPal extends PaymentService implements Gateway
|
||||
if (isset($response['id']) && $response['id'] != null) {
|
||||
Log::error('【Paypal】处理错误:'.var_export($response, true));
|
||||
|
||||
return Response::json(['status' => 'fail', 'message' => trans('user.payment.order_creation.failed')]);
|
||||
return response()->json(['status' => 'fail', 'message' => trans('user.payment.order_creation.failed')]);
|
||||
}
|
||||
$payment->update(['url' => $response['paypal_link']]);
|
||||
|
||||
foreach ($response['links'] as $links) {
|
||||
if ($links['rel'] === 'approve') {
|
||||
return Response::json(['status' => 'success', 'url' => $links['href'], 'message' => trans('user.payment.order_creation.success')]);
|
||||
return response()->json(['status' => 'success', 'url' => $links['href'], 'message' => trans('user.payment.order_creation.success')]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +111,7 @@ class PayPal extends PaymentService implements Gateway
|
||||
$response = self::$provider->capturePaymentOrder($request['token']);
|
||||
|
||||
if (isset($response['status']) && $response['status'] === 'COMPLETED') {
|
||||
if ($this->paymentReceived($request['invoice'])) {
|
||||
if (PaymentHelper::paymentReceived($request['invoice'])) {
|
||||
exit('success');
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -3,22 +3,25 @@
|
||||
namespace App\Utils\Payments;
|
||||
|
||||
use App\Models\Payment;
|
||||
use App\Services\PaymentService;
|
||||
use App\Utils\Library\PaymentHelper;
|
||||
use App\Utils\Library\Templates\Gateway;
|
||||
use Auth;
|
||||
use Exception;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
use Response;
|
||||
use Stripe\Checkout\Session;
|
||||
use Stripe\Exception\SignatureVerificationException;
|
||||
use Stripe\Source;
|
||||
use Stripe\Webhook;
|
||||
use UnexpectedValueException;
|
||||
|
||||
class Stripe extends PaymentService implements Gateway
|
||||
class Stripe implements Gateway
|
||||
{
|
||||
public static array $methodDetails = [
|
||||
'key' => 'stripe',
|
||||
'settings' => ['stripe_public_key', 'stripe_secret_key'],
|
||||
];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
\Stripe\Stripe::setApiKey(sysConfig('stripe_secret_key'));
|
||||
@@ -27,7 +30,7 @@ class Stripe extends PaymentService implements Gateway
|
||||
public function purchase(Request $request): JsonResponse
|
||||
{
|
||||
$type = (int) $request->input('type');
|
||||
$payment = $this->createPayment(Auth::id(), $request->input('id'), $request->input('amount'));
|
||||
$payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
|
||||
|
||||
if ($type === 1 || $type === 3) {
|
||||
$source = Source::create([
|
||||
@@ -49,11 +52,11 @@ class Stripe extends PaymentService implements Gateway
|
||||
Log::warning('创建订单错误:未知错误');
|
||||
$payment->failed();
|
||||
|
||||
return Response::json(['status' => 'fail', 'message' => trans('user.payment.order_creation.failed')]);
|
||||
return response()->json(['status' => 'fail', 'message' => trans('user.payment.order_creation.failed')]);
|
||||
}
|
||||
$payment->update(['qr_code' => 1, 'url' => $source['wechat']['qr_code_url']]);
|
||||
|
||||
return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => trans('user.payment.order_creation.success')]);
|
||||
return response()->json(['status' => 'success', 'data' => $payment->trade_no, 'message' => trans('user.payment.order_creation.success')]);
|
||||
}
|
||||
|
||||
if (! $source['redirect']['url']) {
|
||||
@@ -64,7 +67,7 @@ class Stripe extends PaymentService implements Gateway
|
||||
}
|
||||
$payment->update(['url' => $source['redirect']['url']]);
|
||||
|
||||
return Response::json(['status' => 'success', 'url' => $source['redirect']['url'], 'message' => trans('user.payment.order_creation.success')]);
|
||||
return response()->json(['status' => 'success', 'url' => $source['redirect']['url'], 'message' => trans('user.payment.order_creation.success')]);
|
||||
}
|
||||
|
||||
$data = $this->getCheckoutSessionData($payment->trade_no, $payment->amount, $type);
|
||||
@@ -75,7 +78,7 @@ class Stripe extends PaymentService implements Gateway
|
||||
$url = route('stripe.checkout', ['session_id' => $session->id]);
|
||||
$payment->update(['url' => $url]);
|
||||
|
||||
return Response::json(['status' => 'success', 'url' => $url, 'message' => trans('user.payment.order_creation.success')]);
|
||||
return response()->json(['status' => 'success', 'url' => $url, 'message' => trans('user.payment.order_creation.success')]);
|
||||
} catch (Exception $e) {
|
||||
Log::error('【Stripe】错误: '.$e->getMessage());
|
||||
exit;
|
||||
@@ -102,7 +105,7 @@ class Stripe extends PaymentService implements Gateway
|
||||
'success_url' => route('invoice.index'),
|
||||
'cancel_url' => route('invoice.index'),
|
||||
'client_reference_id' => $tradeNo,
|
||||
'customer_email' => Auth::getUser()->email,
|
||||
'customer_email' => auth()->user()->email,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -143,13 +146,13 @@ class Stripe extends PaymentService implements Gateway
|
||||
// account.
|
||||
if ($session->payment_status === 'paid') {
|
||||
// Fulfill the purchase
|
||||
$this->paymentReceived($session->client_reference_id);
|
||||
PaymentHelper::paymentReceived($session->client_reference_id);
|
||||
}
|
||||
break;
|
||||
case 'checkout.session.async_payment_succeeded':
|
||||
$session = $event->data->object;
|
||||
// Fulfill the purchase
|
||||
$this->paymentReceived($session->client_reference_id);
|
||||
PaymentHelper::paymentReceived($session->client_reference_id);
|
||||
break;
|
||||
case 'checkout.session.async_payment_failed':
|
||||
$session = $event->data->object;
|
||||
|
||||
@@ -2,20 +2,23 @@
|
||||
|
||||
namespace App\Utils\Payments;
|
||||
|
||||
use App\Services\PaymentService;
|
||||
use App\Utils\Library\PaymentHelper;
|
||||
use App\Utils\Library\Templates\Gateway;
|
||||
use Auth;
|
||||
use Http;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
use Response;
|
||||
|
||||
class THeadPay extends PaymentService implements Gateway
|
||||
class THeadPay implements Gateway
|
||||
{
|
||||
public static array $methodDetails = [
|
||||
'key' => 'theadpay',
|
||||
'settings' => ['theadpay_mchid', 'theadpay_key'],
|
||||
];
|
||||
|
||||
public function purchase(Request $request): JsonResponse
|
||||
{
|
||||
$payment = $this->createPayment(Auth::id(), $request->input('id'), $request->input('amount'));
|
||||
$payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
|
||||
|
||||
$data = [
|
||||
'mchid' => sysConfig('theadpay_mchid'),
|
||||
@@ -31,7 +34,7 @@ class THeadPay extends PaymentService implements Gateway
|
||||
if ($result['status'] === 'success') {
|
||||
$payment->update(['qr_code' => 1, 'url' => $result['code_url']]);
|
||||
|
||||
return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => trans('user.payment.order_creation.success')]);
|
||||
return response()->json(['status' => 'success', 'data' => $payment->trade_no, 'message' => trans('user.payment.order_creation.success')]);
|
||||
}
|
||||
$payment->failed();
|
||||
Log::error('【平头哥支付】 返回错误信息:'.$result['message']);
|
||||
@@ -39,7 +42,7 @@ class THeadPay extends PaymentService implements Gateway
|
||||
|
||||
Log::alert('【平头哥支付】 支付渠道建立订单出现问题!');
|
||||
|
||||
return Response::json(['status' => 'fail', 'message' => trans('user.payment.order_creation.failed')]);
|
||||
return response()->json(['status' => 'fail', 'message' => trans('user.payment.order_creation.failed')]);
|
||||
}
|
||||
|
||||
private function sign(array $params): string
|
||||
@@ -56,7 +59,7 @@ class THeadPay extends PaymentService implements Gateway
|
||||
if ($this->verify_notify($request->post())) {
|
||||
$tradeNo = $request->input('out_trade_no');
|
||||
if ($tradeNo) {
|
||||
if ($this->paymentReceived($tradeNo)) {
|
||||
if (PaymentHelper::paymentReceived($tradeNo)) {
|
||||
exit(200);
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user