-
Notifications
You must be signed in to change notification settings - Fork 126
/
manual.js
157 lines (141 loc) · 5.79 KB
/
manual.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
145
146
147
148
149
150
151
152
153
154
155
156
157
import AdyenCheckout from '@adyen/adyen-web';
import '@adyen/adyen-web/dist/es/adyen.css';
import { getPaymentMethods, makePayment, checkBalance, createOrder, cancelOrder, makeDetailsCall } from '../../services';
import { amount, shopperLocale, countryCode } from '../../config/commonConfig';
import { getSearchParameters } from '../../utils';
export async function initManual() {
const paymentMethodsResponse = await getPaymentMethods({ amount, shopperLocale });
window.checkout = await AdyenCheckout({
amount,
countryCode,
clientKey: process.env.__CLIENT_KEY__,
paymentMethodsResponse,
locale: shopperLocale,
environment: process.env.__CLIENT_ENV__,
installmentOptions: {
mc: {
values: [1, 2, 3, 4]
}
},
onSubmit: async (state, component) => {
const result = await makePayment(state.data);
// handle actions
if (result.action) {
// demo only - store paymentData & order
if (result.action.paymentData) localStorage.setItem('storedPaymentData', result.action.paymentData);
component.handleAction(result.action);
} else if (result.order && result.order?.remainingAmount?.value > 0) {
// handle orders
const order = {
orderData: result.order.orderData,
pspReference: result.order.pspReference
};
const orderPaymentMethods = await getPaymentMethods({ order, amount, shopperLocale });
checkout.update({ paymentMethodsResponse: orderPaymentMethods, order, amount: result.order.remainingAmount });
} else {
handleFinalState(result.resultCode, component);
}
},
onChange: state => {
console.log('onChange', state);
},
onAdditionalDetails: async (state, component) => {
const result = await makeDetailsCall(state.data);
if (result.action) {
component.handleAction(result.action);
} else if (result.order && result.order?.remainingAmount?.value > 0) {
// handle orders
const order = {
orderData: result.order.orderData,
pspReference: result.order.pspReference
};
const orderPaymentMethods = await getPaymentMethods({ order, amount, shopperLocale });
checkout.update({ paymentMethodsResponse: orderPaymentMethods, order, amount: result.order.remainingAmount });
} else {
handleFinalState(result.resultCode, component);
}
},
onBalanceCheck: async (resolve, reject, data) => {
resolve(await checkBalance(data));
},
onOrderRequest: async (resolve, reject) => {
resolve(await createOrder({ amount }));
},
onOrderCancel: async order => {
await cancelOrder(order);
checkout.update({ paymentMethodsResponse: await getPaymentMethods({ amount, shopperLocale }), order: null, amount });
},
onError: (error, component) => {
console.info(error.name, error.message, error.stack, component);
},
onActionHandled: rtnObj => {
console.log('onActionHandled', rtnObj);
},
paymentMethodsConfiguration: {
card: {
enableStoreDetails: false,
hasHolderName: true,
holderNameRequired: true
},
paywithgoogle: {
buttonType: 'plain'
}
}
});
function handleFinalState(resultCode, dropin) {
localStorage.removeItem('storedPaymentData');
if (resultCode === 'Authorised' || resultCode === 'Received') {
dropin.setStatus('success');
} else {
dropin.setStatus('error');
}
}
function handleRedirectResult() {
const storedPaymentData = localStorage.getItem('storedPaymentData');
const { amazonCheckoutSessionId, redirectResult, payload } = getSearchParameters(window.location.search);
if (redirectResult || payload) {
dropin.setStatus('loading');
return makeDetailsCall({
...(storedPaymentData && { paymentData: storedPaymentData }),
details: {
...(redirectResult && { redirectResult }),
...(payload && { payload })
}
}).then(result => {
if (result.action) {
dropin.handleAction(result.action);
} else {
handleFinalState(result.resultCode, dropin);
}
return true;
});
}
// Handle Amazon Pay redirect result
if (amazonCheckoutSessionId) {
window.amazonpay = checkout
.create('amazonpay', {
amazonCheckoutSessionId,
showOrderButton: false,
onSubmit: state => {
makePayment(state.data).then(result => {
if (result.action) {
dropin.handleAction(result.action);
} else {
handleFinalState(result.resultCode, dropin);
}
});
}
})
.mount('body');
window.amazonpay.submit();
}
return Promise.resolve(true);
}
const dropin = checkout
.create('dropin', {
instantPaymentTypes: ['googlepay']
})
.mount('#dropin-container');
handleRedirectResult();
return [checkout, dropin];
}