Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update estimate calls to wallet pay with rsa enabled #16

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 49 additions & 27 deletions src/braintree/apple/braintreeOnShippingContactSelectedApple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
getValueByCurrency,
} from 'src';
import {
changeShippingLine,
estimateShippingLines,
estimateTaxes,
getCurrency,
getOrderInitialData,
getShipping,
getShippingLines,
setTaxes
Expand All @@ -20,6 +24,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 +35,53 @@ 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);
zliu7878 marked this conversation as resolved.
Show resolved Hide resolved
if (estimateShippingResponse.success) {
const {selected_shipping: selectedShipping, available_shipping_lines: shippingLines} = getShipping();
if (!selectedShipping && shippingLines.length > 0) {
await changeShippingLine(shippingLines[0].id, API_RETRY);
}
await getShippingLines(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
27 changes: 20 additions & 7 deletions src/braintree/google/braintreeOnPaymentDataChangeGoogle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,33 @@ 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 {API_RETRY, BRAINTREE_GOOGLE_EMPTY_SHIPPING_OPTION} from 'src/types';
import CallbackIntent = google.payments.api.CallbackIntent;
import {braintreeConstants} from 'src/variables';

export async function braintreeOnPaymentDataChangeGoogle(intermediatePaymentData: IntermediatePaymentData): Promise<PaymentDataRequestUpdate> {
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);
praiseBold marked this conversation as resolved.
Show resolved Hide resolved
} else {
const shippingAddressResponse = await callShippingAddressEndpoint(address, false);
if (!shippingAddressResponse.success) {
paymentDataRequestUpdate.error = {
Expand All @@ -34,11 +42,12 @@ 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) {
if (shippingOptionData && shippingOptionData.id !== BRAINTREE_GOOGLE_EMPTY_SHIPPING_OPTION) {
const option = shippingLines.find(line => line.id === shippingOptionData.id);
option && await changeShippingLine(option.id, API_RETRY);
} else if (!selectedShipping && shippingLines.length > 0) {
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.success) {
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