|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
namespace App\Utils\Payments;
|
|
namespace App\Utils\Payments;
|
|
|
|
|
|
|
|
+use App\Models\Payment;
|
|
use App\Services\PaymentService;
|
|
use App\Services\PaymentService;
|
|
use App\Utils\Library\AlipayF2F;
|
|
use App\Utils\Library\AlipayF2F;
|
|
use App\Utils\Library\Templates\Gateway;
|
|
use App\Utils\Library\Templates\Gateway;
|
|
@@ -14,16 +15,16 @@ use Response;
|
|
|
|
|
|
class F2Fpay extends PaymentService implements Gateway
|
|
class F2Fpay extends PaymentService implements Gateway
|
|
{
|
|
{
|
|
- private static array $aliConfig;
|
|
|
|
|
|
+ private static AlipayF2F $aliClient;
|
|
|
|
|
|
public function __construct()
|
|
public function __construct()
|
|
{
|
|
{
|
|
- self::$aliConfig = [
|
|
|
|
|
|
+ self::$aliClient = new AlipayF2F([
|
|
'app_id' => sysConfig('f2fpay_app_id'),
|
|
'app_id' => sysConfig('f2fpay_app_id'),
|
|
'ali_public_key' => sysConfig('f2fpay_public_key'),
|
|
'ali_public_key' => sysConfig('f2fpay_public_key'),
|
|
'rsa_private_key' => sysConfig('f2fpay_private_key'),
|
|
'rsa_private_key' => sysConfig('f2fpay_private_key'),
|
|
'notify_url' => route('payment.notify', ['method' => 'f2fpay']),
|
|
'notify_url' => route('payment.notify', ['method' => 'f2fpay']),
|
|
- ];
|
|
|
|
|
|
+ ]);
|
|
}
|
|
}
|
|
|
|
|
|
public function purchase(Request $request): JsonResponse
|
|
public function purchase(Request $request): JsonResponse
|
|
@@ -37,8 +38,7 @@ class F2Fpay extends PaymentService implements Gateway
|
|
];
|
|
];
|
|
|
|
|
|
try {
|
|
try {
|
|
- $gateWay = new AlipayF2F(self::$aliConfig);
|
|
|
|
- $result = $gateWay->qrCharge($data);
|
|
|
|
|
|
+ $result = self::$aliClient->qrCharge($data);
|
|
$payment->update(['qr_code' => 1, 'url' => $result['qr_code']]);
|
|
$payment->update(['qr_code' => 1, 'url' => $result['qr_code']]);
|
|
} catch (Exception $e) {
|
|
} catch (Exception $e) {
|
|
Log::alert('【支付宝当面付】支付错误: '.$e->getMessage());
|
|
Log::alert('【支付宝当面付】支付错误: '.$e->getMessage());
|
|
@@ -52,26 +52,47 @@ class F2Fpay extends PaymentService implements Gateway
|
|
public function notify(Request $request): void
|
|
public function notify(Request $request): void
|
|
{
|
|
{
|
|
try {
|
|
try {
|
|
- $result = (new AlipayF2F(self::$aliConfig))->tradeQuery($request->only('out_trade_no', 'trade_no'));
|
|
|
|
- Log::notice('【支付宝当面付】回调验证验证:'.var_export($result, true));
|
|
|
|
|
|
+ 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'))) {
|
|
|
|
+ exit('success');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ Log::error('【支付宝当面付】异步验证失败,尝试订单查询');
|
|
|
|
+ if ($this->capture($request->input('out_trade_no'), $request->input('trade_no'))) {
|
|
|
|
+ exit('success');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Log::notice('【支付宝当面付】异步验证失败:'.var_export($request->all(), true));
|
|
} catch (Exception $e) {
|
|
} catch (Exception $e) {
|
|
Log::alert('【支付宝当面付】回调信息错误: '.$e->getMessage());
|
|
Log::alert('【支付宝当面付】回调信息错误: '.$e->getMessage());
|
|
exit;
|
|
exit;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 返回验证结果
|
|
|
|
+ exit('fail');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function capture(string $trade_no = null, string $ali_trade_no = null): bool
|
|
|
|
+ {
|
|
|
|
+ $result = self::$aliClient->tradeQuery(array_filter([
|
|
|
|
+ 'out_trade_no' => $trade_no,
|
|
|
|
+ 'trade_no' => $ali_trade_no,
|
|
|
|
+ ]));
|
|
|
|
+
|
|
if ($result['code'] === '10000' && $result['msg'] === 'Success') {
|
|
if ($result['code'] === '10000' && $result['msg'] === 'Success') {
|
|
- if ($request->has('out_trade_no') && in_array($request->input('trade_status'), ['TRADE_FINISHED', 'TRADE_SUCCESS'])) {
|
|
|
|
- if ($this->paymentReceived($request->input('out_trade_no'))) {
|
|
|
|
- exit('success');
|
|
|
|
|
|
+ if ($result['out_trade_no'] && in_array($result['trade_status'], ['TRADE_FINISHED', 'TRADE_SUCCESS'])) {
|
|
|
|
+ if ($this->paymentReceived($result['out_trade_no'])) {
|
|
|
|
+ return true;
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- Log::error('【支付宝当面付】交易失败:'.var_export($request->all(), true));
|
|
|
|
|
|
+ Log::error('【支付宝当面付】收单交易订单结算失败:'.var_export($result, true));
|
|
|
|
+
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- Log::error('【支付宝当面付】验证失败:'.var_export($result, true));
|
|
|
|
|
|
+ Log::error('【支付宝当面付】收单交易查询失败:'.var_export($result, true));
|
|
}
|
|
}
|
|
|
|
|
|
- // 返回验证结果
|
|
|
|
- exit('fail');
|
|
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
}
|
|
}
|