Repository navigation
fix: improve Stripe checkout handling #4477
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
Open
yanowo
wants to merge
5
commits into
QuantumNous:main
Choose a base branch
from
yanowo:fix/stripe-checkout-customer-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
026c452
fix: improve Stripe checkout handling
yanowo c27c481
Merge remote-tracking branch 'upstream/main' into fix/stripe-checkout…
yanowo 8924182
chore: merge upstream release without workflows
yanowo ae38a48
fix: improve Stripe checkout handling
yanowo 240158b
Merge branch 'fix/stripe-checkout-customer-mode' of https://github.co…
yanowo 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,89 @@ | ||
| package model | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGetStripeCustomerIDSeparatesLiveAndTest(t *testing.T) { | ||
| user := &User{ | ||
| StripeCustomer: "cus_legacy", | ||
| StripeCustomerTest: "cus_test", | ||
| StripeCustomerLive: "cus_live", | ||
| } | ||
|
|
||
| assert.Equal(t, "cus_live", GetStripeCustomerID(user, true)) | ||
| assert.Equal(t, "cus_test", GetStripeCustomerID(user, false)) | ||
| assert.Equal(t, "", GetStripeCustomerID(&User{StripeCustomer: "cus_legacy"}, true)) | ||
| assert.Equal(t, "cus_legacy", GetStripeCustomerID(&User{StripeCustomer: "cus_legacy"}, false)) | ||
| assert.Equal(t, "", GetStripeCustomerID(nil, false)) | ||
| } | ||
|
|
||
| func TestRechargeStoresStripeCustomerByLivemode(t *testing.T) { | ||
| truncateTables(t) | ||
|
|
||
| user := &User{ | ||
| Id: 601, | ||
| Username: "stripe_customer_user", | ||
| Status: common.UserStatusEnabled, | ||
| } | ||
| require.NoError(t, DB.Create(user).Error) | ||
|
|
||
| insertStripeTopUpForCustomerTest(t, "stripe-test-customer", user.Id) | ||
| require.NoError(t, Recharge("stripe-test-customer", "cus_test_123", false, "127.0.0.1")) | ||
|
|
||
| var stored User | ||
| require.NoError(t, DB.First(&stored, "id = ?", user.Id).Error) | ||
| assert.Equal(t, "cus_test_123", stored.StripeCustomer) | ||
| assert.Equal(t, "cus_test_123", stored.StripeCustomerTest) | ||
| assert.Empty(t, stored.StripeCustomerLive) | ||
|
|
||
| insertStripeTopUpForCustomerTest(t, "stripe-live-customer", user.Id) | ||
| require.NoError(t, Recharge("stripe-live-customer", "cus_live_123", true, "127.0.0.1")) | ||
|
|
||
| require.NoError(t, DB.First(&stored, "id = ?", user.Id).Error) | ||
| assert.Equal(t, "cus_test_123", stored.StripeCustomer) | ||
| assert.Equal(t, "cus_test_123", stored.StripeCustomerTest) | ||
| assert.Equal(t, "cus_live_123", stored.StripeCustomerLive) | ||
| } | ||
|
|
||
| func TestCompleteStripeSubscriptionOrderStoresCustomerByLivemode(t *testing.T) { | ||
| truncateTables(t) | ||
|
|
||
| user := &User{ | ||
| Id: 602, | ||
| Username: "stripe_subscription_user", | ||
| Status: common.UserStatusEnabled, | ||
| } | ||
| require.NoError(t, DB.Create(user).Error) | ||
| plan := insertSubscriptionPlanForPaymentGuardTest(t, 702) | ||
| insertSubscriptionOrderForPaymentGuardTest(t, "sub-stripe-customer", user.Id, plan.Id, PaymentProviderStripe) | ||
|
|
||
| err := CompleteStripeSubscriptionOrder("sub-stripe-customer", `{"customer":"cus_live_sub"}`, "cus_live_sub", true) | ||
| require.NoError(t, err) | ||
|
|
||
| var stored User | ||
| require.NoError(t, DB.First(&stored, "id = ?", user.Id).Error) | ||
| assert.Empty(t, stored.StripeCustomer) | ||
| assert.Empty(t, stored.StripeCustomerTest) | ||
| assert.Equal(t, "cus_live_sub", stored.StripeCustomerLive) | ||
| } | ||
|
|
||
| func insertStripeTopUpForCustomerTest(t *testing.T, tradeNo string, userID int) { | ||
| t.Helper() | ||
| topUp := &TopUp{ | ||
| UserId: userID, | ||
| Amount: 1, | ||
| Money: 1, | ||
| TradeNo: tradeNo, | ||
| PaymentMethod: PaymentMethodStripe, | ||
| PaymentProvider: PaymentProviderStripe, | ||
| Status: common.TopUpStatusPending, | ||
| CreateTime: time.Now().Unix(), | ||
| } | ||
| require.NoError(t, topUp.Insert()) | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This turns the new top-up switch into a subscription kill switch.
SubscriptionRequestStripePaynow refuses all Stripe subscription checkouts whensetting.StripeEnabledis false. Based on the PR scope, that flag is the new Stripe recharge/top-up switch, so disabling one-time top-ups would also block selling subscriptions here.🤖 Prompt for AI Agents
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 flagging this. This behavior is intentional.
In this project,
StripeEnabledis intended to control whether users can initiate Stripe payments, not only one-time top-ups. Subscriptions and top-ups both create Stripe Checkout sessions, so if Stripe is disabled from the payment settings, subscription checkout should also be blocked. Otherwise, disabling Stripe recharge would still leave another Stripe payment entry point available.Webhook processing remains independent from this switch, so existing or delayed Stripe events can still be handled even after Stripe payments are disabled.
The UI helper text also states that users cannot pay with Stripe when this option is disabled, so blocking subscription checkout is consistent with the intended behavior.
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.
Only users with a collaborator, contributor, member, or owner role can interact with CodeRabbit.