Skip to content
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "processout.js",
"version": "1.9.21",
"version": "1.9.22",
"description": "ProcessOut.js is a JavaScript library for ProcessOut's payment processing API.",
"scripts": {
"build:processout": "tsc -p src/processout && uglifyjs --compress --keep-fnames --ie8 dist/processout.js -o dist/processout.js",
Expand Down
41 changes: 39 additions & 2 deletions src/dynamic-checkout/payment-methods/apm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ module ProcessOut {
const { apm } = this.paymentMethod
const additionalData = this.paymentConfig.getAdditionalDataForGateway(apm.gateway_name)

const redirectUrl =
const redirectUrl = DynamicCheckoutOpenerLossUtils.markRedirectUrl(
Object.keys(additionalData).length > 0
? this.processOutInstance.appendAdditionalDataToUrl(apm.redirect_url, additionalData)
: apm.redirect_url
: apm.redirect_url,
)

DynamicCheckoutEventsUtils.dispatchPaymentSubmittedEvent({
payment_method_name: apm.gateway_name,
Expand All @@ -114,6 +115,7 @@ module ProcessOut {
return_url: this.paymentConfig.invoiceDetails.return_url || null,
})

const actionStartedAt = Date.now()
this.processOutInstance.handleAction(
redirectUrl,
paymentToken => {
Expand Down Expand Up @@ -183,6 +185,10 @@ module ProcessOut {
)
},
error => {
if (this.handleOpenerLoss(error, actionStartedAt)) {
return
}

if (error.code === "customer.canceled") {
this.resetContainerHtml().appendChild(
new DynamicCheckoutPaymentCancelledView(this.processOutInstance, this.paymentConfig)
Expand Down Expand Up @@ -280,6 +286,7 @@ module ProcessOut {
return
}

const actionStartedAt = Date.now()
this.processOutInstance.handleAction(
data.customer_action.value,
Comment thread
jakub-binczyk-cko marked this conversation as resolved.
Resolved
paymentToken => {
Expand Down Expand Up @@ -371,6 +378,10 @@ module ProcessOut {
)
},
error => {
if (this.handleOpenerLoss(error, actionStartedAt)) {
return
}

if (error.code === "customer.canceled") {
this.resetContainerHtml().appendChild(
new DynamicCheckoutPaymentCancelledView(
Expand Down Expand Up @@ -448,6 +459,32 @@ module ProcessOut {
)
}

/**
* Must run first in the handleAction error callback: PO.js refocuses the window right after it.
* Returns true when the tab_closed was a lost opener and DC shows pending instead of cancelled.
*/
private handleOpenerLoss(error: Exception, actionStartedAt: number): boolean {
const { apm } = this.paymentMethod
const isOpenerLoss = DynamicCheckoutOpenerLossUtils.isOpenerLoss(
this.processOutInstance,
error,
{
mode: apm.opener_loss_handling,
gatewayName: apm.gateway_name,
invoiceId: this.paymentConfig.invoiceId,
actionStartedAt,
},
)
if (!isOpenerLoss) {
return false
}

this.resetContainerHtml().appendChild(
new DynamicCheckoutPaymentPendingView(this.processOutInstance, this.paymentConfig).element,
)
return true
}

private getChildrenElement() {
const saveForFutureAttributes: Record<string, string> = {
type: "checkbox",
Expand Down
45 changes: 39 additions & 6 deletions src/dynamic-checkout/payment-methods/saved-apm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ module ProcessOut {
apm_customer_token.gateway_name,
)

const redirectUrl = Object.keys(additionalData).length > 0
? this.processOutInstance.appendAdditionalDataToUrl(
apm_customer_token.redirect_url,
additionalData,
)
: apm_customer_token.redirect_url
const redirectUrl = DynamicCheckoutOpenerLossUtils.markRedirectUrl(
Object.keys(additionalData).length > 0
? this.processOutInstance.appendAdditionalDataToUrl(
apm_customer_token.redirect_url,
additionalData,
)
: apm_customer_token.redirect_url,
)

DynamicCheckoutEventsUtils.dispatchPaymentSubmittedEvent({
payment_method_name: apm_customer_token.gateway_name,
Expand All @@ -87,6 +89,7 @@ module ProcessOut {
customer_token_id: apm_customer_token.customer_token_id,
})

const actionStartedAt = Date.now()
return this.processOutInstance.handleAction(
redirectUrl,
paymentToken => {
Expand Down Expand Up @@ -168,6 +171,10 @@ module ProcessOut {
)
},
error => {
if (this.handleOpenerLoss(error, actionStartedAt)) {
return
}

if (error.code === "customer.canceled") {
this.resetContainerHtml().appendChild(
new DynamicCheckoutPaymentCancelledView(
Expand Down Expand Up @@ -226,6 +233,32 @@ module ProcessOut {
)
}

/**
* Must run first in the handleAction error callback: PO.js refocuses the window right after it.
* Returns true when the tab_closed was a lost opener and DC shows pending instead of cancelled.
*/
private handleOpenerLoss(error: Exception, actionStartedAt: number): boolean {
const { apm_customer_token } = this.paymentMethod
const isOpenerLoss = DynamicCheckoutOpenerLossUtils.isOpenerLoss(
this.processOutInstance,
error,
{
mode: apm_customer_token.opener_loss_handling,
gatewayName: apm_customer_token.gateway_name,
invoiceId: this.paymentConfig.invoiceId,
actionStartedAt,
},
)
if (!isOpenerLoss) {
return false
}

this.resetContainerHtml().appendChild(
new DynamicCheckoutPaymentPendingView(this.processOutInstance, this.paymentConfig).element,
)
return true
}

private handlePaymentSuccess(invoiceId: string, data) {
this.resetContainerHtml().appendChild(
new DynamicCheckoutPaymentSuccessView(this.processOutInstance, this.paymentConfig).element,
Expand Down
1 change: 1 addition & 0 deletions src/dynamic-checkout/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
/// <reference path="utils/events.ts" />
/// <reference path="utils/translations.ts" />
/// <reference path="utils/status-messages.ts" />
/// <reference path="utils/opener-loss.ts" />
/// <reference path="utils/elements.ts" />
/// <reference path="clients/apple-pay.ts" />
/// <reference path="clients/google-pay.ts" />
2 changes: 2 additions & 0 deletions src/dynamic-checkout/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type Apm = {
redirect_url?: string
customer_token_id?: string
gateway_name?: string
// "off" | "shadow" | "on"; see DynamicCheckoutOpenerLossUtils
opener_loss_handling?: string
gateway_logo: {
light_url: {
raster: string
Expand Down
120 changes: 120 additions & 0 deletions src/dynamic-checkout/utils/opener-loss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
module ProcessOut {
/**
* Rollout mode for treating a `tab_closed` as a lost opener (e.g. after a COOP page) instead of a cancel.
* Sent by the API per APM payment method; a missing value means "off".
*/
export type OpenerLossHandling = "off" | "shadow" | "on"

export type OpenerLossVerdict = "real_close" | "severed_link"

export type OpenerLossPageState = {
hasFocus: boolean
visibilityState: string
}

export type OpenerLossContext = {
mode?: string
gatewayName?: string
invoiceId?: string
actionStartedAt?: number
}

const OPENER_LOSS_MODES: OpenerLossHandling[] = ["off", "shadow", "on"]
// Read by checkout /redirect, so /continue can show a "you can close this tab" landing instead of
// forwarding to the merchant when the PSP tab lost its link to the checkout.
const DYNAMIC_CHECKOUT_FLOW_QUERY = "po_flow=dynamic-checkout"
const OPENER_LOSS_TELEMETRY_MESSAGE = "dynamic checkout opener loss verdict"

export class DynamicCheckoutOpenerLossUtils {
static markRedirectUrl(url: string): string {
if (!url) {
return url
}
return url + (url.indexOf("?") === -1 ? "?" : "&") + DYNAMIC_CHECKOUT_FLOW_QUERY
}

static getMode(value?: string): OpenerLossHandling {
return OPENER_LOSS_MODES.indexOf(value as OpenerLossHandling) !== -1
? (value as OpenerLossHandling)
: "off"
}

static readPageState(doc: Document = document): OpenerLossPageState {
return {
hasFocus: doc.hasFocus(),
visibilityState: doc.visibilityState,
}
}

/**
* After a real close the browser activates the DC page before PO.js notices the closed tab.
* A severed opener leaves the shopper on the PSP tab, so the DC page is hidden or unfocused.
*/
static getVerdict(state: OpenerLossPageState): OpenerLossVerdict {
return state.hasFocus && state.visibilityState === "visible" ? "real_close" : "severed_link"
}

/**
* Decides how DC handles a `customer.canceled` with reason `tab_closed`. It must run synchronously at the start of the error
* callback: PO.js calls `window.focus()` right after it, which would change the page state.
* Returns true when DC should treat the payment as pending instead of cancelled.
*/
static isOpenerLoss(
processOutInstance: ProcessOut,
error: Exception,
context: OpenerLossContext,
doc: Document = document,
): boolean {
if (
error.code !== "customer.canceled" ||
!error.metadata ||
error.metadata.reason !== "tab_closed"
) {
return false
}

const mode = DynamicCheckoutOpenerLossUtils.getMode(context.mode)
if (mode === "off") {
return false
}

const state = DynamicCheckoutOpenerLossUtils.readPageState(doc)
const verdict = DynamicCheckoutOpenerLossUtils.getVerdict(state)

DynamicCheckoutOpenerLossUtils.report(processOutInstance, context, mode, verdict, state)

return mode === "on" && verdict === "severed_link"
}

private static report(
processOutInstance: ProcessOut,
context: OpenerLossContext,
mode: OpenerLossHandling,
verdict: OpenerLossVerdict,
state: OpenerLossPageState,
) {
processOutInstance.telemetryClient.report(
{
host: window && window.location ? window.location.host : "",
fileName: "dynamic-checkout/utils/opener-loss.ts",
lineNumber: 0,
message: OPENER_LOSS_TELEMETRY_MESSAGE,
category: "dynamic-checkout",
invoiceId: context.invoiceId,
data: {
mode: mode,
verdict: verdict,
gateway_name: context.gatewayName || "",
has_focus: state.hasFocus,
visibility_state: state.visibilityState,
ms_since_action_start: context.actionStartedAt
? Date.now() - context.actionStartedAt
: null,
user_agent: typeof navigator !== "undefined" ? navigator.userAgent : "",
},
},
"info",
)
}
}
}
Loading
Loading