@lightspeed/online-payments-sdk 0.1.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,88 +1,4 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
1
+ import { v1 } from './v1';
2
+ export var LightspeedPayments = {
3
+ v1: v1,
9
4
  };
10
- var __generator = (this && this.__generator) || function (thisArg, body) {
11
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
- function verb(n) { return function (v) { return step([n, v]); }; }
14
- function step(op) {
15
- if (f) throw new TypeError("Generator is already executing.");
16
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
- if (y = 0, t) op = [op[0] & 2, t.value];
19
- switch (op[0]) {
20
- case 0: case 1: t = op; break;
21
- case 4: _.label++; return { value: op[1], done: false };
22
- case 5: _.label++; y = op[1]; op = [0]; continue;
23
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
- default:
25
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
- if (t[2]) _.ops.pop();
30
- _.trys.pop(); continue;
31
- }
32
- op = body.call(thisArg, _);
33
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
- }
36
- };
37
- import { FrameMessenger } from './frameMessenger';
38
- var DefaultContentHostname = 'https://online-payment-widgets.lspay.lightspeed.app';
39
- function createOnlinePaymentsInterface(messenger) {
40
- var _this = this;
41
- return {
42
- collectPaymentMethod: function (paymentSessionId) { return __awaiter(_this, void 0, void 0, function () {
43
- var responseListeners, response;
44
- return __generator(this, function (_a) {
45
- switch (_a.label) {
46
- case 0:
47
- responseListeners = Promise.race([
48
- messenger.awaitMessage('COMPLETE'),
49
- messenger.awaitMessage('ERROR'),
50
- ]);
51
- messenger.publishMessage('SET_PAYMENT_SESSION_V1', { paymentSessionId: paymentSessionId });
52
- return [4 /*yield*/, responseListeners];
53
- case 1:
54
- response = _a.sent();
55
- if (response.type === 'COMPLETE') {
56
- return [2 /*return*/, {
57
- success: true,
58
- paymentId: response.payload.data.paymentId,
59
- }];
60
- }
61
- throw Error(response.payload.data.message);
62
- }
63
- });
64
- }); },
65
- };
66
- }
67
- export function mountLightspeedPayments(mountPoint, configuration) {
68
- var _a;
69
- if (configuration === void 0) { configuration = {}; }
70
- return __awaiter(this, void 0, void 0, function () {
71
- var contentHostname, lspIframe, messenger, ready;
72
- return __generator(this, function (_b) {
73
- contentHostname = (_a = configuration === null || configuration === void 0 ? void 0 : configuration.contentHostnameOverride) !== null && _a !== void 0 ? _a : DefaultContentHostname;
74
- lspIframe = window.document.createElement('iframe');
75
- lspIframe.height = '800px';
76
- lspIframe.width = '700px';
77
- lspIframe.src = contentHostname;
78
- if (!mountPoint) {
79
- throw Error('HtmlElement to mount checkout to is required. Received undefined');
80
- }
81
- messenger = FrameMessenger.createFrameMessenger(window, lspIframe, contentHostname);
82
- ready = messenger.awaitMessage('READY');
83
- mountPoint.appendChild(lspIframe);
84
- // Only return the messenger once it is verified that the content within the iframe is listening
85
- return [2 /*return*/, ready.then(function () { return createOnlinePaymentsInterface(messenger); })];
86
- });
87
- });
88
- }
@@ -0,0 +1,5 @@
1
+ import type { ResultCode as AdyenResultCode } from '@adyen/adyen-web';
2
+ import { AdyenContext } from '../AdyenSession';
3
+ import { WidgetInterface, ResultCode } from './common';
4
+ export declare function mapAdyenResultCode(code: AdyenResultCode): ResultCode;
5
+ export declare const AdyenWidget: WidgetInterface<AdyenContext>;
@@ -0,0 +1,121 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { AdyenCheckout, Dropin, Card, GooglePay, ApplePay, } from '@adyen/adyen-web';
38
+ import { PaymentProcessingError } from './errors';
39
+ export function mapAdyenResultCode(code) {
40
+ switch (code) {
41
+ case 'Authorised':
42
+ return 'Authorized';
43
+ case 'Refused':
44
+ return 'Refused';
45
+ default:
46
+ return 'Unexpected';
47
+ }
48
+ }
49
+ function mount(mountElement, context, listeners) {
50
+ return __awaiter(this, void 0, void 0, function () {
51
+ var checkoutConfig, checkout, dropinConfiguration, dropIn;
52
+ return __generator(this, function (_a) {
53
+ switch (_a.label) {
54
+ case 0:
55
+ checkoutConfig = createAdyenConfiguration(context, listeners);
56
+ return [4 /*yield*/, AdyenCheckout(checkoutConfig)];
57
+ case 1:
58
+ checkout = _a.sent();
59
+ dropinConfiguration = createDropinConfiguration();
60
+ dropIn = new Dropin(checkout, dropinConfiguration).mount(mountElement);
61
+ return [2 /*return*/, {
62
+ unmount: function () { return dropIn.unmount(); },
63
+ submit: function () {
64
+ return new Promise(function (resolve) {
65
+ dropIn.submit();
66
+ resolve({
67
+ status: 'Pending',
68
+ code: 'Success',
69
+ });
70
+ });
71
+ },
72
+ }];
73
+ }
74
+ });
75
+ });
76
+ }
77
+ function createAdyenConfiguration(context, listeners) {
78
+ return {
79
+ clientKey: context.clientKey,
80
+ session: {
81
+ id: context.sessionId,
82
+ sessionData: context.sessionData,
83
+ },
84
+ countryCode: context.countryCode,
85
+ amount: {
86
+ value: context.amount,
87
+ currency: context.currency,
88
+ },
89
+ locale: context.locale,
90
+ environment: context.env,
91
+ analytics: {
92
+ enabled: false,
93
+ },
94
+ onPaymentCompleted: function (data) {
95
+ var _a;
96
+ (_a = listeners === null || listeners === void 0 ? void 0 : listeners.onComplete) === null || _a === void 0 ? void 0 : _a.call(listeners, {
97
+ code: mapAdyenResultCode(data.resultCode),
98
+ status: 'Complete',
99
+ });
100
+ },
101
+ onPaymentFailed: function (data) {
102
+ var _a;
103
+ (_a = listeners === null || listeners === void 0 ? void 0 : listeners.onFail) === null || _a === void 0 ? void 0 : _a.call(listeners, {
104
+ status: 'Complete',
105
+ code: mapAdyenResultCode(data.resultCode),
106
+ });
107
+ },
108
+ onError: function (error) {
109
+ throw new PaymentProcessingError(error.message);
110
+ },
111
+ paymentMethodsResponse: context.paymentMethods,
112
+ };
113
+ }
114
+ function createDropinConfiguration() {
115
+ return {
116
+ paymentMethodComponents: [Card, GooglePay, ApplePay],
117
+ };
118
+ }
119
+ export var AdyenWidget = {
120
+ mount: mount,
121
+ };
@@ -0,0 +1,6 @@
1
+ import type { SetupIntent, PaymentIntent } from '@stripe/stripe-js';
2
+ import { StripeContext } from 'src/StripeSession';
3
+ import { Callbacks, ResultCode, WidgetController, WidgetInterface } from './common';
4
+ export declare function mapStripeResultCode(code: SetupIntent.Status | PaymentIntent.Status): ResultCode;
5
+ export declare function mount(mountElement: HTMLElement, context: StripeContext, listeners: Callbacks): Promise<WidgetController>;
6
+ export declare const StripeWidget: WidgetInterface<StripeContext>;
@@ -0,0 +1,144 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { loadStripe } from '@stripe/stripe-js';
38
+ export function mapStripeResultCode(code) {
39
+ switch (code) {
40
+ case 'succeeded':
41
+ return 'Success';
42
+ default:
43
+ return 'Unexpected';
44
+ }
45
+ }
46
+ function init(context) {
47
+ return __awaiter(this, void 0, void 0, function () {
48
+ var publishableKey, clientSecret, customerSessionClientSecret, loadedStripe, stripe, elements;
49
+ return __generator(this, function (_a) {
50
+ switch (_a.label) {
51
+ case 0:
52
+ publishableKey = context.publishableKey, clientSecret = context.clientSecret, customerSessionClientSecret = context.customerSessionClientSecret;
53
+ return [4 /*yield*/, loadStripe(publishableKey)];
54
+ case 1:
55
+ loadedStripe = _a.sent();
56
+ if (!loadedStripe) {
57
+ throw new Error('Failed to initialize Stripe');
58
+ }
59
+ stripe = loadedStripe;
60
+ elements = stripe.elements({
61
+ clientSecret: clientSecret,
62
+ customerSessionClientSecret: customerSessionClientSecret,
63
+ });
64
+ return [2 /*return*/, { stripe: stripe, elements: elements }];
65
+ }
66
+ });
67
+ });
68
+ }
69
+ export function mount(mountElement, context, listeners) {
70
+ return __awaiter(this, void 0, void 0, function () {
71
+ var _a, stripe, elements, paymentElement;
72
+ var _this = this;
73
+ return __generator(this, function (_b) {
74
+ switch (_b.label) {
75
+ case 0: return [4 /*yield*/, init(context)];
76
+ case 1:
77
+ _a = _b.sent(), stripe = _a.stripe, elements = _a.elements;
78
+ paymentElement = elements.create('payment');
79
+ paymentElement.update({
80
+ fields: {
81
+ billingDetails: {
82
+ address: {
83
+ postalCode: 'never',
84
+ country: 'never',
85
+ },
86
+ },
87
+ },
88
+ });
89
+ paymentElement.mount(mountElement);
90
+ return [2 /*return*/, {
91
+ unmount: function () { return paymentElement.unmount(); },
92
+ submit: function () { return __awaiter(_this, void 0, void 0, function () {
93
+ var result, _a, error, setupIntent, _b, error, paymentIntent;
94
+ var _c, _d;
95
+ return __generator(this, function (_e) {
96
+ switch (_e.label) {
97
+ case 0:
98
+ result = { status: 'Pending', code: 'Unexpected' };
99
+ if (!(context.intent === 'save-card')) return [3 /*break*/, 2];
100
+ return [4 /*yield*/, stripe.confirmSetup({
101
+ elements: elements,
102
+ redirect: 'if_required',
103
+ confirmParams: { return_url: window.location.href },
104
+ })];
105
+ case 1:
106
+ _a = _e.sent(), error = _a.error, setupIntent = _a.setupIntent;
107
+ result = {
108
+ status: 'Complete',
109
+ code: error ? 'Failure' : mapStripeResultCode(setupIntent.status),
110
+ };
111
+ _e.label = 2;
112
+ case 2:
113
+ if (!(context.intent === 'payment')) return [3 /*break*/, 4];
114
+ return [4 /*yield*/, stripe.confirmPayment({
115
+ elements: elements,
116
+ redirect: 'if_required',
117
+ confirmParams: { return_url: window.location.href },
118
+ })];
119
+ case 3:
120
+ _b = _e.sent(), error = _b.error, paymentIntent = _b.paymentIntent;
121
+ result = {
122
+ status: 'Complete',
123
+ code: error ? 'Failure' : mapStripeResultCode(paymentIntent.status),
124
+ };
125
+ _e.label = 4;
126
+ case 4:
127
+ if (result.code === 'Failure') {
128
+ (_c = listeners.onFail) === null || _c === void 0 ? void 0 : _c.call(listeners, result);
129
+ }
130
+ else {
131
+ (_d = listeners.onComplete) === null || _d === void 0 ? void 0 : _d.call(listeners, result);
132
+ }
133
+ return [2 /*return*/, result];
134
+ }
135
+ });
136
+ }); },
137
+ }];
138
+ }
139
+ });
140
+ });
141
+ }
142
+ export var StripeWidget = {
143
+ mount: mount,
144
+ };
@@ -0,0 +1,17 @@
1
+ export type ResultCode = 'Authorized' | 'Success' | 'Failure' | 'Refused' | 'Unexpected';
2
+ export type Status = 'Pending' | 'Complete';
3
+ export type Result = {
4
+ status: Status;
5
+ code: ResultCode;
6
+ };
7
+ export type WidgetController = {
8
+ unmount: () => void;
9
+ submit: () => Promise<Result>;
10
+ };
11
+ export type Callbacks = {
12
+ onComplete?(result: Result): void;
13
+ onFail?(result: Result): void;
14
+ };
15
+ export type WidgetInterface<T> = {
16
+ mount: (mountElement: HTMLElement, context: T, listeners: Callbacks) => Promise<WidgetController>;
17
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,42 @@
1
+ export declare enum ErrorCode {
2
+ UnsupportedLocation = "UnsupportedLocation",
3
+ InvalidSessionStructure = "InvalidSessionStructure",
4
+ InvalidSessionError = "InvalidSessionError",
5
+ PaymentProcessingError = "PaymentProcessingError"
6
+ }
7
+ export interface CustomError {
8
+ code: ErrorCode;
9
+ }
10
+ declare class BaseError extends Error implements CustomError {
11
+ code: ErrorCode;
12
+ constructor(code: ErrorCode, message: string);
13
+ }
14
+ /**
15
+ * Error thrown when the location is unsupported.
16
+ * @typedef {Error} UnsupportedLocationError
17
+ */
18
+ export declare class UnsupportedLocationError extends BaseError {
19
+ constructor();
20
+ }
21
+ /**
22
+ * Error thrown when the session structure is invalid.
23
+ * @typedef {Error} InvalidSessionStructureError
24
+ */
25
+ export declare class InvalidSessionStructureError extends BaseError {
26
+ constructor();
27
+ }
28
+ /**
29
+ * Error thrown when the session is invalid.
30
+ * @typedef {Error} InvalidSessionError
31
+ */
32
+ export declare class InvalidSessionError extends BaseError {
33
+ constructor();
34
+ }
35
+ /**
36
+ * Error thrown when the session structure is invalid.
37
+ * @typedef {Error} PaymentProcessingError
38
+ */
39
+ export declare class PaymentProcessingError extends BaseError {
40
+ constructor(message: string);
41
+ }
42
+ export {};
@@ -0,0 +1,79 @@
1
+ var __extends = (this && this.__extends) || (function () {
2
+ var extendStatics = function (d, b) {
3
+ extendStatics = Object.setPrototypeOf ||
4
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
+ return extendStatics(d, b);
7
+ };
8
+ return function (d, b) {
9
+ if (typeof b !== "function" && b !== null)
10
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
+ extendStatics(d, b);
12
+ function __() { this.constructor = d; }
13
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
+ };
15
+ })();
16
+ export var ErrorCode;
17
+ (function (ErrorCode) {
18
+ ErrorCode["UnsupportedLocation"] = "UnsupportedLocation";
19
+ ErrorCode["InvalidSessionStructure"] = "InvalidSessionStructure";
20
+ ErrorCode["InvalidSessionError"] = "InvalidSessionError";
21
+ ErrorCode["PaymentProcessingError"] = "PaymentProcessingError";
22
+ })(ErrorCode || (ErrorCode = {}));
23
+ var BaseError = /** @class */ (function (_super) {
24
+ __extends(BaseError, _super);
25
+ function BaseError(code, message) {
26
+ var _this = _super.call(this, message) || this;
27
+ _this.code = code;
28
+ return _this;
29
+ }
30
+ return BaseError;
31
+ }(Error));
32
+ /**
33
+ * Error thrown when the location is unsupported.
34
+ * @typedef {Error} UnsupportedLocationError
35
+ */
36
+ var UnsupportedLocationError = /** @class */ (function (_super) {
37
+ __extends(UnsupportedLocationError, _super);
38
+ function UnsupportedLocationError() {
39
+ return _super.call(this, ErrorCode.UnsupportedLocation, 'The location you are attempting to process with is not supported') || this;
40
+ }
41
+ return UnsupportedLocationError;
42
+ }(BaseError));
43
+ export { UnsupportedLocationError };
44
+ /**
45
+ * Error thrown when the session structure is invalid.
46
+ * @typedef {Error} InvalidSessionStructureError
47
+ */
48
+ var InvalidSessionStructureError = /** @class */ (function (_super) {
49
+ __extends(InvalidSessionStructureError, _super);
50
+ function InvalidSessionStructureError() {
51
+ return _super.call(this, ErrorCode.InvalidSessionStructure, 'The contents of the session are invalid') || this;
52
+ }
53
+ return InvalidSessionStructureError;
54
+ }(BaseError));
55
+ export { InvalidSessionStructureError };
56
+ /**
57
+ * Error thrown when the session is invalid.
58
+ * @typedef {Error} InvalidSessionError
59
+ */
60
+ var InvalidSessionError = /** @class */ (function (_super) {
61
+ __extends(InvalidSessionError, _super);
62
+ function InvalidSessionError() {
63
+ return _super.call(this, ErrorCode.InvalidSessionError, 'The provided session is invalid. Verify the correct session is being passed') || this;
64
+ }
65
+ return InvalidSessionError;
66
+ }(BaseError));
67
+ export { InvalidSessionError };
68
+ /**
69
+ * Error thrown when the session structure is invalid.
70
+ * @typedef {Error} PaymentProcessingError
71
+ */
72
+ var PaymentProcessingError = /** @class */ (function (_super) {
73
+ __extends(PaymentProcessingError, _super);
74
+ function PaymentProcessingError(message) {
75
+ return _super.call(this, ErrorCode.PaymentProcessingError, "An error occurred while processing the payment. ".concat(message)) || this;
76
+ }
77
+ return PaymentProcessingError;
78
+ }(BaseError));
79
+ export { PaymentProcessingError };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @fileoverview Entry point for version 1 of the LSP Online Payments SDK.
3
+ * This module exports the necessary functions and types for mounting the payment widget
4
+ * and handling errors related to payment sessions.
5
+ */
6
+ import { WidgetController } from './common';
7
+ import { PaymentWidgetConfiguration } from './widget';
8
+ export type { Result, WidgetController } from './common';
9
+ export { InvalidSessionStructureError, UnsupportedLocationError, InvalidSessionError, } from './errors';
10
+ export type { PaymentWidgetConfiguration } from './widget';
11
+ export type LightspeedPaymentsV1 = {
12
+ /**
13
+ * Mounts the payment widget to the specified mount point.
14
+ * @function mountPaymentWidget
15
+ * @param {string} session - The session ID for the payment session.
16
+ * @param {PaymentWidgetConfiguration} config - The configuration options for the payment widget.
17
+ */
18
+ mountPaymentWidget(session: string, { mountPoint, onComplete, onFail }: PaymentWidgetConfiguration): Promise<WidgetController>;
19
+ };
20
+ export declare const v1: LightspeedPaymentsV1;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @fileoverview Entry point for version 1 of the LSP Online Payments SDK.
3
+ * This module exports the necessary functions and types for mounting the payment widget
4
+ * and handling errors related to payment sessions.
5
+ */
6
+ import { mountPaymentWidget } from './widget';
7
+ export { InvalidSessionStructureError, UnsupportedLocationError, InvalidSessionError, } from './errors';
8
+ export var v1 = {
9
+ mountPaymentWidget: mountPaymentWidget,
10
+ };
@@ -0,0 +1,13 @@
1
+ import { StripeSession } from 'src/StripeSession';
2
+ import { AdyenSession } from 'src/AdyenSession';
3
+ import { Callbacks, WidgetController } from 'src/v1/common';
4
+ type Session = {
5
+ metadata: Record<string, string>;
6
+ } & (AdyenSession | StripeSession);
7
+ export type PaymentWidgetConfiguration = {
8
+ mountPoint: HTMLElement;
9
+ } & Callbacks;
10
+ export declare function mountPaymentWidget(session: string, { mountPoint, onComplete, onFail }: PaymentWidgetConfiguration): Promise<WidgetController>;
11
+ export declare function decodePaymentSessionToContext(paymentSession: string): Session;
12
+ export declare function isTokenValid(token: unknown): token is Session;
13
+ export {};
@@ -0,0 +1,41 @@
1
+ import { AdyenWidget } from './AdyenWidget';
2
+ import { InvalidSessionError, InvalidSessionStructureError, UnsupportedLocationError, } from './errors';
3
+ import { StripeWidget } from './StripeWidget';
4
+ export function mountPaymentWidget(session, _a) {
5
+ var mountPoint = _a.mountPoint, onComplete = _a.onComplete, onFail = _a.onFail;
6
+ var decodedSession = decodePaymentSessionToContext(session);
7
+ switch (decodedSession.psp) {
8
+ case 'ADYEN':
9
+ return AdyenWidget.mount(mountPoint, decodedSession.context, {
10
+ onComplete: onComplete,
11
+ onFail: onFail,
12
+ });
13
+ case 'STRIPE':
14
+ return StripeWidget.mount(mountPoint, decodedSession.context, {
15
+ onComplete: onComplete,
16
+ onFail: onFail,
17
+ });
18
+ default:
19
+ throw new UnsupportedLocationError();
20
+ }
21
+ }
22
+ export function decodePaymentSessionToContext(paymentSession) {
23
+ try {
24
+ var decoded = atob(paymentSession);
25
+ var jsonToken = JSON.parse(decoded);
26
+ if (jsonToken && isTokenValid(jsonToken)) {
27
+ return jsonToken;
28
+ }
29
+ throw new InvalidSessionStructureError();
30
+ }
31
+ catch (e) {
32
+ throw new InvalidSessionError();
33
+ }
34
+ }
35
+ export function isTokenValid(token) {
36
+ var t = token;
37
+ if (t && !!t.psp && !!t.context) {
38
+ return true;
39
+ }
40
+ return false;
41
+ }
package/package.json CHANGED
@@ -1,14 +1,19 @@
1
1
  {
2
2
  "name": "@lightspeed/online-payments-sdk",
3
- "version": "0.1.5",
3
+ "version": "0.2.0",
4
4
  "description": "Process online-payments with Lightspeed Payments",
5
5
  "author": "Lightspeed Commerce Inc.",
6
6
  "license": "SEE LICENSE IN LICENSE.md",
7
7
  "main": "dist/cjs/index.js",
8
8
  "module": "dist/index.js",
9
9
  "types": "dist/index.d.ts",
10
- "publishConfig": { "access": "public" },
11
- "files": ["dist"],
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "styles"
16
+ ],
12
17
  "sideEffects": false,
13
18
  "scripts": {
14
19
  "test": "jest --no-cache --detectOpenHandles",
@@ -22,13 +27,17 @@
22
27
  },
23
28
  "devDependencies": {
24
29
  "@types/jest": "^29.5.13",
25
- "gts": "^5.3.1",
30
+ "gts": "^6.0.2",
26
31
  "jest": "^29.7.0",
27
32
  "jest-environment-jsdom": "^29.7.0",
28
33
  "jest-junit": "^16.0.0",
29
- "rollup": "^2.79.1",
34
+ "rollup": "^4.34.8",
30
35
  "ts-jest": "^29.2.5",
31
36
  "tslib": "^2.7.0",
32
37
  "typescript": "^5.1.5"
38
+ },
39
+ "dependencies": {
40
+ "@adyen/adyen-web": "^6.8.0",
41
+ "@stripe/stripe-js": "^7.3.0"
33
42
  }
34
43
  }
@@ -0,0 +1 @@
1
+ @import '@adyen/adyen-web/styles/adyen.css';