Skip to content

Commit

Permalink
Add support for the BillingPortal namespace and the Session API a…
Browse files Browse the repository at this point in the history
…nd resource
  • Loading branch information
remi-stripe committed Apr 14, 2020
1 parent 4f3efa9 commit b5856b8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
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 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}
}
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"
_ "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)
}
42 changes: 42 additions & 0 deletions billingportal_session.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b5856b8

Please sign in to comment.