|
1 |
| -import {OnApproveActions, OnApproveData} from '@paypal/paypal-js/types/components/buttons'; |
2 |
| -import {OrderResponseBody, ShippingInfo} from '@paypal/paypal-js/types/apis/orders'; |
3 |
| -import {getFirstAndLastName, getPhoneNumber, getTotals,} from 'src/utils'; |
4 |
| -import {formatPaypalToApiAddress} from 'src/paypal/formatPaypalToApiAddress'; |
5 |
| -import {addPayment, apiTypeKeys, batchRequest, getCurrency, IAddPaymentRequest, IBatchableRequest, buildAddressBatchRequest, buildCustomerBatchRequest} from '@boldcommerce/checkout-frontend-library'; |
| 1 | +import {OnApproveData} from '@paypal/paypal-js/types/components/buttons'; |
| 2 | +import {getTotals,} from 'src/utils'; |
| 3 | +import { |
| 4 | + addPayment, |
| 5 | + getCurrency, |
| 6 | + IAddPaymentRequest, |
| 7 | + IWalletPayOnApproveRequest, walletPayOnApprove |
| 8 | +} from '@boldcommerce/checkout-frontend-library'; |
6 | 9 | import {API_RETRY} from 'src/types';
|
7 | 10 | import {getPaypalGatewayPublicId} from 'src/paypal/managePaypalState';
|
8 | 11 | import {orderProcessing, displayError} from 'src/actions';
|
9 | 12 |
|
10 |
| -export async function ppcpOnApprove(data: OnApproveData, actions: OnApproveActions): Promise<void> { |
| 13 | +export async function ppcpOnApprove(data: OnApproveData): Promise<void> { |
11 | 14 | const {iso_code: currencyCode} = getCurrency();
|
12 |
| - return actions.order?.get().then(async ({ payer, purchase_units, payment_source}: OrderResponseBody) => { |
13 | 15 |
|
14 |
| - // Build Batch Requests |
15 |
| - const requests: Array<IBatchableRequest> = []; |
16 |
| - |
17 |
| - // extract all shipping info |
18 |
| - const { name, address: shippingAddress } = purchase_units[0].shipping as ShippingInfo; |
19 |
| - const shippingNames = getFirstAndLastName(name?.full_name); |
20 |
| - |
21 |
| - // extract all billing info |
22 |
| - const {name: payerName, address: billingAddress} = payer; |
23 |
| - const billingNames = {firstName: payerName?.given_name || '', lastName: payerName?.surname || ''}; |
24 |
| - const phone = getPhoneNumber(payer.phone?.phone_number.national_number); |
25 |
| - const email = payer.email_address || ''; |
26 |
| - const isBillingAddressFilled = ( |
27 |
| - !!billingAddress?.address_line_1 |
28 |
| - && !!billingAddress.admin_area_1 |
29 |
| - && !!billingAddress.admin_area_2 |
30 |
| - && !!billingAddress.country_code |
31 |
| - && !!billingAddress.postal_code |
32 |
| - ); |
33 |
| - |
34 |
| - const formattedShippingAddress = formatPaypalToApiAddress(shippingAddress, shippingNames.firstName, shippingNames.lastName, phone); |
35 |
| - const formattedBillingAddress = formatPaypalToApiAddress(isBillingAddressFilled ? billingAddress : shippingAddress, billingNames.firstName, billingNames.lastName, phone); |
36 |
| - |
37 |
| - const shippingAddressRequest = buildAddressBatchRequest(formattedShippingAddress, 'shipping'); |
38 |
| - const billingAddressRequest = buildAddressBatchRequest(formattedBillingAddress, 'billing'); |
39 |
| - |
40 |
| - const customerRequest = buildCustomerBatchRequest(billingNames.firstName, billingNames.lastName, email); |
41 |
| - |
42 |
| - customerRequest && requests.push(customerRequest); |
43 |
| - shippingAddressRequest && requests.push(shippingAddressRequest); |
44 |
| - billingAddressRequest && requests.push(billingAddressRequest); |
45 |
| - requests.push({apiType: apiTypeKeys.setTaxes, payload: {}}); |
46 |
| - |
47 |
| - const batchResponse = await batchRequest(requests, API_RETRY); |
48 |
| - |
49 |
| - if (batchResponse.success) { |
50 |
| - // add payment |
51 |
| - const totals = getTotals(); |
52 |
| - const payment: IAddPaymentRequest = { |
53 |
| - token: data.orderID, |
54 |
| - gateway_public_id: getPaypalGatewayPublicId(), |
55 |
| - currency: currencyCode, |
56 |
| - amount: totals.totalAmountDue, |
57 |
| - wallet_pay_type: 'paypal', |
58 |
| - extra_payment_data: { |
59 |
| - orderId: data.orderID, |
60 |
| - facilitatorAccessToken: data.facilitatorAccessToken, |
61 |
| - payerId: data.payerID, |
62 |
| - paymentSource: payment_source, |
63 |
| - } |
64 |
| - } as IAddPaymentRequest; |
65 |
| - const paymentResult = await addPayment(payment, API_RETRY); |
66 |
| - if (!paymentResult.success) { |
67 |
| - displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); |
68 |
| - return; |
69 |
| - } |
70 |
| - // finalize order |
71 |
| - orderProcessing(); |
72 |
| - } else { |
73 |
| - displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); |
74 |
| - return; |
| 16 | + const body: IWalletPayOnApproveRequest = { |
| 17 | + gateway_type: 'paypal', |
| 18 | + payment_data: { |
| 19 | + locale: navigator.language, |
| 20 | + paypal_order_id: data.orderID |
| 21 | + } |
| 22 | + }; |
| 23 | + |
| 24 | + const res = await walletPayOnApprove(body, API_RETRY); |
| 25 | + |
| 26 | + if (!res.success) { |
| 27 | + displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const totals = getTotals(); |
| 32 | + const payment: IAddPaymentRequest = { |
| 33 | + token: data.orderID, |
| 34 | + gateway_public_id: getPaypalGatewayPublicId(), |
| 35 | + currency: currencyCode, |
| 36 | + amount: totals.totalAmountDue, |
| 37 | + extra_payment_data: { |
| 38 | + orderId: data.orderID, |
| 39 | + facilitatorAccessToken: data.facilitatorAccessToken, |
| 40 | + payerId: data.payerID, |
75 | 41 | }
|
76 |
| - }); |
| 42 | + } as IAddPaymentRequest; |
| 43 | + const paymentResult = await addPayment(payment, API_RETRY); |
| 44 | + if (!paymentResult.success) { |
| 45 | + displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // finalize order |
| 50 | + orderProcessing(); |
77 | 51 | }
|
0 commit comments