nishimura.clubnishimura.club

Node.js Stripe PaymentMethod 保存

作成日
2021-10-03
更新日
2021-10-03

フロントエンド(React.js)

バックエンド(Node.js)にフロントエンドで作成したpaymentMethodオブジェクトをそのまま送ってあげる。

await axios.post( 'http://localhost:8080/users/attach-payment-method', { paymentMethod: paymentMethod, }, { headers: { Authorization: `Bearer ${accessToken}`, }, } );

バックエンド(Nest.js)

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.brandpaymentMethod.card.last4も保存しておいて、ユーザーが決済手段を確認しやすいようにしておく。

ドキュメント

Stripe API Reference - Attach a PaymentMethod to a Customer

Related