我正在关注 stripe 官方文档来集成订阅模块。但我在编写相同的代码时遇到错误:
未定义付款意图:TypeScript
错误:类型“string | ”上不存在属性“ payment_intent” 发票'。类型“string”上不存在属性“ payment_intent”。ts(2339)
exports.doSubscribe = functions.https.onRequest(async (data, res) => {
try {
///Create Customer
const customer = await stripe.customers.create({
description: 'My First Test Customer (created for API docs at https://www.stripe.com/docs/api)',
});
///Create Payment Method
const paymentMethod = await stripe.paymentMethods.create({
type: 'card',
card: {
number: '4242424242424242',
exp_month: 10,
exp_year: 2023,
cvc: '314',
},
});
///Attach Payment Method with Customer
await stripe.paymentMethods.attach(
paymentMethod.id,
{customer:customer.id}
);
///Crate Product
const product = await stripe.products.create({
name: 'Gold Special',
});
///Create Price
const price = await stripe.prices.create({
unit_amount: 1000,
currency: 'usd',
recurring: {interval: 'month'},
product: product.id,
});
///Create Subscription
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [
{price: price.id},
],
payment_behavior: 'default_incomplete',
payment_settings: { save_default_payment_method: 'on_subscription' },
expand: ['latest_invoice.payment_intent'],
});
res.send({
subscriptionId: subscription.id,
clientSecret: subscription!.latest_invoice!.payment_intent.client_secret,
Property 'payment_intent' does not exist on type 'string | Invoice'.
Property 'payment_intent' does not exist on type 'string'.ts(2339)
});
} catch (error) {
console.log(`error: ${error}`);
// return null;
}
});
Run Code Online (Sandbox Code Playgroud)
yut*_*ing 10
看起来 TypeScript 无法确定subscription.latest_invoice
为 Invoice 对象。
我建议手动转换subscription.latest_invoice
为 Invoice 对象和subscription.latest_invoice.payment_intent
PaymentIntent 对象。例如,
const invoice = subscription.latest_invoice as Stripe.Invoice;
if (invoice.payment_intent) {
const intent = invoice.payment_intent as Stripe.PaymentIntent;
res.send({
subscriptionId: subscription.id,
clientSecret: intent.client_secret,
});
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1103 次 |
最近记录: |