diff --git a/src/Payments/PaypalSDK.res b/src/Payments/PaypalSDK.res index 952c304b..5825e5fd 100644 --- a/src/Payments/PaypalSDK.res +++ b/src/Payments/PaypalSDK.res @@ -2,9 +2,12 @@ open PaypalSDKTypes @react.component let make = (~sessionObj: SessionsType.token, ~paymentType: CardThemeType.mode) => { - let {iframeId, publishableKey, sdkHandleOneClickConfirmPayment} = Recoil.useRecoilValueFromAtom( - RecoilAtoms.keys, - ) + let { + iframeId, + publishableKey, + sdkHandleOneClickConfirmPayment, + clientSecret, + } = Recoil.useRecoilValueFromAtom(RecoilAtoms.keys) let sdkHandleIsThere = Recoil.useRecoilValueFromAtom( RecoilAtoms.isPaymentButtonHandlerProvidedAtom, ) @@ -100,6 +103,7 @@ let make = (~sessionObj: SessionsType.token, ~paymentType: CardThemeType.mode) = ~isCallbackUsedVal, ~sdkHandleIsThere, ~sessions, + ~clientSecret, ) }) Window.body->Window.appendChild(paypalScript) diff --git a/src/Payments/PaypalSDKHelpers.res b/src/Payments/PaypalSDKHelpers.res index 0b4bc071..ffdd8002 100644 --- a/src/Payments/PaypalSDKHelpers.res +++ b/src/Payments/PaypalSDKHelpers.res @@ -1,6 +1,7 @@ open PaypalSDKTypes open Promise open Utils +open TaxCalculation let loadPaypalSDK = ( ~loggerState: OrcaLogger.loggerMake, @@ -25,6 +26,7 @@ let loadPaypalSDK = ( ~isCallbackUsedVal as _: bool, ~sdkHandleIsThere: bool, ~sessions: PaymentType.loadType, + ~clientSecret, ) => { loggerState.setLogInfo( ~value="Paypal SDK Button Clicked", @@ -113,6 +115,35 @@ let loadPaypalSDK = ( } }) }, + onShippingAddressChange: data => { + let isTaxCalculationEnabled = paymentMethodListValue.is_tax_calculation_enabled + if isTaxCalculationEnabled { + let newShippingAddressObj = + data + ->getDictFromJson + ->getDictFromObj("shippingAddress") + ->shippingAddressItemToObjMapper + let newShippingAddress = + [ + ("state", newShippingAddressObj.state->Option.getOr("")->JSON.Encode.string), + ("country", newShippingAddressObj.countryCode->Option.getOr("")->JSON.Encode.string), + ("zip", newShippingAddressObj.postalCode->Option.getOr("")->JSON.Encode.string), + ]->getJsonFromArrayOfJson + + let paymentMethodType = "paypal"->JSON.Encode.string + + calculateTax( + ~shippingAddress=[("address", newShippingAddress)]->getJsonFromArrayOfJson, + ~logger=loggerState, + ~publishableKey, + ~clientSecret=clientSecret->Option.getOr(""), + ~paymentMethodType, + ~sessionId=data->getDictFromJson->Dict.get("orderID"), + ) + } else { + Js.Json.null->Js.Promise.resolve + } + }, onApprove: (_data, actions) => { if !options.readOnly { actions.order.get() @@ -141,8 +172,7 @@ let loadPaypalSDK = ( let (connectors, _) = paymentMethodListValue->PaymentUtils.getConnectors(Wallets(Paypal(SDK))) - - let orderId = val->Utils.getDictFromJson->Utils.getString("id", "") + let orderId = val->getDictFromJson->Utils.getString("id", "") let body = PaymentBody.paypalSdkBody(~token=orderId, ~connectors) let modifiedPaymentBody = PaymentUtils.appendedCustomerAcceptance( ~isGuestCustomer, diff --git a/src/Types/ApplePayTypes.res b/src/Types/ApplePayTypes.res index 998f42fc..f644e938 100644 --- a/src/Types/ApplePayTypes.res +++ b/src/Types/ApplePayTypes.res @@ -31,7 +31,7 @@ type lineItem = { \"type": string, } type shippingAddressChangeEvent = {shippingContact: JSON.t} -type updatedOrderDetails = {newTotal: lineItem, newLineItems: array} +type orderDetails = {newTotal: lineItem, newLineItems: array} type innerSession type session = { begin: unit => unit, @@ -41,8 +41,8 @@ type session = { mutable onvalidatemerchant: event => unit, completeMerchantValidation: JSON.t => unit, mutable onpaymentauthorized: event => unit, - mutable onshippingcontactselected: shippingAddressChangeEvent => unit, - completeShippingContactSelection: updatedOrderDetails => unit, + mutable onshippingcontactselected: shippingAddressChangeEvent => promise, + completeShippingContactSelection: orderDetails => unit, completePayment: JSON.t => unit, \"STATUS_SUCCESS": string, \"STATUS_FAILURE": string, diff --git a/src/Types/PaypalSDKTypes.res b/src/Types/PaypalSDKTypes.res index 48c01ac9..6404cf73 100644 --- a/src/Types/PaypalSDKTypes.res +++ b/src/Types/PaypalSDKTypes.res @@ -79,6 +79,7 @@ type buttons = { onApprove: (data, actions) => unit, onCancel: data => unit, onError?: err => unit, + onShippingAddressChange?: JSON.t => promise, } let getLabel = (var: PaymentType.paypalStyleType) => { switch var { @@ -177,3 +178,14 @@ let getOrderDetails = (orderDetails, paymentType) => { shippingAddressOverride, } } + +let shippingAddressItemToObjMapper=dict=>{ + recipientName: dict->Utils.getOptionString("recipientName"), + line1: dict->Utils.getOptionString("line1"), + line2: dict->Utils.getOptionString("line2"), + city: dict->Utils.getOptionString("city"), + countryCode: dict->Utils.getOptionString("countryCode"), + postalCode: dict->Utils.getOptionString("postalCode"), + state: dict->Utils.getOptionString("state"), + phone: dict->Utils.getOptionString("phone"), +} \ No newline at end of file diff --git a/src/Utilities/ApplePayHelpers.res b/src/Utilities/ApplePayHelpers.res index 107ee1a2..688b9d64 100644 --- a/src/Utilities/ApplePayHelpers.res +++ b/src/Utilities/ApplePayHelpers.res @@ -106,6 +106,20 @@ let startApplePaySession = ( } ssn.onshippingcontactselected = shippingAddressChangeEvent => { + let currentTotal = paymentRequest->getDictFromJson->getDictFromDict("total") + let label = currentTotal->getString("label", "apple") + let currentAmount = currentTotal->getString("amount", "0.00") + let \"type" = currentTotal->getString("type", "final") + + let oldTotal: lineItem = { + label, + amount: currentAmount, + \"type", + } + let currentOrderDetails: orderDetails = { + newTotal: oldTotal, + newLineItems: [oldTotal], + } if isTaxCalculationEnabled { let newShippingContact = shippingAddressChangeEvent.shippingContact @@ -113,7 +127,7 @@ let startApplePaySession = ( ->shippingContactItemToObjMapper let newShippingAddress = [ - ("state", newShippingContact.locality->JSON.Encode.string), + ("state", newShippingContact.administrativeArea->JSON.Encode.string), ("country", newShippingContact.countryCode->JSON.Encode.string), ("zip", newShippingContact.postalCode->JSON.Encode.string), ]->getJsonFromArrayOfJson @@ -126,8 +140,7 @@ let startApplePaySession = ( ~publishableKey, ~clientSecret, ~paymentMethodType, - ) - ->thenResolve(response => { + )->thenResolve(response => { switch response->taxResponseToObjMapper { | Some(taxCalculationResponse) => { let (netAmount, ordertaxAmount, shippingCost) = ( @@ -136,13 +149,13 @@ let startApplePaySession = ( taxCalculationResponse.shipping_cost, ) let newTotal: lineItem = { - label: "Net Amount", + label, amount: netAmount->minorUnitToString, - \"type": "final", + \"type", } let newLineItems: array = [ { - label: "Bag Subtotal", + label: "Subtotal", amount: (netAmount - ordertaxAmount - shippingCost)->minorUnitToString, \"type": "final", }, @@ -157,16 +170,18 @@ let startApplePaySession = ( \"type": "final", }, ] - let updatedOrderDetails: updatedOrderDetails = { + let updatedOrderDetails: orderDetails = { newTotal, newLineItems, } ssn.completeShippingContactSelection(updatedOrderDetails) } - | None => ssn.abort() + | None => ssn.completeShippingContactSelection(currentOrderDetails) } }) - ->ignore + } else { + ssn.completeShippingContactSelection(currentOrderDetails) + resolve() } } diff --git a/src/Utilities/PaymentHelpers.res b/src/Utilities/PaymentHelpers.res index 0fa7d7b5..ed4da597 100644 --- a/src/Utilities/PaymentHelpers.res +++ b/src/Utilities/PaymentHelpers.res @@ -2017,6 +2017,7 @@ let calculateTax = ( ~shippingAddress, ~logger, ~customPodUri, + ~sessionId, ) => { open Promise let endpoint = ApiEndpoint.getApiEndPoint() @@ -2027,6 +2028,8 @@ let calculateTax = ( ("shipping", shippingAddress), ("payment_method_type", paymentMethodType), ] + sessionId->Option.mapOr((), id => body->Array.push(("session_id", id))->ignore) + logApi( ~optLogger=Some(logger), ~url=uri, diff --git a/src/Utilities/TaxCalculation.res b/src/Utilities/TaxCalculation.res index 0d9d0c33..5f70f50c 100644 --- a/src/Utilities/TaxCalculation.res +++ b/src/Utilities/TaxCalculation.res @@ -24,6 +24,7 @@ let calculateTax = ( ~clientSecret, ~publishableKey, ~paymentMethodType, + ~sessionId=None, ) => { PaymentHelpers.calculateTax( ~clientSecret=clientSecret->JSON.Encode.string, @@ -33,5 +34,6 @@ let calculateTax = ( ~shippingAddress, ~logger, ~customPodUri="", + ~sessionId, ) }