ndtreviv
ndtreviv

Reputation: 3624

How can I identify specific products from Stripe charge.succeeded events?

I've setup a Pipedream workflow that listens to Stripe events using the webhook, and then adds purchasers to a mailerlite group.

I want to sell multiple products, but have users added to a different mailerlite group depending on what product they've purchased.

I thought I could do this by adding the target mailerlite group id to the product metadata, but when I test this, the metadata object comes into pipedream as empty.

How can I identify the product from a stripe charge.succeeded event?

Upvotes: 0

Views: 1296

Answers (2)

ndtreviv
ndtreviv

Reputation: 3624

There is no action to retrieve the product from a charge.succeeded, nor is there any data in the charge.succeeded event that can lead you back to the source product.

The metadata in this case isn't the metadata associated with the product, but rather with the charge.succeeded event.

Update June 2022

I've since contributed Stripe actions in Pipedream to do these two calls, so there's no need to code custom API calls.

Pre June 2022

To do what I wanted I had to:

  1. change the trigger to look for checkout.session.completed, which occurs when a checkout has finished.
  2. grab the checkout session ID and use that to call the stripe API and get the line items:
import { axios } from "@pipedream/platform"

export default defineComponent({
  props: {
    stripe: {
      type: "app",
      app: "stripe",
    }
  },
  async run({steps, $}) {
    return await axios($, {
      url: `https://api.stripe.com/v1/checkout/sessions/${steps.trigger.event.data.object.id}/line_items?limit=5`,
      auth: {
        username: `${this.stripe.$auth.api_key}`,
        password: ``,
      },
    })
  },
})

  1. The line items contain a price which in turn references a product, so use the output of the checkout session line items request to grab the product id:
import { axios } from "@pipedream/platform"
export default defineComponent({
  props: {
    stripe: {
      type: "app",
      app: "stripe",
    }
  },
  async run({steps, $}) {
    return await axios($, {
      url: `https://api.stripe.com/v1/products/${steps.stripe.$return_value.data[0].price.product}`,
      auth: {
        username: `${this.stripe.$auth.api_key}`,
        password: ``,
      },
    })
  },
})
  1. Use the output of that to get the metadata and use it in the mailerlite _subscribe_to_group action.

Upvotes: 3

karbi
karbi

Reputation: 2163

With a standard Checkout integration you create the Checkout Session and you know which products will be purchased because you pass them into the line_items (api ref) you pass in. Instead of setting metadata on the Product, you could add logic on your end to know which "target mailerlite group id" before you create the Checkout Session and then pass that information into metadata (see apiref). You'll get all that information back from the checkout.session.completed event.

This won't work if you're doing things like cross-sells, but it may be a good alternative and would save you the trouble of having to re-retrieve the Products from stripe when you want to check the metadata.

Upvotes: 1

Related Questions