@flink-app/stripe-plugin 2.0.0-alpha.55 → 2.0.0-alpha.57

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.
Files changed (88) hide show
  1. package/.flink/generatedAgents.ts +1 -1
  2. package/.flink/generatedHandlers.ts +1 -1
  3. package/.flink/generatedJobs.ts +1 -1
  4. package/.flink/generatedRepos.ts +1 -1
  5. package/.flink/generatedTools.ts +1 -1
  6. package/.flink/start.ts +1 -1
  7. package/CHANGELOG.md +25 -0
  8. package/dist/.flink/generatedAgents.js +13 -6
  9. package/dist/.flink/generatedAgents.js.map +1 -0
  10. package/dist/.flink/generatedHandlers.js +13 -6
  11. package/dist/.flink/generatedHandlers.js.map +1 -0
  12. package/dist/.flink/generatedJobs.js +13 -6
  13. package/dist/.flink/generatedJobs.js.map +1 -0
  14. package/dist/.flink/generatedRepos.js +13 -6
  15. package/dist/.flink/generatedRepos.js.map +1 -0
  16. package/dist/.flink/generatedTools.js +13 -6
  17. package/dist/.flink/generatedTools.js.map +1 -0
  18. package/dist/.flink/schema-manifest.json +234 -0
  19. package/dist/.flink/schemas/schemas.js +1 -1
  20. package/dist/.flink/start.js +12 -3
  21. package/dist/.flink/start.js.map +1 -0
  22. package/dist/.tsbuildinfo +1 -0
  23. package/dist/src/Ctx.js +3 -1
  24. package/dist/src/Ctx.js.map +1 -0
  25. package/dist/src/handlers/HandlebarsTemplateHandler.d.ts +1 -1
  26. package/dist/src/handlers/HandlebarsTemplateHandler.js +24 -60
  27. package/dist/src/handlers/HandlebarsTemplateHandler.js.map +1 -0
  28. package/dist/src/index.js +225 -341
  29. package/dist/src/index.js.map +1 -0
  30. package/dist/src/lib/customer.js +101 -116
  31. package/dist/src/lib/customer.js.map +1 -0
  32. package/dist/src/lib/payment.js +64 -107
  33. package/dist/src/lib/payment.js.map +1 -0
  34. package/dist/src/reposx/ConnectSessionRepo.js +12 -24
  35. package/dist/src/reposx/ConnectSessionRepo.js.map +1 -0
  36. package/dist/src/schemas/ConnectSession.js +3 -1
  37. package/dist/src/schemas/ConnectSession.js.map +1 -0
  38. package/dist/src/schemas/createStripeCustomerOptions.js +3 -1
  39. package/dist/src/schemas/createStripeCustomerOptions.js.map +1 -0
  40. package/dist/src/schemas/customer/connectToStripeConnectOptions.js +3 -1
  41. package/dist/src/schemas/customer/connectToStripeConnectOptions.js.map +1 -0
  42. package/dist/src/schemas/customer/hasPaymentMethodOptions.js +3 -1
  43. package/dist/src/schemas/customer/hasPaymentMethodOptions.js.map +1 -0
  44. package/dist/src/schemas/customer/setupCardOptions.js +3 -1
  45. package/dist/src/schemas/customer/setupCardOptions.js.map +1 -0
  46. package/dist/src/schemas/customer/setupCardResponse.js +3 -1
  47. package/dist/src/schemas/customer/setupCardResponse.js.map +1 -0
  48. package/dist/src/schemas/customer/setupCardTokenOptions.js +3 -1
  49. package/dist/src/schemas/customer/setupCardTokenOptions.js.map +1 -0
  50. package/dist/src/schemas/payment/capturePaymentOptions.js +3 -1
  51. package/dist/src/schemas/payment/capturePaymentOptions.js.map +1 -0
  52. package/dist/src/schemas/payment/confirmPaymentOptions.js +3 -1
  53. package/dist/src/schemas/payment/confirmPaymentOptions.js.map +1 -0
  54. package/dist/src/schemas/payment/confirmPaymentResponse.js +3 -1
  55. package/dist/src/schemas/payment/confirmPaymentResponse.js.map +1 -0
  56. package/dist/src/schemas/payment/confirmPaymentTokenOptions.js +3 -1
  57. package/dist/src/schemas/payment/confirmPaymentTokenOptions.js.map +1 -0
  58. package/dist/src/schemas/payment/createPaymentResponse.js +3 -1
  59. package/dist/src/schemas/payment/createPaymentResponse.js.map +1 -0
  60. package/dist/src/schemas/stripeAPIToken.js +3 -1
  61. package/dist/src/schemas/stripeAPIToken.js.map +1 -0
  62. package/dist/src/schemas/stripePluginOptions.js +3 -1
  63. package/dist/src/schemas/stripePluginOptions.js.map +1 -0
  64. package/dist/src/stripeAPI.js +87 -154
  65. package/dist/src/stripeAPI.js.map +1 -0
  66. package/dist/src/stripePluginContext.js +3 -1
  67. package/dist/src/stripePluginContext.js.map +1 -0
  68. package/dist/src/templates/connect-done.js +23 -3
  69. package/dist/src/templates/connect-done.js.map +1 -0
  70. package/dist/src/templates/error.js +10 -3
  71. package/dist/src/templates/error.js.map +1 -0
  72. package/dist/src/templates/master.js +29 -3
  73. package/dist/src/templates/master.js.map +1 -0
  74. package/dist/src/templates/pay-done.d.ts +1 -0
  75. package/dist/src/templates/pay-done.js +24 -0
  76. package/dist/src/templates/pay-done.js.map +1 -0
  77. package/dist/src/templates/pay-enter-card.js +95 -3
  78. package/dist/src/templates/pay-enter-card.js.map +1 -0
  79. package/dist/src/templates/pay-select-card.js +79 -3
  80. package/dist/src/templates/pay-select-card.js.map +1 -0
  81. package/dist/src/templates/setup-card.js +102 -3
  82. package/dist/src/templates/setup-card.js.map +1 -0
  83. package/dist/src/templates/setup-done.js +23 -3
  84. package/dist/src/templates/setup-done.js.map +1 -0
  85. package/dist/src/templates/style.js +144 -3
  86. package/dist/src/templates/style.js.map +1 -0
  87. package/package.json +3 -3
  88. package/.flink/schemas/schemas.ts +0 -1
package/dist/src/index.js CHANGED
@@ -1,387 +1,271 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "stripePlugin", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return stripePlugin;
7
9
  }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
- return new (P || (P = Promise))(function (resolve, reject) {
19
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
- step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ const _flink = require("@flink-app/flink");
12
+ const _HandlebarsTemplateHandler = require("./handlers/HandlebarsTemplateHandler");
13
+ const _stripeAPI = require("./stripeAPI");
14
+ _export_star(require("./stripePluginContext"), exports);
15
+ const _connectdone = require("./templates/connect-done");
16
+ const _error = require("./templates/error");
17
+ const _master = require("./templates/master");
18
+ const _payentercard = require("./templates/pay-enter-card");
19
+ const _payselectcard = require("./templates/pay-select-card");
20
+ const _setupcard = require("./templates/setup-card");
21
+ const _setupdone = require("./templates/setup-done");
22
+ const _style = require("./templates/style");
23
+ const _jsonwebtoken = require("jsonwebtoken");
24
+ const _ConnectSessionRepo = /*#__PURE__*/ _interop_require_default(require("./reposx/ConnectSessionRepo"));
25
+ function _export_star(from, to) {
26
+ Object.keys(from).forEach(function(k) {
27
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
28
+ Object.defineProperty(to, k, {
29
+ enumerable: true,
30
+ get: function() {
31
+ return from[k];
32
+ }
33
+ });
34
+ }
23
35
  });
24
- };
25
- var __generator = (this && this.__generator) || function (thisArg, body) {
26
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
27
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
28
- function verb(n) { return function (v) { return step([n, v]); }; }
29
- function step(op) {
30
- if (f) throw new TypeError("Generator is already executing.");
31
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
32
- 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;
33
- if (y = 0, t) op = [op[0] & 2, t.value];
34
- switch (op[0]) {
35
- case 0: case 1: t = op; break;
36
- case 4: _.label++; return { value: op[1], done: false };
37
- case 5: _.label++; y = op[1]; op = [0]; continue;
38
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
39
- default:
40
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
41
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
42
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
43
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
44
- if (t[2]) _.ops.pop();
45
- _.trys.pop(); continue;
46
- }
47
- op = body.call(thisArg, _);
48
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
49
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
50
- }
51
- };
52
- var __importDefault = (this && this.__importDefault) || function (mod) {
53
- return (mod && mod.__esModule) ? mod : { "default": mod };
54
- };
55
- Object.defineProperty(exports, "__esModule", { value: true });
56
- exports.stripePlugin = void 0;
57
- var flink_1 = require("@flink-app/flink");
58
- var HandlebarsTemplateHandler_1 = require("./handlers/HandlebarsTemplateHandler");
59
- var stripeAPI_1 = require("./stripeAPI");
60
- __exportStar(require("./stripePluginContext"), exports);
61
- var connect_done_1 = require("./templates/connect-done");
62
- var error_1 = require("./templates/error");
63
- var master_1 = require("./templates/master");
64
- var pay_enter_card_1 = require("./templates/pay-enter-card");
65
- var pay_select_card_1 = require("./templates/pay-select-card");
66
- var setup_card_1 = require("./templates/setup-card");
67
- var setup_done_1 = require("./templates/setup-done");
68
- var style_1 = require("./templates/style");
69
- var jsonwebtoken_1 = require("jsonwebtoken");
70
- var ConnectSessionRepo_1 = __importDefault(require("./reposx/ConnectSessionRepo"));
71
- var stripePlugin = function (options) {
72
- var _a, _b, _c, _d, _e, _f, _g, _h;
73
- if (options.paymentCallback == null)
74
- options.paymentCallback = function () { };
75
- if (options.stripeConnectCallback == null)
76
- options.stripeConnectCallback = function () { };
77
- var stripeAPI = new stripeAPI_1.StripeAPI(options);
78
- var templates = {
79
- master: ((_a = options.templates) === null || _a === void 0 ? void 0 : _a.master) || master_1.masterTemplate,
80
- style: ((_b = options.templates) === null || _b === void 0 ? void 0 : _b.style) || style_1.styleTemplate,
81
- setupCard: ((_c = options.templates) === null || _c === void 0 ? void 0 : _c.setupCard) || setup_card_1.setupCardTemplate,
82
- setupDone: ((_d = options.templates) === null || _d === void 0 ? void 0 : _d.setupDone) || setup_done_1.setupDoneTemplate,
83
- error: ((_e = options.templates) === null || _e === void 0 ? void 0 : _e.error) || error_1.errorTemplate,
84
- paySelectCard: ((_f = options.templates) === null || _f === void 0 ? void 0 : _f.paySelectCard) || pay_select_card_1.paySelectCardTemplate,
85
- payEnterCard: ((_g = options.templates) === null || _g === void 0 ? void 0 : _g.payEnterCard) || pay_enter_card_1.payEnterCardTemplate,
86
- connectDone: ((_h = options.templates) === null || _h === void 0 ? void 0 : _h.connectDone) || connect_done_1.connectDoneTemplate,
36
+ return from;
37
+ }
38
+ function _interop_require_default(obj) {
39
+ return obj && obj.__esModule ? obj : {
40
+ default: obj
41
+ };
42
+ }
43
+ const stripePlugin = (options)=>{
44
+ var _options_templates, _options_templates1, _options_templates2, _options_templates3, _options_templates4, _options_templates5, _options_templates6, _options_templates7;
45
+ if (options.paymentCallback == null) options.paymentCallback = ()=>{};
46
+ if (options.stripeConnectCallback == null) options.stripeConnectCallback = ()=>{};
47
+ const stripeAPI = new _stripeAPI.StripeAPI(options);
48
+ const templates = {
49
+ master: ((_options_templates = options.templates) === null || _options_templates === void 0 ? void 0 : _options_templates.master) || _master.masterTemplate,
50
+ style: ((_options_templates1 = options.templates) === null || _options_templates1 === void 0 ? void 0 : _options_templates1.style) || _style.styleTemplate,
51
+ setupCard: ((_options_templates2 = options.templates) === null || _options_templates2 === void 0 ? void 0 : _options_templates2.setupCard) || _setupcard.setupCardTemplate,
52
+ setupDone: ((_options_templates3 = options.templates) === null || _options_templates3 === void 0 ? void 0 : _options_templates3.setupDone) || _setupdone.setupDoneTemplate,
53
+ error: ((_options_templates4 = options.templates) === null || _options_templates4 === void 0 ? void 0 : _options_templates4.error) || _error.errorTemplate,
54
+ paySelectCard: ((_options_templates5 = options.templates) === null || _options_templates5 === void 0 ? void 0 : _options_templates5.paySelectCard) || _payselectcard.paySelectCardTemplate,
55
+ payEnterCard: ((_options_templates6 = options.templates) === null || _options_templates6 === void 0 ? void 0 : _options_templates6.payEnterCard) || _payentercard.payEnterCardTemplate,
56
+ connectDone: ((_options_templates7 = options.templates) === null || _options_templates7 === void 0 ? void 0 : _options_templates7.connectDone) || _connectdone.connectDoneTemplate
87
57
  };
88
58
  return {
89
59
  id: "stripePlugin",
90
- init: function (app) { return init(app, options); },
60
+ init: (app)=>init(app, options),
91
61
  ctx: {
92
- stripeAPI: stripeAPI,
93
- templates: templates,
94
- },
62
+ stripeAPI,
63
+ templates
64
+ }
95
65
  };
96
66
  };
97
- exports.stripePlugin = stripePlugin;
98
67
  function init(app, options) {
99
- var _this = this;
100
- var _a, _b, _c, _d, _e, _f, _g, _h;
101
- if (options.baseUrl == null)
102
- options.baseUrl = "/stripe";
103
- flink_1.log.info("Registered route GET " + options.baseUrl + "/customer/setup-card/:token");
104
- (_a = app.expressApp) === null || _a === void 0 ? void 0 : _a.get(options.baseUrl + "/customer/setup-card/:token", function (req, res) {
105
- var _a, _b;
68
+ var _app_expressApp, _app_expressApp1, _app_expressApp2, _app_expressApp3, _app_expressApp4, _app_expressApp5, _app_expressApp6, _app_expressApp7;
69
+ if (options.baseUrl == null) options.baseUrl = "/stripe";
70
+ _flink.log.info("Registered route GET " + options.baseUrl + "/customer/setup-card/:token");
71
+ (_app_expressApp = app.expressApp) === null || _app_expressApp === void 0 ? void 0 : _app_expressApp.get(options.baseUrl + "/customer/setup-card/:token", (req, res)=>{
106
72
  try {
107
- var tokenData = (0, jsonwebtoken_1.verify)(req.params.token, options.JTW_TOKEN);
73
+ var _options_phrases, _options_phrases1;
74
+ const tokenData = (0, _jsonwebtoken.verify)(req.params.token, options.JTW_TOKEN);
108
75
  if (tokenData.type != "setup_card") {
109
76
  throw "Invalid token type";
110
77
  }
111
- (0, HandlebarsTemplateHandler_1.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.setupCard, {
112
- setupDescription: (_a = options.phrases) === null || _a === void 0 ? void 0 : _a.setupDescription,
78
+ (0, _HandlebarsTemplateHandler.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.setupCard, {
79
+ setupDescription: (_options_phrases = options.phrases) === null || _options_phrases === void 0 ? void 0 : _options_phrases.setupDescription,
113
80
  logo: options.logo,
114
81
  stripePublishableKey: options.stripePublishableKey,
115
82
  client_secret: tokenData.options.client_secret,
116
83
  baseUrl: options.baseUrl,
117
- setupButtonText: ((_b = options.phrases) === null || _b === void 0 ? void 0 : _b.setupButtonText) || "Spara",
84
+ setupButtonText: ((_options_phrases1 = options.phrases) === null || _options_phrases1 === void 0 ? void 0 : _options_phrases1.setupButtonText) || "Spara"
118
85
  }, res);
119
- }
120
- catch (ex) {
121
- flink_1.log.error(ex);
86
+ } catch (ex) {
87
+ _flink.log.error(ex);
122
88
  res.redirect(options.baseUrl + "/error");
123
89
  return;
124
90
  }
125
91
  });
126
- flink_1.log.info("Registered route GET " + options.baseUrl + "/customer/setup-card-done");
127
- (_b = app.expressApp) === null || _b === void 0 ? void 0 : _b.get(options.baseUrl + "/customer/setup-card-done/", function (req, res) {
128
- var _a;
92
+ _flink.log.info("Registered route GET " + options.baseUrl + "/customer/setup-card-done");
93
+ (_app_expressApp1 = app.expressApp) === null || _app_expressApp1 === void 0 ? void 0 : _app_expressApp1.get(options.baseUrl + "/customer/setup-card-done/", (req, res)=>{
129
94
  try {
130
- (0, HandlebarsTemplateHandler_1.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.setupDone, {
95
+ var _options_phrases;
96
+ (0, _HandlebarsTemplateHandler.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.setupDone, {
131
97
  redirectUrl: options.redirectUrl,
132
- message: ((_a = options.phrases) === null || _a === void 0 ? void 0 : _a.setupDoneMessage) || "Kortet har sparats.",
98
+ message: ((_options_phrases = options.phrases) === null || _options_phrases === void 0 ? void 0 : _options_phrases.setupDoneMessage) || "Kortet har sparats."
133
99
  }, res);
134
- }
135
- catch (ex) {
136
- flink_1.log.error(ex);
100
+ } catch (ex) {
101
+ _flink.log.error(ex);
137
102
  res.redirect(options.baseUrl + "/error");
138
103
  return;
139
104
  }
140
105
  });
141
- (_c = app.expressApp) === null || _c === void 0 ? void 0 : _c.get(options.baseUrl + "/error", function (req, res) {
106
+ (_app_expressApp2 = app.expressApp) === null || _app_expressApp2 === void 0 ? void 0 : _app_expressApp2.get(options.baseUrl + "/error", (req, res)=>{
142
107
  try {
143
- (0, HandlebarsTemplateHandler_1.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.error, {}, res);
144
- }
145
- catch (ex) {
146
- flink_1.log.error(ex);
108
+ (0, _HandlebarsTemplateHandler.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.error, {}, res);
109
+ } catch (ex) {
110
+ _flink.log.error(ex);
147
111
  res.redirect(options.baseUrl + "/");
148
112
  return;
149
113
  }
150
114
  });
151
- flink_1.log.info("Registered route GET " + options.baseUrl + "/payment/confirm/:token");
152
- (_d = app.expressApp) === null || _d === void 0 ? void 0 : _d.get(options.baseUrl + "/payment/confirm/:token", function (req, res) { return __awaiter(_this, void 0, void 0, function () {
153
- var tokenData, stripe, paymentIntent, paymentMethods, ex_1;
154
- var _a;
155
- return __generator(this, function (_b) {
156
- switch (_b.label) {
157
- case 0:
158
- _b.trys.push([0, 3, , 4]);
159
- tokenData = (0, jsonwebtoken_1.verify)(req.params.token, options.JTW_TOKEN);
160
- if (tokenData.type != "payment") {
161
- throw "Invalid token type";
162
- }
163
- stripe = app.ctx.plugins.stripePlugin.stripeAPI.stripe;
164
- return [4 /*yield*/, stripe.paymentIntents.retrieve(tokenData.options.paymentIntentId)];
165
- case 1:
166
- paymentIntent = _b.sent();
167
- return [4 /*yield*/, stripe.paymentMethods.list({
168
- customer: (_a = paymentIntent.customer) === null || _a === void 0 ? void 0 : _a.toString(),
169
- type: "card",
170
- })];
171
- case 2:
172
- paymentMethods = _b.sent();
173
- if (paymentMethods.data.length == 0) {
174
- res.redirect(options.baseUrl + "/payment/enter-card/" + req.params.token);
175
- }
176
- else {
177
- res.redirect(options.baseUrl + "/payment/select-card/" + req.params.token);
178
- }
179
- return [3 /*break*/, 4];
180
- case 3:
181
- ex_1 = _b.sent();
182
- flink_1.log.error(ex_1);
183
- res.redirect(options.baseUrl + "/error");
184
- return [2 /*return*/];
185
- case 4: return [2 /*return*/];
115
+ _flink.log.info("Registered route GET " + options.baseUrl + "/payment/confirm/:token");
116
+ (_app_expressApp3 = app.expressApp) === null || _app_expressApp3 === void 0 ? void 0 : _app_expressApp3.get(options.baseUrl + "/payment/confirm/:token", async (req, res)=>{
117
+ try {
118
+ var _paymentIntent_customer;
119
+ const tokenData = (0, _jsonwebtoken.verify)(req.params.token, options.JTW_TOKEN);
120
+ if (tokenData.type != "payment") {
121
+ throw "Invalid token type";
186
122
  }
187
- });
188
- }); });
189
- flink_1.log.info("Registered route GET " + options.baseUrl + "/payment/select-card/:token");
190
- (_e = app.expressApp) === null || _e === void 0 ? void 0 : _e.get(options.baseUrl + "/payment/select-card/:token", function (req, res) { return __awaiter(_this, void 0, void 0, function () {
191
- var tokenData, stripe, paymentIntent, paymentMethods, ctx, ex_2;
192
- var _a, _b, _c;
193
- return __generator(this, function (_d) {
194
- switch (_d.label) {
195
- case 0:
196
- _d.trys.push([0, 3, , 4]);
197
- tokenData = (0, jsonwebtoken_1.verify)(req.params.token, options.JTW_TOKEN);
198
- if (tokenData.type != "payment") {
199
- throw "Invalid token type";
200
- }
201
- stripe = app.ctx.plugins.stripePlugin.stripeAPI.stripe;
202
- return [4 /*yield*/, stripe.paymentIntents.retrieve(tokenData.options.paymentIntentId)];
203
- case 1:
204
- paymentIntent = _d.sent();
205
- return [4 /*yield*/, stripe.paymentMethods.list({
206
- customer: (_a = paymentIntent.customer) === null || _a === void 0 ? void 0 : _a.toString(),
207
- type: "card",
208
- })];
209
- case 2:
210
- paymentMethods = _d.sent();
211
- if (paymentMethods.data.length == 0) {
212
- res.redirect(options.baseUrl + "/payment/enter-card/" + req.params.token);
213
- }
214
- ctx = {
215
- card: paymentMethods.data[0].card,
216
- paymentSelectCardPayButtonText: ((_b = options.phrases) === null || _b === void 0 ? void 0 : _b.paymentSelectCardPayButtonText) || "Betala",
217
- paymentSelectCardChangeCardButtonText: ((_c = options.phrases) === null || _c === void 0 ? void 0 : _c.paymentSelectCardChangeCardButtonText) || "Byt kort",
218
- publishableKey: options.stripePublishableKey,
219
- token: req.params.token,
220
- baseUrl: options.baseUrl,
221
- logo: options.logo,
222
- client_secrect: paymentIntent.client_secret,
223
- price: (paymentIntent.amount / 100).toFixed(2) + " " + paymentIntent.currency.toUpperCase(),
224
- description: paymentIntent.description,
225
- };
226
- (0, HandlebarsTemplateHandler_1.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.paySelectCard, ctx, res);
227
- return [3 /*break*/, 4];
228
- case 3:
229
- ex_2 = _d.sent();
230
- flink_1.log.error(ex_2);
231
- res.redirect(options.baseUrl + "/error");
232
- return [2 /*return*/];
233
- case 4: return [2 /*return*/];
123
+ const stripe = app.ctx.plugins.stripePlugin.stripeAPI.stripe;
124
+ const paymentIntent = await stripe.paymentIntents.retrieve(tokenData.options.paymentIntentId);
125
+ const paymentMethods = await stripe.paymentMethods.list({
126
+ customer: (_paymentIntent_customer = paymentIntent.customer) === null || _paymentIntent_customer === void 0 ? void 0 : _paymentIntent_customer.toString(),
127
+ type: "card"
128
+ });
129
+ if (paymentMethods.data.length == 0) {
130
+ res.redirect(options.baseUrl + "/payment/enter-card/" + req.params.token);
131
+ } else {
132
+ res.redirect(options.baseUrl + "/payment/select-card/" + req.params.token);
234
133
  }
235
- });
236
- }); });
237
- flink_1.log.info("Registered route GET " + options.baseUrl + "/payment/enter-card/:token");
238
- (_f = app.expressApp) === null || _f === void 0 ? void 0 : _f.get(options.baseUrl + "/payment/enter-card/:token", function (req, res) { return __awaiter(_this, void 0, void 0, function () {
239
- var tokenData, stripe, paymentIntent, paymentMethods, _a, _b, _c, _i, x, pi, ctx, ex_3;
240
- var _d, _e;
241
- return __generator(this, function (_f) {
242
- switch (_f.label) {
243
- case 0:
244
- _f.trys.push([0, 7, , 8]);
245
- tokenData = (0, jsonwebtoken_1.verify)(req.params.token, options.JTW_TOKEN);
246
- if (tokenData.type != "payment") {
247
- throw "Invalid token type";
248
- }
249
- stripe = app.ctx.plugins.stripePlugin.stripeAPI.stripe;
250
- return [4 /*yield*/, stripe.paymentIntents.retrieve(tokenData.options.paymentIntentId)];
251
- case 1:
252
- paymentIntent = _f.sent();
253
- return [4 /*yield*/, stripe.paymentMethods.list({
254
- customer: (_d = paymentIntent.customer) === null || _d === void 0 ? void 0 : _d.toString(),
255
- type: "card",
256
- })];
257
- case 2:
258
- paymentMethods = _f.sent();
259
- _a = paymentMethods.data;
260
- _b = [];
261
- for (_c in _a)
262
- _b.push(_c);
263
- _i = 0;
264
- _f.label = 3;
265
- case 3:
266
- if (!(_i < _b.length)) return [3 /*break*/, 6];
267
- _c = _b[_i];
268
- if (!(_c in _a)) return [3 /*break*/, 5];
269
- x = _c;
270
- pi = paymentMethods.data[x];
271
- return [4 /*yield*/, stripe.paymentMethods.detach(pi.id)];
272
- case 4:
273
- _f.sent();
274
- _f.label = 5;
275
- case 5:
276
- _i++;
277
- return [3 /*break*/, 3];
278
- case 6:
279
- ctx = {
280
- paymentEnterCardPayButtonText: ((_e = options.phrases) === null || _e === void 0 ? void 0 : _e.paymentEnterCardPayButtonText) || "Betala",
281
- publishableKey: options.stripePublishableKey,
282
- token: req.params.token,
283
- baseUrl: options.baseUrl,
284
- logo: options.logo,
285
- client_secrect: paymentIntent.client_secret,
286
- price: (paymentIntent.amount / 100).toFixed(2) + " " + paymentIntent.currency.toUpperCase(),
287
- description: paymentIntent.description,
288
- };
289
- (0, HandlebarsTemplateHandler_1.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.payEnterCard, ctx, res);
290
- return [3 /*break*/, 8];
291
- case 7:
292
- ex_3 = _f.sent();
293
- flink_1.log.error(ex_3);
294
- res.redirect(options.baseUrl + "/error");
295
- return [2 /*return*/];
296
- case 8: return [2 /*return*/];
134
+ } catch (ex) {
135
+ _flink.log.error(ex);
136
+ res.redirect(options.baseUrl + "/error");
137
+ return;
138
+ }
139
+ });
140
+ _flink.log.info("Registered route GET " + options.baseUrl + "/payment/select-card/:token");
141
+ (_app_expressApp4 = app.expressApp) === null || _app_expressApp4 === void 0 ? void 0 : _app_expressApp4.get(options.baseUrl + "/payment/select-card/:token", async (req, res)=>{
142
+ try {
143
+ var _paymentIntent_customer, _options_phrases, _options_phrases1;
144
+ const tokenData = (0, _jsonwebtoken.verify)(req.params.token, options.JTW_TOKEN);
145
+ if (tokenData.type != "payment") {
146
+ throw "Invalid token type";
297
147
  }
298
- });
299
- }); });
300
- flink_1.log.info("Registered route GET " + options.baseUrl + "/callback/:event");
301
- (_g = app.expressApp) === null || _g === void 0 ? void 0 : _g.post(options.baseUrl + "/callback/:event", function (req, res) { return __awaiter(_this, void 0, void 0, function () {
302
- var paymentIntentId, stripe, paymentIntent, ex_4;
303
- var _a, _b, _c;
304
- return __generator(this, function (_d) {
305
- switch (_d.label) {
306
- case 0:
307
- if (!(req.params.event == "charge.succeeded")) return [3 /*break*/, 4];
308
- paymentIntentId = (_c = (_b = (_a = req.body) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.object) === null || _c === void 0 ? void 0 : _c.payment_intent;
309
- if (paymentIntentId == null) {
310
- res.status(400).send("Bad request");
311
- return [2 /*return*/];
312
- }
313
- stripe = app.ctx.plugins.stripePlugin.stripeAPI.stripe;
314
- _d.label = 1;
315
- case 1:
316
- _d.trys.push([1, 3, , 4]);
317
- return [4 /*yield*/, stripe.paymentIntents.retrieve(paymentIntentId)];
318
- case 2:
319
- paymentIntent = _d.sent();
320
- if (paymentIntent == null) {
321
- res.status(400).send("Bad request");
322
- return [2 /*return*/];
323
- }
324
- if (paymentIntent.status == "succeeded" || paymentIntent.status == "requires_capture") {
325
- try {
326
- options.paymentCallback(paymentIntent.id, paymentIntent.status);
327
- }
328
- catch (ex) {
329
- flink_1.log.error("paymentCallback:" + ex);
330
- }
331
- }
332
- return [3 /*break*/, 4];
333
- case 3:
334
- ex_4 = _d.sent();
335
- flink_1.log.error(ex_4);
336
- res.status(500).send("Internal error");
337
- return [3 /*break*/, 4];
338
- case 4:
339
- res.status(200).send("Completed");
340
- return [2 /*return*/];
148
+ const stripe = app.ctx.plugins.stripePlugin.stripeAPI.stripe;
149
+ const paymentIntent = await stripe.paymentIntents.retrieve(tokenData.options.paymentIntentId);
150
+ const paymentMethods = await stripe.paymentMethods.list({
151
+ customer: (_paymentIntent_customer = paymentIntent.customer) === null || _paymentIntent_customer === void 0 ? void 0 : _paymentIntent_customer.toString(),
152
+ type: "card"
153
+ });
154
+ if (paymentMethods.data.length == 0) {
155
+ res.redirect(options.baseUrl + "/payment/enter-card/" + req.params.token);
156
+ }
157
+ const ctx = {
158
+ card: paymentMethods.data[0].card,
159
+ paymentSelectCardPayButtonText: ((_options_phrases = options.phrases) === null || _options_phrases === void 0 ? void 0 : _options_phrases.paymentSelectCardPayButtonText) || "Betala",
160
+ paymentSelectCardChangeCardButtonText: ((_options_phrases1 = options.phrases) === null || _options_phrases1 === void 0 ? void 0 : _options_phrases1.paymentSelectCardChangeCardButtonText) || "Byt kort",
161
+ publishableKey: options.stripePublishableKey,
162
+ token: req.params.token,
163
+ baseUrl: options.baseUrl,
164
+ logo: options.logo,
165
+ client_secrect: paymentIntent.client_secret,
166
+ price: (paymentIntent.amount / 100).toFixed(2) + " " + paymentIntent.currency.toUpperCase(),
167
+ description: paymentIntent.description
168
+ };
169
+ (0, _HandlebarsTemplateHandler.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.paySelectCard, ctx, res);
170
+ } catch (ex) {
171
+ _flink.log.error(ex);
172
+ res.redirect(options.baseUrl + "/error");
173
+ return;
174
+ }
175
+ });
176
+ _flink.log.info("Registered route GET " + options.baseUrl + "/payment/enter-card/:token");
177
+ (_app_expressApp5 = app.expressApp) === null || _app_expressApp5 === void 0 ? void 0 : _app_expressApp5.get(options.baseUrl + "/payment/enter-card/:token", async (req, res)=>{
178
+ try {
179
+ var _paymentIntent_customer, _options_phrases;
180
+ const tokenData = (0, _jsonwebtoken.verify)(req.params.token, options.JTW_TOKEN);
181
+ if (tokenData.type != "payment") {
182
+ throw "Invalid token type";
183
+ }
184
+ const stripe = app.ctx.plugins.stripePlugin.stripeAPI.stripe;
185
+ const paymentIntent = await stripe.paymentIntents.retrieve(tokenData.options.paymentIntentId);
186
+ const paymentMethods = await stripe.paymentMethods.list({
187
+ customer: (_paymentIntent_customer = paymentIntent.customer) === null || _paymentIntent_customer === void 0 ? void 0 : _paymentIntent_customer.toString(),
188
+ type: "card"
189
+ });
190
+ for(var x in paymentMethods.data){
191
+ const pi = paymentMethods.data[x];
192
+ await stripe.paymentMethods.detach(pi.id);
341
193
  }
342
- });
343
- }); });
344
- flink_1.log.info("Registered route GET " + options.baseUrl + "/connect");
345
- (_h = app.expressApp) === null || _h === void 0 ? void 0 : _h.get(options.baseUrl + "/connect", function (req, res) { return __awaiter(_this, void 0, void 0, function () {
346
- var _a, code, state, stripe, token, repo, session;
347
- var _b;
348
- return __generator(this, function (_c) {
349
- switch (_c.label) {
350
- case 0:
351
- _a = req.query, code = _a.code, state = _a.state;
352
- if (!(code && state)) return [3 /*break*/, 3];
353
- stripe = app.ctx.plugins.stripePlugin.stripeAPI.stripe;
354
- return [4 /*yield*/, stripe.oauth.token({
355
- grant_type: "authorization_code",
356
- code: code.toString(),
357
- })];
358
- case 1:
359
- token = _c.sent();
360
- repo = app.ctx.repos.connectsessionRepo;
361
- return [4 /*yield*/, repo.getById(state.toString())];
362
- case 2:
363
- session = _c.sent();
194
+ const ctx = {
195
+ paymentEnterCardPayButtonText: ((_options_phrases = options.phrases) === null || _options_phrases === void 0 ? void 0 : _options_phrases.paymentEnterCardPayButtonText) || "Betala",
196
+ publishableKey: options.stripePublishableKey,
197
+ token: req.params.token,
198
+ baseUrl: options.baseUrl,
199
+ logo: options.logo,
200
+ client_secrect: paymentIntent.client_secret,
201
+ price: (paymentIntent.amount / 100).toFixed(2) + " " + paymentIntent.currency.toUpperCase(),
202
+ description: paymentIntent.description
203
+ };
204
+ (0, _HandlebarsTemplateHandler.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.payEnterCard, ctx, res);
205
+ } catch (ex) {
206
+ _flink.log.error(ex);
207
+ res.redirect(options.baseUrl + "/error");
208
+ return;
209
+ }
210
+ });
211
+ _flink.log.info("Registered route GET " + options.baseUrl + "/callback/:event");
212
+ (_app_expressApp6 = app.expressApp) === null || _app_expressApp6 === void 0 ? void 0 : _app_expressApp6.post(options.baseUrl + "/callback/:event", async (req, res)=>{
213
+ if (req.params.event == "charge.succeeded") {
214
+ var _req_body_data_object, _req_body_data, _req_body;
215
+ const paymentIntentId = (_req_body = req.body) === null || _req_body === void 0 ? void 0 : (_req_body_data = _req_body.data) === null || _req_body_data === void 0 ? void 0 : (_req_body_data_object = _req_body_data.object) === null || _req_body_data_object === void 0 ? void 0 : _req_body_data_object.payment_intent;
216
+ if (paymentIntentId == null) {
217
+ res.status(400).send("Bad request");
218
+ return;
219
+ }
220
+ const stripe = app.ctx.plugins.stripePlugin.stripeAPI.stripe;
221
+ try {
222
+ const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId);
223
+ if (paymentIntent == null) {
224
+ res.status(400).send("Bad request");
225
+ return;
226
+ }
227
+ if (paymentIntent.status == "succeeded" || paymentIntent.status == "requires_capture") {
364
228
  try {
365
- options.stripeConnectCallback(session.userId, token.stripe_user_id);
366
- (0, HandlebarsTemplateHandler_1.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.connectDone, {
367
- redirectUrl: options.redirectUrl,
368
- message: ((_b = options.phrases) === null || _b === void 0 ? void 0 : _b.connectDoneMessage) || "Kontot har kopplats till Stripe.",
369
- }, res);
370
- }
371
- catch (ex) {
372
- flink_1.log.error("paymentCallback:" + ex);
373
- res.redirect(options.baseUrl + "/error");
229
+ options.paymentCallback(paymentIntent.id, paymentIntent.status);
230
+ } catch (ex) {
231
+ _flink.log.error("paymentCallback:" + ex);
374
232
  }
375
- return [3 /*break*/, 4];
376
- case 3:
377
- res.redirect(options.baseUrl + "/error");
378
- _c.label = 4;
379
- case 4: return [2 /*return*/];
233
+ }
234
+ } catch (ex) {
235
+ _flink.log.error(ex);
236
+ res.status(500).send("Internal error");
380
237
  }
381
- });
382
- }); });
238
+ }
239
+ res.status(200).send("Completed");
240
+ });
241
+ _flink.log.info("Registered route GET " + options.baseUrl + "/connect");
242
+ (_app_expressApp7 = app.expressApp) === null || _app_expressApp7 === void 0 ? void 0 : _app_expressApp7.get(options.baseUrl + "/connect", async (req, res)=>{
243
+ const { code, state } = req.query;
244
+ if (code && state) {
245
+ const stripe = app.ctx.plugins.stripePlugin.stripeAPI.stripe;
246
+ const token = await stripe.oauth.token({
247
+ grant_type: "authorization_code",
248
+ code: code.toString()
249
+ });
250
+ const repo = app.ctx.repos.connectsessionRepo;
251
+ const session = await repo.getById(state.toString());
252
+ try {
253
+ var _options_phrases;
254
+ options.stripeConnectCallback(session.userId, token.stripe_user_id);
255
+ (0, _HandlebarsTemplateHandler.HandlebarsTemplateHandler)(app.ctx.plugins.stripePlugin.templates.master, app.ctx.plugins.stripePlugin.templates.style, app.ctx.plugins.stripePlugin.templates.connectDone, {
256
+ redirectUrl: options.redirectUrl,
257
+ message: ((_options_phrases = options.phrases) === null || _options_phrases === void 0 ? void 0 : _options_phrases.connectDoneMessage) || "Kontot har kopplats till Stripe."
258
+ }, res);
259
+ } catch (ex) {
260
+ _flink.log.error("paymentCallback:" + ex);
261
+ res.redirect(options.baseUrl + "/error");
262
+ }
263
+ } else {
264
+ res.redirect(options.baseUrl + "/error");
265
+ }
266
+ });
383
267
  if (app.db != null) {
384
- app.addRepo("connectsessionRepo", new ConnectSessionRepo_1.default("ConnectSessionRepo", app.db));
268
+ app.addRepo("connectsessionRepo", new _ConnectSessionRepo.default("ConnectSessionRepo", app.db));
385
269
  }
386
270
  app.ctx.plugins.stripePlugin.stripeAPI.config.setRepo(app.ctx.repos.connectsessionRepo);
387
271
  }