如何在成功页面 stripe php slim 中检索客户信息或付款意图

发布于 2025-01-15 14:58:19 字数 3944 浏览 2 评论 0原文

您好,我想在我的成功页面上获取金额、客户名称和电子邮件,但我很难做到。

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱&纷飞 2025-01-22 14:58:19

我不确定您的 fetch('/get-session?id=' + sessionId) 调用会执行什么操作,因为您在 Slim< 中没有为此配置任何 API 路由/code> 您共享的应用程序代码。但基本上,您想要在该端点中执行的操作是检索结账会话,这样

$session = $stripe->checkout->sessions->retrieve(
  'cs_xxxxx',
  [['expand' => ['payment_intent', 'customer']]
);
return $response->withJson($session);

您就可以访问 session. payment_intent.amount 或者您可以使用 session.amount_total

对于客户名称和电子邮件,您可以通过客户对象 session.customer.namesession.customer.email 访问它们。

PS:将整个 Checkout Session 对象发送到前端并不理想。相反,您可以发送包含您需要的一些详细信息的 JSON,并相应地调整您的前端。

编辑:

根据您的评论,请找到我建议进行的更改。

//Changes on the backend
return $response->withJson([[
  //you could add any other info you'd like to send to the frontend
  'amount' => $session->payment_intent->amount,
  'email' => $session->customer->email,
  'name' => $session->customer->name
]]);

//Changes on the frontend
fetch('/get-session?id=' + sessionId)
.then((response) => response.json())
.then((session) => {
    amount.innerText = session.amount;
    //you could also use session.email and session.name
})
.catch((error) => {
    console.error('Error:', error);
});

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 your Slim app code you shared. But basically what you want to do in that endpoint is to retrieve the Checkout Session like so

$session = $stripe->checkout->sessions->retrieve(
  'cs_xxxxx',
  [['expand' => ['payment_intent', 'customer']]
);
return $response->withJson($session);

which would give you access to session.payment_intent.amount or you could use session.amount_total.

For the customer name and email you could access those via the Customer object session.customer.name and session.customer.email.

P.S.: It’s not ideal to send the whole Checkout Session object to the frontend. Instead you could send a JSON with the few details that you need and adjust your frontend accordingly.

EDIT:

As per your comment please find the changes that I would recommend doing.

//Changes on the backend
return $response->withJson([[
  //you could add any other info you'd like to send to the frontend
  'amount' => $session->payment_intent->amount,
  'email' => $session->customer->email,
  'name' => $session->customer->name
]]);

//Changes on the frontend
fetch('/get-session?id=' + sessionId)
.then((response) => response.json())
.then((session) => {
    amount.innerText = session.amount;
    //you could also use session.email and session.name
})
.catch((error) => {
    console.error('Error:', error);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文