From bd7c98329d8718980f770d76a94742efe3ef8184 Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Mon, 13 Apr 2020 22:06:17 -0700 Subject: [PATCH] Add support for the `BillingPortal` namespace and the `Session` API and resource --- .travis.yml | 2 +- billingportal/session/client.go | 30 +++++++++++++++++++ billingportal/session/client_test.go | 18 ++++++++++++ billingportal_session.go | 43 ++++++++++++++++++++++++++++ client/api.go | 6 +++- testing/testing.go | 2 +- 6 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 billingportal/session/client.go create mode 100644 billingportal/session/client_test.go create mode 100644 billingportal_session.go diff --git a/.travis.yml b/.travis.yml index f9a413a4c6..f213ec9364 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,7 +32,7 @@ cache: env: global: # If changing this number, please also change it in `testing/testing.go`. - - STRIPE_MOCK_VERSION=0.87.0 + - STRIPE_MOCK_VERSION=0.88.0 go: - "1.10.x" diff --git a/billingportal/session/client.go b/billingportal/session/client.go new file mode 100644 index 0000000000..553914b6cb --- /dev/null +++ b/billingportal/session/client.go @@ -0,0 +1,30 @@ +// Package session provides API functions related to billing portal sessions. +package session + +import ( + "net/http" + + stripe "github.com/stripe/stripe-go/v71" +) + +// Client is used to invoke /billing_portal/sessions APIs. +type Client struct { + B stripe.Backend + Key string +} + +// New creates a new session. +func New(params *stripe.BillingPortalSessionParams) (*stripe.BillingPortalSession, error) { + return getC().New(params) +} + +// New creates a new session. +func (c Client) New(params *stripe.BillingPortalSessionParams) (*stripe.BillingPortalSession, error) { + session := &stripe.BillingPortalSession{} + err := c.B.Call(http.MethodPost, "/v1/billing_portal/sessions", c.Key, params, session) + return session, err +} + +func getC() Client { + return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key} +} diff --git a/billingportal/session/client_test.go b/billingportal/session/client_test.go new file mode 100644 index 0000000000..9bc682f9a9 --- /dev/null +++ b/billingportal/session/client_test.go @@ -0,0 +1,18 @@ +package session + +import ( + "testing" + + assert "github.com/stretchr/testify/require" + stripe "github.com/stripe/stripe-go/v71" + _ "github.com/stripe/stripe-go/v71/testing" +) + +func TestBillingPortalSessionNew(t *testing.T) { + session, err := New(&stripe.BillingPortalSessionParams{ + Customer: stripe.String("cus_123"), + ReturnURL: stripe.String("https://stripe.com/return"), + }) + assert.Nil(t, err) + assert.NotNil(t, session) +} diff --git a/billingportal_session.go b/billingportal_session.go new file mode 100644 index 0000000000..4294a931fd --- /dev/null +++ b/billingportal_session.go @@ -0,0 +1,43 @@ +package stripe + +import ( + "encoding/json" +) + +// BillingPortalSessionParams is the set of parameters that can be used when creating a billing portal session. +type BillingPortalSessionParams struct { + Params `form:"*"` + Customer *string `form:"customer"` + ReturnURL *string `form:"return_url"` +} + +// BillingPortalSession is the resource representing a billing portal session. +type BillingPortalSession struct { + APIResource + Created int64 `json:"created"` + Customer string `json:"customer"` + ID string `json:"id"` + Livemode bool `json:"livemode"` + Object string `json:"object"` + ReturnURL string `json:"return_url"` + URL string `json:"url"` +} + +// UnmarshalJSON handles deserialization of a billing portal session. +// This custom unmarshaling is needed because the resulting +// property may be an id or the full struct if it was expanded. +func (p *BillingPortalSession) UnmarshalJSON(data []byte) error { + if id, ok := ParseID(data); ok { + p.ID = id + return nil + } + + type session BillingPortalSession + var v session + if err := json.Unmarshal(data, &v); err != nil { + return err + } + + *p = BillingPortalSession(v) + return nil +} diff --git a/client/api.go b/client/api.go index fc4e7d1fde..a6fba07aa5 100644 --- a/client/api.go +++ b/client/api.go @@ -9,6 +9,7 @@ import ( "github.com/stripe/stripe-go/v71/balance" "github.com/stripe/stripe-go/v71/balancetransaction" "github.com/stripe/stripe-go/v71/bankaccount" + billingportalsession "github.com/stripe/stripe-go/v71/billingportal/session" "github.com/stripe/stripe-go/v71/bitcoinreceiver" "github.com/stripe/stripe-go/v71/bitcointransaction" "github.com/stripe/stripe-go/v71/capability" @@ -93,6 +94,8 @@ type API struct { BalanceTransaction *balancetransaction.Client // BankAccounts is the client used to invoke bank account related APIs. BankAccounts *bankaccount.Client + // BillingPortalSessions is the client used to invoke /billing_portal/sessions APIs. + BillingPortalSessions *billingportalsession.Client // BitcoinReceivers is the client used to invoke /bitcoin/receivers APIs. BitcoinReceivers *bitcoinreceiver.Client // BitcoinTransactions is the client used to invoke /bitcoin/transactions APIs. @@ -103,7 +106,7 @@ 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 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 @@ -248,6 +251,7 @@ func (a *API) Init(key string, backends *stripe.Backends) { a.Balance = &balance.Client{B: backends.API, Key: key} a.BalanceTransaction = &balancetransaction.Client{B: backends.API, Key: key} a.BankAccounts = &bankaccount.Client{B: backends.API, Key: key} + a.BillingPortalSessions = &billingportalsession.Client{B: backends.API, Key: key} a.BitcoinReceivers = &bitcoinreceiver.Client{B: backends.API, Key: key} a.BitcoinTransactions = &bitcointransaction.Client{B: backends.API, Key: key} a.Capabilities = &capability.Client{B: backends.API, Key: key} diff --git a/testing/testing.go b/testing/testing.go index 6231677aa7..83e533f7e3 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -25,7 +25,7 @@ const ( // added in a more recent version of stripe-mock, we can show people a // better error message instead of the test suite crashing with a bunch of // confusing 404 errors or the like. - MockMinimumVersion = "0.87.0" + MockMinimumVersion = "0.88.0" // TestMerchantID is a token that can be used to represent a merchant ID in // simple tests.