From 4136f365a66b7ef1401ef355d08643b23454dd4e Mon Sep 17 00:00:00 2001 From: betaxab Date: Sun, 9 Oct 2022 13:51:44 +0800 Subject: [PATCH] Stripe: add Checkout support & directly show the payments id --- app/Payments/StripeAlipay.php | 5 +- app/Payments/StripeCheckout.php | 124 ++++++++++++++++++++++++++++++++ app/Payments/StripeCredit.php | 5 +- app/Payments/StripeWepay.php | 5 +- 4 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 app/Payments/StripeCheckout.php diff --git a/app/Payments/StripeAlipay.php b/app/Payments/StripeAlipay.php index 10c2837b..43669136 100644 --- a/app/Payments/StripeAlipay.php +++ b/app/Payments/StripeAlipay.php @@ -91,11 +91,14 @@ class StripeAlipay { case 'charge.succeeded': $object = $event->data->object; if ($object->status === 'succeeded') { + if (!isset($object->metadata->out_trade_no) && !isset($object->source->metadata)) { + die('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->balance_transaction + 'callback_no' => $object->id ]; } break; diff --git a/app/Payments/StripeCheckout.php b/app/Payments/StripeCheckout.php new file mode 100644 index 00000000..25b29ea9 --- /dev/null +++ b/app/Payments/StripeCheckout.php @@ -0,0 +1,124 @@ +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', + ] + ]; + } + + public function pay($order) + { + $currency = $this->config['currency']; + $exchange = $this->exchange('CNY', strtoupper($currency)); + if (!$exchange) { + abort(500, __('Currency conversion has timed out, please try again later')); + } + + $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' + // 'customer_email' => $user['email'] not support + + ]; + + Stripe::setApiKey($this->config['stripe_sk_live']); + try { + $session = Session::create($params); + } catch (\Exception $e) { + info($e); + abort(500, "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( + file_get_contents('php://input'), + $_SERVER['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: + abort(500, 'event is not support'); + } + die('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]; + } +} diff --git a/app/Payments/StripeCredit.php b/app/Payments/StripeCredit.php index 6397ee9b..a066e835 100644 --- a/app/Payments/StripeCredit.php +++ b/app/Payments/StripeCredit.php @@ -98,11 +98,14 @@ class StripeCredit { case 'charge.succeeded': $object = $event->data->object; if ($object->status === 'succeeded') { + if (!isset($object->metadata->out_trade_no) && !isset($object->source->metadata)) { + die('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->balance_transaction + 'callback_no' => $object->id ]; } break; diff --git a/app/Payments/StripeWepay.php b/app/Payments/StripeWepay.php index 85e1cf61..8b0fc1b0 100644 --- a/app/Payments/StripeWepay.php +++ b/app/Payments/StripeWepay.php @@ -91,11 +91,14 @@ class StripeWepay { case 'charge.succeeded': $object = $event->data->object; if ($object->status === 'succeeded') { + if (!isset($object->metadata->out_trade_no) && !isset($object->source->metadata)) { + die('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->balance_transaction + 'callback_no' => $object->id ]; } break;