Home > Back-end >  How to get more data from Stripe webhook/Payment Intent
How to get more data from Stripe webhook/Payment Intent

Time:02-27

I'm testing Stripe webhooks on my localhost using the Stripe CLI, and so far I've managed to use it to successfully return a PaymentIntent object that contains most of the data I need to capture and persist to my database, but I'm still not able to get a few details that the documentation says should be in the Payment Intent, namely:

  • The payment method type that the user paid with
  • The postcode entered by the user in the Stripe Payment Element form

I'm using the following code to get these two pieces of information, which are also the only lines in the function that don't work when the payment_intent.succeeded webhook is triggered:

// Webhooks by default return an Event object that is stored in 
// $payload object, so ["data"]["object"] should return the 
// PaymentIntent object corresponding to the retrieved Event object

$paymentIntent = $this->webhookCall->payload["data"]["object"]; 

...

$transaction->payment_method = $paymentIntent["charges"]["data"]["payment_method_details"]["type"]; 
$transaction->postcode = $paymentIntent["charges"]["data"]["billing_details"]["address"]["postal_code"];

These lines result in "undefined index" errors in the Laravel log file, which seems to indicate they either don't exist in the PaymentIntent or something is wrong with how I'm accessing them.

CodePudding user response:

I haven't used stripe but what I see from there documentation link you shared. The ["data"] property is not an object but an array which means you've to access it like an array. So your code should look something like this.

$transaction->payment_method = $paymentIntent["charges"]["data"][0]["payment_method_details"]["type"]; 
$transaction->postcode = $paymentIntent["charges"]["data"][0]["billing_details"]["address"]["postal_code"];
  • Related