Skip to content

Commit 5ca67d4

Browse files
Initial groundwork for generating test mode Stripe Connect URLs (#3320)
* Add support for sending test mode requests to api.woocommerce.com * Update the account connection UI to support test and live account connections * Translate connect button text and make it consistent with other connect UI * Remove legacy oauth function * Update Woo Connect Server enpoints for test/sandbox mode * Add Connect a test account button to onboarding page * Update JS tests for Connect account buttons * Pass the account ID to the oauth init request if its connected to an application * Add tests for get_cached_account_data() with param * Fix unit tests * Include check for is_null * Remove extra line added by mistake * Add $mode param function doc block comments * When caching an account in a specific mode, ensure we set the correct secret * Add unit tests for WC_Stripe_API * fix unit test --------- Co-authored-by: Diego Curbelo <[email protected]>
1 parent 55daf17 commit 5ca67d4

12 files changed

+409
-101
lines changed

client/settings/connect-stripe-account/__tests__/connect-stripe-account.test.js

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,26 @@ describe( 'ConnectStripeAccount', () => {
2626
).toBeInTheDocument();
2727
} );
2828

29-
it( 'should render both the Connect Account and Enter keys buttons when the Stripe OAuth link is provided', () => {
29+
it( 'should render both the "Create or connect an account" and "Create or connect a test account" buttons when both Stripe OAuth links are provided', () => {
30+
render(
31+
<ConnectStripeAccount
32+
oauthUrl="https://connect.stripe.com/oauth/v2/authorize?response_type=code&client_id=ca_1234&scope=read_write&state=1234"
33+
testOauthUrl="https://connect.stripe.com/oauth/v2/authorize?response_type=code&client_id=ca_5678&scope=read_write&state=5678"
34+
/>
35+
);
36+
37+
expect( screen.queryByText( 'Terms of service.' ) ).toBeInTheDocument();
38+
39+
expect(
40+
screen.getByText( 'Create or connect an account' )
41+
).toBeInTheDocument();
42+
43+
expect(
44+
screen.getByText( 'Create or connect a test account instead' )
45+
).toBeInTheDocument();
46+
} );
47+
48+
it( 'should render only the "Create or connect an account" button when only the Stripe OAuth link is provided', () => {
3049
render(
3150
<ConnectStripeAccount oauthUrl="https://connect.stripe.com/oauth/v2/authorize?response_type=code&client_id=ca_1234&scope=read_write&state=1234" />
3251
);
@@ -38,7 +57,32 @@ describe( 'ConnectStripeAccount', () => {
3857
).toBeInTheDocument();
3958

4059
expect(
41-
screen.queryByText( 'Enter account keys (advanced)' )
60+
screen.queryByText( 'Create or connect a test account' )
61+
).not.toBeInTheDocument();
62+
63+
expect(
64+
screen.queryByText( 'Create or connect a test account instead' )
65+
).not.toBeInTheDocument();
66+
} );
67+
68+
it( 'should render only the "Create or connect a test account" button when only the Stripe Test OAuth link is provided', () => {
69+
render(
70+
<ConnectStripeAccount testOauthUrl="https://connect.stripe.com/oauth/v2/authorize?response_type=code&client_id=ca_5678&scope=read_write&state=5678" />
71+
);
72+
73+
expect( screen.queryByText( 'Terms of service.' ) ).toBeInTheDocument();
74+
75+
expect(
76+
screen.getByText( 'Create or connect a test account' )
77+
).toBeInTheDocument();
78+
79+
// It should not have the "instead" word at the end
80+
expect(
81+
screen.queryByText( 'Create or connect a test account instead' )
82+
).not.toBeInTheDocument();
83+
84+
expect(
85+
screen.queryByText( 'Create or connect an account' )
4286
).not.toBeInTheDocument();
4387
} );
4488

@@ -60,6 +104,11 @@ describe( 'ConnectStripeAccount', () => {
60104
);
61105
userEvent.click( connectAccountButton );
62106

107+
expect( recordEvent ).toHaveBeenCalledWith(
108+
'wcstripe_create_or_connect_account_click',
109+
{}
110+
);
111+
63112
expect( window.location.assign ).toHaveBeenCalledWith( oauthUrl );
64113

65114
// Set the original function back to keep further tests working as expected.
@@ -68,19 +117,42 @@ describe( 'ConnectStripeAccount', () => {
68117
} );
69118
} );
70119

71-
it( 'should record a "wcstripe_create_or_connect_account_click" Track event when clicking on the Connect account button', () => {
120+
it( 'should redirect to the Stripe Test OAuth link when clicking on the "Create or connect a test account" button', () => {
121+
// Keep the original function at hand.
122+
const assign = window.location.assign;
123+
124+
Object.defineProperty( window, 'location', {
125+
value: { assign: jest.fn() },
126+
} );
127+
128+
const oauthUrl =
129+
'https://connect.stripe.com/oauth/v2/authorize?response_type=code&client_id=ca_1234&scope=read_write&state=1234';
130+
131+
const testOauthUrl =
132+
'https://connect.stripe.com/oauth/v2/authorize?response_type=code&client_id=ca_5678&scope=read_write&state=5678';
133+
72134
render(
73-
<ConnectStripeAccount oauthUrl="https://connect.stripe.com/oauth/v2/authorize?response_type=code&client_id=ca_1234&scope=read_write&state=1234" />
135+
<ConnectStripeAccount
136+
oauthUrl={ oauthUrl }
137+
testOauthUrl={ testOauthUrl }
138+
/>
74139
);
75140

76-
const connectAccountButton = screen.getByText(
77-
'Create or connect an account'
141+
const connectTestAccountButton = screen.getByText(
142+
'Create or connect a test account instead'
78143
);
79-
userEvent.click( connectAccountButton );
144+
userEvent.click( connectTestAccountButton );
80145

81146
expect( recordEvent ).toHaveBeenCalledWith(
82-
'wcstripe_create_or_connect_account_click',
147+
'wcstripe_create_or_connect_test_account_click',
83148
{}
84149
);
150+
151+
expect( window.location.assign ).toHaveBeenCalledWith( testOauthUrl );
152+
153+
// Set the original function back to keep further tests working as expected.
154+
Object.defineProperty( window, 'location', {
155+
value: { assign },
156+
} );
85157
} );
86158
} );

client/settings/connect-stripe-account/index.js

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,17 @@ const ButtonWrapper = styled.div`
4747
}
4848
`;
4949

50-
const ConnectStripeAccount = ( { oauthUrl } ) => {
50+
const ConnectStripeAccount = ( { oauthUrl, testOauthUrl } ) => {
5151
const handleCreateOrConnectAccount = () => {
5252
recordEvent( 'wcstripe_create_or_connect_account_click', {} );
5353
window.location.assign( oauthUrl );
5454
};
5555

56+
const handleCreateOrConnectTestAccount = () => {
57+
recordEvent( 'wcstripe_create_or_connect_test_account_click', {} );
58+
window.location.assign( testOauthUrl );
59+
};
60+
5661
return (
5762
<CardWrapper>
5863
<StripeBanner />
@@ -70,38 +75,58 @@ const ConnectStripeAccount = ( { oauthUrl } ) => {
7075
) }
7176
</InformationText>
7277

73-
{ oauthUrl && (
74-
<TermsOfServiceText>
75-
{ interpolateComponents( {
76-
mixedString: __(
77-
'By clicking "Create or connect an account", you agree to the {{tosLink}}Terms of service.{{/tosLink}}',
78-
'woocommerce-gateway-stripe'
79-
),
80-
components: {
81-
tosLink: (
82-
// eslint-disable-next-line jsx-a11y/anchor-has-content
83-
<a
84-
target="_blank"
85-
rel="noreferrer"
86-
href="https://wordpress.com/tos"
87-
/>
78+
{ oauthUrl || testOauthUrl ? (
79+
<>
80+
<TermsOfServiceText>
81+
{ interpolateComponents( {
82+
mixedString: __(
83+
'By clicking "Create or connect an account", you agree to the {{tosLink}}Terms of service.{{/tosLink}}',
84+
'woocommerce-gateway-stripe'
8885
),
89-
},
90-
} ) }
91-
</TermsOfServiceText>
92-
) }
93-
{ oauthUrl ? (
94-
<ButtonWrapper>
95-
<Button
96-
variant="primary"
97-
onClick={ handleCreateOrConnectAccount }
98-
>
99-
{ __(
100-
'Create or connect an account',
101-
'woocommerce-gateway-stripe'
86+
components: {
87+
tosLink: (
88+
// eslint-disable-next-line jsx-a11y/anchor-has-content
89+
<a
90+
target="_blank"
91+
rel="noreferrer"
92+
href="https://wordpress.com/tos"
93+
/>
94+
),
95+
},
96+
} ) }
97+
</TermsOfServiceText>
98+
<ButtonWrapper>
99+
{ oauthUrl && (
100+
<Button
101+
variant="primary"
102+
onClick={ handleCreateOrConnectAccount }
103+
>
104+
{ __(
105+
'Create or connect an account',
106+
'woocommerce-gateway-stripe'
107+
) }
108+
</Button>
109+
) }
110+
{ testOauthUrl && (
111+
<Button
112+
variant={
113+
oauthUrl ? 'secondary' : 'primary'
114+
}
115+
onClick={ handleCreateOrConnectTestAccount }
116+
>
117+
{ oauthUrl
118+
? __(
119+
'Create or connect a test account instead',
120+
'woocommerce-gateway-stripe'
121+
)
122+
: __(
123+
'Create or connect a test account',
124+
'woocommerce-gateway-stripe'
125+
) }
126+
</Button>
102127
) }
103-
</Button>
104-
</ButtonWrapper>
128+
</ButtonWrapper>
129+
</>
105130
) : (
106131
<InlineNotice isDismissible={ false } status="error">
107132
{ interpolateComponents( {

client/settings/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ if ( newAccountContainer ) {
4949
ReactDOM.render(
5050
<ConnectStripeAccount
5151
oauthUrl={ wc_stripe_settings_params.stripe_oauth_url }
52+
testOauthUrl={ wc_stripe_settings_params.stripe_test_oauth_url }
5253
/>,
5354
newAccountContainer
5455
);

client/settings/stripe-auth-account/stripe-auth-actions.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* global wc_stripe_settings_params */
2+
import { __ } from '@wordpress/i18n';
23
import { React } from 'react';
34
import { Button } from '@wordpress/components';
45
import ConfigureWebhookButton from './configure-webhook-button';
@@ -17,9 +18,21 @@ const StripeAuthActions = ( { testMode, displayWebhookConfigure } ) => {
1718
<div className="woocommerce-stripe-auth__actions">
1819
<Button
1920
variant="primary"
20-
href={ wc_stripe_settings_params.stripe_oauth_url }
21+
href={
22+
testMode
23+
? wc_stripe_settings_params.stripe_test_oauth_url
24+
: wc_stripe_settings_params.stripe_oauth_url
25+
}
2126
text={
22-
testMode ? 'Connect a test account' : 'Connect an account'
27+
testMode
28+
? __(
29+
'Create or connect a test account',
30+
'woocommerce-gateway-stripe'
31+
)
32+
: __(
33+
'Create or connect an account',
34+
'woocommerce-gateway-stripe'
35+
)
2336
}
2437
/>
2538
{ displayWebhookConfigure && (

includes/admin/class-wc-stripe-settings-controller.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ public function admin_scripts( $hook_suffix ) {
130130
);
131131

132132
$oauth_url = woocommerce_gateway_stripe()->connect->get_oauth_url();
133-
if ( is_wp_error( $oauth_url ) ) {
134-
$oauth_url = '';
135-
}
133+
$oauth_url = is_wp_error( $oauth_url ) ? '' : $oauth_url;
134+
135+
$test_oauth_url = woocommerce_gateway_stripe()->connect->get_oauth_url( '', 'test' );
136+
$test_oauth_url = is_wp_error( $test_oauth_url ) ? '' : $test_oauth_url;
136137

137138
$message = sprintf(
138139
/* translators: 1) Html strong opening tag 2) Html strong closing tag */
@@ -146,6 +147,7 @@ public function admin_scripts( $hook_suffix ) {
146147
'i18n_out_of_sync' => $message,
147148
'is_upe_checkout_enabled' => WC_Stripe_Feature_Flags::is_upe_checkout_enabled(),
148149
'stripe_oauth_url' => $oauth_url,
150+
'stripe_test_oauth_url' => $test_oauth_url,
149151
'show_customization_notice' => get_option( 'wc_stripe_show_customization_notice', 'yes' ) === 'yes' ? true : false,
150152
'is_test_mode' => $this->gateway->is_in_test_mode(),
151153
'plugin_version' => WC_STRIPE_VERSION,

includes/class-wc-gateway-stripe.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,20 +1122,6 @@ public function get_required_settings_keys() {
11221122
return [ 'publishable_key', 'secret_key' ];
11231123
}
11241124

1125-
/**
1126-
* Get the connection URL.
1127-
*
1128-
* @return string Connection URL.
1129-
*/
1130-
public function get_connection_url( $return_url = '' ) {
1131-
$api = new WC_Stripe_Connect_API();
1132-
$connect = new WC_Stripe_Connect( $api );
1133-
1134-
$url = $connect->get_oauth_url( $return_url );
1135-
1136-
return is_wp_error( $url ) ? null : $url;
1137-
}
1138-
11391125
/**
11401126
* Get help text to display during quick setup.
11411127
*

0 commit comments

Comments
 (0)