条纹“没有这样的价格”

时间:2020-09-11 21:00:43

标签: node.js stripe-payments

我在Stripe仪表板中创建了具有分层定价的产品。我复制了价格API ID,并将其放在应用的前端。当我运行代码时,后端会生成错误:No such price: 'PRICE_1HPYAGLJZYBC5S5KGBKT8UDY'。此价格ID与我的Stripe仪表板上的价格之一匹配,但我从未设置产品,所以我想知道这是否是问题。这是我的客户端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,
        };
      })
  );
}

这是我的后端代码:

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);
  }
});

1 个答案:

答案 0 :(得分:3)

检查价格ID,看起来您的前端中的东西会将所有字符串都转换为大写。通常,价格ID以小写字母('price ....')开头,然后字符串是数字与小写和大写字母的混合。

相关问题