我在 Stripe 中定義了一個產品,我希望將其設定為 7 天免費試用,然後向客戶收費(除非他們事先取消)
透過 Firebase 使用自訂 Nodejs 函數 - 我收到以下錯誤:
Missing value for stripe.confirmCardPayment intent secret: value should be a client_secret string
我的前端功能:
const createSubscription = async () => { setProcessing(true); setWait('PLEASE WAIT'); try { // create a payment method const paymentMethod = await stripe?.createPaymentMethod({ type: "card", card: elements?.getElement(CardElement), billing_details: { name, email, }, }); // call the backend to create subscription const response = await fetch("https://XXX.cloudfunctions.net/tplannersubscription", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ paymentMethod: paymentMethod?.paymentMethod?.id, name, lname, email, priceId }), }).then((res) => res.json()); const confirmPayment = await stripe?.confirmCardPayment( response.clientSecret ); };
和我的節點功能:
cors(request, response, async () => { // create a stripe customer try { if (request.method != "POST") return response.status(400); const { name, email, paymentMethod } = request.body; // Create a customer const customer = await stripe.customers.create({ email, name, payment_method: paymentMethod, invoice_settings: { default_payment_method: paymentMethod }, }); // const priceId = request.priceId; // Create a subscription const subscription = await stripe.subscriptions.create({ customer: customer.id, items: [{ price: "price_1KZ3nTGxUje7SlyIDUfIXkr3" }], payment_settings: { payment_method_options: { card: { request_three_d_secure: "any", }, }, payment_method_types: ["card"], save_default_payment_method: "on_subscription", }, payment_behavior: "default_incomplete", trial_period_days: 7, expand: ["latest_invoice.payment_intent"], }); // Send back the client secret for payment response.json({ message: "Subscription successfully initiated", clientSecret: subscription.latest_invoice.payment_intent.client_secret, subID:subscription.id }); } catch (err) { console.error(err); response.status(500).json({ message: "Internal server error" }); } });
如果我取出 Trial_period_days 那麼它可以正常工作,但是如果包含它,則會傳回如上所述的錯誤。
當您建立試用訂閱時,不會建立 PaymentIntent,因為目前不需要付款。
相反,SetupIntent 可用於收集付款方式詳細資訊。您可以在建立訂閱時擴充
pending_setup_intent
[0],並透過 confirmCardSetup一个>.[0] https://stripe.com/docs/api/訂閱/物件#subscription_object-pending_setup_intent