Skip to content

Commit

Permalink
Update estimate calls to wallet pay with rsa enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
zliu7878 committed Dec 21, 2023
1 parent 0baf119 commit 0cb5cd1
Show file tree
Hide file tree
Showing 14 changed files with 593 additions and 148 deletions.
68 changes: 41 additions & 27 deletions src/braintree/apple/braintreeOnShippingContactSelectedApple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
getValueByCurrency,
} from 'src';
import {
estimateShippingLines,
estimateTaxes,
getCurrency,
getOrderInitialData,
getShipping,
getShippingLines,
setTaxes
Expand All @@ -20,6 +23,8 @@ export async function braintreeOnShippingContactSelectedApple(event: ApplePayShi
const applePaySession = getBraintreeApplePaySessionChecked();
const shippingAddress = event.shippingContact;
const address = formatApplePayContactToCheckoutAddress(shippingAddress, true);
const {general_settings} = getOrderInitialData();
const rsaEnabled = general_settings.checkout_process.rsa_enabled;

const fail = () => {
const {totalAmountDue} = getTotals();
Expand All @@ -29,37 +34,46 @@ export async function braintreeOnShippingContactSelectedApple(event: ApplePayShi
});
};

const response = await callShippingAddressEndpoint(address, false);

if(response.success){
const shippingLinesResponse = await getShippingLines(API_RETRY);
const taxResponse = await setTaxes(API_RETRY);

if(shippingLinesResponse.success && taxResponse.success){
const {totalAmountDue} = getTotals();
const displayItems = getPaymentRequestDisplayItems().map(
({label, amount}) => ({
label,
amount: getValueByCurrency(amount, currencyCode),
}));
const {available_shipping_lines: shippingLines} = getShipping();
let responseSuccess = false;
if (rsaEnabled) {
const estimateShippingResponse = await estimateShippingLines(address, API_RETRY);
const estimateTaxResponse = await estimateTaxes(address, API_RETRY);
if (estimateShippingResponse.success && estimateTaxResponse.success) {
responseSuccess = true;
}
} else {
const shippingResponse = await callShippingAddressEndpoint(address, false);
if (shippingResponse.success) {
const shippingLinesResponse = await getShippingLines(API_RETRY);
const taxResponse = await setTaxes(API_RETRY);
if (shippingLinesResponse.success && taxResponse.success) {
responseSuccess = true;
}
}
}

const shippingOptions = shippingLines.map(p => ({
label: p.description,
detail: '',
amount: getValueByCurrency(p.amount, currencyCode),
identifier: p.id
if (responseSuccess) {
const {totalAmountDue} = getTotals();
const displayItems = getPaymentRequestDisplayItems().map(
({label, amount}) => ({
label,
amount: getValueByCurrency(amount, currencyCode),
}));
const {available_shipping_lines: shippingLines} = getShipping();

applePaySession.completeShippingContactSelection({
newLineItems: displayItems,
newShippingMethods: shippingOptions,
newTotal: {label: 'Total', amount: getValueByCurrency(totalAmountDue, currencyCode)},
});
const shippingOptions = shippingLines.map(p => ({
label: p.description,
detail: '',
amount: getValueByCurrency(p.amount, currencyCode),
identifier: p.id
}));

applePaySession.completeShippingContactSelection({
newLineItems: displayItems,
newShippingMethods: shippingOptions,
newTotal: {label: 'Total', amount: getValueByCurrency(totalAmountDue, currencyCode)},
});

} else {
fail();
}
} else {
fail();
}
Expand Down
15 changes: 13 additions & 2 deletions src/braintree/apple/braintreeOnShippingMethodSelectedApple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import {
import {
changeShippingLine,
getCurrency,
getOrderInitialData,
getShipping,
getShippingLines,
setTaxes
setTaxes,
getShippingAddress,
estimateTaxes
} from '@boldcommerce/checkout-frontend-library';
import ApplePayLineItem = ApplePayJS.ApplePayLineItem;
import ApplePayShippingMethodSelectedEvent = ApplePayJS.ApplePayShippingMethodSelectedEvent;
Expand All @@ -21,13 +24,21 @@ export async function braintreeOnShippingMethodSelectedApple(event: ApplePayShip
const {available_shipping_lines: shippingLines} = getShipping();
const selectedShippingMethod = event.shippingMethod;
const option = shippingLines.find(line => line.id === selectedShippingMethod.identifier);
const {general_settings} = getOrderInitialData();
const rsaEnabled = general_settings.checkout_process.rsa_enabled;
const address = getShippingAddress();

if (option) {
const response = await changeShippingLine(option.id, API_RETRY);

if (response.success) {
const shippingLinesResponse = await getShippingLines(API_RETRY);
const taxResponse = await setTaxes(API_RETRY);
let taxResponse;
if (rsaEnabled) {
taxResponse = await estimateTaxes(address, API_RETRY);
} else {
taxResponse = await setTaxes(API_RETRY);
}

if (shippingLinesResponse.success && taxResponse.success) {
const {totalAmountDue} = getTotals();
Expand Down
23 changes: 18 additions & 5 deletions src/braintree/google/braintreeOnPaymentDataChangeGoogle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import {callShippingAddressEndpoint, getTotals, getValueByCurrency} from 'src/ut
import {
changeShippingLine,
getCurrency,
getOrderInitialData,
getShipping,
getShippingLines,
setTaxes
setTaxes,
estimateShippingLines,
estimateTaxes,
} from '@boldcommerce/checkout-frontend-library';
import {API_RETRY} from 'src/types';
import CallbackIntent = google.payments.api.CallbackIntent;
Expand All @@ -18,13 +21,18 @@ export async function braintreeOnPaymentDataChangeGoogle(intermediatePaymentData
const {callbackTrigger, shippingAddress, shippingOptionData} = intermediatePaymentData;
const paymentDataRequestUpdate: PaymentDataRequestUpdate = {};
const intent = callbackTrigger === braintreeConstants.GOOGLEPAY_TRIGGER_INITIALIZE ? braintreeConstants.GOOGLEPAY_INTENT_SHIPPING_ADDRESS : callbackTrigger as CallbackIntent;
const {general_settings} = getOrderInitialData();
const rsaEnabled = general_settings.checkout_process.rsa_enabled;

switch (callbackTrigger) {
case braintreeConstants.GOOGLEPAY_TRIGGER_INITIALIZE:
case braintreeConstants.GOOGLEPAY_INTENT_SHIPPING_OPTION:
case braintreeConstants.GOOGLEPAY_INTENT_SHIPPING_ADDRESS: {
if (shippingAddress) {
const address = formatBraintreeShippingAddressGoogle(shippingAddress, true);
let shippingLinesResponse;
const address = formatBraintreeShippingAddressGoogle(shippingAddress, true);
if (rsaEnabled) {
shippingLinesResponse = await estimateShippingLines(address, API_RETRY);
} else {
const shippingAddressResponse = await callShippingAddressEndpoint(address, false);
if (!shippingAddressResponse.success) {
paymentDataRequestUpdate.error = {
Expand All @@ -34,8 +42,9 @@ export async function braintreeOnPaymentDataChangeGoogle(intermediatePaymentData
};
return paymentDataRequestUpdate;
}
shippingLinesResponse = await getShippingLines(API_RETRY);
}
const shippingLinesResponse = await getShippingLines(API_RETRY);

const {selected_shipping: selectedShipping, available_shipping_lines: shippingLines} = getShipping();
if (shippingLinesResponse.success) {
if (shippingOptionData) {
Expand All @@ -47,7 +56,11 @@ export async function braintreeOnPaymentDataChangeGoogle(intermediatePaymentData
await getShippingLines(API_RETRY);
}

await setTaxes(API_RETRY);
if (rsaEnabled) {
await estimateTaxes(address, API_RETRY);
} else {
await setTaxes(API_RETRY);
}

const {iso_code: currencyCode} = getCurrency();
const {totalAmountDue} = getTotals();
Expand Down
52 changes: 36 additions & 16 deletions src/paypal/paypalOnShippingChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
} from 'src';
import {
changeShippingLine,
estimateShippingLines,
estimateTaxes,
getOrderInitialData,
getShipping,
getShippingLines,
setTaxes,
Expand All @@ -20,31 +23,48 @@ export async function paypalOnShippingChange(data: OnShippingChangeData, actions
const {shipping_address: address, selected_shipping_option: selectedOption} = data;
const {reject, order: {patch: patch}} = actions;
const {MAX_STRING_LENGTH: maxStringSize} = paypalConstants;
const {general_settings} = getOrderInitialData();
const rsaEnabled = general_settings.checkout_process.rsa_enabled;

if (address) {
const formattedAddress = formatPaypalToApiAddress(address, undefined, undefined , getPhoneNumber());
const success = true;

const shippingAddressResponse = await callShippingAddressEndpoint(formattedAddress, false);
if (!shippingAddressResponse.success) {
return reject();
let success = false;
if (rsaEnabled) {
const shippingLinesResponse = await estimateShippingLines(formattedAddress, API_RETRY);
if (shippingLinesResponse.success) {
success = true;
}
} else {
const shippingAddressResponse = await callShippingAddressEndpoint(formattedAddress, false);
if (!shippingAddressResponse.success) {
return reject();
}
const shippingLinesResponse = await getShippingLines(API_RETRY);
if (shippingLinesResponse.success) {
success = true;
}
}

if(success) {
const shippingLinesResponse = await getShippingLines(API_RETRY);

if (success) {
const {selected_shipping: selectedShipping, available_shipping_lines: shippingLines} = getShipping();
if (shippingLinesResponse.success) {
if (selectedOption) {
const option = shippingLines.find(line => isSimilarStrings(line.description.substring(0, maxStringSize), selectedOption.label));
option && await changeShippingLine(option.id, API_RETRY);
} else if (!selectedShipping && shippingLines.length > 0) {
await changeShippingLine(shippingLines[0].id, API_RETRY);
}
await getShippingLines(API_RETRY);
if (selectedOption) {
const option = shippingLines.find(line => isSimilarStrings(line.description.substring(0, maxStringSize), selectedOption.label));
option && await changeShippingLine(option.id, API_RETRY);
} else if (!selectedShipping && shippingLines.length > 0) {
await changeShippingLine(shippingLines[0].id, API_RETRY);
}
await getShippingLines(API_RETRY);
}
}
const taxResponse = await setTaxes(API_RETRY);

let taxResponse;
if (rsaEnabled && address) {
const formattedAddress = formatPaypalToApiAddress(address, undefined, undefined , getPhoneNumber());
taxResponse = await estimateTaxes(formattedAddress, API_RETRY);
} else {
taxResponse = await setTaxes(API_RETRY);
}

if (taxResponse.success) {
const patchOperations = getPaypalPatchOperations(!!selectedOption);
Expand Down
33 changes: 19 additions & 14 deletions src/stripe/addStripePayment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import {
getOrderInitialData,
IAddPaymentRequest,
setBillingAddress,
updateShippingAddress
setTaxes,
} from '@boldcommerce/checkout-frontend-library';
import {
formatStripeBillingAddress,
callGuestCustomerEndpoint,
getFirstAndLastName,
orderProcessing,
formatStripeShippingAddress,
getTotals
getTotals,
callShippingAddressEndpoint
} from 'src';

export async function addStripePayment(event: IStripePaymentEvent, stripeGatewayId: string): Promise<void> {
Expand All @@ -32,22 +33,26 @@ export async function addStripePayment(event: IStripePaymentEvent, stripeGateway
shippingPhoneNumber = event.payerPhone;
}
const address = formatStripeShippingAddress(shippingAddress, shippingPhoneNumber);
await updateShippingAddress(address, API_RETRY).then(async (shippingResult) => {
await callShippingAddressEndpoint(address, true).then(async (shippingResult) => {
if (shippingResult.success) {
const billingAddress = formatStripeBillingAddress(card, event.payerName, event.payerPhone);
await setBillingAddress(billingAddress, API_RETRY).then(async (billingResult) => {
if (billingResult.success) {
const walletPayType = window.ApplePaySession && ApplePaySession.canMakePayments() ? 'applepay' : 'paywithgoogle';
const payment: IAddPaymentRequest = {
token: token.id,
gateway_public_id: stripeGatewayId,
currency: card.currency,
amount: totalAmountDue,
wallet_pay_type: walletPayType,
};
await addPayment(payment, API_RETRY).then(async (paymentResult) => {
if (paymentResult.success) {
success = true;
await setTaxes(API_RETRY).then(async (taxResult) => {
if (taxResult.success) {
const walletPayType = window.ApplePaySession && ApplePaySession.canMakePayments() ? 'applepay' : 'paywithgoogle';
const payment: IAddPaymentRequest = {
token: token.id,
gateway_public_id: stripeGatewayId,
currency: card.currency,
amount: totalAmountDue,
wallet_pay_type: walletPayType,
};
await addPayment(payment, API_RETRY).then(async (paymentResult) => {
if (paymentResult.success) {
success = true;
}
});
}
});
}
Expand Down
48 changes: 36 additions & 12 deletions src/stripe/changeStripeShippingLines.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
import {changeShippingLine} from '@boldcommerce/checkout-frontend-library';
import {
changeShippingLine,
estimateTaxes,
getOrderInitialData,
getShippingAddress,
getShippingLines,
setTaxes
} from '@boldcommerce/checkout-frontend-library';
import {API_RETRY, IStripeEvent, IStripeShippingOptions, getPaymentRequestDisplayItems, getTotals} from 'src';

export async function changeStripeShippingLines(event: IStripeEvent): Promise<void> {
const shippingOption = event.shippingOption as IStripeShippingOptions;
const response = await changeShippingLine(shippingOption.id, API_RETRY);
const displayItems = getPaymentRequestDisplayItems();
const {general_settings} = getOrderInitialData();
const rsaEnabled = general_settings.checkout_process.rsa_enabled;
const address = getShippingAddress();

if(response.success){
const {totalAmountDue} = getTotals();
const total = {
label: 'Total',
amount: totalAmountDue
};
if (response) {
const shippingLinesResponse = await getShippingLines(API_RETRY);
let taxResponse;
if (rsaEnabled) {
taxResponse = await estimateTaxes(address, API_RETRY);
} else {
taxResponse = await setTaxes(API_RETRY);
}

event.updateWith({
total: total,
status: 'success',
displayItems: displayItems
});
if (shippingLinesResponse.success && taxResponse.success) {
const {totalAmountDue} = getTotals();
const total = {
label: 'Total',
amount: totalAmountDue
};

event.updateWith({
total: total,
status: 'success',
displayItems: displayItems
});
} else {
event.updateWith({
status:'fail'
});
}
} else {
event.updateWith({
status:'fail'
Expand Down
Loading

0 comments on commit 0cb5cd1

Please sign in to comment.