mirror of
https://github.com/v2board/v2board.git
synced 2024-11-10 09:39:10 +08:00
Merge pull request #539 from betaxab/add-stripe-checkout-support
Stripe: add Checkout support & directly show the payments id
This commit is contained in:
commit
b65fa65e75
@ -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;
|
||||
|
124
app/Payments/StripeCheckout.php
Normal file
124
app/Payments/StripeCheckout.php
Normal 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];
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user