怠慢プログラマーの備忘録

怠慢でナマケモノなプログラマーの備忘録です。

【Golang x Stripe】Customerを作成・取得(備忘録)

Getting started guide

Install stripe-go

$ go get -u github.com/stripe/stripe-go

API Doc

stripe.com

Create Customer

endpoint: https://api.stripe.com/v1/customers

import (
    "fmt"
    "github.com/stripe/stripe-go"
    "github.com/stripe/stripe-go/customer"
    "log"
)

func CreateCustomer() {
    stripe.Key = "sk_test_xxxxxxxxxxxxxx"
    params := &stripe.CustomerParams{
        Email: stripe.String("test@example.com"),
    }
    customer, error := customer.New(params)
    if error != nil {
        log.Fatal(error)
    }
    fmt.Print(customer.ID)
}

func main() {
    CreateCustomer()
}

GetCustomer

endpoint: https://api.stripe.com/v1/customers/{customer_id}

import (
    "fmt"
    "github.com/stripe/stripe-go"
    "github.com/stripe/stripe-go/customer"
    "log"
)

func GetCustomer(customerId string) {
    stripe.Key = "sk_test_xxxxxxxxxxxxxx"

    result, error := customer.Get(customerId, nil)
    if error != nil {
        log.Fatal(error)
    }
    fmt.Println(result.ID)

}

func main() {
    GetCustomer("cus_xxxxxxxxx")
  • クラインアント側では直接通信するのはNG。(For security, the Customer API is not directly accessible from the client.ってちゃんと書いてある)
  • これをAWSLambdaにデプロイしてAPIGatewayでクライアント側で叩けるようにする(GCPでもいい)。
  • サインアップをした際にFirebaseのAuth -> StripeCustomer(emailやname、metadataにFirebaseのuidなど入れておけばいい) ->Firestore(StripeのCustomerIdを入れておく)で保存しておく
  • クライアント側のSDKにPresetされてるUIを使用するときにEphemeralKeyの発行が必要になるのでその際にCustomerIdとAPIVersionが必要になる。

スターティングGo言語

スターティングGo言語