NodeJS 如何使用Stripe支付?

x6yk4ghg  于 2023-06-22  发布在  Node.js
关注(0)|答案(2)|浏览(229)

我有一个条纹应用程序与一些订阅计划。我通过下面的代码为用户创建了一个订阅。

var subscription = await stripe.subscriptions.create({
        customer: user.stripe_account_id,
        items: [{ price: plan.plan_id }], // Replace with the actual price ID
      });

我对这个订阅过程和订阅付款感到困惑。我应该在创建订阅后为该客户启动付款吗(如上面的代码)?
我试图查找代码来处理API(nestjs/node)的付款,但教程主要是展示如何从条纹 Jmeter 板进行订阅付款。
我的问题是:

1.如何通过我的API(节点)为该客户付款?.
2.本计划结束时(如:30天计划),如何重新订阅该用户相同的计划?如何为该客户支付续订费用?

fnx2tebb

fnx2tebb1#

请务必阅读并遵循指南。https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=elements
1/创建订阅后,您可以使用PaymentIntent进行第一次即将到来的付款(latest_invoice.payment_intent),您可以在前端使用Stripe Elements来接受卡详细信息并处理付款。
2/这只是默认情况下发生的,您不必做任何其他事情(这就是Stripe中的订阅,它在您创建价格(定义金额和计费周期)时指定的期间向客户收取费用)。
强烈建议阅读Stripe的文档并遵循指南。

nle07wnf

nle07wnf2#

当然!下面是Node.js中的一个示例代码片段,用于为客户创建订阅并处理付款:

const stripe = require('stripe')(YOUR_STRIPE_SECRET_KEY);

async function createSubscription(customerId, priceId) {
  try {
    const subscription = await stripe.subscriptions.create({
      customer: customerId,
      items: [{ price: priceId }],
    });
    console.log('Subscription created:', subscription);
    // Subscription created successfully, payment will be automatically handled by Stripe
  } catch (error) {
    console.error('Error creating subscription:', error);
    // Handle the error appropriately
  }
}

// Usage example
const customerId = 'CUSTOMER_ID'; // Replace with the customer ID from your database
const priceId = 'PRICE_ID'; // Replace with the price ID of the subscription plan

createSubscription(customerId, priceId);

相关问题