Skip to content

Commit

Permalink
Pull request #241: Praful/gpay/intentcall
Browse files Browse the repository at this point in the history
Merge in EXC/orca-elements from praful/gpay/intentcall to master

* commit 'fd56f5ace14de1be79762f3e89cedd09a3a6cced':
  removed indentation change
  removed webpack highlights
  log removed
  removed comments
  hotfix complete
  added comments
  hotfix to make gpay work on all platforms
  WIP
  fix: intent being called even on the session close
  • Loading branch information
prafulkoppalkar authored and arun.mishra committed Sep 26, 2023
2 parents 9fd425b + fd56f5a commit afe75e9
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 172 deletions.
9 changes: 9 additions & 0 deletions src/LoaderController.res
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger) => {
let (options, setOptions) = Recoil.useRecoilState(elementOptions)
let (optionsPayment, setOptionsPayment) = Recoil.useRecoilState(optionAtom)
let setSessionId = Recoil.useSetRecoilState(sessionId)
let setIsGooglePayReady = Recoil.useSetRecoilState(isGooglePayReady)
let (divH, setDivH) = React.useState(_ => 0.0)
let {showCardFormByDefault, paymentMethodOrder} = optionsPayment

Expand Down Expand Up @@ -272,6 +273,14 @@ let make = (~children, ~paymentMode, ~setIntegrateErrorError, ~logger) => {
if dict->getDictIsSome("sessions") {
setSessions(._ => Loaded(dict->getJsonObjectFromDict("sessions")))
}
if dict->getDictIsSome("isReadyToPay") {
setIsGooglePayReady(._ =>
dict
->getJsonObjectFromDict("isReadyToPay")
->Js.Json.decodeBoolean
->Belt.Option.getWithDefault(false)
)
}
if dict->getDictIsSome("paymentMethodList") {
let list = dict->getJsonObjectFromDict("paymentMethodList")
list == Js.Dict.empty()->Js.Json.object_
Expand Down
110 changes: 9 additions & 101 deletions src/Payments/GPay.res
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
open Utils
open RecoilAtoms
let arrayJsonToCamelCase = arr => {
arr->Js.Array2.map(item => {
item->transformKeys(CamelCase)
})
}

type props = {
sessionObj: option<SessionsType.token>,
Expand All @@ -14,41 +9,22 @@ type props = {

open GooglePayType

external toJson: 'a => Js.Json.t = "%identity"

let default = (props: props) => {
let (loggerState, _setLoggerState) = Recoil.useRecoilState(loggerAtom)
let {iframeId} = Recoil.useRecoilValueFromAtom(keys)
let {publishableKey} = Recoil.useRecoilValueFromAtom(keys)
let options = Recoil.useRecoilValueFromAtom(optionAtom)
let intent = PaymentHelpers.usePaymentIntent(Some(loggerState), Gpay)
let sync = PaymentHelpers.usePaymentSync(Some(loggerState), Gpay)
let isGPayReady = Recoil.useRecoilValueFromAtom(isGooglePayReady)
let status = CommonHooks.useScript("https://pay.google.com/gp/p/js/pay.js")
let baseRequest = {
apiVersion: 2,
apiVersionMinor: 0,
}
let isGooglePaySDKFlow = React.useMemo1(() => {
props.sessionObj->Belt.Option.isSome
}, [props.sessionObj])
let isGooglePayThirdPartyFlow = React.useMemo1(() => {
props.thirdPartySessionObj->Belt.Option.isSome
}, [props.sessionObj])

let googlePaySessionObj = React.useMemo1(() => {
switch props.sessionObj {
| Some(val) => val
| None => {
walletName: NONE,
token: "",
sessionId: "",
allowed_payment_methods: [],
transaction_info: Js.Json.null,
merchant_info: Js.Json.null,
}
}
}, [props.sessionObj])

let googlePayPaymentMethodType = switch PaymentMethodsRecord.getPaymentMethodTypeFromList(
~list=props.list,
~paymentMethod="wallet",
Expand All @@ -71,15 +47,6 @@ let default = (props: props) => {
? props.list->PaymentUtils.getConnectors(Wallets(Gpay(SDK)))
: props.list->PaymentUtils.getConnectors(Wallets(Gpay(Redirect)))

let getGoogleIsReadyToPayRequest = () => {
assign(
Js.Dict.empty()->Js.Json.object_,
baseRequest->toJson,
{
"allowedPaymentMethods": googlePaySessionObj.allowed_payment_methods->arrayJsonToCamelCase,
}->toJson,
)
}
let processPayment = (body: array<(string, Js.Json.t)>) => {
intent(
~bodyArr=body,
Expand All @@ -102,6 +69,9 @@ let default = (props: props) => {
let body = PaymentBody.gpayBody(~payObj=obj, ~connectors)
processPayment(body)
}
if dict->Js.Dict.get("gpayError")->Belt.Option.isSome {
Utils.handlePostMessage([("fullscreen", false->Js.Json.boolean)])
}
}
Window.addEventListener("message", handle)
Some(() => {Window.removeEventListener("message", handle)})
Expand All @@ -113,17 +83,6 @@ let default = (props: props) => {
| GooglePay(val) => val
| _ => 48
}
let getGooglePaymentDataRequest = () => {
let paymentDataRequest = assign2(Js.Dict.empty()->Js.Json.object_, baseRequest->toJson)

paymentDataRequest.allowedPaymentMethods =
googlePaySessionObj.allowed_payment_methods->arrayJsonToCamelCase

paymentDataRequest.transactionInfo =
googlePaySessionObj.transaction_info->transformKeys(CamelCase)
paymentDataRequest.merchantInfo = googlePaySessionObj.merchant_info->transformKeys(CamelCase)
paymentDataRequest
}

let getGooglePaymentsClient = () => {
google({"environment": GlobalVars.isProd ? "PRODUCTION" : "TEST"}->toJson)
Expand Down Expand Up @@ -159,45 +118,17 @@ let default = (props: props) => {
->Belt.Option.getWithDefault(false)

if isDelayedSessionToken {
loggerState.setLogInfo(
~logType=DEBUG,
~eventName=GOOGLE_PAY_FLOW,
~paymentMethod="GOOGLE_PAY",
~value="isDelayedSessionToken",
(),
)
let bodyDict = PaymentBody.gPayThirdPartySdkBody(~connectors)
processPayment(bodyDict)
} else {
loggerState.setLogInfo(
~logType=DEBUG,
~eventName=GOOGLE_PAY_FLOW,
~paymentMethod="GOOGLE_PAY",
~value="not isDelayedSessionToken",
(),
)
handlePostMessage([
("fullscreen", true->Js.Json.boolean),
("param", "paymentloader"->Js.Json.string),
("iframeId", iframeId->Js.Json.string),
])

let paymentDataRequest = getGooglePaymentDataRequest()
options.readOnly
? ()
: handlePostMessage([
("GpayClicked", true->Js.Json.boolean),
("gpayPaymentRequestData", paymentDataRequest->toJson),
])
options.readOnly ? () : handlePostMessage([("GpayClicked", true->Js.Json.boolean)])
}
} else {
loggerState.setLogInfo(
~logType=DEBUG,
~eventName=GOOGLE_PAY_FLOW,
~paymentMethod="GOOGLE_PAY",
~value="not isInvokeSDKFlow",
(),
)
let bodyDict = PaymentBody.gpayRedirectBody(~connectors)
processPayment(bodyDict)
}
Expand All @@ -208,6 +139,7 @@ let default = (props: props) => {
(),
)
}

let buttonStyle = {
let obj = {
"onClick": onGooglePaymentButtonClicked,
Expand All @@ -228,36 +160,12 @@ let default = (props: props) => {
gpayWrapper.innerHTML = ""
gpayWrapper.appendChild(. button)
}

let onGooglePayLoaded = () => {
open Promise
let paymentsClient = getGooglePaymentsClient()

if paymentExperience == PaymentMethodsRecord.RedirectToURL || isGooglePayThirdPartyFlow {
React.useEffect3(() => {
if status == "ready" && isGPayReady {
addGooglePayButton()
} else if isInvokeSDKFlow {
paymentsClient.isReadyToPay(. getGoogleIsReadyToPayRequest())
->then(_ => {
addGooglePayButton()
resolve()
})
->catch(_err => {
resolve()
})
->ignore
}
}

React.useEffect2(() => {
if status == "ready" {
try {
onGooglePayLoaded()
} catch {
| _err => Utils.logInfo(Js.log("Error loading Gpay"))
}
}
None
}, (status, props))
}, (status, props, isGPayReady))

React.useEffect0(() => {
let handleGooglePayMessages = (ev: Window.event) => {
Expand Down
2 changes: 1 addition & 1 deletion src/Types/GooglePayType.res
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type document
@val external document: document = "document"
@send external getElementById: (document, string) => element = "getElementById"
type client = {
isReadyToPay: (. Js.Json.t) => Js.Promise.t<Fetch.Response.t>,
isReadyToPay: (. Js.Json.t) => Js.Promise.t<Js.Json.t>,
createButton: (. Js.Json.t) => Dom.element,
loadPaymentData: (. Js.Json.t) => Js.Promise.t<Fetch.Response.t>,
}
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/RecoilAtoms.res
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ let userPhoneNumber = Recoil.atom(.
errorString: "",
},
)
let isGooglePayReady = Recoil.atom(. "isGooglePayReady", false)
let userCountry = Recoil.atom(. "userCountry", "")
let userBank = Recoil.atom(. "userBank", "")
let userAddressline1 = Recoil.atom(. "userAddressline1", defaultFieldValues)
Expand Down
5 changes: 5 additions & 0 deletions src/Utilities/Utils.res
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,11 @@ let delay = timeOut => {
})
}

let arrayJsonToCamelCase = arr => {
arr->Js.Array2.map(item => {
item->transformKeys(CamelCase)
})
}
let formatException = exc => {
let exceptionMessage = exc->toJson->getDictFromJson
let exceptionMessage =
Expand Down
Loading

0 comments on commit afe75e9

Please sign in to comment.