Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe charge after subscription, get metadata from subscription

When I'm creating a subscription, I set some metadata to identify my order on the database. When I receive the webhook charge.succeeded, the metadata from subscription, is not passed in this event and I can't identify the order related to this payment. How can I send the metadata on every webhook related to subscription.

like image 848
jalanga Avatar asked May 07 '18 12:05

jalanga


1 Answers

The subscription's metadata live on the Subscription not the Charge object. A charge is associated with a specific invoice (if any) which itself is associated with a Subscription (if any).

It is simply not possible to set metadata on a subscription to see it ported over to the corresponding charge.

Instead, you would use the API to retrieve the Charge and use the Expand feature to also fetch the associated invoice and subscription in one go.

In PHP it would look like this:

$charge = \Stripe\Charge::retrieve(
  array(
    "id" => "ch_1CP95G2eZvKYlo2C4pcS2pxm",
    "expand" => array("invoice.subscription")
  )
);

You can then access the subscription's metadata in your code directly.

like image 59
koopajah Avatar answered Oct 15 '22 21:10

koopajah