8

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.

jalanga
  • 1,456
  • 2
  • 17
  • 44

1 Answers1

6

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.

koopajah
  • 23,792
  • 9
  • 78
  • 104
  • What if someone wants to apply Radar rules based on metadata? When you create a Subscription you pay immediately (and that's something I want, because if the payment fails I do not want to have an invalid subscription around), however this means there is no time to update the charge afterwards to add metadata stuff. – GACy20 Jan 19 '21 at 09:36
  • @GACy20 that's not possible with Stripe's API today at least. You'd have to build your own logic based on other criteria unfortunately – koopajah Jan 19 '21 at 21:00
  • Is there some other way (I mean: different from using metadata) to be able to distinguish *why* a payment was made? Basically my application may trigger payments for any number of reasons, I want to apply different Radar rules depending on the "type" of payment (type from my application point of view!). For example: accept prepaid cards only for one-off product purchases but not for recurring payments. – GACy20 Jan 20 '21 at 08:33
  • There are various rules that could apply here based on whether it's a recurring transaction, it's the first one or custom metadata: https://stripe.com/docs/radar/rules/reference – koopajah Jan 20 '21 at 23:40
  • Ah, so finally I found that there is an `is_recurring` field... however it is not much use when dealing with Subscriptions with trials.. I want `Subscription::create(...)` to fail if the user tries to pay with a prepaid card even if the subscription has a free trial at the beginning, but radar rules only apply to the payments (right?) so it is not possible to tell Stripe "forbid the creationg of `Subscription`s that use prepaid cards" you can only deny the payments, not the creation of the subscription. What a shame. – GACy20 Jan 27 '21 at 14:07
  • Radar rules work with SetupIntents too if you enable the rule in your Dashboard settings at the bottom of this page: https://dashboard.stripe.com/settings/radar – koopajah Jan 28 '21 at 05:50