|
| 1 | +/* @flow */ |
| 2 | +/** @jsx node */ |
| 3 | +/* eslint max-lines: 0 */ |
| 4 | + |
| 5 | +import { node, dom } from "@krakenjs/jsx-pragmatic/src"; |
| 6 | +import { create, type ZoidComponent } from "@krakenjs/zoid/src"; |
| 7 | +import { inlineMemoize, noop } from "@krakenjs/belter/src"; |
| 8 | +import { getSDKMeta, getClientID, getCSPNonce } from "@paypal/sdk-client/src"; |
| 9 | +import { ZalgoPromise } from "@krakenjs/zalgo-promise/src"; |
| 10 | + |
| 11 | +import { Overlay } from "../overlay"; |
| 12 | +import { getCaptchaUrl } from "../config"; |
| 13 | + |
| 14 | +export type CaptchaResult = {||}; |
| 15 | + |
| 16 | +export const USER_TYPE = { |
| 17 | + BRANDED_GUEST: ("BRANDED_GUEST": "BRANDED_GUEST"), // inline guest flow |
| 18 | + UNBRANDED_GUEST: ("UNBRANDED_GUEST": "UNBRANDED_GUEST"), // UCC |
| 19 | + MEMBER: ("MEMBER": "MEMBER"), |
| 20 | +}; |
| 21 | + |
| 22 | +export type CaptchaProps = {| |
| 23 | + action: string, |
| 24 | + xcomponent: string, |
| 25 | + flow: string, |
| 26 | + orderID: string, |
| 27 | + onSuccess: (CaptchaResult) => void, |
| 28 | + onError: (mixed) => void, |
| 29 | + sdkMeta: string, |
| 30 | + content?: void | {| |
| 31 | + windowMessage?: string, |
| 32 | + continueMessage?: string, |
| 33 | + cancelMessage?: string, |
| 34 | + interrogativeMessage?: string, |
| 35 | + |}, |
| 36 | + userType: ?$Values<typeof USER_TYPE>, |
| 37 | + nonce: string, |
| 38 | +|}; |
| 39 | + |
| 40 | +export type CaptchaComponent = ZoidComponent<CaptchaProps>; |
| 41 | + |
| 42 | +export function getCaptchaComponent(): CaptchaComponent { |
| 43 | + return inlineMemoize(getCaptchaComponent, () => { |
| 44 | + const component = create({ |
| 45 | + tag: "captcha", |
| 46 | + url: getCaptchaUrl, |
| 47 | + |
| 48 | + attributes: { |
| 49 | + iframe: { |
| 50 | + scrolling: "no", |
| 51 | + }, |
| 52 | + }, |
| 53 | + |
| 54 | + containerTemplate: ({ |
| 55 | + context, |
| 56 | + focus, |
| 57 | + close, |
| 58 | + frame, |
| 59 | + prerenderFrame, |
| 60 | + doc, |
| 61 | + event, |
| 62 | + props, |
| 63 | + }) => { |
| 64 | + return ( |
| 65 | + <Overlay |
| 66 | + context={context} |
| 67 | + close={close} |
| 68 | + focus={focus} |
| 69 | + event={event} |
| 70 | + frame={frame} |
| 71 | + prerenderFrame={prerenderFrame} |
| 72 | + content={props.content} |
| 73 | + nonce={props.nonce} |
| 74 | + isUnbrandedFlow={true} |
| 75 | + /> |
| 76 | + ).render(dom({ doc })); |
| 77 | + }, |
| 78 | + |
| 79 | + props: { |
| 80 | + action: { |
| 81 | + type: "string", |
| 82 | + queryParam: true, |
| 83 | + value: (data) => (data.props.action ? data.props.action : "verify"), |
| 84 | + }, |
| 85 | + xcomponent: { |
| 86 | + type: "string", |
| 87 | + queryParam: true, |
| 88 | + value: () => "1", |
| 89 | + }, |
| 90 | + flow: { |
| 91 | + type: "string", |
| 92 | + queryParam: true, |
| 93 | + value: () => "rca", |
| 94 | + }, |
| 95 | + createOrder: { |
| 96 | + type: "function", |
| 97 | + queryParam: "token", |
| 98 | + // $FlowFixMe[incompatible-call] |
| 99 | + queryValue: ({ value }) => ZalgoPromise.try(value), |
| 100 | + required: false, |
| 101 | + }, |
| 102 | + token: { |
| 103 | + type: "string", |
| 104 | + queryParam: "token", |
| 105 | + queryValue: ({ value }) => value, |
| 106 | + required: false, |
| 107 | + }, |
| 108 | + clientID: { |
| 109 | + type: "string", |
| 110 | + value: getClientID, |
| 111 | + queryParam: true, |
| 112 | + }, |
| 113 | + onError: { |
| 114 | + type: "function", |
| 115 | + required: false, |
| 116 | + }, |
| 117 | + onSuccess: { |
| 118 | + type: "function", |
| 119 | + alias: "onContingencyResult", |
| 120 | + decorate: ({ props, value, onError }) => { |
| 121 | + return (err, result) => { |
| 122 | + const isCardFieldFlow = props?.userType === "UNBRANDED_GUEST"; |
| 123 | + |
| 124 | + const hasError = isCardFieldFlow |
| 125 | + ? Boolean(err) |
| 126 | + : // $FlowFixMe[incompatible-use] |
| 127 | + Boolean(err) || result?.success === false; |
| 128 | + |
| 129 | + if (hasError) { |
| 130 | + if (onError) { |
| 131 | + return onError( |
| 132 | + err || new Error("CAPTCHA verification failed") |
| 133 | + ); |
| 134 | + } |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + return value(result); |
| 139 | + }; |
| 140 | + }, |
| 141 | + }, |
| 142 | + onCancel: { |
| 143 | + type: "function", |
| 144 | + required: false, |
| 145 | + }, |
| 146 | + sdkMeta: { |
| 147 | + type: "string", |
| 148 | + queryParam: true, |
| 149 | + sendToChild: false, |
| 150 | + value: getSDKMeta, |
| 151 | + }, |
| 152 | + content: { |
| 153 | + type: "object", |
| 154 | + required: false, |
| 155 | + }, |
| 156 | + userType: { |
| 157 | + type: "string", |
| 158 | + required: false, |
| 159 | + }, |
| 160 | + nonce: { |
| 161 | + type: "string", |
| 162 | + default: getCSPNonce, |
| 163 | + }, |
| 164 | + integrationType: { |
| 165 | + type: "string", |
| 166 | + required: false, |
| 167 | + queryParam: true, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }); |
| 171 | + |
| 172 | + if (component.isChild()) { |
| 173 | + window.xchild = { |
| 174 | + props: component.xprops, |
| 175 | + close: noop, |
| 176 | + }; |
| 177 | + } |
| 178 | + return component; |
| 179 | + }); |
| 180 | +} |
0 commit comments