-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the
BillingPortal
namespace and the Session
API a…
…nd resource
- Loading branch information
1 parent
4f3efa9
commit b5856b8
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |