|
1 |
| -import {OnApproveActions, OnApproveData} from '@paypal/paypal-js/types/components/buttons'; |
2 |
| -import {OrderResponseBody, ShippingInfo} from '@paypal/paypal-js/types/apis/orders'; |
| 1 | +import {OnApproveData} from '@paypal/paypal-js/types/components/buttons'; |
3 | 2 | import {
|
4 |
| - callBillingAddressEndpoint, |
5 |
| - callGuestCustomerEndpoint, |
6 |
| - callShippingAddressEndpoint, |
7 |
| - getFirstAndLastName, |
8 | 3 | getTotals,
|
9 |
| - isObjectEquals |
10 | 4 | } from 'src/utils';
|
11 |
| -import {formatPaypalToApiAddress} from 'src/paypal/formatPaypalToApiAddress'; |
12 | 5 | import {
|
13 | 6 | addPayment,
|
14 | 7 | getCurrency,
|
15 | 8 | IAddPaymentRequest,
|
16 |
| - setTaxes, |
| 9 | + IWalletPayOnApproveRequest, |
| 10 | + walletPayOnApprove, |
17 | 11 | } from '@boldcommerce/checkout-frontend-library';
|
18 | 12 | import {API_RETRY} from 'src/types';
|
19 | 13 | import {getPaypalGatewayPublicId} from 'src/paypal/managePaypalState';
|
20 | 14 | import {orderProcessing, displayError} from 'src/actions';
|
21 | 15 |
|
22 |
| -export async function paypalOnApprove(data: OnApproveData, actions: OnApproveActions): Promise<void> { |
| 16 | +export async function paypalOnApprove(data: OnApproveData): Promise<void> { |
23 | 17 | const {iso_code: currencyCode} = getCurrency();
|
24 |
| - return actions.order?.get().then(async ({ id, payer, purchase_units }: OrderResponseBody) => { |
25 | 18 |
|
26 |
| - // extract all shipping info |
27 |
| - const { name, address: shippingAddress } = purchase_units[0].shipping as ShippingInfo; |
28 |
| - const shippingNames = getFirstAndLastName(name?.full_name); |
29 |
| - |
30 |
| - // extract all billing info |
31 |
| - const {name: payerName, address: billingAddress} = payer; |
32 |
| - const billingNames = {firstName: payerName?.given_name || '', lastName: payerName?.surname || ''}; |
33 |
| - const phone = payer.phone?.phone_number.national_number || ''; |
34 |
| - const email = payer.email_address || ''; |
35 |
| - const isBillingAddressFilled = ( |
36 |
| - !!billingAddress?.address_line_1 |
37 |
| - && !!billingAddress.admin_area_1 |
38 |
| - && !!billingAddress.admin_area_2 |
39 |
| - && !!billingAddress.country_code |
40 |
| - && !!billingAddress.postal_code |
41 |
| - ); |
42 |
| - |
43 |
| - // set customer |
44 |
| - const customerResult = await callGuestCustomerEndpoint(billingNames.firstName, billingNames.lastName, email); |
45 |
| - const success = customerResult.success; |
46 |
| - if(!success){ |
47 |
| - displayError('There was an unknown error while validating your customer information.', 'generic', 'unknown_error'); |
48 |
| - return; |
49 |
| - } |
50 |
| - |
51 |
| - // check if shipping and billing are the same |
52 |
| - const isSameNames = isObjectEquals(shippingNames, billingNames); |
53 |
| - const isSameAddress = isBillingAddressFilled && isObjectEquals(shippingAddress, billingAddress); |
54 |
| - const isBillingSame = isSameNames && isSameAddress; |
55 |
| - const formattedShippingAddress = formatPaypalToApiAddress(shippingAddress, shippingNames.firstName, shippingNames.lastName, phone); |
56 |
| - const formattedBillingAddress = formatPaypalToApiAddress(isBillingAddressFilled ? billingAddress : shippingAddress, billingNames.firstName, billingNames.lastName, phone); |
57 |
| - |
58 |
| - // check and update shipping address |
59 |
| - const shippingAddressResponse = await callShippingAddressEndpoint(formattedShippingAddress, true); |
60 |
| - if(!shippingAddressResponse.success){ |
61 |
| - displayError('There was an unknown error while validating your shipping address.', 'shipping', 'unknown_error'); |
62 |
| - return; |
63 |
| - } |
64 |
| - |
65 |
| - |
66 |
| - // check and update billing address |
67 |
| - const billingAddressToSet = isBillingSame ? formattedShippingAddress : formattedBillingAddress; |
68 |
| - const billingAddressResponse = await callBillingAddressEndpoint(billingAddressToSet, (!isBillingSame && isBillingAddressFilled)); |
69 |
| - if(!billingAddressResponse.success){ |
70 |
| - displayError('There was an unknown error while validating your billing address.', 'generic', 'unknown_error'); |
71 |
| - return; |
72 |
| - } |
73 |
| - |
74 |
| - |
75 |
| - // update taxes |
76 |
| - |
77 |
| - const taxResponse = await setTaxes(API_RETRY); |
78 |
| - if(!taxResponse.success){ |
79 |
| - displayError('There was an unknown error while calculating the taxes.', 'payment_gateway', 'no_tax'); |
80 |
| - return; |
81 |
| - } |
82 |
| - |
83 |
| - |
84 |
| - // add payment |
85 |
| - const totals = getTotals(); |
86 |
| - const payment: IAddPaymentRequest = { |
87 |
| - token: `${id}:${payer.payer_id}`, |
88 |
| - nonce: `${id}:${payer.payer_id}`, // TODO: Temporarily required - It is not in the API documentation, but required for Paypal Express |
89 |
| - gateway_public_id: getPaypalGatewayPublicId(), |
90 |
| - currency: currencyCode, |
91 |
| - amount: totals.totalAmountDue, |
92 |
| - wallet_pay_type: 'paypal', |
93 |
| - } as IAddPaymentRequest; |
94 |
| - const paymentResult = await addPayment(payment, API_RETRY); |
95 |
| - if(!paymentResult.success){ |
96 |
| - displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); |
97 |
| - return; |
| 19 | + const body: IWalletPayOnApproveRequest = { |
| 20 | + gateway_type: 'paypal', |
| 21 | + payment_data: { |
| 22 | + locale: navigator.language, |
| 23 | + paypal_order_id: data.orderID |
98 | 24 | }
|
| 25 | + }; |
| 26 | + |
| 27 | + const res = await walletPayOnApprove(body, API_RETRY); |
| 28 | + |
| 29 | + if (!res.success) { |
| 30 | + displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const totals = getTotals(); |
| 35 | + const payment: IAddPaymentRequest = { |
| 36 | + token: `${data.orderID}:${data.payerID}`, |
| 37 | + nonce: `${data.orderID}:${data.payerID}`, // TODO: Temporarily required - It is not in the API documentation, but required for Paypal Express |
| 38 | + gateway_public_id: getPaypalGatewayPublicId(), |
| 39 | + currency: currencyCode, |
| 40 | + amount: totals.totalAmountDue, |
| 41 | + wallet_pay_type: 'paypal', |
| 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(); |
99 | 51 |
|
100 |
| - // finalize order |
101 |
| - orderProcessing(); |
102 |
| - }); |
103 | 52 | }
|
0 commit comments