@lightspeed/online-payments-sdk 0.1.6 → 0.2.1

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.
@@ -1,4 +1,10 @@
1
1
  export type StripeSession = {
2
2
  psp: 'STRIPE';
3
- context: object;
3
+ context: StripeContext;
4
+ };
5
+ export type StripeContext = {
6
+ clientSecret: string;
7
+ publishableKey: string;
8
+ customerSessionClientSecret?: string;
9
+ intent: 'save-card' | 'payment';
4
10
  };
@@ -36,11 +36,23 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.AdyenWidget = void 0;
39
+ exports.AdyenWidget = exports.mapAdyenResultCode = void 0;
40
40
  var adyen_web_1 = require("@adyen/adyen-web");
41
+ var errors_1 = require("./errors");
42
+ function mapAdyenResultCode(code) {
43
+ switch (code) {
44
+ case 'Authorised':
45
+ return 'Authorized';
46
+ case 'Refused':
47
+ return 'Refused';
48
+ default:
49
+ return 'Unexpected';
50
+ }
51
+ }
52
+ exports.mapAdyenResultCode = mapAdyenResultCode;
41
53
  function mount(mountElement, context, listeners) {
42
54
  return __awaiter(this, void 0, void 0, function () {
43
- var checkoutConfig, checkout, dropinConfiguration;
55
+ var checkoutConfig, checkout, dropinConfiguration, dropIn;
44
56
  return __generator(this, function (_a) {
45
57
  switch (_a.label) {
46
58
  case 0:
@@ -49,8 +61,19 @@ function mount(mountElement, context, listeners) {
49
61
  case 1:
50
62
  checkout = _a.sent();
51
63
  dropinConfiguration = createDropinConfiguration();
52
- new adyen_web_1.Dropin(checkout, dropinConfiguration).mount(mountElement);
53
- return [2 /*return*/];
64
+ dropIn = new adyen_web_1.Dropin(checkout, dropinConfiguration).mount(mountElement);
65
+ return [2 /*return*/, {
66
+ unmount: function () { return dropIn.unmount(); },
67
+ submit: function () {
68
+ return new Promise(function (resolve) {
69
+ dropIn.submit();
70
+ resolve({
71
+ status: 'Pending',
72
+ code: 'Success',
73
+ });
74
+ });
75
+ },
76
+ }];
54
77
  }
55
78
  });
56
79
  });
@@ -74,17 +97,21 @@ function createAdyenConfiguration(context, listeners) {
74
97
  },
75
98
  onPaymentCompleted: function (data) {
76
99
  var _a;
77
- (_a = listeners === null || listeners === void 0 ? void 0 : listeners.onPaymentComplete) === null || _a === void 0 ? void 0 : _a.call(listeners, {
78
- code: data.resultCode,
100
+ (_a = listeners === null || listeners === void 0 ? void 0 : listeners.onComplete) === null || _a === void 0 ? void 0 : _a.call(listeners, {
101
+ code: mapAdyenResultCode(data.resultCode),
102
+ status: 'Complete',
79
103
  });
80
104
  },
81
105
  onPaymentFailed: function (data) {
82
106
  var _a;
83
- console.log(data);
84
- (_a = listeners === null || listeners === void 0 ? void 0 : listeners.onPaymentFail) === null || _a === void 0 ? void 0 : _a.call(listeners, {
85
- code: data.resultCode,
107
+ (_a = listeners === null || listeners === void 0 ? void 0 : listeners.onFail) === null || _a === void 0 ? void 0 : _a.call(listeners, {
108
+ status: 'Complete',
109
+ code: mapAdyenResultCode(data.resultCode),
86
110
  });
87
111
  },
112
+ onError: function (error) {
113
+ throw new errors_1.PaymentProcessingError(error.message);
114
+ },
88
115
  paymentMethodsResponse: context.paymentMethods,
89
116
  };
90
117
  }
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.StripeWidget = exports.mount = exports.mapStripeResultCode = void 0;
40
+ var stripe_js_1 = require("@stripe/stripe-js");
41
+ function mapStripeResultCode(code) {
42
+ switch (code) {
43
+ case 'succeeded':
44
+ return 'Success';
45
+ default:
46
+ return 'Unexpected';
47
+ }
48
+ }
49
+ exports.mapStripeResultCode = mapStripeResultCode;
50
+ function init(context) {
51
+ return __awaiter(this, void 0, void 0, function () {
52
+ var publishableKey, clientSecret, customerSessionClientSecret, loadedStripe, stripe, elements;
53
+ return __generator(this, function (_a) {
54
+ switch (_a.label) {
55
+ case 0:
56
+ publishableKey = context.publishableKey, clientSecret = context.clientSecret, customerSessionClientSecret = context.customerSessionClientSecret;
57
+ return [4 /*yield*/, (0, stripe_js_1.loadStripe)(publishableKey)];
58
+ case 1:
59
+ loadedStripe = _a.sent();
60
+ if (!loadedStripe) {
61
+ throw new Error('Failed to initialize Stripe');
62
+ }
63
+ stripe = loadedStripe;
64
+ elements = stripe.elements({
65
+ clientSecret: clientSecret,
66
+ customerSessionClientSecret: customerSessionClientSecret,
67
+ });
68
+ return [2 /*return*/, { stripe: stripe, elements: elements }];
69
+ }
70
+ });
71
+ });
72
+ }
73
+ function mount(mountElement, context, listeners, defaultValues) {
74
+ return __awaiter(this, void 0, void 0, function () {
75
+ var _a, stripe, elements, paymentElement;
76
+ var _this = this;
77
+ return __generator(this, function (_b) {
78
+ switch (_b.label) {
79
+ case 0: return [4 /*yield*/, init(context)];
80
+ case 1:
81
+ _a = _b.sent(), stripe = _a.stripe, elements = _a.elements;
82
+ paymentElement = elements.create('payment', defaultValues
83
+ ? {
84
+ defaultValues: {
85
+ billingDetails: {
86
+ address: {
87
+ country: defaultValues.country,
88
+ postal_code: defaultValues.postalCode,
89
+ },
90
+ },
91
+ },
92
+ }
93
+ : {});
94
+ paymentElement.mount(mountElement);
95
+ return [2 /*return*/, {
96
+ unmount: function () { return paymentElement.unmount(); },
97
+ submit: function () { return __awaiter(_this, void 0, void 0, function () {
98
+ var result, _a, error, setupIntent, _b, error, paymentIntent;
99
+ var _c, _d;
100
+ return __generator(this, function (_e) {
101
+ switch (_e.label) {
102
+ case 0:
103
+ result = { status: 'Pending', code: 'Unexpected' };
104
+ if (!(context.intent === 'save-card')) return [3 /*break*/, 2];
105
+ return [4 /*yield*/, stripe.confirmSetup({
106
+ elements: elements,
107
+ redirect: 'if_required',
108
+ confirmParams: { return_url: window.location.href },
109
+ })];
110
+ case 1:
111
+ _a = _e.sent(), error = _a.error, setupIntent = _a.setupIntent;
112
+ result = {
113
+ status: 'Complete',
114
+ code: error ? 'Failure' : mapStripeResultCode(setupIntent.status),
115
+ };
116
+ _e.label = 2;
117
+ case 2:
118
+ if (!(context.intent === 'payment')) return [3 /*break*/, 4];
119
+ return [4 /*yield*/, stripe.confirmPayment({
120
+ elements: elements,
121
+ redirect: 'if_required',
122
+ confirmParams: { return_url: window.location.href },
123
+ })];
124
+ case 3:
125
+ _b = _e.sent(), error = _b.error, paymentIntent = _b.paymentIntent;
126
+ result = {
127
+ status: 'Complete',
128
+ code: error ? 'Failure' : mapStripeResultCode(paymentIntent.status),
129
+ };
130
+ _e.label = 4;
131
+ case 4:
132
+ if (result.code === 'Failure') {
133
+ (_c = listeners.onFail) === null || _c === void 0 ? void 0 : _c.call(listeners, result);
134
+ }
135
+ else {
136
+ (_d = listeners.onComplete) === null || _d === void 0 ? void 0 : _d.call(listeners, result);
137
+ }
138
+ return [2 /*return*/, result];
139
+ }
140
+ });
141
+ }); },
142
+ }];
143
+ }
144
+ });
145
+ });
146
+ }
147
+ exports.mount = mount;
148
+ exports.StripeWidget = {
149
+ mount: mount,
150
+ };
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.PaymentProcessingError = exports.InvalidSessionError = exports.InvalidSessionStructureError = exports.UnsupportedLocationError = exports.ErrorCode = void 0;
19
+ var ErrorCode;
20
+ (function (ErrorCode) {
21
+ ErrorCode["UnsupportedLocation"] = "UnsupportedLocation";
22
+ ErrorCode["InvalidSessionStructure"] = "InvalidSessionStructure";
23
+ ErrorCode["InvalidSessionError"] = "InvalidSessionError";
24
+ ErrorCode["PaymentProcessingError"] = "PaymentProcessingError";
25
+ })(ErrorCode || (exports.ErrorCode = ErrorCode = {}));
26
+ var BaseError = /** @class */ (function (_super) {
27
+ __extends(BaseError, _super);
28
+ function BaseError(code, message) {
29
+ var _this = _super.call(this, message) || this;
30
+ _this.code = code;
31
+ return _this;
32
+ }
33
+ return BaseError;
34
+ }(Error));
35
+ /**
36
+ * Error thrown when the location is unsupported.
37
+ * @typedef {Error} UnsupportedLocationError
38
+ */
39
+ var UnsupportedLocationError = /** @class */ (function (_super) {
40
+ __extends(UnsupportedLocationError, _super);
41
+ function UnsupportedLocationError() {
42
+ return _super.call(this, ErrorCode.UnsupportedLocation, 'The location you are attempting to process with is not supported') || this;
43
+ }
44
+ return UnsupportedLocationError;
45
+ }(BaseError));
46
+ exports.UnsupportedLocationError = UnsupportedLocationError;
47
+ /**
48
+ * Error thrown when the session structure is invalid.
49
+ * @typedef {Error} InvalidSessionStructureError
50
+ */
51
+ var InvalidSessionStructureError = /** @class */ (function (_super) {
52
+ __extends(InvalidSessionStructureError, _super);
53
+ function InvalidSessionStructureError() {
54
+ return _super.call(this, ErrorCode.InvalidSessionStructure, 'The contents of the session are invalid') || this;
55
+ }
56
+ return InvalidSessionStructureError;
57
+ }(BaseError));
58
+ exports.InvalidSessionStructureError = InvalidSessionStructureError;
59
+ /**
60
+ * Error thrown when the session is invalid.
61
+ * @typedef {Error} InvalidSessionError
62
+ */
63
+ var InvalidSessionError = /** @class */ (function (_super) {
64
+ __extends(InvalidSessionError, _super);
65
+ function InvalidSessionError() {
66
+ return _super.call(this, ErrorCode.InvalidSessionError, 'The provided session is invalid. Verify the correct session is being passed') || this;
67
+ }
68
+ return InvalidSessionError;
69
+ }(BaseError));
70
+ exports.InvalidSessionError = InvalidSessionError;
71
+ /**
72
+ * Error thrown when the session structure is invalid.
73
+ * @typedef {Error} PaymentProcessingError
74
+ */
75
+ var PaymentProcessingError = /** @class */ (function (_super) {
76
+ __extends(PaymentProcessingError, _super);
77
+ function PaymentProcessingError(message) {
78
+ return _super.call(this, ErrorCode.PaymentProcessingError, "An error occurred while processing the payment. ".concat(message)) || this;
79
+ }
80
+ return PaymentProcessingError;
81
+ }(BaseError));
82
+ exports.PaymentProcessingError = PaymentProcessingError;
@@ -1,41 +1,16 @@
1
1
  "use strict";
2
+ /**
3
+ * @fileoverview Entry point for version 1 of the LSP Online Payments SDK.
4
+ * This module exports the necessary functions and types for mounting the payment widget
5
+ * and handling errors related to payment sessions.
6
+ */
2
7
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.v1 = void 0;
4
- var AdyenWidget_1 = require("./AdyenWidget");
5
- function mountPaymentWidget(sessionId, _a) {
6
- var mountPoint = _a.mountPoint, onPaymentComplete = _a.onPaymentComplete, onPaymentFail = _a.onPaymentFail;
7
- var session = decodePaymentSessionToContext(sessionId);
8
- switch (session.psp) {
9
- case 'ADYEN':
10
- return AdyenWidget_1.AdyenWidget.mount(mountPoint, session.context, {
11
- onPaymentComplete: onPaymentComplete,
12
- onPaymentFail: onPaymentFail,
13
- });
14
- case 'STRIPE':
15
- default:
16
- throw Error('Unsupported PSP');
17
- }
18
- }
19
- function decodePaymentSessionToContext(paymentSession) {
20
- try {
21
- var decoded = atob(paymentSession);
22
- var jsonToken = JSON.parse(decoded);
23
- if (jsonToken && isTokenValid(jsonToken)) {
24
- return jsonToken;
25
- }
26
- throw Error('invalid token structure');
27
- }
28
- catch (e) {
29
- throw Error("Payment token is not valid, ".concat(e));
30
- }
31
- }
32
- function isTokenValid(token) {
33
- var t = token;
34
- if (t && !!t.psp && !!t.context) {
35
- return true;
36
- }
37
- return false;
38
- }
8
+ exports.v1 = exports.InvalidSessionError = exports.UnsupportedLocationError = exports.InvalidSessionStructureError = void 0;
9
+ var widget_1 = require("./widget");
10
+ var errors_1 = require("./errors");
11
+ Object.defineProperty(exports, "InvalidSessionStructureError", { enumerable: true, get: function () { return errors_1.InvalidSessionStructureError; } });
12
+ Object.defineProperty(exports, "UnsupportedLocationError", { enumerable: true, get: function () { return errors_1.UnsupportedLocationError; } });
13
+ Object.defineProperty(exports, "InvalidSessionError", { enumerable: true, get: function () { return errors_1.InvalidSessionError; } });
39
14
  exports.v1 = {
40
- mountPaymentWidget: mountPaymentWidget,
15
+ mountPaymentWidget: widget_1.mountPaymentWidget,
41
16
  };
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isTokenValid = exports.decodePaymentSessionToContext = exports.mountPaymentWidget = void 0;
4
+ var AdyenWidget_1 = require("./AdyenWidget");
5
+ var errors_1 = require("./errors");
6
+ var StripeWidget_1 = require("./StripeWidget");
7
+ function mountPaymentWidget(session, _a) {
8
+ var mountPoint = _a.mountPoint, onComplete = _a.onComplete, onFail = _a.onFail, defaultValues = _a.defaultValues;
9
+ var decodedSession = decodePaymentSessionToContext(session);
10
+ switch (decodedSession.psp) {
11
+ case 'ADYEN':
12
+ return AdyenWidget_1.AdyenWidget.mount(mountPoint, decodedSession.context, {
13
+ onComplete: onComplete,
14
+ onFail: onFail,
15
+ });
16
+ case 'STRIPE':
17
+ return StripeWidget_1.StripeWidget.mount(mountPoint, decodedSession.context, {
18
+ onComplete: onComplete,
19
+ onFail: onFail,
20
+ }, defaultValues);
21
+ default:
22
+ throw new errors_1.UnsupportedLocationError();
23
+ }
24
+ }
25
+ exports.mountPaymentWidget = mountPaymentWidget;
26
+ function decodePaymentSessionToContext(paymentSession) {
27
+ try {
28
+ var decoded = atob(paymentSession);
29
+ var jsonToken = JSON.parse(decoded);
30
+ if (jsonToken && isTokenValid(jsonToken)) {
31
+ return jsonToken;
32
+ }
33
+ throw new errors_1.InvalidSessionStructureError();
34
+ }
35
+ catch (e) {
36
+ throw new errors_1.InvalidSessionError();
37
+ }
38
+ }
39
+ exports.decodePaymentSessionToContext = decodePaymentSessionToContext;
40
+ function isTokenValid(token) {
41
+ var t = token;
42
+ if (t && !!t.psp && !!t.context) {
43
+ return true;
44
+ }
45
+ return false;
46
+ }
47
+ exports.isTokenValid = isTokenValid;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,4 @@
1
- export type { PaymentWidgetConfiguration } from './v1';
2
- export type { CompleteResult, FailedResult } from './common';
1
+ export type * from './v1';
3
2
  export declare const LightspeedPayments: {
4
- v1: {
5
- mountPaymentWidget(sessionId: string, { mountPoint, onPaymentComplete, onPaymentFail }: import("./v1").PaymentWidgetConfiguration): void;
6
- };
3
+ v1: import("./v1").LightspeedPaymentsV1;
7
4
  };
@@ -1,3 +1,5 @@
1
+ import type { ResultCode as AdyenResultCode } from '@adyen/adyen-web';
1
2
  import { AdyenContext } from '../AdyenSession';
2
- import { WidgetInterface } from '../common';
3
+ import { WidgetInterface, ResultCode } from './common';
4
+ export declare function mapAdyenResultCode(code: AdyenResultCode): ResultCode;
3
5
  export declare const AdyenWidget: WidgetInterface<AdyenContext>;
@@ -35,9 +35,20 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  }
36
36
  };
37
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
+ }
38
49
  function mount(mountElement, context, listeners) {
39
50
  return __awaiter(this, void 0, void 0, function () {
40
- var checkoutConfig, checkout, dropinConfiguration;
51
+ var checkoutConfig, checkout, dropinConfiguration, dropIn;
41
52
  return __generator(this, function (_a) {
42
53
  switch (_a.label) {
43
54
  case 0:
@@ -46,8 +57,19 @@ function mount(mountElement, context, listeners) {
46
57
  case 1:
47
58
  checkout = _a.sent();
48
59
  dropinConfiguration = createDropinConfiguration();
49
- new Dropin(checkout, dropinConfiguration).mount(mountElement);
50
- return [2 /*return*/];
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
+ }];
51
73
  }
52
74
  });
53
75
  });
@@ -71,17 +93,21 @@ function createAdyenConfiguration(context, listeners) {
71
93
  },
72
94
  onPaymentCompleted: function (data) {
73
95
  var _a;
74
- (_a = listeners === null || listeners === void 0 ? void 0 : listeners.onPaymentComplete) === null || _a === void 0 ? void 0 : _a.call(listeners, {
75
- code: data.resultCode,
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',
76
99
  });
77
100
  },
78
101
  onPaymentFailed: function (data) {
79
102
  var _a;
80
- console.log(data);
81
- (_a = listeners === null || listeners === void 0 ? void 0 : listeners.onPaymentFail) === null || _a === void 0 ? void 0 : _a.call(listeners, {
82
- code: data.resultCode,
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),
83
106
  });
84
107
  },
108
+ onError: function (error) {
109
+ throw new PaymentProcessingError(error.message);
110
+ },
85
111
  paymentMethodsResponse: context.paymentMethods,
86
112
  };
87
113
  }
@@ -0,0 +1,6 @@
1
+ import type { SetupIntent, PaymentIntent } from '@stripe/stripe-js';
2
+ import { StripeContext } from 'src/StripeSession';
3
+ import { Callbacks, DefaultValues, 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, defaultValues?: DefaultValues): Promise<WidgetController>;
6
+ export declare const StripeWidget: WidgetInterface<StripeContext>;
@@ -0,0 +1,145 @@
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, defaultValues) {
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', defaultValues
79
+ ? {
80
+ defaultValues: {
81
+ billingDetails: {
82
+ address: {
83
+ country: defaultValues.country,
84
+ postal_code: defaultValues.postalCode,
85
+ },
86
+ },
87
+ },
88
+ }
89
+ : {});
90
+ paymentElement.mount(mountElement);
91
+ return [2 /*return*/, {
92
+ unmount: function () { return paymentElement.unmount(); },
93
+ submit: function () { return __awaiter(_this, void 0, void 0, function () {
94
+ var result, _a, error, setupIntent, _b, error, paymentIntent;
95
+ var _c, _d;
96
+ return __generator(this, function (_e) {
97
+ switch (_e.label) {
98
+ case 0:
99
+ result = { status: 'Pending', code: 'Unexpected' };
100
+ if (!(context.intent === 'save-card')) return [3 /*break*/, 2];
101
+ return [4 /*yield*/, stripe.confirmSetup({
102
+ elements: elements,
103
+ redirect: 'if_required',
104
+ confirmParams: { return_url: window.location.href },
105
+ })];
106
+ case 1:
107
+ _a = _e.sent(), error = _a.error, setupIntent = _a.setupIntent;
108
+ result = {
109
+ status: 'Complete',
110
+ code: error ? 'Failure' : mapStripeResultCode(setupIntent.status),
111
+ };
112
+ _e.label = 2;
113
+ case 2:
114
+ if (!(context.intent === 'payment')) return [3 /*break*/, 4];
115
+ return [4 /*yield*/, stripe.confirmPayment({
116
+ elements: elements,
117
+ redirect: 'if_required',
118
+ confirmParams: { return_url: window.location.href },
119
+ })];
120
+ case 3:
121
+ _b = _e.sent(), error = _b.error, paymentIntent = _b.paymentIntent;
122
+ result = {
123
+ status: 'Complete',
124
+ code: error ? 'Failure' : mapStripeResultCode(paymentIntent.status),
125
+ };
126
+ _e.label = 4;
127
+ case 4:
128
+ if (result.code === 'Failure') {
129
+ (_c = listeners.onFail) === null || _c === void 0 ? void 0 : _c.call(listeners, result);
130
+ }
131
+ else {
132
+ (_d = listeners.onComplete) === null || _d === void 0 ? void 0 : _d.call(listeners, result);
133
+ }
134
+ return [2 /*return*/, result];
135
+ }
136
+ });
137
+ }); },
138
+ }];
139
+ }
140
+ });
141
+ });
142
+ }
143
+ export var StripeWidget = {
144
+ mount: mount,
145
+ };
@@ -0,0 +1,21 @@
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 DefaultValues = {
16
+ country?: string;
17
+ postalCode?: string;
18
+ };
19
+ export type WidgetInterface<T> = {
20
+ mount: (mountElement: HTMLElement, context: T, listeners: Callbacks, defaultValues?: DefaultValues) => Promise<WidgetController>;
21
+ };
@@ -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 };
@@ -1,9 +1,20 @@
1
- import { Callbacks } from '../common';
2
- export type PaymentWidgetConfiguration = {
3
- mountPoint: HTMLElement;
4
- } & Callbacks;
5
- type V1ApiContract = {
6
- mountPaymentWidget(sessionId: string, { mountPoint, onPaymentComplete, onPaymentFail }: PaymentWidgetConfiguration): void;
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, defaultValues }: PaymentWidgetConfiguration): Promise<WidgetController>;
7
19
  };
8
- export declare const v1: V1ApiContract;
9
- export {};
20
+ export declare const v1: LightspeedPaymentsV1;
package/dist/v1/index.js CHANGED
@@ -1,38 +1,10 @@
1
- import { AdyenWidget } from './AdyenWidget';
2
- function mountPaymentWidget(sessionId, _a) {
3
- var mountPoint = _a.mountPoint, onPaymentComplete = _a.onPaymentComplete, onPaymentFail = _a.onPaymentFail;
4
- var session = decodePaymentSessionToContext(sessionId);
5
- switch (session.psp) {
6
- case 'ADYEN':
7
- return AdyenWidget.mount(mountPoint, session.context, {
8
- onPaymentComplete: onPaymentComplete,
9
- onPaymentFail: onPaymentFail,
10
- });
11
- case 'STRIPE':
12
- default:
13
- throw Error('Unsupported PSP');
14
- }
15
- }
16
- function decodePaymentSessionToContext(paymentSession) {
17
- try {
18
- var decoded = atob(paymentSession);
19
- var jsonToken = JSON.parse(decoded);
20
- if (jsonToken && isTokenValid(jsonToken)) {
21
- return jsonToken;
22
- }
23
- throw Error('invalid token structure');
24
- }
25
- catch (e) {
26
- throw Error("Payment token is not valid, ".concat(e));
27
- }
28
- }
29
- function isTokenValid(token) {
30
- var t = token;
31
- if (t && !!t.psp && !!t.context) {
32
- return true;
33
- }
34
- return false;
35
- }
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';
36
8
  export var v1 = {
37
9
  mountPaymentWidget: mountPaymentWidget,
38
10
  };
@@ -0,0 +1,14 @@
1
+ import { StripeSession } from 'src/StripeSession';
2
+ import { AdyenSession } from 'src/AdyenSession';
3
+ import { Callbacks, WidgetController, DefaultValues } from 'src/v1/common';
4
+ type Session = {
5
+ metadata: Record<string, string>;
6
+ } & (AdyenSession | StripeSession);
7
+ export type PaymentWidgetConfiguration = {
8
+ mountPoint: HTMLElement;
9
+ defaultValues?: DefaultValues;
10
+ } & Callbacks;
11
+ export declare function mountPaymentWidget(session: string, { mountPoint, onComplete, onFail, defaultValues }: PaymentWidgetConfiguration): Promise<WidgetController>;
12
+ export declare function decodePaymentSessionToContext(paymentSession: string): Session;
13
+ export declare function isTokenValid(token: unknown): token is Session;
14
+ 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, defaultValues = _a.defaultValues;
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
+ }, defaultValues);
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightspeed/online-payments-sdk",
3
- "version": "0.1.6",
3
+ "version": "0.2.1",
4
4
  "description": "Process online-payments with Lightspeed Payments",
5
5
  "author": "Lightspeed Commerce Inc.",
6
6
  "license": "SEE LICENSE IN LICENSE.md",
@@ -18,26 +18,27 @@
18
18
  "scripts": {
19
19
  "test": "jest --no-cache --detectOpenHandles",
20
20
  "lint": "gts lint",
21
- "clean": "gts clean && rm -rf styles",
21
+ "fix": "gts fix",
22
+ "clean": "gts clean",
22
23
  "typecheck": "tsc --noEmit",
23
24
  "check-package-content": "npm pack --dry-run",
24
- "build": "npm run build:esm && npm run build:cjs && npm run build:css",
25
+ "build": "npm run build:esm && npm run build:cjs",
25
26
  "build:esm": "tsc --module esnext --target es5 --outDir dist --downlevelIteration",
26
- "build:cjs": "tsc --module commonjs --target es5 --outDir dist/cjs --downlevelIteration --declaration false",
27
- "build:css": "mkdir -p styles && cp src/**/*.css styles/"
27
+ "build:cjs": "tsc --module commonjs --target es5 --outDir dist/cjs --downlevelIteration --declaration false"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/jest": "^29.5.13",
31
- "gts": "^5.3.1",
31
+ "gts": "^6.0.2",
32
32
  "jest": "^29.7.0",
33
33
  "jest-environment-jsdom": "^29.7.0",
34
34
  "jest-junit": "^16.0.0",
35
- "rollup": "^2.79.1",
35
+ "rollup": "^4.34.8",
36
36
  "ts-jest": "^29.2.5",
37
37
  "tslib": "^2.7.0",
38
38
  "typescript": "^5.1.5"
39
39
  },
40
40
  "dependencies": {
41
- "@adyen/adyen-web": "^6.8.0"
41
+ "@adyen/adyen-web": "^6.8.0",
42
+ "@stripe/stripe-js": "^7.3.0"
42
43
  }
43
44
  }
package/dist/common.d.ts DELETED
@@ -1,14 +0,0 @@
1
- export type ResultCode = 'AuthenticationFinished' | 'AuthenticationNotRequired' | 'Authorised' | 'Cancelled' | 'ChallengeShopper' | 'Error' | 'IdentifyShopper' | 'PartiallyAuthorised' | 'Pending' | 'PresentToShopper' | 'Received' | 'RedirectShopper' | 'Refused';
2
- type Result<T> = {
3
- code: T;
4
- };
5
- export type CompleteResult = Result<ResultCode>;
6
- export type FailedResult = Result<ResultCode>;
7
- export type Callbacks = {
8
- onPaymentComplete?(result: CompleteResult): void;
9
- onPaymentFail?(result: FailedResult): void;
10
- };
11
- export type WidgetInterface<T> = {
12
- mount: (mountElement: HTMLElement, context: T, listeners: Callbacks) => void;
13
- };
14
- export {};
File without changes
File without changes