设置由订阅 Stripe 创建的支付意向的元数据。

huangapple go评论64阅读模式
英文:

set metadata for the payment intent that was created by subscription stripe

问题

我正在我的程序中添加订阅功能。我正在使用Stripe。当我的Webhook接收到状态为"succeeded"的付款意图时,我会从该付款意图中获取元数据。元数据包含了我的数据库中的产品ID,购买该产品的用户ID,并将其传递给更新数据库并为客户提供产品的函数。

所以问题是我只能将元数据传递给订阅对象,而不能传递自动创建的每个计费周期的付款意图。可能的解决方案是像这样更新付款元数据:

...

const subscription = await this.stripe.subscriptions.create({
    customer: customer.id,
    items: [{ price: "price_........." }],
    expand: ["latest_invoice.payment_intent"],
    metadata: {
        productId: productId,
        userId: user.id,
    },
});

await this.stripe.paymentIntents.update(subscription.latest_invoice.payment_intent.id, {
    metadata: {
        subscriptionId: subscription.id,
        userId: user.id,
        productId: product.id,
    }
});

但这不会为计费周期中的下一个付款意图设置元数据。

也许你有相同的问题,你有其他解决方案。请告诉我你是如何解决的。

英文:

Im adding a subscription feachure in my program. Im using stripe.
When my webhook getting payment intent with status "succeeded" im taking metadata from that payment intent. Metadata contains productId form my database, userId (user that bought the product) and passing it to my function that updates the database and giving the customer his product.

So the problem is I can only pass the metadata to the subscription object and not the payment intent that creates automaticly every billing cycle. The possible solution is to update the payment metadata like this:

...

        const subscription = await this.stripe.subscriptions.create({
            customer: customer.id,
            items: [{ price: "price_........." }],
            expand: ["latest_invoice.payment_intent"],
            metadata: {
                productId: productId,
                userId: user.id,
            },
        });

        
        await this.stripe.paymentIntents.update(subscription.latest_invoice.payment_intent.id, {
            metadata: {
                subscriptionId: subscription.id,
                userId: user.id,
                productId: product.id,
            }
        });

but this will not set the metadata for the next payment intent in the billing cycle

May be you have the same problem and you have some other solution. Please let me know how you worked it out.

答案1

得分: 1

有几种方法可以解决这个问题。如果元数据仅包含静态信息,例如产品 ID 和用户 ID,那么您可以在数据库中存储订阅 ID 到产品 ID、用户 ID 等的映射。而不是侦听 payment_intent.succeeded 事件,您可以侦听 invoice.payment_succeeded 事件。然后,您可以检查发票上的订阅 ID,并在数据库中查找与该订阅 ID 关联的所有数据。如果您所指的用户 ID 和产品 ID 是 Stripe 客户 ID 和 Stripe 产品 ID,那么您还可以直接从发票对象中获取这些数据(customerproduct)。

如果强制要求侦听 payment_intent.succeeded 事件,那么您可以检查 Webhook 事件负载中的付款意图的发票,并检索它。然后,您可以按照上面段落中概述的步骤进行操作。

英文:

There's a couple ways you can solve for this. If metadata only contains static info like product id and user id, then you could store a mapping of subscription id -> product id, user id, etc. in your database. And instead of listening to payment_intent.succeeded events, you could listen to invoice.payment_succeeded events. Then, you can inspect the subscription id on the invoice and lookup all the data associated with that subscription id in your database. If the user id and product id you're referring to are the Stripe customer id and Stripe product id, then you also could just get that data directly from the invoice object (customer and product).

If it's a hard requirement that you listen to payment_intent.succeeded events instead, then you can inspect the payment intent's invoice in the webhook event payload and retrieve it. From there you can proceed with the steps I outlined in the above paragraph.

huangapple
  • 本文由 发表于 2023年7月3日 22:09:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定