Stripeによって決済機能を実装しようとしています。しかし以下のサーバーに対するリクエストが失敗します。
Node
1import stripe from "../../../../lib/stripe"; 2 3export default async (req, res) => { 4 try { 5 //itemオブジェクト 6 // { 7 // name: "キノコのかさ", 8 // price: 100, 9 // } 10 const item = req.body.item; 11 // 店舗のID 12 const stripeConnectedAccountId = req.query.id; 13 // 顧客のID 14 const customerId = req.body.customer_id; 15 16 // 顧客のカードの登録情報を取得(複数のカードが登録されている場合は、複数件のカード情報をする) 17 const paymentMethodData = await stripe.paymentMethods.list({ 18 customer: customerId, 19 type: "card", 20 }); 21 22 // 店舗毎に顧客と支払い情報を登録する必要がある 23 // 参考:https://stripe.com/docs/payments/payment-methods/connect#cloning-payment-methods 24 25 // 店舗毎(stripeConnectedAccountId)にクレジットカード情報(payment_method)を複製 26 const clonedPaymentMethod = await stripe.paymentMethods.create( 27 { 28 customer: customerId, 29 payment_method: paymentMethodData.data[0].id, 30 }, 31 { 32 stripeAccount: stripeConnectedAccountId, 33 } 34 ); 35 36 // 店舗毎(stripeConnectedAccountId)に顧客情報を複製(customer))を複製 37 const clonedCustomer = await stripe.customers.create( 38 { 39 payment_method: clonedPaymentMethod.id, 40 }, 41 { 42 stripeAccount: stripeConnectedAccountId, 43 } 44 ); 45 46 // 上記の複製したpayment_methodとaccountを使用し、支払いのためのセットアップを行う 47 // intentは支払いフローを定義するオブジェクト 48 const paymentIntent = await stripe.paymentIntents.create( 49 { 50 amount: item.price, 51 currency: "jpy", 52 payment_method_types: ["card"], 53 payment_method: clonedPaymentMethod.id, 54 customer: clonedCustomer.id, 55 description: `${item.name}の購入代金`, 56 metadata: { name: item.name, price: item.price }, 57 }, 58 { 59 stripeAccount: stripeConnectedAccountId, 60 } 61 ); 62 63 // 支払い処理自体はブラウザから行う必要があるため、決済に必要なキー(client_secret)をフロントに渡す 64 // intentのsecretをクライアントがstripeに送るとintentが実行される 65 res.statusCode = 201; 66 res.json({ 67 client_secret: String(paymentIntent.client_secret), 68 }); 69 } catch (err) { 70 console.error(err); 71 res.status(500).send({ 72 error: err.message, 73 }); 74 } 75}; 76
何か問題ありますでしょうか?
情報が足りないとあれば追記します。
あなたの回答
tips
プレビュー