From acb40cc1f9de13d1816baa8bc034ba4e52068be7 Mon Sep 17 00:00:00 2001 From: LinusX Date: Tue, 2 Jul 2024 19:02:29 +0800 Subject: [PATCH] =?UTF-8?q?[update]=20=E6=96=B0=E5=A2=9Ecredit=20card?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=E8=87=B3checkout=E9=A1=B5=E9=9D=A2=E4=BB=98?= =?UTF-8?q?=E6=AC=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Payments/StripeALLInOne.php | 154 ++++++++++++++++++++------------ 1 file changed, 97 insertions(+), 57 deletions(-) diff --git a/app/Payments/StripeALLInOne.php b/app/Payments/StripeALLInOne.php index 2847beb..9c7e003 100644 --- a/app/Payments/StripeALLInOne.php +++ b/app/Payments/StripeALLInOne.php @@ -37,7 +37,7 @@ class StripeALLInOne { ], 'payment_method' => [ 'label' => '支付方式', - 'description' => '请输入alipay或者wechat_pay', + 'description' => '请输入alipay, wechat_pay, cards', 'type' => 'input', ] ]; @@ -50,66 +50,91 @@ class StripeALLInOne { if (!$exchange) { throw new ApiException('Currency conversion has timed out, please try again later', 500); } - $stripe = new \Stripe\StripeClient($this->config['stripe_sk_live']); - - - $stripePaymentMethod = $stripe->paymentMethods->create([ - 'type' => $this->config['payment_method'], - ]); - // 准备支付意图的基础参数 - $params = [ - 'amount' => floor($order['total_amount'] * $exchange), - 'currency' => $currency, - 'confirm' => true, - 'payment_method' => $stripePaymentMethod->id, - 'automatic_payment_methods' => ['enabled' => true], - 'statement_descriptor' => 'sub-' . $order['user_id'] . '-' . substr($order['trade_no'], -8), - 'description' => $this->config['description'], - 'metadata' => [ - 'user_id' => $order['user_id'], - 'out_trade_no' => $order['trade_no'], - 'identifier' => '' - ], - 'return_url' => $order['return_url'] - ]; - - // 如果支付方式为 wechat_pay,添加相应的支付方式选项 - if ($this->config['payment_method'] === 'wechat_pay') { - $params['payment_method_options'] = [ - 'wechat_pay' => [ - 'client' => 'web' - ], - ]; - } - //更新支持最新的paymentIntents方法,Sources API将在今年被彻底替 - $stripeIntents = $stripe->paymentIntents->create($params); - - $nextAction = null; //jump url $jumpUrl = null; $actionType = 0; - if (!$stripeIntents['next_action']) { - throw new ApiException(__('Payment gateway request failed')); - }else { - $nextAction = $stripeIntents['next_action']; + $stripe = new \Stripe\StripeClient($this->config['stripe_sk_live']); + + if ($this->config['payment_method'] != "cards"){ + $stripePaymentMethod = $stripe->paymentMethods->create([ + 'type' => $this->config['payment_method'], + ]); + // 准备支付意图的基础参数 + $params = [ + 'amount' => floor($order['total_amount'] * $exchange), + 'currency' => $currency, + 'confirm' => true, + 'payment_method' => $stripePaymentMethod->id, + 'automatic_payment_methods' => ['enabled' => true], + 'statement_descriptor' => 'sub-' . $order['user_id'] . '-' . substr($order['trade_no'], -8), + 'description' => $this->config['description'], + 'metadata' => [ + 'user_id' => $order['user_id'], + 'out_trade_no' => $order['trade_no'], + 'identifier' => '' + ], + 'return_url' => $order['return_url'] + ]; + + // 如果支付方式为 wechat_pay,添加相应的支付方式选项 + if ($this->config['payment_method'] === 'wechat_pay') { + $params['payment_method_options'] = [ + 'wechat_pay' => [ + 'client' => 'web' + ], + ]; + } + //更新支持最新的paymentIntents方法,Sources API将在今年被彻底替 + $stripeIntents = $stripe->paymentIntents->create($params); + + $nextAction = null; + + if (!$stripeIntents['next_action']) { + throw new ApiException(__('Payment gateway request failed')); + }else { + $nextAction = $stripeIntents['next_action']; + } + + switch ($this->config['payment_method']){ + case "alipay": + if (isset($nextAction['alipay_handle_redirect'])){ + $jumpUrl = $nextAction['alipay_handle_redirect']['url']; + $actionType = 1; + }else { + throw new ApiException('unable get Alipay redirect url', 500); + } + break; + case "wechat_pay": + if (isset($nextAction['wechat_pay_display_qr_code'])){ + $jumpUrl = $nextAction['wechat_pay_display_qr_code']['data']; + }else { + throw new ApiException('unable get WeChat Pay redirect url', 500); + } + } + } else { + $creditCheckOut = $stripe->checkout->sessions->create([ + 'success_url' => $order['return_url'], + 'client_reference_id' => $order['trade_no'], + 'payment_method_types' => ['card'], + 'line_items' => [ + [ + 'price_data' => [ + 'currency' => $currency, + 'unit_amount' => floor($order['total_amount'] * $exchange), + 'product_data' => [ + 'name' => 'sub-' . $order['user_id'] . '-' . substr($order['trade_no'], -8), + 'description' => $this->config['description'], + ] + ], + 'quantity' => 1, + ], + ], + 'mode' => 'payment', + ]); + $jumpUrl = $creditCheckOut['url']; + $actionType = 1; } - switch ($this->config['payment_method']){ - case "alipay": - if (isset($nextAction['alipay_handle_redirect'])){ - $jumpUrl = $nextAction['alipay_handle_redirect']['url']; - $actionType = 1; - }else { - throw new ApiException('unable get Alipay redirect url', 500); - } - break; - case "wechat_pay": - if (isset($nextAction['wechat_pay_display_qr_code'])){ - $jumpUrl = $nextAction['wechat_pay_display_qr_code']['data']; - }else { - throw new ApiException('unable get WeChat Pay redirect url', 500); - } - } return [ 'type' => $actionType, 'data' => $jumpUrl @@ -133,7 +158,6 @@ class StripeALLInOne { } catch (\UnexpectedValueException $e){ throw new ApiException('Error parsing payload', 400); - } catch (\Stripe\Exception\SignatureVerificationException $e) { throw new ApiException('signature not match', 400); @@ -153,6 +177,22 @@ class StripeALLInOne { ]; } break; + 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'); }