如何在成功页面 stripe php slim 中检索客户信息或付款意图
您好,我想在我的成功页面上获取金额、客户名称和电子邮件,但我很难做到。
这是我的 PHP 服务器端代码,它将请求发送到 stripe。
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
use Slim\Http\Request;
use Slim\Http\Response;
use Stripe\Stripe;
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::create(__DIR__);
$dotenv->load();
require './config.php';
$app = new \Slim\App;
$app->add(function ($request, $response, $next) {
Stripe::setApiKey(getenv('STRIPE_SECRET_KEY'));
return $next($request, $response);
});
$app->get('/', function (Request $request, Response $response, array $args) {
return $response->write(file_get_contents(getenv('STATIC_DIR') . './grid-test.html','/index-copy.html'));
});
$app->post('/checkout_sessions', function(Request $request, Response $response) use ($app) {
$params = json_decode($request->getBody());
$payment_method_types = [
'usd' => ['card'],
'eur' => ['card'],
'cad' => ['card']
];
$products = [
'Private' => 'prod_435646574',
];
$session = \Stripe\Checkout\Session::create([
'success_url' => 'http://localhost:4242/success.html?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => 'http://localhost:4242/?cancel=true',
'mode' => 'payment',
'payment_method_types' => ['card'],
'payment_intent_data' => [
'metadata' => [
'package' => $params->package,
'date' => $params->datepicker,
'no of adults' => $params->adults,
'no of children' => $params->children,
'occupancy' => $params->occupancy,
'tour' => $params->tour,
'location' => $params->location,
]
],
'metadata' => [
'package' => $params->package,
'date' => $params->datepicker,
'no of adults' => $params->adults,
'no of children' => $params->children,
'occupancy' => $params->occupancy,
'tour' => $params->tour,
'location' => $params->location,
],
'submit_type' => 'donate',
'line_items' => [[
'price_data' => [
'currency' => 'aed',
'product' => $products[$params->package],
'unit_amount' => $params->amount,
],
'quantity' => 1,
]],
'phone_number_collection' => [
'enabled' => true,
],
]);
return $response->withJson([
'id' => $session->id
]);
});
$app->run();
这是我的成功页面,我无法获取结帐会话或付款意图详细信息
var params = new URLSearchParams(window.location.search);
var sessionId = params.get('id');
var amount = document.getElementById('amount')
fetch('/get-session?id=' + sessionId)
.then((Response) => Response.json())
.then((session) => {
amount.innerText = session.payment_intent.amount;
})
.catch((error) => {
console.error('Error:', error);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Thank You For Order</title>
</head>
<body>
<div id="main">
<div id="checkout">
<div id="payment-form">
<h1>Success!</h1>
<p>
Thanks so much for donating <strong id="amount"></strong> to
</p>
<a href="/">Donate More?!</a>
</div>
</div>
</div>
</body>
hi i want to take amount, customer name and email on my success page but I am struggling to do.hi i want to take amount, customer name and email on my success page but I am struggling to do.
this is my PHP server side codes which send request to stripe.
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
use Slim\Http\Request;
use Slim\Http\Response;
use Stripe\Stripe;
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::create(__DIR__);
$dotenv->load();
require './config.php';
$app = new \Slim\App;
$app->add(function ($request, $response, $next) {
Stripe::setApiKey(getenv('STRIPE_SECRET_KEY'));
return $next($request, $response);
});
$app->get('/', function (Request $request, Response $response, array $args) {
return $response->write(file_get_contents(getenv('STATIC_DIR') . './grid-test.html','/index-copy.html'));
});
$app->post('/checkout_sessions', function(Request $request, Response $response) use ($app) {
$params = json_decode($request->getBody());
$payment_method_types = [
'usd' => ['card'],
'eur' => ['card'],
'cad' => ['card']
];
$products = [
'Private' => 'prod_435646574',
];
$session = \Stripe\Checkout\Session::create([
'success_url' => 'http://localhost:4242/success.html?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => 'http://localhost:4242/?cancel=true',
'mode' => 'payment',
'payment_method_types' => ['card'],
'payment_intent_data' => [
'metadata' => [
'package' => $params->package,
'date' => $params->datepicker,
'no of adults' => $params->adults,
'no of children' => $params->children,
'occupancy' => $params->occupancy,
'tour' => $params->tour,
'location' => $params->location,
]
],
'metadata' => [
'package' => $params->package,
'date' => $params->datepicker,
'no of adults' => $params->adults,
'no of children' => $params->children,
'occupancy' => $params->occupancy,
'tour' => $params->tour,
'location' => $params->location,
],
'submit_type' => 'donate',
'line_items' => [[
'price_data' => [
'currency' => 'aed',
'product' => $products[$params->package],
'unit_amount' => $params->amount,
],
'quantity' => 1,
]],
'phone_number_collection' => [
'enabled' => true,
],
]);
return $response->withJson([
'id' => $session->id
]);
});
$app->run();
this is my success page which I am not able to get checkout session or payment intent details
var params = new URLSearchParams(window.location.search);
var sessionId = params.get('id');
var amount = document.getElementById('amount')
fetch('/get-session?id=' + sessionId)
.then((Response) => Response.json())
.then((session) => {
amount.innerText = session.payment_intent.amount;
})
.catch((error) => {
console.error('Error:', error);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Thank You For Order</title>
</head>
<body>
<div id="main">
<div id="checkout">
<div id="payment-form">
<h1>Success!</h1>
<p>
Thanks so much for donating <strong id="amount"></strong> to
</p>
<a href="/">Donate More?!</a>
</div>
</div>
</div>
</body>
hi i want to take amount, customer name and email on my success page but I am struggling to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您的
fetch('/get-session?id=' + sessionId)
调用会执行什么操作,因为您在Slim< 中没有为此配置任何 API 路由/code> 您共享的应用程序代码。但基本上,您想要在该端点中执行的操作是检索结账会话,这样
您就可以访问
session. payment_intent.amount
或者您可以使用session.amount_total
。对于客户名称和电子邮件,您可以通过客户对象
session.customer.name
和session.customer.email
访问它们。编辑:
根据您的评论,请找到我建议进行的更改。
I’m not sure what your
fetch('/get-session?id=' + sessionId)
call would do since you don’t have any API route configured for that in yourSlim
app code you shared. But basically what you want to do in that endpoint is to retrieve the Checkout Session like sowhich would give you access to
session.payment_intent.amount
or you could usesession.amount_total
.For the customer name and email you could access those via the Customer object
session.customer.name
andsession.customer.email
.EDIT:
As per your comment please find the changes that I would recommend doing.