skip to Main Content

I am developing an app. Part of this app is to pay for courses into a connected stripe account that is managed by the app.

That part is working very well.

I am now trying to find a way to filter the payments by course_id. My idea is to put the course id in the payment_intent_metadata. That way I can fetch all payments by connected account and then filter the list locally by metadata.course_id.

Ive got the following code that creates the checkout session and attaches the course_id:

private async createCheckout(
    p: IResponseData<Participant>,
    input: supportParticipantInput,
    courseId: string
  ) {
    //create checkout
    return await this.stripe.checkout.sessions.create({
      success_url: input.redirect,
      cancel_url: input.redirect,
      mode: 'payment',
      payment_method_types: simpleConfig.payment_method_types,
      customer_email: input?.supporterEmail,
      payment_intent_data: {
        metadata: {
        course_id: courseId,
        },
        description: 'testdescription',
        transfer_data: {
          destination: p.attributes.stripeAccountId,
        },
      },
      line_items: [
        {
          price_data: {
            currency: 'eur',
            product_data: {
              name: 'support Participant2',
            },
            unit_amount: input.amount,
          },
          quantity: 1,
        },
      ],
    });
  }

According to the stripe documentation at
https://stripe.com/docs/api/checkout/sessions/create this should work.

Ive tried a lot and cant seem to find the mistake. Hopefully someone here knows something I could try/change

2

Answers


  1. Looks like this is a "newer" feature (or at least the documentation is in flux), and looking at various sources/online discussions, it looks like to get it to work, you must also pass a client_reference_id which is listed in the docs as a property.

    Please try something like the following – please note, I used your entry and added a placeholder piece of data, please fit to your needs. Note the comments in the code which you can remove:

    private async createCheckout(
        p: IResponseData<Participant>,
        input: supportParticipantInput,
        courseId: string
      ) {
        //create checkout
        return await this.stripe.checkout.sessions.create({
          // ! added this string to your session, please fit to your needs
          client_reference_id: courseId,
          //^ ! added this string to your session, please fit to your needs
          success_url: input.redirect,
          cancel_url: input.redirect,
          mode: 'payment',
          payment_method_types: simpleConfig.payment_method_types,
          customer_email: input?.supporterEmail,
          payment_intent_data: {
            metadata: {
            course_id: courseId,
            },
            description: 'testdescription',
            transfer_data: {
              destination: p.attributes.stripeAccountId,
            },
          },
          line_items: [
            {
              price_data: {
                currency: 'eur',
                product_data: {
                  name: 'support Participant2',
                },
                unit_amount: input.amount,
              },
              quantity: 1,
            },
          ],
        });
      }
    

    Also, point 2: Make sure you’re looking at the right place to see if it’s working. AFAIK the session’s metadata gets put on the charge’s metadata and saved there. (And not in the checkout complete event nor at the dashboard when looking at the logs.)

    H/T to the respondents of this question in PHP. Check out that question, and especially at the responses and comments around 2023.

    Login or Signup to reply.
  2. The code you shared is definitely correct. You are passing payment_intent_data[metadata] to control this. The resulting PaymentIntent that will be created will definitely have that metadata information on it automatically set.

    If you go through Checkout and pay, you will get a resulting PaymentIntent that has the right metadata

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search