server side
<?php
$app->get('/order', function (Request $request, Response $response) {
$id = $_GET['sessionId'];
$checkout_session = \Stripe\Checkout\Session::retrieve($id);
$customer = \Stripe\Customer::retrieve($checkout_session->customer);
echo json_encode($customer);
});
$app->run();
client side codes
<!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>Order Confirm</title>
</head>
<body>
<div id="main">
<div id="checkout">
<div id="payment-forum">
<h1>Success</h1>
<section >
<li>name <span id="customer_name"></span></li>
</section>
payment status: <span id="payment-status"></span>
<pre>
</pre>
</div>
</div>
</div>
</body>
<script>
var paymentStatus = document.getElementById('payment-status');
var customer = document.getElementById('customer_name');
var urlParams = new URLSearchParams(window.location.search);
var sessionId = urlParams.get("session_id")
if (sessionId) {
fetch("/order?sessionId=" sessionId).then(function(result){
return result.json()
}).then(function(session){
var sessionJSON = JSON.stringify(session, null, 2);
document.querySelector("pre").textContent = sessionJSON;
paymentStatus.innerText = session.payment_status;
}).catch(function(err){
console.log('Error when fetching Checkout session', err);
});
}
</script>
</html>
hi
i was working on success page i do see whole payment intent on my screen but i want to pick name of customer and payment status only and why does it print whole payment intent.
thanks in advance for helping out
edited------------------------------
paymentStatus.innerText = session.payment_status;
i manage to get payment status but if i change $customer to $checkout_session
but as one gentlemen said call for $checkout_session is risky and if i use $customer what javascript of payment status be
paymentStatus.innerText = session.payment_status;
and how will i manage to take other things like metadata, price, email which are not part of customer
edited 2--------------------------------------------
like i did some changes for extra safety like if i send those values in backend so whole intent wont be exposed so i got another problem
$app->get('/order', function (Request $request, Response $response) {
$id = $_GET['sessionId'];
$checkout_session = \Stripe\Checkout\Session::retrieve($id);
$amount = $checkout_session->amount_total;
$payment_status = $checkout_session->payment_status;
echo json_encode([$payment_status,$amount]);
});
i do get only amount in front end to catch this in front end i did some changes like
<script>
var paymentStatus = document.getElementById('payment-status');
var amount = document.getElementById('amount1');
var urlParams = new URLSearchParams(window.location.search);
var sessionId = urlParams.get("session_id")
if (sessionId) {
fetch("/order?sessionId=" sessionId).then(function(result){
return result.json()
}).then(function(session){
var sessionJSON = JSON.stringify(session, null, 2);
document.querySelector("pre").textContent = sessionJSON;
amount.innerText = session;
paymentStatus.innerText = session;
}).catch(function(err){
console.log('Error when fetching Checkout session', err);
});
}
</script>
</html>
but now the problem is i can only have one value how will i add payment status also in it
echo json_encode([$amount, $payment_status])
dosent works and if it work also how will iget both values for seperate id in client side
CodePudding user response:
Okay so a couple things here. I would start by logging the $id
and $checkout_session
to be 100% sure of the data you are retrieving server-side before you send it to the client. Your code should not be returning a Payment Intent at any point. You may be returning the entire Checkout Session, in which case logging all retrieved values on your server should help identify which variables are holding what values at runtime.
AFAIK The only PHP client maintained by Stripe is this one. If this isn't the version you are using I would consider switching.
Another thing to consider is how you retrieve the Customer object. You can expand the Customer
property so the full object is returned instead of just the id
. You can read about expanding responses here. In your case the request would look something like this (revised instantiation of Stripe library following current syntax):
$stripe = new \Stripe\StripeClient("sk_test_XXXXXXXXXXXX");
$checkout = $stripe->checkout->sessions->retrieve(
'cs_test_XXXXXXXXXXX',
['expand' => ['customer']]
);
From here you could return the Customer object directly from $checkout->customer
.