diff --git a/checkout/session/client.go b/checkout/session/client.go new file mode 100644 index 0000000000..ad59dfff93 --- /dev/null +++ b/checkout/session/client.go @@ -0,0 +1,30 @@ +// Package session provides API functions related to checkout sessions. +package session + +import ( + "net/http" + + stripe "github.com/stripe/stripe-go" +) + +// Client is used to invoke /checkout_sessions APIs. +type Client struct { + B stripe.Backend + Key string +} + +// New creates a new session. +func New(params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) { + return getC().New(params) +} + +// New creates a new session. +func (c Client) New(params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) { + session := &stripe.CheckoutSession{} + err := c.B.Call(http.MethodPost, "/v1/checkout/sessions", c.Key, params, session) + return session, err +} + +func getC() Client { + return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key} +} diff --git a/checkout/session/client_test.go b/checkout/session/client_test.go new file mode 100644 index 0000000000..bec3c301f2 --- /dev/null +++ b/checkout/session/client_test.go @@ -0,0 +1,45 @@ +package session + +import ( + "testing" + + assert "github.com/stretchr/testify/require" + stripe "github.com/stripe/stripe-go" + _ "github.com/stripe/stripe-go/testing" +) + +func TestCheckoutSessionNew(t *testing.T) { + session, err := New(&stripe.CheckoutSessionParams{ + AllowedSourceTypes: []*string{ + stripe.String("card"), + }, + CancelURL: stripe.String("https://stripe.com/cancel"), + ClientReferenceID: stripe.String("1234"), + LineItems: []*stripe.CheckoutSessionLineItemParams{ + { + Amount: stripe.Int64(1234), + Currency: stripe.String(string(stripe.CurrencyUSD)), + Description: stripe.String("description"), + Images: []*string{ + stripe.String("https://stripe.com/image1"), + }, + Name: stripe.String("name"), + Quantity: stripe.Int64(2), + }, + }, + PaymentIntentData: &stripe.CheckoutSessionPaymentIntentDataParams{ + Description: stripe.String("description"), + Shipping: &stripe.ShippingDetailsParams{ + Address: &stripe.AddressParams{ + Line1: stripe.String("line1"), + City: stripe.String("city"), + }, + Carrier: stripe.String("carrier"), + Name: stripe.String("name"), + }, + }, + SuccessURL: stripe.String("https://stripe.com/success"), + }) + assert.Nil(t, err) + assert.NotNil(t, session) +} diff --git a/checkout_session.go b/checkout_session.go new file mode 100644 index 0000000000..4d50f5fe2b --- /dev/null +++ b/checkout_session.go @@ -0,0 +1,42 @@ +package stripe + +// CheckoutSessionLineItemParams is the set of parameters allowed for a line item +// on a Checkout Session. +type CheckoutSessionLineItemParams struct { + Amount *int64 `form:"amount"` + Currency *string `form:"currency"` + Description *string `form:"description"` + Name *string `form:"name"` + Images []*string `form:"images"` + Quantity *int64 `form:"quantity"` +} + +// CheckoutSessionPaymentIntentDataParams is the set of parameters allowed for the +// payment intent creation on a Checkout Session. +type CheckoutSessionPaymentIntentDataParams struct { + Description *string `form:"description"` + ReceiptEmail *string `form:"receipt_email"` + Shipping *ShippingDetailsParams `form:"shipping"` + StatementDescriptor *string `form:"statement_descriptor"` +} + +// CheckoutSessionParams is the set of parameters that can be used when creating +// a checkout session. +// For more details see https://stripe.com/docs/api#create_checkout_session. +type CheckoutSessionParams struct { + Params `form:"*"` + AllowedSourceTypes []*string `form:"allowed_source_types"` + CancelURL *string `form:"cancel_url"` + ClientReferenceID *string `form:"client_reference_id"` + LineItems []*CheckoutSessionLineItemParams `form:"line_items"` + PaymentIntentData *CheckoutSessionPaymentIntentDataParams `form:"payment_intent_data"` + SuccessURL *string `form:"success_url"` +} + +// CheckoutSession is the resource representing a Stripe checkout session. +// For more details see https://stripe.com/docs/api#checkout_sessions. +type CheckoutSession struct { + ID string `json:"id"` + Livemode bool `json:"livemode"` + Object string `json:"object"` +} diff --git a/client/api.go b/client/api.go index 18c8fb939a..0b22e040df 100644 --- a/client/api.go +++ b/client/api.go @@ -11,6 +11,7 @@ import ( "github.com/stripe/stripe-go/bitcointransaction" "github.com/stripe/stripe-go/card" "github.com/stripe/stripe-go/charge" + checkoutsession "github.com/stripe/stripe-go/checkout/session" "github.com/stripe/stripe-go/countryspec" "github.com/stripe/stripe-go/coupon" "github.com/stripe/stripe-go/customer" @@ -80,6 +81,8 @@ type API struct { Cards *card.Client // Charges is the client used to invoke /charges APIs. Charges *charge.Client + // CheckoutSessions is the client used to invoke /checkout_sessions APIs. + CheckoutSessions *checkoutsession.Client // CountrySpec is the client used to invoke /country_specs APIs. CountrySpec *countryspec.Client // Coupons is the client used to invoke /coupons APIs. @@ -198,6 +201,7 @@ func (a *API) Init(key string, backends *stripe.Backends) { a.BitcoinTransactions = &bitcointransaction.Client{B: backends.API, Key: key} a.Cards = &card.Client{B: backends.API, Key: key} a.Charges = &charge.Client{B: backends.API, Key: key} + a.CheckoutSessions = &checkoutsession.Client{B: backends.API, Key: key} a.CountrySpec = &countryspec.Client{B: backends.API, Key: key} a.Coupons = &coupon.Client{B: backends.API, Key: key} a.Customers = &customer.Client{B: backends.API, Key: key}