John Davidson

Stripe accessing the metadata within a PHP script

0 comments
Message:


I'm trying to retrieve a line of metadata (order_no) within Stripe's checkout.session.completed event but I don't know how to go about it. I'm using PHP. I don't know how to query the event to retrieve the checkout.session object from within a PHP script, so I can read the metadata. Could someone please show by example how to achieve that without installing addons such as Slim, etc. I have looked at multiple examples on this site but have not been able to understand them. Thanks in advance for any help you can give.


header('Content-Type: application/json');

$order = $mysqli->escape_string(md5(rand(0,20)));
$amt = '299';

$checkout_session = \Stripe\Checkout\Session::create(

[
'payment_intent_data'=>['description' =>'my purchases'],
'metadata' => [
'order_id' => $order
],
'line_items' => [
[
'name' => 'My Products',
'description' => 'My online products',
'currency' => 'gbp',
'amount' => $amt,
'quantity' => 1
]],
'success_url' => $MySite.'success.php',
'cancel_url' => $MySite.'cancel.html'
]);

header("HTTP/1.1 303 See Other");
header("Location: " . $checkout_session->url);
}


My Answer:
>query("SELECT * FROM orders WHERE order_no = '$_POST[order_no]'")->fetch_assoc();

// Assuming you have the order_no stored in the metadata of the checkout session
$event_json = file_get_contents('php://input');
$event = json_decode($event_json);

$checkout_session_id = $event->data->object->id;

$checkout_session = \Stripe\Checkout\Session::retrieve($checkout_session_id);

$order_no = $checkout_session->metadata->order_no;

echo json_encode($order_no);

Rate this post

5 of 5 based on 5995 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us