-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #538 from stripe/alexander/flexible-billing
Flexible billing primitives
- Loading branch information
Showing
9 changed files
with
242 additions
and
29 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 |
---|---|---|
|
@@ -23,7 +23,7 @@ cache: | |
|
||
env: | ||
global: | ||
- STRIPE_MOCK_VERSION=0.8.0 | ||
- STRIPE_MOCK_VERSION=0.11.0 | ||
|
||
go: | ||
- "1.7" | ||
|
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
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
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
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
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,33 @@ | ||
package stripe | ||
|
||
const ( | ||
// UsageRecordParamsActionIncrement indicates that if two usage records conflict | ||
// (i.E. are reported a the same timestamp), their Quantity will be summed | ||
UsageRecordParamsActionIncrement string = "increment" | ||
|
||
// UsageRecordParamsActionSet indicates that if two usage records conflict | ||
// (i.E. are reported a the same timestamp), the Quantity of the most recent | ||
// usage record will overwrite any other quantity. | ||
UsageRecordParamsActionSet string = "set" | ||
) | ||
|
||
// UsageRecord represents a usage record. | ||
// See https://stripe.com/docs/api#usage_records | ||
type UsageRecord struct { | ||
ID string `json:"id"` | ||
Live bool `json:"livemode"` | ||
Quantity uint64 `json:"quantity"` | ||
SubscriptionItem string `json:"subscription_item"` | ||
Timestamp uint64 `json:"timestamp"` | ||
} | ||
|
||
// UsageRecordParams create a usage record for a specified subscription item | ||
// and date, and fills it with a quantity. | ||
type UsageRecordParams struct { | ||
Params `form:"*"` | ||
Action string `form:"action"` | ||
Quantity uint64 `form:"quantity"` | ||
QuantityZero bool `form:"quantity,zero"` | ||
SubscriptionItem string `form:"-"` // passed in the URL | ||
Timestamp uint64 `form:"timestamp"` | ||
} |
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 stripe | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
assert "github.com/stretchr/testify/require" | ||
"github.com/stripe/stripe-go/form" | ||
) | ||
|
||
func TestUsageRecordParams_AppendTo(t *testing.T) { | ||
testCases := []struct { | ||
field string | ||
params *UsageRecordParams | ||
want interface{} | ||
}{ | ||
{"quantity", &UsageRecordParams{Quantity: 2000}, strconv.FormatUint(2000, 10)}, | ||
{"quantity", &UsageRecordParams{QuantityZero: true}, strconv.FormatUint(0, 10)}, | ||
{"timestamp", &UsageRecordParams{Timestamp: 123123123}, strconv.FormatUint(123123123, 10)}, | ||
{"action", &UsageRecordParams{Action: "increment"}, "increment"}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.field, func(t *testing.T) { | ||
body := &form.Values{} | ||
form.AppendTo(body, tc.params) | ||
values := body.ToValues() | ||
assert.Equal(t, tc.want, values.Get(tc.field)) | ||
}) | ||
} | ||
} |
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,38 @@ | ||
// Package usage_record provides the /subscription_items/{SUBSCRIPTION_ITEM_ID}/usage_records APIs | ||
package usagerecord | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
stripe "github.com/stripe/stripe-go" | ||
"github.com/stripe/stripe-go/form" | ||
) | ||
|
||
// Client is used to invoke /plans APIs. | ||
type Client struct { | ||
B stripe.Backend | ||
Key string | ||
} | ||
|
||
// New creates a new usage record. | ||
// For more details see https://stripe.com/docs/api#usage_records | ||
func New(params *stripe.UsageRecordParams) (*stripe.UsageRecord, error) { | ||
return getC().New(params) | ||
} | ||
|
||
// New internal implementation to create a new usage record. | ||
func (c Client) New(params *stripe.UsageRecordParams) (*stripe.UsageRecord, error) { | ||
body := &form.Values{} | ||
form.AppendTo(body, params) | ||
|
||
url := fmt.Sprintf("/subscription_items/%s/usage_records", url.QueryEscape(params.SubscriptionItem)) | ||
record := &stripe.UsageRecord{} | ||
err := c.B.Call("POST", url, c.Key, body, ¶ms.Params, record) | ||
|
||
return record, 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,22 @@ | ||
package usagerecord | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
assert "github.com/stretchr/testify/require" | ||
stripe "github.com/stripe/stripe-go" | ||
_ "github.com/stripe/stripe-go/testing" | ||
) | ||
|
||
func TestUsageRecordNew(t *testing.T) { | ||
now := uint64(time.Now().Unix()) | ||
usageRecord, err := New(&stripe.UsageRecordParams{ | ||
Quantity: 123, | ||
Timestamp: now, | ||
Action: stripe.UsageRecordParamsActionIncrement, | ||
SubscriptionItem: "si_123", | ||
}) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, usageRecord) | ||
} |