-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PPCP Wallet pay button implementation (#10)
* ppcp wallet pay button implementation --------- Co-authored-by: shagufa-ali <80478231+shagufa-ali@users.noreply.github.com>
- Loading branch information
1 parent
e9b6703
commit 665f4bc
Showing
10 changed files
with
186 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,51 @@ | ||
import {OnApproveActions, OnApproveData} from '@paypal/paypal-js/types/components/buttons'; | ||
import {OrderResponseBody, ShippingInfo} from '@paypal/paypal-js/types/apis/orders'; | ||
import {getFirstAndLastName, getPhoneNumber, getTotals,} from 'src/utils'; | ||
import {formatPaypalToApiAddress} from 'src/paypal/formatPaypalToApiAddress'; | ||
import {addPayment, apiTypeKeys, batchRequest, getCurrency, IAddPaymentRequest, IBatchableRequest, buildAddressBatchRequest, buildCustomerBatchRequest} from '@boldcommerce/checkout-frontend-library'; | ||
import {OnApproveData} from '@paypal/paypal-js/types/components/buttons'; | ||
import {getTotals,} from 'src/utils'; | ||
import { | ||
addPayment, | ||
getCurrency, | ||
IAddPaymentRequest, | ||
IWalletPayOnApproveRequest, walletPayOnApprove | ||
} from '@boldcommerce/checkout-frontend-library'; | ||
import {API_RETRY} from 'src/types'; | ||
import {getPaypalGatewayPublicId} from 'src/paypal/managePaypalState'; | ||
import {orderProcessing, displayError} from 'src/actions'; | ||
|
||
export async function ppcpOnApprove(data: OnApproveData, actions: OnApproveActions): Promise<void> { | ||
export async function ppcpOnApprove(data: OnApproveData): Promise<void> { | ||
const {iso_code: currencyCode} = getCurrency(); | ||
return actions.order?.get().then(async ({ payer, purchase_units, payment_source}: OrderResponseBody) => { | ||
|
||
// Build Batch Requests | ||
const requests: Array<IBatchableRequest> = []; | ||
|
||
// extract all shipping info | ||
const { name, address: shippingAddress } = purchase_units[0].shipping as ShippingInfo; | ||
const shippingNames = getFirstAndLastName(name?.full_name); | ||
|
||
// extract all billing info | ||
const {name: payerName, address: billingAddress} = payer; | ||
const billingNames = {firstName: payerName?.given_name || '', lastName: payerName?.surname || ''}; | ||
const phone = getPhoneNumber(payer.phone?.phone_number.national_number); | ||
const email = payer.email_address || ''; | ||
const isBillingAddressFilled = ( | ||
!!billingAddress?.address_line_1 | ||
&& !!billingAddress.admin_area_1 | ||
&& !!billingAddress.admin_area_2 | ||
&& !!billingAddress.country_code | ||
&& !!billingAddress.postal_code | ||
); | ||
|
||
const formattedShippingAddress = formatPaypalToApiAddress(shippingAddress, shippingNames.firstName, shippingNames.lastName, phone); | ||
const formattedBillingAddress = formatPaypalToApiAddress(isBillingAddressFilled ? billingAddress : shippingAddress, billingNames.firstName, billingNames.lastName, phone); | ||
|
||
const shippingAddressRequest = buildAddressBatchRequest(formattedShippingAddress, 'shipping'); | ||
const billingAddressRequest = buildAddressBatchRequest(formattedBillingAddress, 'billing'); | ||
|
||
const customerRequest = buildCustomerBatchRequest(billingNames.firstName, billingNames.lastName, email); | ||
|
||
customerRequest && requests.push(customerRequest); | ||
shippingAddressRequest && requests.push(shippingAddressRequest); | ||
billingAddressRequest && requests.push(billingAddressRequest); | ||
requests.push({apiType: apiTypeKeys.setTaxes, payload: {}}); | ||
|
||
const batchResponse = await batchRequest(requests, API_RETRY); | ||
|
||
if (batchResponse.success) { | ||
// add payment | ||
const totals = getTotals(); | ||
const payment: IAddPaymentRequest = { | ||
token: data.orderID, | ||
gateway_public_id: getPaypalGatewayPublicId(), | ||
currency: currencyCode, | ||
amount: totals.totalAmountDue, | ||
wallet_pay_type: 'paypal', | ||
extra_payment_data: { | ||
orderId: data.orderID, | ||
facilitatorAccessToken: data.facilitatorAccessToken, | ||
payerId: data.payerID, | ||
paymentSource: payment_source, | ||
} | ||
} as IAddPaymentRequest; | ||
const paymentResult = await addPayment(payment, API_RETRY); | ||
if (!paymentResult.success) { | ||
displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); | ||
return; | ||
} | ||
// finalize order | ||
orderProcessing(); | ||
} else { | ||
displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); | ||
return; | ||
const body: IWalletPayOnApproveRequest = { | ||
gateway_type: 'paypal', | ||
payment_data: { | ||
locale: navigator.language, | ||
paypal_order_id: data.orderID | ||
} | ||
}; | ||
|
||
const res = await walletPayOnApprove(body, API_RETRY); | ||
|
||
if (!res.success) { | ||
displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); | ||
return; | ||
} | ||
|
||
const totals = getTotals(); | ||
const payment: IAddPaymentRequest = { | ||
token: data.orderID, | ||
gateway_public_id: getPaypalGatewayPublicId(), | ||
currency: currencyCode, | ||
amount: totals.totalAmountDue, | ||
extra_payment_data: { | ||
orderId: data.orderID, | ||
facilitatorAccessToken: data.facilitatorAccessToken, | ||
payerId: data.payerID, | ||
} | ||
}); | ||
} as IAddPaymentRequest; | ||
const paymentResult = await addPayment(payment, API_RETRY); | ||
if (!paymentResult.success) { | ||
displayError('There was an unknown error while processing your payment.', 'payment_gateway', 'unknown_error'); | ||
return; | ||
} | ||
|
||
// finalize order | ||
orderProcessing(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {OnShippingChangeActions, OnShippingChangeData} from '@paypal/paypal-js/types/components/buttons'; | ||
import { | ||
IWalletPayOnShippingRequest, | ||
walletPayOnShipping | ||
} from '@boldcommerce/checkout-frontend-library'; | ||
import {API_RETRY, displayError,} from 'src'; | ||
|
||
|
||
export async function ppcpOnShippingChange(data: OnShippingChangeData, actions: OnShippingChangeActions): Promise<void> { | ||
|
||
const body: IWalletPayOnShippingRequest = { | ||
gateway_type: 'paypal', | ||
payment_data: { | ||
locale: navigator.language, | ||
paypal_order_id: data.orderID, | ||
shipping_address: data.shipping_address, | ||
shipping_options: data.selected_shipping_option, | ||
} | ||
}; | ||
|
||
const res = await walletPayOnShipping(body, API_RETRY); | ||
if (!res.success) { | ||
displayError('There was an unknown error while getting the shipping details.', 'payment_gateway', 'unknown_error'); | ||
return actions.reject(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.