diff --git a/billingportal/session/client.go b/billingportal/session/client.go new file mode 100644 index 0000000000..d93d1dd84e --- /dev/null +++ b/billingportal/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 /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..f8a4a5b3cc --- /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" + _ "github.com/stripe/stripe-go/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..fb158a88e3 --- /dev/null +++ b/billingportal_session.go @@ -0,0 +1,42 @@ +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 { + 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 +}