Skip to content

Commit

Permalink
Merge pull request #1063 from stripe/remi-add-portal
Browse files Browse the repository at this point in the history
Add support for the `BillingPortal` namespace and the `Session` API and resource
  • Loading branch information
remi-stripe authored Apr 22, 2020
2 parents ca00f45 + bd7c983 commit b1b8e17
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 30 additions & 0 deletions billingportal/session/client.go
Original file line number Diff line number Diff line change
@@ -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}
}
18 changes: 18 additions & 0 deletions billingportal/session/client_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
43 changes: 43 additions & 0 deletions billingportal_session.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 5 additions & 1 deletion client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b1b8e17

Please sign in to comment.