Skip to content

Commit 5adac12

Browse files
authored
Update estimate calls to wallet pay with rsa enabled (#16)
1 parent 0baf119 commit 5adac12

15 files changed

+680
-152
lines changed

src/braintree/apple/braintreeOnShippingContactSelectedApple.ts

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
getValueByCurrency,
1010
} from 'src';
1111
import {
12+
changeShippingLine,
13+
estimateShippingLines,
14+
estimateTaxes,
1215
getCurrency,
16+
getOrderInitialData,
1317
getShipping,
1418
getShippingLines,
1519
setTaxes
@@ -20,6 +24,8 @@ export async function braintreeOnShippingContactSelectedApple(event: ApplePayShi
2024
const applePaySession = getBraintreeApplePaySessionChecked();
2125
const shippingAddress = event.shippingContact;
2226
const address = formatApplePayContactToCheckoutAddress(shippingAddress, true);
27+
const {general_settings} = getOrderInitialData();
28+
const rsaEnabled = general_settings.checkout_process.rsa_enabled;
2329

2430
const fail = () => {
2531
const {totalAmountDue} = getTotals();
@@ -29,37 +35,53 @@ export async function braintreeOnShippingContactSelectedApple(event: ApplePayShi
2935
});
3036
};
3137

32-
const response = await callShippingAddressEndpoint(address, false);
33-
34-
if(response.success){
35-
const shippingLinesResponse = await getShippingLines(API_RETRY);
36-
const taxResponse = await setTaxes(API_RETRY);
37-
38-
if(shippingLinesResponse.success && taxResponse.success){
39-
const {totalAmountDue} = getTotals();
40-
const displayItems = getPaymentRequestDisplayItems().map(
41-
({label, amount}) => ({
42-
label,
43-
amount: getValueByCurrency(amount, currencyCode),
44-
}));
45-
const {available_shipping_lines: shippingLines} = getShipping();
38+
let responseSuccess = false;
39+
if (rsaEnabled) {
40+
const estimateShippingResponse = await estimateShippingLines(address, API_RETRY);
41+
if (estimateShippingResponse.success) {
42+
const {selected_shipping: selectedShipping, available_shipping_lines: shippingLines} = getShipping();
43+
if (!selectedShipping && shippingLines.length > 0) {
44+
await changeShippingLine(shippingLines[0].id, API_RETRY);
45+
}
46+
await getShippingLines(API_RETRY);
47+
}
48+
const estimateTaxResponse = await estimateTaxes(address, API_RETRY);
49+
if (estimateShippingResponse.success && estimateTaxResponse.success) {
50+
responseSuccess = true;
51+
}
52+
} else {
53+
const shippingResponse = await callShippingAddressEndpoint(address, false);
54+
if (shippingResponse.success) {
55+
const shippingLinesResponse = await getShippingLines(API_RETRY);
56+
const taxResponse = await setTaxes(API_RETRY);
57+
if (shippingLinesResponse.success && taxResponse.success) {
58+
responseSuccess = true;
59+
}
60+
}
61+
}
4662

47-
const shippingOptions = shippingLines.map(p => ({
48-
label: p.description,
49-
detail: '',
50-
amount: getValueByCurrency(p.amount, currencyCode),
51-
identifier: p.id
63+
if (responseSuccess) {
64+
const {totalAmountDue} = getTotals();
65+
const displayItems = getPaymentRequestDisplayItems().map(
66+
({label, amount}) => ({
67+
label,
68+
amount: getValueByCurrency(amount, currencyCode),
5269
}));
70+
const {available_shipping_lines: shippingLines} = getShipping();
5371

54-
applePaySession.completeShippingContactSelection({
55-
newLineItems: displayItems,
56-
newShippingMethods: shippingOptions,
57-
newTotal: {label: 'Total', amount: getValueByCurrency(totalAmountDue, currencyCode)},
58-
});
72+
const shippingOptions = shippingLines.map(p => ({
73+
label: p.description,
74+
detail: '',
75+
amount: getValueByCurrency(p.amount, currencyCode),
76+
identifier: p.id
77+
}));
78+
79+
applePaySession.completeShippingContactSelection({
80+
newLineItems: displayItems,
81+
newShippingMethods: shippingOptions,
82+
newTotal: {label: 'Total', amount: getValueByCurrency(totalAmountDue, currencyCode)},
83+
});
5984

60-
} else {
61-
fail();
62-
}
6385
} else {
6486
fail();
6587
}

src/braintree/apple/braintreeOnShippingMethodSelectedApple.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import {
88
import {
99
changeShippingLine,
1010
getCurrency,
11+
getOrderInitialData,
1112
getShipping,
1213
getShippingLines,
13-
setTaxes
14+
setTaxes,
15+
getShippingAddress,
16+
estimateTaxes
1417
} from '@boldcommerce/checkout-frontend-library';
1518
import ApplePayLineItem = ApplePayJS.ApplePayLineItem;
1619
import ApplePayShippingMethodSelectedEvent = ApplePayJS.ApplePayShippingMethodSelectedEvent;
@@ -21,13 +24,21 @@ export async function braintreeOnShippingMethodSelectedApple(event: ApplePayShip
2124
const {available_shipping_lines: shippingLines} = getShipping();
2225
const selectedShippingMethod = event.shippingMethod;
2326
const option = shippingLines.find(line => line.id === selectedShippingMethod.identifier);
27+
const {general_settings} = getOrderInitialData();
28+
const rsaEnabled = general_settings.checkout_process.rsa_enabled;
29+
const address = getShippingAddress();
2430

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

2834
if (response.success) {
2935
const shippingLinesResponse = await getShippingLines(API_RETRY);
30-
const taxResponse = await setTaxes(API_RETRY);
36+
let taxResponse;
37+
if (rsaEnabled) {
38+
taxResponse = await estimateTaxes(address, API_RETRY);
39+
} else {
40+
taxResponse = await setTaxes(API_RETRY);
41+
}
3142

3243
if (shippingLinesResponse.success && taxResponse.success) {
3344
const {totalAmountDue} = getTotals();

src/braintree/google/braintreeOnPaymentDataChangeGoogle.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,33 @@ import {callShippingAddressEndpoint, getTotals, getValueByCurrency} from 'src/ut
66
import {
77
changeShippingLine,
88
getCurrency,
9+
getOrderInitialData,
910
getShipping,
1011
getShippingLines,
11-
setTaxes
12+
setTaxes,
13+
estimateShippingLines,
14+
estimateTaxes,
1215
} from '@boldcommerce/checkout-frontend-library';
13-
import {API_RETRY} from 'src/types';
16+
import {API_RETRY, BRAINTREE_GOOGLE_EMPTY_SHIPPING_OPTION} from 'src/types';
1417
import CallbackIntent = google.payments.api.CallbackIntent;
1518
import {braintreeConstants} from 'src/variables';
1619

1720
export async function braintreeOnPaymentDataChangeGoogle(intermediatePaymentData: IntermediatePaymentData): Promise<PaymentDataRequestUpdate> {
1821
const {callbackTrigger, shippingAddress, shippingOptionData} = intermediatePaymentData;
1922
const paymentDataRequestUpdate: PaymentDataRequestUpdate = {};
2023
const intent = callbackTrigger === braintreeConstants.GOOGLEPAY_TRIGGER_INITIALIZE ? braintreeConstants.GOOGLEPAY_INTENT_SHIPPING_ADDRESS : callbackTrigger as CallbackIntent;
24+
const {general_settings} = getOrderInitialData();
25+
const rsaEnabled = general_settings.checkout_process.rsa_enabled;
2126

2227
switch (callbackTrigger) {
2328
case braintreeConstants.GOOGLEPAY_TRIGGER_INITIALIZE:
2429
case braintreeConstants.GOOGLEPAY_INTENT_SHIPPING_OPTION:
2530
case braintreeConstants.GOOGLEPAY_INTENT_SHIPPING_ADDRESS: {
26-
if (shippingAddress) {
27-
const address = formatBraintreeShippingAddressGoogle(shippingAddress, true);
31+
let shippingLinesResponse;
32+
const address = formatBraintreeShippingAddressGoogle(shippingAddress, true);
33+
if (rsaEnabled) {
34+
shippingLinesResponse = await estimateShippingLines(address, API_RETRY);
35+
} else {
2836
const shippingAddressResponse = await callShippingAddressEndpoint(address, false);
2937
if (!shippingAddressResponse.success) {
3038
paymentDataRequestUpdate.error = {
@@ -34,11 +42,12 @@ export async function braintreeOnPaymentDataChangeGoogle(intermediatePaymentData
3442
};
3543
return paymentDataRequestUpdate;
3644
}
45+
shippingLinesResponse = await getShippingLines(API_RETRY);
3746
}
38-
const shippingLinesResponse = await getShippingLines(API_RETRY);
47+
3948
const {selected_shipping: selectedShipping, available_shipping_lines: shippingLines} = getShipping();
4049
if (shippingLinesResponse.success) {
41-
if (shippingOptionData) {
50+
if (shippingOptionData && shippingOptionData.id !== BRAINTREE_GOOGLE_EMPTY_SHIPPING_OPTION) {
4251
const option = shippingLines.find(line => line.id === shippingOptionData.id);
4352
option && await changeShippingLine(option.id, API_RETRY);
4453
} else if (!selectedShipping && shippingLines.length > 0) {
@@ -47,7 +56,11 @@ export async function braintreeOnPaymentDataChangeGoogle(intermediatePaymentData
4756
await getShippingLines(API_RETRY);
4857
}
4958

50-
await setTaxes(API_RETRY);
59+
if (rsaEnabled) {
60+
await estimateTaxes(address, API_RETRY);
61+
} else {
62+
await setTaxes(API_RETRY);
63+
}
5164

5265
const {iso_code: currencyCode} = getCurrency();
5366
const {totalAmountDue} = getTotals();

src/paypal/paypalOnShippingChange.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import {
1010
} from 'src';
1111
import {
1212
changeShippingLine,
13+
estimateShippingLines,
14+
estimateTaxes,
15+
getOrderInitialData,
1316
getShipping,
1417
getShippingLines,
1518
setTaxes,
@@ -20,31 +23,48 @@ export async function paypalOnShippingChange(data: OnShippingChangeData, actions
2023
const {shipping_address: address, selected_shipping_option: selectedOption} = data;
2124
const {reject, order: {patch: patch}} = actions;
2225
const {MAX_STRING_LENGTH: maxStringSize} = paypalConstants;
26+
const {general_settings} = getOrderInitialData();
27+
const rsaEnabled = general_settings.checkout_process.rsa_enabled;
2328

2429
if (address) {
2530
const formattedAddress = formatPaypalToApiAddress(address, undefined, undefined , getPhoneNumber());
26-
const success = true;
27-
28-
const shippingAddressResponse = await callShippingAddressEndpoint(formattedAddress, false);
29-
if (!shippingAddressResponse.success) {
30-
return reject();
31+
let success = false;
32+
if (rsaEnabled) {
33+
const shippingLinesResponse = await estimateShippingLines(formattedAddress, API_RETRY);
34+
if (shippingLinesResponse.success) {
35+
success = true;
36+
}
37+
} else {
38+
const shippingAddressResponse = await callShippingAddressEndpoint(formattedAddress, false);
39+
if (!shippingAddressResponse.success) {
40+
return reject();
41+
}
42+
const shippingLinesResponse = await getShippingLines(API_RETRY);
43+
if (shippingLinesResponse.success) {
44+
success = true;
45+
}
3146
}
3247

33-
if(success) {
34-
const shippingLinesResponse = await getShippingLines(API_RETRY);
48+
49+
if (success) {
3550
const {selected_shipping: selectedShipping, available_shipping_lines: shippingLines} = getShipping();
36-
if (shippingLinesResponse.success) {
37-
if (selectedOption) {
38-
const option = shippingLines.find(line => isSimilarStrings(line.description.substring(0, maxStringSize), selectedOption.label));
39-
option && await changeShippingLine(option.id, API_RETRY);
40-
} else if (!selectedShipping && shippingLines.length > 0) {
41-
await changeShippingLine(shippingLines[0].id, API_RETRY);
42-
}
43-
await getShippingLines(API_RETRY);
51+
if (selectedOption) {
52+
const option = shippingLines.find(line => isSimilarStrings(line.description.substring(0, maxStringSize), selectedOption.label));
53+
option && await changeShippingLine(option.id, API_RETRY);
54+
} else if (!selectedShipping && shippingLines.length > 0) {
55+
await changeShippingLine(shippingLines[0].id, API_RETRY);
4456
}
57+
await getShippingLines(API_RETRY);
4558
}
4659
}
47-
const taxResponse = await setTaxes(API_RETRY);
60+
61+
let taxResponse;
62+
if (rsaEnabled && address) {
63+
const formattedAddress = formatPaypalToApiAddress(address, undefined, undefined , getPhoneNumber());
64+
taxResponse = await estimateTaxes(formattedAddress, API_RETRY);
65+
} else {
66+
taxResponse = await setTaxes(API_RETRY);
67+
}
4868

4969
if (taxResponse.success) {
5070
const patchOperations = getPaypalPatchOperations(!!selectedOption);

src/stripe/addStripePayment.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import {
44
getOrderInitialData,
55
IAddPaymentRequest,
66
setBillingAddress,
7-
updateShippingAddress
7+
setTaxes,
88
} from '@boldcommerce/checkout-frontend-library';
99
import {
1010
formatStripeBillingAddress,
1111
callGuestCustomerEndpoint,
1212
getFirstAndLastName,
1313
orderProcessing,
1414
formatStripeShippingAddress,
15-
getTotals
15+
getTotals,
16+
callShippingAddressEndpoint
1617
} from 'src';
1718

1819
export async function addStripePayment(event: IStripePaymentEvent, stripeGatewayId: string): Promise<void> {
@@ -32,22 +33,26 @@ export async function addStripePayment(event: IStripePaymentEvent, stripeGateway
3233
shippingPhoneNumber = event.payerPhone;
3334
}
3435
const address = formatStripeShippingAddress(shippingAddress, shippingPhoneNumber);
35-
await updateShippingAddress(address, API_RETRY).then(async (shippingResult) => {
36+
await callShippingAddressEndpoint(address, true).then(async (shippingResult) => {
3637
if (shippingResult.success) {
3738
const billingAddress = formatStripeBillingAddress(card, event.payerName, event.payerPhone);
3839
await setBillingAddress(billingAddress, API_RETRY).then(async (billingResult) => {
3940
if (billingResult.success) {
40-
const walletPayType = window.ApplePaySession && ApplePaySession.canMakePayments() ? 'applepay' : 'paywithgoogle';
41-
const payment: IAddPaymentRequest = {
42-
token: token.id,
43-
gateway_public_id: stripeGatewayId,
44-
currency: card.currency,
45-
amount: totalAmountDue,
46-
wallet_pay_type: walletPayType,
47-
};
48-
await addPayment(payment, API_RETRY).then(async (paymentResult) => {
49-
if (paymentResult.success) {
50-
success = true;
41+
await setTaxes(API_RETRY).then(async (taxResult) => {
42+
if (taxResult.success) {
43+
const walletPayType = window.ApplePaySession && ApplePaySession.canMakePayments() ? 'applepay' : 'paywithgoogle';
44+
const payment: IAddPaymentRequest = {
45+
token: token.id,
46+
gateway_public_id: stripeGatewayId,
47+
currency: card.currency,
48+
amount: totalAmountDue,
49+
wallet_pay_type: walletPayType,
50+
};
51+
await addPayment(payment, API_RETRY).then(async (paymentResult) => {
52+
if (paymentResult.success) {
53+
success = true;
54+
}
55+
});
5156
}
5257
});
5358
}

src/stripe/changeStripeShippingLines.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
1-
import {changeShippingLine} from '@boldcommerce/checkout-frontend-library';
1+
import {
2+
changeShippingLine,
3+
estimateTaxes,
4+
getOrderInitialData,
5+
getShippingAddress,
6+
getShippingLines,
7+
setTaxes
8+
} from '@boldcommerce/checkout-frontend-library';
29
import {API_RETRY, IStripeEvent, IStripeShippingOptions, getPaymentRequestDisplayItems, getTotals} from 'src';
310

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

9-
if(response.success){
10-
const {totalAmountDue} = getTotals();
11-
const total = {
12-
label: 'Total',
13-
amount: totalAmountDue
14-
};
19+
if (response.success) {
20+
const shippingLinesResponse = await getShippingLines(API_RETRY);
21+
let taxResponse;
22+
if (rsaEnabled) {
23+
taxResponse = await estimateTaxes(address, API_RETRY);
24+
} else {
25+
taxResponse = await setTaxes(API_RETRY);
26+
}
1527

16-
event.updateWith({
17-
total: total,
18-
status: 'success',
19-
displayItems: displayItems
20-
});
28+
if (shippingLinesResponse.success && taxResponse.success) {
29+
const {totalAmountDue} = getTotals();
30+
const total = {
31+
label: 'Total',
32+
amount: totalAmountDue
33+
};
34+
35+
event.updateWith({
36+
total: total,
37+
status: 'success',
38+
displayItems: displayItems
39+
});
40+
} else {
41+
event.updateWith({
42+
status:'fail'
43+
});
44+
}
2145
} else {
2246
event.updateWith({
2347
status:'fail'

0 commit comments

Comments
 (0)