Merge pull request #539 from betaxab/add-stripe-checkout-support

Stripe: add Checkout support & directly show the payments id
This commit is contained in:
tokumeikoi 2022-10-12 02:00:45 +08:00 committed by GitHub
commit b65fa65e75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 136 additions and 3 deletions

View File

@ -91,11 +91,14 @@ class StripeAlipay {
case 'charge.succeeded': case 'charge.succeeded':
$object = $event->data->object; $object = $event->data->object;
if ($object->status === 'succeeded') { 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; $metaData = isset($object->metadata->out_trade_no) ? $object->metadata : $object->source->metadata;
$tradeNo = $metaData->out_trade_no; $tradeNo = $metaData->out_trade_no;
return [ return [
'trade_no' => $tradeNo, 'trade_no' => $tradeNo,
'callback_no' => $object->balance_transaction 'callback_no' => $object->id
]; ];
} }
break; break;

View File

@ -0,0 +1,124 @@
<?php
namespace App\Payments;
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',
]
];
}
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];
}
}

View File

@ -98,11 +98,14 @@ class StripeCredit {
case 'charge.succeeded': case 'charge.succeeded':
$object = $event->data->object; $object = $event->data->object;
if ($object->status === 'succeeded') { 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; $metaData = isset($object->metadata->out_trade_no) ? $object->metadata : $object->source->metadata;
$tradeNo = $metaData->out_trade_no; $tradeNo = $metaData->out_trade_no;
return [ return [
'trade_no' => $tradeNo, 'trade_no' => $tradeNo,
'callback_no' => $object->balance_transaction 'callback_no' => $object->id
]; ];
} }
break; break;

View File

@ -91,11 +91,14 @@ class StripeWepay {
case 'charge.succeeded': case 'charge.succeeded':
$object = $event->data->object; $object = $event->data->object;
if ($object->status === 'succeeded') { 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; $metaData = isset($object->metadata->out_trade_no) ? $object->metadata : $object->source->metadata;
$tradeNo = $metaData->out_trade_no; $tradeNo = $metaData->out_trade_no;
return [ return [
'trade_no' => $tradeNo, 'trade_no' => $tradeNo,
'callback_no' => $object->balance_transaction 'callback_no' => $object->id
]; ];
} }
break; break;