-
Notifications
You must be signed in to change notification settings - Fork 30
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
feat: use customer id when present whilst creating a premium order #2715
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5c62979
feat: use customer id when present whilst creating a premium order
pavelbrm 90d6129
test: fix test for setOrderTrialDays
pavelbrm f1e263c
fix: use customer id only when email is present on old session whilst…
pavelbrm a179d50
Merge remote-tracking branch 'origin/master' into handle-checkout-cus…
pavelbrm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -4388,7 +4388,7 @@ func TestService_recreateStripeSession(t *testing.T) { | |
}, | ||
|
||
{ | ||
name: "success_email_from_session", | ||
name: "success_email_cust_from_session", | ||
given: tcGiven{ | ||
ordRepo: &repository.MockOrder{ | ||
FnAppendMetadata: func(ctx context.Context, dbi sqlx.ExecerContext, id uuid.UUID, key, val string) error { | ||
|
@@ -4405,7 +4405,7 @@ func TestService_recreateStripeSession(t *testing.T) { | |
ID: "cs_test_id_old", | ||
SuccessURL: "https://example.com/success", | ||
CancelURL: "https://example.com/cancel", | ||
Customer: &stripe.Customer{Email: "[email protected]"}, | ||
Customer: &stripe.Customer{ID: "cus_id", Email: "[email protected]"}, | ||
} | ||
|
||
return result, nil | ||
|
@@ -4455,6 +4455,79 @@ func TestService_recreateStripeSession(t *testing.T) { | |
}, | ||
}, | ||
|
||
{ | ||
name: "success_email_from_request_cust_without_email", | ||
given: tcGiven{ | ||
ordRepo: &repository.MockOrder{ | ||
FnAppendMetadata: func(ctx context.Context, dbi sqlx.ExecerContext, id uuid.UUID, key, val string) error { | ||
if key == "stripeCheckoutSessionId" && val == "cs_test_id" { | ||
return nil | ||
} | ||
|
||
return model.Error("unexpected") | ||
}, | ||
}, | ||
cl: &xstripe.MockClient{ | ||
FnSession: func(ctx context.Context, id string, params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) { | ||
result := &stripe.CheckoutSession{ | ||
ID: "cs_test_id_old", | ||
SuccessURL: "https://example.com/success", | ||
CancelURL: "https://example.com/cancel", | ||
Customer: &stripe.Customer{ID: "cus_id"}, | ||
} | ||
|
||
return result, nil | ||
}, | ||
|
||
FnFindCustomer: func(ctx context.Context, email string) (*stripe.Customer, bool) { | ||
return nil, false | ||
}, | ||
|
||
FnCreateSession: func(ctx context.Context, params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) { | ||
if params.Customer != nil { | ||
return nil, model.Error("unexpected_customer") | ||
} | ||
|
||
if *params.CustomerEmail != "[email protected]" { | ||
return nil, model.Error("unexpected_customer_email") | ||
} | ||
|
||
result := &stripe.CheckoutSession{ | ||
ID: "cs_test_id", | ||
PaymentMethodTypes: []string{"card"}, | ||
Mode: stripe.CheckoutSessionModeSubscription, | ||
SuccessURL: *params.SuccessURL, | ||
CancelURL: *params.CancelURL, | ||
ClientReferenceID: *params.ClientReferenceID, | ||
Subscription: &stripe.Subscription{ | ||
ID: "sub_id", | ||
Metadata: map[string]string{ | ||
"orderID": *params.ClientReferenceID, | ||
}, | ||
}, | ||
AllowPromotionCodes: true, | ||
} | ||
|
||
return result, nil | ||
}, | ||
}, | ||
ord: &model.Order{ | ||
ID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")), | ||
Items: []model.OrderItem{ | ||
{ | ||
Quantity: 1, | ||
Metadata: datastore.Metadata{"stripe_item_id": "stripe_item_id"}, | ||
}, | ||
}, | ||
}, | ||
oldSessID: "cs_test_id_old", | ||
email: "[email protected]", | ||
}, | ||
exp: tcExpected{ | ||
val: "cs_test_id", | ||
}, | ||
}, | ||
|
||
{ | ||
name: "success_email_from_request", | ||
given: tcGiven{ | ||
|
@@ -4473,7 +4546,6 @@ func TestService_recreateStripeSession(t *testing.T) { | |
ID: "cs_test_id_old", | ||
SuccessURL: "https://example.com/success", | ||
CancelURL: "https://example.com/cancel", | ||
Customer: &stripe.Customer{Email: "[email protected]"}, | ||
} | ||
|
||
return result, nil | ||
|
@@ -4564,7 +4636,84 @@ func TestCreateStripeSession(t *testing.T) { | |
|
||
tests := []testCase{ | ||
{ | ||
name: "success_found_customer", | ||
name: "success_cust_id", | ||
given: tcGiven{ | ||
cl: &xstripe.MockClient{ | ||
FnCreateSession: func(ctx context.Context, params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) { | ||
if params.Customer == nil || *params.Customer != "cus_id" { | ||
return nil, model.Error("unexpected") | ||
} | ||
|
||
result := &stripe.CheckoutSession{ID: "cs_test_id"} | ||
|
||
return result, nil | ||
}, | ||
|
||
FnFindCustomer: func(ctx context.Context, email string) (*stripe.Customer, bool) { | ||
panic("unexpected_find_customer") | ||
}, | ||
}, | ||
|
||
req: createStripeSessionRequest{ | ||
orderID: "facade00-0000-4000-a000-000000000000", | ||
customerID: "cus_id", | ||
successURL: "https://example.com/success", | ||
cancelURL: "https://example.com/cancel", | ||
trialDays: 7, | ||
items: []*stripe.CheckoutSessionLineItemParams{ | ||
{ | ||
Quantity: ptrTo[int64](1), | ||
Price: ptrTo("stripe_item_id"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
exp: tcExpected{ | ||
val: "cs_test_id", | ||
}, | ||
}, | ||
|
||
{ | ||
name: "success_cust_id_email", | ||
given: tcGiven{ | ||
cl: &xstripe.MockClient{ | ||
FnCreateSession: func(ctx context.Context, params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) { | ||
if params.Customer == nil || *params.Customer != "cus_id" { | ||
return nil, model.Error("unexpected") | ||
} | ||
|
||
result := &stripe.CheckoutSession{ID: "cs_test_id"} | ||
|
||
return result, nil | ||
}, | ||
|
||
FnFindCustomer: func(ctx context.Context, email string) (*stripe.Customer, bool) { | ||
panic("unexpected_find_customer") | ||
}, | ||
}, | ||
|
||
req: createStripeSessionRequest{ | ||
orderID: "facade00-0000-4000-a000-000000000000", | ||
customerID: "cus_id", | ||
email: "[email protected]", | ||
successURL: "https://example.com/success", | ||
cancelURL: "https://example.com/cancel", | ||
trialDays: 7, | ||
items: []*stripe.CheckoutSessionLineItemParams{ | ||
{ | ||
Quantity: ptrTo[int64](1), | ||
Price: ptrTo("stripe_item_id"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
exp: tcExpected{ | ||
val: "cs_test_id", | ||
}, | ||
}, | ||
|
||
{ | ||
name: "success_email_found_customer", | ||
given: tcGiven{ | ||
cl: &xstripe.MockClient{ | ||
FnCreateSession: func(ctx context.Context, params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) { | ||
|
@@ -4598,7 +4747,7 @@ func TestCreateStripeSession(t *testing.T) { | |
}, | ||
|
||
{ | ||
name: "success_customer_not_found", | ||
name: "success_email_customer_not_found", | ||
given: tcGiven{ | ||
cl: &xstripe.MockClient{ | ||
FnFindCustomer: func(ctx context.Context, email string) (*stripe.Customer, bool) { | ||
|
@@ -4636,7 +4785,7 @@ func TestCreateStripeSession(t *testing.T) { | |
}, | ||
|
||
{ | ||
name: "success_no_customer_email", | ||
name: "success_email_no_customer_email", | ||
given: tcGiven{ | ||
cl: &xstripe.MockClient{ | ||
FnFindCustomer: func(ctx context.Context, email string) (*stripe.Customer, bool) { | ||
|
@@ -4663,7 +4812,7 @@ func TestCreateStripeSession(t *testing.T) { | |
}, | ||
|
||
{ | ||
name: "success_no_trial_days", | ||
name: "success_email_no_trial_days", | ||
given: tcGiven{ | ||
cl: &xstripe.MockClient{}, | ||
|
||
|
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 |
---|---|---|
|
@@ -15,7 +15,10 @@ type MockClient struct { | |
|
||
func (c *MockClient) Session(ctx context.Context, id string, params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) { | ||
if c.FnSession == nil { | ||
result := &stripe.CheckoutSession{ID: id} | ||
result := &stripe.CheckoutSession{ | ||
ID: id, | ||
Customer: &stripe.Customer{ID: "cus_id", Email: "[email protected]"}, | ||
} | ||
|
||
return result, nil | ||
} | ||
|
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 |
---|---|---|
|
@@ -65,3 +65,52 @@ func TestCustomerEmailFromSession(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestCustomerIDFromSession(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
exp string | ||
given *stripe.CheckoutSession | ||
}{ | ||
{ | ||
name: "nil_customer_no_email", | ||
given: &stripe.CheckoutSession{}, | ||
}, | ||
|
||
{ | ||
name: "customer_empty_email", | ||
given: &stripe.CheckoutSession{ | ||
Customer: &stripe.Customer{}, | ||
}, | ||
}, | ||
|
||
{ | ||
name: "customer_email_no_id", | ||
given: &stripe.CheckoutSession{ | ||
Customer: &stripe.Customer{ | ||
Email: "[email protected]", | ||
}, | ||
}, | ||
}, | ||
|
||
{ | ||
name: "customer_email_id", | ||
given: &stripe.CheckoutSession{ | ||
Customer: &stripe.Customer{ | ||
ID: "cus_id", | ||
Email: "[email protected]", | ||
}, | ||
}, | ||
exp: "cus_id", | ||
}, | ||
} | ||
|
||
for i := range tests { | ||
tc := tests[i] | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
actual := CustomerIDFromSession(tc.given) | ||
should.Equal(t, tc.exp, actual) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding these tests in here - it made it much simpler to understand what this change was doing.