-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathevent-handlers.js
144 lines (129 loc) · 3.36 KB
/
event-handlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Internal dependencies
*/
import {
getErrorMessageFromNotice,
normalizeOrderData,
normalizePayForOrderData,
normalizeShippingAddress,
normalizeLineItems,
getExpressCheckoutData,
} from './utils';
import {
trackExpressCheckoutButtonClick,
trackExpressCheckoutButtonLoad,
} from './tracking';
import { __ } from '@wordpress/i18n';
export const shippingAddressChangeHandler = async ( api, event, elements ) => {
try {
const response = await api.expressCheckoutECECalculateShippingOptions(
normalizeShippingAddress( event.address )
);
if ( response.result === 'success' ) {
elements.update( {
amount: response.total.amount,
} );
event.resolve( {
shippingRates: response.shipping_options,
lineItems: normalizeLineItems( response.displayItems ),
} );
} else {
event.reject();
}
} catch ( e ) {
event.reject();
}
};
export const shippingRateChangeHandler = async ( api, event, elements ) => {
try {
const response = await api.paymentRequestUpdateShippingDetails(
event.shippingRate
);
if ( response.result === 'success' ) {
elements.update( { amount: response.total.amount } );
event.resolve( {
lineItems: normalizeLineItems( response.displayItems ),
} );
} else {
event.reject();
}
} catch ( e ) {
event.reject();
}
};
export const onConfirmHandler = async (
api,
stripe,
elements,
completePayment,
abortPayment,
event,
order = 0 // Order ID for the pay for order flow.
) => {
const { error: submitError } = await elements.submit();
if ( submitError ) {
return abortPayment( event, submitError.message );
}
const { paymentMethod, error } = await stripe.createPaymentMethod( {
elements,
} );
if ( error ) {
return abortPayment( event, error.message );
}
try {
// Kick off checkout processing step.
let orderResponse;
if ( ! order ) {
orderResponse = await api.expressCheckoutECECreateOrder(
normalizeOrderData( event, paymentMethod.id )
);
} else {
orderResponse = await api.expressCheckoutECEPayForOrder(
order,
normalizePayForOrderData( event, paymentMethod.id )
);
}
if ( orderResponse.result !== 'success' ) {
return abortPayment(
event,
getErrorMessageFromNotice( orderResponse.messages )
);
}
const confirmationRequest = api.confirmIntent( orderResponse.redirect );
// `true` means there is no intent to confirm.
if ( confirmationRequest === true ) {
completePayment( orderResponse.redirect );
} else {
const redirectUrl = await confirmationRequest;
completePayment( redirectUrl );
}
} catch ( e ) {
return abortPayment(
event,
e.message ??
__(
'There was a problem processing the order.',
'woocommerce-payments'
)
);
}
};
export const onReadyHandler = async function ( { availablePaymentMethods } ) {
if ( availablePaymentMethods ) {
const enabledMethods = Object.entries( availablePaymentMethods )
// eslint-disable-next-line no-unused-vars
.filter( ( [ _, isEnabled ] ) => isEnabled )
// eslint-disable-next-line no-unused-vars
.map( ( [ methodName, _ ] ) => methodName );
trackExpressCheckoutButtonLoad( {
paymentMethods: enabledMethods,
source: getExpressCheckoutData( 'button_context' ),
} );
}
};
export const onClickHandler = async function ( { expressPaymentType } ) {
trackExpressCheckoutButtonClick(
expressPaymentType,
getExpressCheckoutData( 'button_context' )
);
};