Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for the now on StartDate on Subscription Schedule creation #994

Merged
merged 1 commit into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions subschedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package stripe

import (
"encoding/json"

"github.com/stripe/stripe-go/form"
)

// SubscriptionScheduleEndBehavior describe what happens to a schedule when it ends.
type SubscriptionScheduleEndBehavior string

// List of values that SubscriptionScheduleEndBehavior can take.
const (
SubscriptionScheduleEndBehaviorCancel SubscriptionScheduleEndBehavior = "cancel"
SubscriptionScheduleEndBehaviorRenew SubscriptionScheduleEndBehavior = "release"
SubscriptionScheduleEndBehaviorCancel SubscriptionScheduleEndBehavior = "cancel"
SubscriptionScheduleEndBehaviorRelease SubscriptionScheduleEndBehavior = "release"

// TODO: Remove in the next major version as the name is incorrect.
SubscriptionScheduleEndBehaviorRenew SubscriptionScheduleEndBehavior = "release"
)

// SubscriptionScheduleStatus is the list of allowed values for the schedule's status.
Expand Down Expand Up @@ -82,6 +87,15 @@ type SubscriptionScheduleParams struct {
Phases []*SubscriptionSchedulePhaseParams `form:"phases"`
Prorate *bool `form:"prorate"`
StartDate *int64 `form:"start_date"`
StartDateNow *bool `form:"-"` // See custom AppendTo
}

// AppendTo implements custom encoding logic for SubscriptionScheduleParams so that the special
// "now" value for start_date can be implemented (they're otherwise timestamps rather than strings).
func (p *SubscriptionScheduleParams) AppendTo(body *form.Values, keyParts []string) {
if BoolValue(p.StartDateNow) {
body.Add(form.FormatKey(append(keyParts, "start_date")), "now")
}
}

// SubscriptionScheduleCancelParams is the set of parameters that can be used when canceling a
Expand Down
3 changes: 2 additions & 1 deletion subschedule/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func TestSubscriptionScheduleList(t *testing.T) {

func TestSubscriptionScheduleNew(t *testing.T) {
params := &stripe.SubscriptionScheduleParams{
Customer: stripe.String("cus_123"),
Customer: stripe.String("cus_123"),
StartDateNow: stripe.Bool(true),
Phases: []*stripe.SubscriptionSchedulePhaseParams{
{
Plans: []*stripe.SubscriptionSchedulePhaseItemParams{
Expand Down
40 changes: 40 additions & 0 deletions subschedule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package stripe

import (
"encoding/json"
"testing"

assert "github.com/stretchr/testify/require"
"github.com/stripe/stripe-go/form"
)

func TestSubscriptionScheduleParams_AppendTo(t *testing.T) {
{
params := &SubscriptionScheduleParams{StartDateNow: Bool(true)}
body := &form.Values{}
form.AppendTo(body, params)
t.Logf("body = %+v", body)
assert.Equal(t, []string{"now"}, body.Get("start_date"))
}
}

func TestSubscriptionSchedule_UnmarshalJSON(t *testing.T) {
// Unmarshals from a JSON string
{
var v SubscriptionSchedule
err := json.Unmarshal([]byte(`"sub_sched_123"`), &v)
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)
assert.Equal(t, "sub_sched_123", v.ID)
}

// Unmarshals from a JSON object
{
v := SubscriptionSchedule{ID: "sub_sched_123"}
data, err := json.Marshal(&v)
assert.NoError(t, err)

err = json.Unmarshal(data, &v)
assert.NoError(t, err)
assert.Equal(t, "sub_sched_123", v.ID)
}
}