-
Notifications
You must be signed in to change notification settings - Fork 576
/
Copy pathcomponent.jsx
160 lines (145 loc) · 4.25 KB
/
component.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* @flow */
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable no-restricted-globals, promise/no-native */
import { type LoggerType } from "@krakenjs/beaver-logger/src";
import { type ZoidComponent } from "@krakenjs/zoid/src";
import { ZalgoPromise } from "@krakenjs/zalgo-promise/src";
import { FPTI_KEY } from "@paypal/sdk-constants/src";
import { ValidationError } from "../lib";
import type {
requestData,
responseBody,
Request,
MerchantPayloadData,
SdkConfig,
threeDSResponse,
} from "./types";
import { getThreeDomainSecureComponent } from "./utils";
const parseSdkConfig = ({ sdkConfig, logger }): SdkConfig => {
if (!sdkConfig.authenticationToken) {
throw new ValidationError(
`script data attribute sdk-client-token is required but was not passed`
);
}
logger.info("three domain secure v2 invoked").track({
[FPTI_KEY.TRANSITION]: "three_DS_auth_v2",
});
return sdkConfig;
};
const parseMerchantPayload = ({
merchantPayload,
}: {|
merchantPayload: MerchantPayloadData,
|}): requestData => {
const { threeDSRequested, amount, currency, nonce, transactionContext } =
merchantPayload;
return {
intent: "THREE_DS_VERIFICATION",
payment_source: {
card: {
single_use_token: nonce,
verification_method: threeDSRequested
? "SCA_ALWAYS"
: "SCA_WHEN_REQUIRED",
},
},
amount: {
currency_code: currency,
value: amount,
},
...transactionContext,
};
};
export interface ThreeDomainSecureComponentInterface {
isEligible(): Promise<boolean>;
show(): ZalgoPromise<threeDSResponse>;
}
export class ThreeDomainSecureComponent {
logger: LoggerType;
request: Request;
sdkConfig: SdkConfig;
authenticationURL: string;
threeDSIframe: ZoidComponent<void>;
constructor({
logger,
request,
sdkConfig,
}: {|
logger: LoggerType,
request: Request,
sdkConfig: SdkConfig,
|}) {
this.logger = logger;
this.request = request;
this.sdkConfig = parseSdkConfig({ sdkConfig, logger });
}
async isEligible(merchantPayload: MerchantPayloadData): Promise<boolean> {
// eslint-disable-next-line no-console
console.log("Entered IsEligible");
const data = parseMerchantPayload({ merchantPayload });
const idToken = merchantPayload.idToken;
try {
// $FlowFixMe
const { status, links } = await this.request<requestData, responseBody>({
method: "POST",
url: `https://te-fastlane-3ds.qa.paypal.com:12326/v2/payments/payment`,
data,
accessToken: idToken, // this.sdkConfig.authenticationToken,
});
let responseStatus = false;
if (status === "PAYER_ACTION_REQUIRED") {
this.authenticationURL = links.find(
(link) => link.rel === "payer-action"
).href;
responseStatus = true;
this.threeDSIframe = getThreeDomainSecureComponent(
this.authenticationURL
);
}
return responseStatus;
} catch (error) {
this.logger.warn(error);
throw error;
}
}
show(): ZalgoPromise<threeDSResponse> {
if (!this.threeDSIframe) {
return new ValidationError(`Ineligible for three domain secure`);
}
const promise = new ZalgoPromise();
const cancelThreeDS = () => {
return ZalgoPromise.try(() => {
// eslint-disable-next-line no-console
console.log("cancelled");
}).then(() => {
// eslint-disable-next-line no-use-before-define
instance.close();
});
};
const instance = this.threeDSIframe({
onSuccess: (data) => {
// const {threeDSRefID, authentication_status, liability_shift } = data;
// let enrichedNonce;
// if(threeDSRefID) {
// enrichedNonce = await updateNonceWith3dsData(threeDSRefID, this.fastlaneNonce)
// }
return promise.resolve(data);
},
onClose: cancelThreeDS,
onError: (err) => {
return promise.reject(
new Error(
`Error with obtaining 3DS contingency, ${JSON.stringify(err)}`
)
);
},
});
const TARGET_ELEMENT = {
BODY: "body",
};
return instance
.renderTo(window.parent, TARGET_ELEMENT.BODY)
.then(() => promise)
.finally(instance.close);
}
}