Skip to content

Commit 23fac9d

Browse files
authored
Account Keys Connection Status (#2273)
* Code cleanup: Remove repetitive code Before, each store-module was spread into actions, selectors, and resolvers. This created some repetition and would continue to create more repetition in the future, as more store-modules would be added. Now, the store-modules can be added into an array, which auto-handles the spreading into actions, selectors, and resolvers. * Code Cleanup: Capitalize the POST rest call * Add frontend functionality to test account keys connection status This adds the frontend UI that tests whether the publishable key and the secret key are valid. It does this by creating a token with the publishable key, using Stripe's library. Then retrieving the token with the secret key, using our REST API, which uses Stripe's client library. * Update tests to include newly added account-keys properties Account key's store-module's state has newly added properties: isTesting and isValid. It also includes newly added selectors. This commit accounts for these new properties. * Add frontend test to test account-keys connection status functionality * Add backend functionality to test account keys connection status This adds the backend functionality to determine if the secret key is valid. It does this by retrieving a token with a given secret key and token ID. * Update changelog: Add ability to test Stripe account keys' validity * Wrap account-key connection text on mobile Before, the connection text would be too wide and would make the modal footer's elements look scattered. This commit forces the connection text's width to shrink, on mobile. * Replace expanded REST API URL with NAMESPACE constant Before, the REST API URL contained the expanded version as opposed to using the NAMESPACE constant. This made it so that if the URL were to ever be updated, it would require multiple places to be updated, as well. * Remove unused code: useGetAccountKeysValidity * Reset account-key validity upon switching between live/test tabs Before, when account-keys were tested while on a certain tab and then the tab was switched, the account-keys' status would remain. This would be a bit confusing since the form is reset, once the user switches tabs. * Internationalize text for account-key connection status * Add notification for when incorrect account keys are entered Before, test account-keys could be entered in the "live" tab and vice- versa. This created inconsistent behavior. * Handle live account-keys status connection on insecure (HTTP) connection Live account-keys must use HTTPS connection. This commit handles the case where an insecure (HTTP) connection is used to test a live account- key. * Allow Stripe to handle secret-key validation when testing keys Removing backend validation to remove repetitive code and to allow Stripe to validate the secret-key, instead. * Allow connection status test for secret-keys that begin with 'rk' Before, restrictive secret keys were incorrectly considered invalid secret keys because they did not begin with the 'sk' prefix. Now, the 'rk' prefix is considered to be valid.
1 parent 457530f commit 23fac9d

20 files changed

+515
-55
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
= 6.1.0 - 2022-xx-xx =
44
* Tweak - Use the newly exposed LoadableMask component provided by WooCommerce Blocks to trigger the loading state for Payment Request Buttons.
55
* Add - Add filter call when updating an existent intent (wc_stripe_update_existing_intent_request).
6+
* Add - Add ability to test Stripe account keys' validity.
67

78
= 6.0.0 - 2022-01-05 =
89
* Fix - Fixed capitalization for payment method names: iDEAL, giropay, and Sofort.

client/data/account-keys/__tests__/actions.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe( 'Account keys actions tests', () => {
4141
const yielded = [ ...saveAccountKeys( accountKeysMock ) ];
4242

4343
expect( apiFetch ).toHaveBeenCalledWith( {
44-
method: 'post',
44+
method: 'POST',
4545
path: '/wc/v3/wc_stripe/account_keys',
4646
data: accountKeysMock,
4747
} );

client/data/account-keys/__tests__/hooks.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ describe( 'Account keys hooks tests', () => {
3232

3333
selectors = {
3434
getAccountKeys: jest.fn( () => ( { foo: 'bar' } ) ),
35+
getIsTestingAccountKeys: jest.fn(),
36+
getIsValidAccountKeys: jest.fn(),
3537
hasFinishedResolution: jest.fn(),
3638
isResolving: jest.fn(),
3739
isSavingAccountKeys: jest.fn(),

client/data/account-keys/__tests__/reducer.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ describe( 'Account keys reducer tests', () => {
77

88
expect( defaultState ).toEqual( {
99
isSaving: false,
10+
isTesting: false,
11+
isValid: null,
1012
data: {},
1113
savingError: null,
1214
} );

client/data/account-keys/action-types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ export default {
22
SET_ACCOUNT_KEYS: 'SET_ACCOUNT_KEYS',
33
SET_ACCOUNT_KEYS_VALUES: 'SET_ACCOUNT_KEYS_VALUES',
44
SET_IS_SAVING_ACCOUNT_KEYS: 'SET_IS_SAVING_ACCOUNT_KEYS',
5+
SET_IS_TESTING_ACCOUNT_KEYS: 'SET_IS_TESTING_ACCOUNT_KEYS',
6+
SET_IS_VALID_KEYS: 'SET_IS_VALID_KEYS',
57
};

client/data/account-keys/actions.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ export function updateIsSavingAccountKeys( isSaving, error ) {
2727
};
2828
}
2929

30+
export function updateIsTestingAccountKeys( isTesting ) {
31+
return {
32+
type: ACTION_TYPES.SET_IS_TESTING_ACCOUNT_KEYS,
33+
isTesting,
34+
};
35+
}
36+
37+
export function updateIsValidAccountKeys( isValid ) {
38+
return {
39+
type: ACTION_TYPES.SET_IS_VALID_KEYS,
40+
isValid,
41+
};
42+
}
43+
3044
export function* saveAccountKeys( accountKeys ) {
3145
const isDisconnecting =
3246
! accountKeys.publishable_key && ! accountKeys.test_publishable_key;
@@ -37,7 +51,7 @@ export function* saveAccountKeys( accountKeys ) {
3751

3852
const accountData = yield apiFetch( {
3953
path: `${ NAMESPACE }/account_keys`,
40-
method: 'post',
54+
method: 'POST',
4155
data: accountKeys,
4256
} );
4357

client/data/account-keys/hooks.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,31 @@ import { useCallback } from 'react';
33
import { STORE_NAME } from '../constants';
44

55
export const useAccountKeys = () => {
6-
const { saveAccountKeys, updateAccountKeys } = useDispatch( STORE_NAME );
6+
const {
7+
saveAccountKeys,
8+
updateAccountKeys,
9+
updateIsTestingAccountKeys,
10+
updateIsValidAccountKeys,
11+
} = useDispatch( STORE_NAME );
712

813
const accountKeys = useSelect( ( select ) => {
914
const { getAccountKeys } = select( STORE_NAME );
1015

1116
return getAccountKeys();
1217
}, [] );
1318

19+
const isTesting = useSelect( ( select ) => {
20+
const { getIsTestingAccountKeys } = select( STORE_NAME );
21+
22+
return getIsTestingAccountKeys();
23+
}, [] );
24+
25+
const isValid = useSelect( ( select ) => {
26+
const { getIsValidAccountKeys } = select( STORE_NAME );
27+
28+
return getIsValidAccountKeys();
29+
}, [] );
30+
1431
const isLoading = useSelect( ( select ) => {
1532
const { hasFinishedResolution, isResolving } = select( STORE_NAME );
1633

@@ -30,7 +47,11 @@ export const useAccountKeys = () => {
3047
accountKeys,
3148
isLoading,
3249
isSaving,
50+
isTesting,
51+
isValid,
3352
updateAccountKeys,
53+
updateIsTestingAccountKeys,
54+
updateIsValidAccountKeys,
3455
saveAccountKeys,
3556
};
3657
};

client/data/account-keys/reducer.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import ACTION_TYPES from './action-types';
22

33
const defaultState = {
4+
isTesting: false,
5+
isValid: null,
46
isSaving: false,
57
savingError: null,
68
data: {},
@@ -33,6 +35,18 @@ export const accountKeysReducer = (
3335
isSaving: action.isSaving,
3436
savingError: action.error,
3537
};
38+
39+
case ACTION_TYPES.SET_IS_TESTING_ACCOUNT_KEYS:
40+
return {
41+
...state,
42+
isTesting: action.isTesting,
43+
};
44+
45+
case ACTION_TYPES.SET_IS_VALID_KEYS:
46+
return {
47+
...state,
48+
isValid: action.isValid,
49+
};
3650
}
3751

3852
return state;

client/data/account-keys/selectors.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ export const isSavingAccountKeys = ( state ) => {
1919
export const getAccountKeysSavingError = ( state ) => {
2020
return getAccountKeysState( state ).savingError;
2121
};
22+
23+
export const getIsTestingAccountKeys = ( state ) => {
24+
return getAccountKeysState( state ).isTesting || false;
25+
};
26+
27+
export const getIsValidAccountKeys = ( state ) => {
28+
return getAccountKeysState( state ).isValid;
29+
};

client/data/account/__tests__/actions.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe( 'Account actions tests', () => {
3838

3939
expect( apiFetch ).toHaveBeenCalledWith( {
4040
path: '/wc/v3/wc_stripe/account/refresh',
41-
method: 'post',
41+
method: 'POST',
4242
} );
4343
expect( yielded ).toContainEqual(
4444
expect.objectContaining( {

0 commit comments

Comments
 (0)