Stripe "no such price"

Viewed 6189

I created a product with tiered pricing in my Stripe dashboard. I copied the price API IDs and put them in my app's frontend. When I run my code, the backend generates the error: No such price: 'PRICE_1HPYAGLJZYBC5S5KGBKT8UDY'. This price id matches one of the prices on my Stripe dashboard, but I never set the product so I'm wondering if that's the issue. Here is my client js:

function createSubscription({ customerId, paymentMethodId, priceId }) {
  return (
    fetch('/create-subscription', {
      method: 'post',
      headers: {
        'Content-type': 'application/json',
      },
      body: JSON.stringify({
        customerId: customerId,
        paymentMethodId: paymentMethodId,
        priceId: priceId,
      }),
    })
      .then((response) => {
        return response.json();
      })
      // If the card is declined, display an error to the user.
      .then((result) => {
        if (result.error) {
          // The card had an error when trying to attach it to a customer
          throw result;
        }
        return result;
      })
      // Normalize the result to contain the object returned
      // by Stripe. Add the addional details we need.
      .then((result) => {
        console.log("RETURNING SUBSCRIPTION")
        return {
          // Use the Stripe 'object' property on the
          // returned result to understand what object is returned.
          subscription: result,
          paymentMethodId: paymentMethodId,
          priceId: priceId,
        };
      })
  );
}

And here is my backend code:

app.post('/create-subscription', async function(req, res) {
  console.log(req.body);
  User.findOne({"_id": req.session.auth_user._id}, async function(err, user) {
    if (user.stripe_id) {
      console.log("RETRIEVING CUSTOMER");
      var customer = await stripe.customers.retrieve(user.stripe_id);
      if (user.stripe_subscription) {
        console.log("RETRIEVING SUBSCRIPTION");
        var subscription = await stripe.subscriptions.retrieve(user.stripe_subscription);
        update_customer(customer, subscription);
      }
      else {
        console.log("CREATING SUBSCRIPTION");
        var subscription = await stripe.subscriptions.create({
          customer: customer.id,
          items: [{
            price: req.body.priceId,
          }]
        });
        user.stripe_subscription = subscription.id;
        user.save(function(err) {
          update_customer(customer, subscription);
        })
      }
    }
    else {
      console.log("CREATING CUSTOMER");
      var customer = await stripe.customers.create({
        email: req.body.email,
      });
      user.stripe_id = customer.id;
      user.save( async function(err, user) {
        if (user.stripe_subscription) {
          console.log("RETRIEVING SUBSCRIPTION");
          var subscription = await stripe.subscriptions.retrieve(user.stripe_subscription);
          update_customer(customer, subscription);
        }
        else {
          console.log("CREATING SUBSCRIPTION");
          var subscription = await stripe.subscriptions.create({
            customer: customer.id,
            items: [{
              price: req.body.priceId,
            }]
          });
          user.stripe_subscription = subscription.id;
          user.save(function(err) {
            update_customer(customer, subscription);
          });
        }
      });
    }
  });

  async function update_customer(customer, subscription) {
    const paymentMethod = await stripe.paymentMethods.attach(
      req.body.paymentMethodId,
      {customer: customer.id}
    );

    console.log(subscription);
    res.send(subscription);
  }
});
4 Answers

Check the price ID, it looks like something in your frontend converts all the string to uppercase. Usually price id start in lowercase ('price....') and then the string is a mix between numbers and lowercase and uppercase characters.

Incase anyone faces this issue in the future. I had the same issue, but mine was caused by the stripe secret being wrong.

It's wise to:

  • Double check the price,
  • Trim the string,
  • Check your config keys all through

Hope this helps someone

In my case, the API keys were not correct.

If like me, you just followed the tutorial from the docs, the API keys from the code snippets that are proposed are not correct. You have to setup the ones from the dashboard page (for the test environment: https://dashboard.stripe.com/test/dashboard)

In my case I was following the docs and the docs had it like this: Price = "{{price_1234}}" So I changed it to this: Price = "price_1234" and it worked.

Related