Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass custom data to stripe checkout?

Tags:

I'm following the documentation for Stripe Checkout with server integration: https://stripe.com/docs/payments/checkout/server

The code in the examples works fine, but the problem I'm having is being unable to track the user, or their order, when a purchase is complete.

I have a webhook setup that Stripe pings when a payment is complete. But the response session from Stripe contains no information about the product ordered except for it's name, description, and image. I could use the product name to query the database but I'd much rather an ID, or a slug, of sorts.

$app->post("/stripe-pingback", function(Request $request, Response $response, array $args) {     \Stripe\Stripe::setApiKey("xxxxx");      // You can find your endpoint's secret in your webhook settings     $endpoint_secret = 'xxxxx';      $payload = $request->getBody();     $sig_header = isset($_SERVER['HTTP_STRIPE_SIGNATURE']) ? $_SERVER['HTTP_STRIPE_SIGNATURE'] : null;     $event = null;      try {         $event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret);     } catch(\UnexpectedValueException $e) {         // Invalid payload         http_response_code(400); // PHP 5.4 or greater         exit();     } catch(\Stripe\Error\SignatureVerification $e) {         // Invalid signature         http_response_code(400); // PHP 5.4 or greater         exit();     }      // Handle the checkout.session.completed event     if ($event->type == 'checkout.session.completed') {         $session = $event->data->object;         var_dump($session);          // Possible to get custom data from session?         $customer = $session->customer;         $customerEmail = $session->customer_email;          // Fulfill the purchase...         $this->db->insertAudioPurchase();     }      http_response_code(200); // PHP 5.4 or greater }); 

Is it possible to pass an ID along with the checkout request that Stripe can ping back to allow me to lookup the order and generate a download link?

like image 230
BugHunterUK Avatar asked Apr 18 '19 08:04

BugHunterUK


People also ask

Is Stripe hard to integrate?

Testing Stripe's payment integrationTesting Stripe integration is super easy. 1. You can use the test secret and test publishable keys to test in local or staging. You can use Stripe's test credit card number to test various scenarios.

How do you implement Stripe checkout?

Create a Checkout Session Client and ServerAdd a checkout button to your website that calls a server-side endpoint to create a Checkout Session. On your server, make the following call to the Stripe API. After creating a Checkout Session, redirect your customer to the URL returned in the response.


1 Answers

Edit: metadata now does exist on the Session object. Although you will probably need to be on a more recent API version to be able to set it.

metadata doesn't exist on the Session object yet, so you can't use that.

You can use the client_reference_id field to store some custom data in your Session. Just be aware that the contents has to be unique. Have a look here.

like image 100
Paul Asjes Avatar answered Sep 22 '22 16:09

Paul Asjes