Stripeクレジットカード決済メモ
やったこと
- 環境
- 大まかな決済の流れ
- StripeAPIを用いたクレジットカード決済の実装
環境
- サーバ側 : golang
- フロント側 : Nuxt.js
大まかな決済の流れ
PaymentMethod > PaymentIntent > PaymentConfirm
(画像はApplePayのシーケンス図)
クレカ入力の場合
- PaymentMethod(フロント)作成
- PaymentIntent(サーバ)作成
- PaymentConfirm(サーバ)
Apple Pay等の決済の流れ
- PaymentIntent(サーバ) 作成
- フロント側決済
PaymentIntentは必ずサーバ側で作る必要があり。
StripeAPIを用いたクレジットカード決済の実装
サーバ側
1000円の支払い
import ( "fmt" "github.com/stripe/stripe-go/v72" "github.com/stripe/stripe-go/v72/paymentintent" "github.com/stripe/stripe-go/v72/paymentmethod" ) func main() { stripe.Key = "sk_test_xxxxxxxxxxx" // 支払い内容の作成 piParams := &stripe.PaymentIntentParams{ Amount: stripe.Imnt64(1000), Currency: stripe.String(string(stripe.CurrencyJPY)), PaymentMethodTypes: stripe.StringSlice([]string{ string(stripe.PaymentMethodTypeCard), }), } pi, err := paymentintent.New(piParams) if err != nil { // error handling } // 支払い方法の取得 pm, err := paymentmethod.Get("pm_xxxxxxxx", nil) if err != nil { // error handling } // 支払いの確認 picParams := &stripe.PaymentIntentConfirmParams{ PaymentMethod: stripe.String(pm.ID), } pic, err := paymentintent.Confirm(pi.ID, picParams) if err != nil { // error handling } fmt.Printf("pay : ", pic.ID)
front側
const stripe = require('stripe')('sk_test_xxxxxxxxxxxx'); const paymentMethod = await stripe.paymentMethods.create({ type: 'card', card: { number: '4242424242424242', exp_month: 3, exp_year: 2022, cvc: '314', }, });
テスト用のクレカ情報は下記から
https://stripe.com/docs/testing