バックエンド(Node.js)にフロントエンドで作成したpaymentMethod
オブジェクトをそのまま送ってあげる。
await axios.post( 'http://localhost:8080/users/attach-payment-method', { paymentMethod: paymentMethod, }, { headers: { Authorization: `Bearer ${accessToken}`, }, } );
PaymentMethodをStripeの顧客に追加する。 DBには、Userに紐づくStripeの顧客IDを登録しておく。
// Controllerから呼び出している想定 async attachPaymentMethod( paymentMethodId: string, stripeCustomerId: string, ): Promise<any> { const paymentMethod = await this.stripe.paymentMethods.attach( paymentMethodId, // ⚠️ paymentMethod.id { customer: stripeCustomerId }, // 👈 MyApp の User.stripeCustomerId ); return paymentMethod; }
Userモデルに、paymentMethod.card.brand
とpaymentMethod.card.last4
も保存しておいて、ユーザーが決済手段を確認しやすいようにしておく。