Skip to content

Commit

Permalink
feat: polling status for 3ds flow
Browse files Browse the repository at this point in the history
  • Loading branch information
prafulkoppalkar committed Apr 24, 2024
1 parent 07bf0bb commit 54847fe
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/Utilities/PaymentHelpers.res
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,94 @@ let rec pollRetrievePaymentIntent = (
})
}

let retrieveStatus = (~headers, ~switchToCustomPod, pollID) => {
open Promise
let endpoint = ApiEndpoint.getApiEndPoint()
let uri = `${endpoint}/poll/status/${pollID}`
let logger = OrcaLogger.make()
logApi(
~optLogger=Some(logger),
~url=uri,
~apiLogType=Request,
~eventName=POLL_STATUS_INIT,
~logType=INFO,
~logCategory=API,
(),
)
fetchApi(
uri,
~method=#GET,
~headers=headers->ApiEndpoint.addCustomPodHeader(~switchToCustomPod, ()),
(),
)
->then(res => {
let statusCode = res->Fetch.Response.status->Int.toString
if statusCode->String.charAt(0) !== "2" {
res
->Fetch.Response.json
->then(data => {
logApi(
~optLogger=Some(logger),
~url=uri,
~data,
~statusCode,
~apiLogType=Err,
~eventName=POLL_STATUS_CALL,
~logType=ERROR,
~logCategory=API,
(),
)
JSON.Encode.null->resolve
})
} else {
logApi(
~optLogger=Some(logger),
~url=uri,
~statusCode,
~apiLogType=Response,
~eventName=POLL_STATUS_CALL,
~logType=INFO,
~logCategory=API,
(),
)
res->Fetch.Response.json
}
})
->catch(e => {
Console.log2("Unable to Poll status details because of ", e)
JSON.Encode.null->resolve
})
}

let rec pollStatus = (~headers, ~switchToCustomPod, ~pollId, ~interval, ~count) => {
open Promise
retrieveStatus(~headers, ~switchToCustomPod, pollId)
->then(json => {
let dict = json->JSON.Decode.object->Option.getOr(Dict.make())
let status = dict->getString("status", "")
Promise.make((resolve, _) => {
if status === "completed" {
resolve(json)
} else if !(count > 0) {
handlePostMessage([("fullscreen", false->JSON.Encode.bool)])
postFailedSubmitResponse(~errortype="server_error", ~message="Something went wrong") // redirect to return url
} else {
delay(interval)
->then(
_ => {
pollStatus(~headers, ~switchToCustomPod, ~pollId, ~interval, ~count=count - 1)
},
)
->ignore
}
})
})
->catch(e => {
Console.log2("Unable to retrieve payment due to following error", e)
pollStatus(~headers, ~switchToCustomPod, ~pollId, ~interval, ~count=count - 1)
})
}

let rec intentCall = (
~fetchApi: (
string,
Expand Down
37 changes: 37 additions & 0 deletions src/orca-loader/Elements.res
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,44 @@ let make = (
}
}

let handlePollStatusMessage = (ev: Types.event) => {
let eventDataObject = ev.data->anyTypeToJson
let headers = [("Content-Type", "application/json"), ("api-key", publishableKey)]
switch eventDataObject->getOptionalJsonFromJson("poll_status") {
| Some(val) => {
handlePostMessage([
("fullscreen", true->JSON.Encode.bool),
("param", "paymentloader"->JSON.Encode.string),
("iframeId", selectorString->JSON.Encode.string),
])
let dict = val->getDictFromJson
let pollId = dict->getString("poll_id", "")
let interval =
dict->getString("delay_in_secs", "")->Int.fromString->Option.getOr(1) * 1000
let count = dict->getString("frequency", "")->Int.fromString->Option.getOr(5)
let url = dict->getString("return_url_with_query_params", "")
PaymentHelpers.pollStatus(~headers, ~switchToCustomPod, ~pollId, ~interval, ~count)
->then(res => {
let dict = res->JSON.Decode.object->Option.getOr(Dict.make())
let status = dict->getString("status", "")
if status === "completed" {
Window.Location.replace(url)->ignore // retrive status
}->resolve
})
->catch(_e =>
postFailedSubmitResponse(
~errortype="Server_error",
~message="Something went Wrong",
)->resolve
)
->ignore
}
| None => ()
}
}

addSmartEventListener("message", handleApplePayMounted, "onApplePayMount")
addSmartEventListener("message", handlePollStatusMessage, "onPollStatusMsg")
addSmartEventListener("message", handleGooglePayThirdPartyFlow, "onGooglePayThirdParty")
Window.removeEventListener("message", handleApplePayMessages.contents)

Expand Down
4 changes: 4 additions & 0 deletions src/orca-log-catcher/OrcaLogger.res
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type eventName =
| PAYMENT_METHODS_RESPONSE
| LOADER_CHANGED
| PAYMENT_SESSION_INITIATED
| POLL_STATUS_INIT
| POLL_STATUS_CALL

let eventNameToStrMapper = eventName => {
switch eventName {
Expand Down Expand Up @@ -126,6 +128,8 @@ let eventNameToStrMapper = eventName => {
| PAYMENT_METHODS_RESPONSE => "PAYMENT_METHODS_RESPONSE"
| LOADER_CHANGED => "LOADER_CHANGED"
| PAYMENT_SESSION_INITIATED => "PAYMENT_SESSION_INITIATED"
| POLL_STATUS_INIT => "POLL_STATUS_INIT"
| POLL_STATUS_CALL => "POLL_STATUS_CALL"
}
}

Expand Down

0 comments on commit 54847fe

Please sign in to comment.