Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 3dc721b

Browse files
Alex Floriscagithub-actions[bot]Tarun Vijwani
authored
Move paymentResult to the payment store (#7692)
* Move paymentResult to the payment store * bot: update checkstyle.xml * Update docs * Fix typerror * bot: update checkstyle.xml * bot: update checkstyle.xml Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tarun Vijwani <[email protected]>
1 parent 3448b3c commit 3dc721b

File tree

17 files changed

+139
-129
lines changed

17 files changed

+139
-129
lines changed

assets/js/base/context/providers/cart-checkout/checkout-events/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ export const CheckoutEventsProvider = ( {
197197
checkoutState.orderId,
198198
checkoutState.customerId,
199199
checkoutState.orderNotes,
200-
checkoutState.paymentResult,
201200
previousStatus,
202201
previousHasError,
203202
createErrorNotice,

assets/js/data/checkout/action-types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export const ACTION_TYPES = {
44
SET_COMPLETE: 'SET_CHECKOUT_COMPLETE',
55
SET_BEFORE_PROCESSING: 'SET_BEFORE_PROCESSING',
66
SET_AFTER_PROCESSING: 'SET_AFTER_PROCESSING',
7-
SET_PAYMENT_RESULT: 'SET_PAYMENT_RESULT',
87
SET_PROCESSING: 'SET_CHECKOUT_IS_PROCESSING',
98
SET_HAS_ERROR: 'SET_CHECKOUT_HAS_ERROR',
109
SET_CUSTOMER_ID: 'SET_CHECKOUT_CUSTOMER_ID',

assets/js/data/checkout/actions.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/**
2-
* External dependencies
3-
*/
4-
import { PaymentResult } from '@woocommerce/types';
5-
61
/**
72
* Internal dependencies
83
*/
@@ -60,16 +55,6 @@ export const __internalSetRedirectUrl = ( redirectUrl: string ) => ( {
6055
redirectUrl,
6156
} );
6257

63-
/**
64-
* Store the result of the payment attempt from the /checkout StoreApi call
65-
*
66-
* @param data The result of the payment attempt through the StoreApi /checkout endpoints
67-
*/
68-
export const __internalSetPaymentResult = ( data: PaymentResult ) => ( {
69-
type: types.SET_PAYMENT_RESULT,
70-
data,
71-
} );
72-
7358
/**
7459
* Set whether the checkout has an error or not
7560
*
@@ -157,7 +142,6 @@ export type CheckoutAction =
157142
| typeof __internalSetIdle
158143
| typeof __internalSetComplete
159144
| typeof __internalSetProcessing
160-
| typeof __internalSetPaymentResult
161145
| typeof __internalSetBeforeProcessing
162146
| typeof __internalSetAfterProcessing
163147
| typeof __internalSetRedirectUrl

assets/js/data/checkout/default-state.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* External dependencies
33
*/
44
import { isSameAddress } from '@woocommerce/base-utils';
5-
import { PaymentResult } from '@woocommerce/types';
65

76
/**
87
* Internal dependencies
@@ -14,8 +13,6 @@ export type CheckoutState = {
1413
status: STATUS;
1514
// If any of the totals, taxes, shipping, etc need to be calculated, the count will be increased here
1615
calculatingCount: number;
17-
// The result of the payment processing
18-
paymentResult: PaymentResult | null;
1916
// True when the checkout is in an error state. Whatever caused the error (validation/payment method) will likely have triggered a notice.
2017
hasError: boolean;
2118
// This is the url that checkout will redirect to when it's ready.
@@ -47,6 +44,5 @@ export const defaultState: CheckoutState = {
4744
checkoutData.shipping_address
4845
),
4946
shouldCreateAccount: false,
50-
paymentResult: null,
5147
extensionData: {},
5248
};

assets/js/data/checkout/reducers.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/**
2-
* External dependencies
3-
*/
4-
import { PaymentResult } from '@woocommerce/types';
5-
61
/**
72
* Internal dependencies
83
*/
@@ -35,13 +30,6 @@ const reducer = ( state = defaultState, action: CheckoutAction ) => {
3530
: state;
3631
break;
3732

38-
case types.SET_PAYMENT_RESULT:
39-
newState = {
40-
...state,
41-
paymentResult: action.data as PaymentResult,
42-
};
43-
break;
44-
4533
case types.SET_COMPLETE:
4634
newState = {
4735
...state,

assets/js/data/checkout/test/reducer.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,6 @@ describe.only( 'Checkout Store Reducer', () => {
3737
).toEqual( expectedState );
3838
} );
3939

40-
it( 'should handle SET_PAYMENT_RESULT', () => {
41-
const mockResponse = {
42-
message: 'success',
43-
redirectUrl: 'https://example.com',
44-
paymentStatus: 'not set' as const,
45-
paymentDetails: {},
46-
};
47-
48-
const expectedState = {
49-
...defaultState,
50-
status: STATUS.IDLE,
51-
paymentResult: mockResponse,
52-
};
53-
54-
expect(
55-
reducer(
56-
defaultState,
57-
actions.__internalSetPaymentResult( mockResponse )
58-
)
59-
).toEqual( expectedState );
60-
} );
61-
6240
it( 'should handle SET_COMPLETE', () => {
6341
const expectedState = {
6442
...defaultState,

assets/js/data/checkout/thunks.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
*/
44
import type { CheckoutResponse } from '@woocommerce/types';
55
import { store as noticesStore } from '@wordpress/notices';
6+
import { dispatch as wpDispatch, select as wpSelect } from '@wordpress/data';
67

78
/**
89
* Internal dependencies
910
*/
11+
import { STORE_KEY as PAYMENT_STORE_KEY } from '../payment/constants';
1012
import { removeNoticesByStatus } from '../../utils/notices';
1113
import {
1214
getPaymentResultFromCheckoutResponse,
@@ -40,7 +42,11 @@ export const __internalProcessCheckoutResponse = (
4042
} ) => {
4143
const paymentResult = getPaymentResultFromCheckoutResponse( response );
4244
dispatch.__internalSetRedirectUrl( paymentResult?.redirectUrl || '' );
43-
dispatch.__internalSetPaymentResult( paymentResult );
45+
// The local `dispatch` here is bound to the actions of the data store. We need to use the global dispatch here
46+
// to dispatch an action on a different store.
47+
wpDispatch( PAYMENT_STORE_KEY ).__internalSetPaymentResult(
48+
paymentResult
49+
);
4450
dispatch.__internalSetAfterProcessing();
4551
};
4652
};
@@ -90,15 +96,16 @@ export const __internalEmitAfterProcessingEvents: emitAfterProcessingEventsType
9096
( { observers, notices } ) => {
9197
return ( { select, dispatch, registry } ) => {
9298
const { createErrorNotice } = registry.dispatch( noticesStore );
93-
const state = select.getCheckoutState();
99+
const checkoutState = select.getCheckoutState();
94100
const data = {
95-
redirectUrl: state.redirectUrl,
96-
orderId: state.orderId,
97-
customerId: state.customerId,
98-
orderNotes: state.orderNotes,
99-
processingResponse: state.paymentResult,
101+
redirectUrl: checkoutState.redirectUrl,
102+
orderId: checkoutState.orderId,
103+
customerId: checkoutState.customerId,
104+
orderNotes: checkoutState.orderNotes,
105+
processingResponse:
106+
wpSelect( PAYMENT_STORE_KEY ).getPaymentResult(),
100107
};
101-
if ( state.hasError ) {
108+
if ( checkoutState.hasError ) {
102109
// allow payment methods or other things to customize the error
103110
// with a fallback if nothing customizes it.
104111
emitEventWithAbort(

assets/js/data/checkout/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { DataRegistry } from '@wordpress/data';
99
*/
1010
import type { EventObserversType } from '../../base/context/event-emit/types';
1111
import type { CheckoutState } from './default-state';
12+
import type { PaymentState } from '../payment/default-state';
1213
import type { DispatchFromMap, SelectFromMap } from '../mapped-types';
1314
import * as selectors from './selectors';
1415
import * as actions from './actions';
@@ -19,7 +20,7 @@ export type CheckoutAfterProcessingWithErrorEventData = {
1920
orderId: CheckoutState[ 'orderId' ];
2021
customerId: CheckoutState[ 'customerId' ];
2122
orderNotes: CheckoutState[ 'orderNotes' ];
22-
processingResponse: CheckoutState[ 'paymentResult' ];
23+
processingResponse: PaymentState[ 'paymentResult' ];
2324
};
2425
export type CheckoutAndPaymentNotices = {
2526
checkoutNotices: Notice[];

assets/js/data/payment/action-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ export enum ACTION_TYPES {
1515
REMOVE_AVAILABLE_EXPRESS_PAYMENT_METHOD = 'REMOVE_AVAILABLE_EXPRESS_PAYMENT_METHOD',
1616
INITIALIZE_PAYMENT_METHODS = 'INITIALIZE_PAYMENT_METHODS',
1717
SET_PAYMENT_METHOD_DATA = 'SET_PAYMENT_METHOD_DATA',
18+
SET_PAYMENT_RESULT = 'SET_PAYMENT_RESULT',
1819
}

assets/js/data/payment/actions.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
PlainPaymentMethods,
66
PlainExpressPaymentMethods,
77
} from '@woocommerce/type-defs/payments';
8+
import type { PaymentResult } from '@woocommerce/types';
89

910
/**
1011
* Internal dependencies
@@ -112,6 +113,16 @@ export const __internalSetPaymentMethodData = (
112113
paymentMethodData,
113114
} );
114115

116+
/**
117+
* Store the result of the payment attempt from the /checkout StoreApi call
118+
*
119+
* @param data The result of the payment attempt through the StoreApi /checkout endpoints
120+
*/
121+
export const __internalSetPaymentResult = ( data: PaymentResult ) => ( {
122+
type: ACTION_TYPES.SET_PAYMENT_RESULT,
123+
data,
124+
} );
125+
115126
/**
116127
* Set the available payment methods.
117128
* An available payment method is one that has been validated and can make a payment.

0 commit comments

Comments
 (0)