How to Get Stripe Subscription Status Using User Email Address

Stripe doesn’t have any direct way to do this, but there is a way.

In a previous article, we discussed creating a Stripe subscription in a ReactJS application. How to Create a Stripe Subscription with ReactJS and NodeJS

After creating a subscription with stripe, You will want to get a user's subscription status using their email address, right? > # Unfortunately, Stripe has no straightforward way to get user's subscription status using email

The reason is it’s possible to create multiple customers using the same email address, which makes it tricky to check for subscriptions using the email address.

Old solution

There are some old-school solutions to this issue. The closest one I found is here.

What it does is.

  1. List all customers.

  2. Filter the customers using the email address

  3. Get all subscriptions using the customer parameter.

  4. Filter the subscriptions.

Not so straightforward, ha?

Let me show you a better way.

Possible Solution

One interesting way to get around this issue is to utilize the metadata field provided by stripe.

You can store anything in the metadata field and query on that later on.

We will take advantage of that.

What to do?

To get around this, we can do two things.

  1. Make sure that only one user is created per email address.

  2. Store some metadata

Let’s get to code.

Make sure only one customer is created per email.

We can do the following check before creating a customer.

let customer = await this.stripe.customers.search({
  query: `email:'${createSubscriptionDto.email}'`,
});
    
 if (customer.data.length == 0) {
     console.log(' No Custoemr is found. Let us create one!');
     customer = await this.stripe.customers.create({
       name: createSubscriptionDto.name,
       email: createSubscriptionDto.email,
       payment_method: createSubscriptionDto.paymentMethod,
       invoice_settings: {
              default_payment_method: createSubscriptionDto.paymentMethod,
       },
     });
 }

Create a subscription and store the metadata

In the next step, we need to store the customer’s email address in the subscription's metadata.

const subscription = await this.stripe.subscriptions.create({
      customer: customer.id,
      items: [{ price: priceId }],
      payment_settings: {
        payment_method_options: {
          card: {
            request_three_d_secure: 'any',
          },
        },
        payment_method_types: ['card'],
        save_default_payment_method: 'on_subscription',
      },
      expand: ['latest_invoice.payment_intent'],
      metadata: { customerEmail: createSubscriptionDto.email }, // NOTICE HERE!
    });

Notice the metadata field. We are storing the customer’s email as customerEmail field.

Finally, Get the Subscription

Now comes the fun part. Let’s search for the subscription using the metadata field.

const subscription = await this.stripe.subscriptions.search({
      query: `status:'active' AND metadata['customerEmail']:'${email}'`,
});

This query should return you a valid subscription list for that particular email address.

Final words

This is not the cleanest solution, but it’s one of the best because it solves the problem nicely.

Unless stripe provides us with an easier way to do things, this is what we get.

Hope you learned something new today. Have a wonderful day!

Profile Image

Who I am

Hi, I amMohammad Faisal, A full-stack software engineer @Cruise , working remotely from a small but beautiful country named Bangladesh.

I am most experienced inReactJS,NodeJS andAWS

Buy Me a Coffee Widget