@thorprovider/adapters 4.2.1 → 4.3.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.
Files changed (37) hide show
  1. package/PROVIDER_IMPLEMENTATION_ANALYSIS.md +659 -0
  2. package/dist/index.d.mts +160 -1
  3. package/dist/index.d.ts +160 -1
  4. package/dist/index.js +384 -2
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +384 -2
  7. package/dist/index.mjs.map +1 -1
  8. package/docs/RETRY_SOLUTION_SUMMARY.md +26 -11
  9. package/docs/authentication.md +115 -62
  10. package/docs/capabilities-system.md +4 -2
  11. package/docs/category-customization.md +37 -29
  12. package/docs/fetch-reliability-solution.md +127 -278
  13. package/docs/getting-started.md +43 -29
  14. package/docs/profile-management.md +68 -38
  15. package/docs/services-overview.md +4 -2
  16. package/package.json +1 -1
  17. package/quick-reference.md +33 -0
  18. package/src/providers/medusa/admin.test.ts +404 -0
  19. package/src/providers/medusa/admin.ts +442 -0
  20. package/src/providers/medusa/auth.test.ts +137 -0
  21. package/src/providers/medusa/auth.ts +96 -1
  22. package/src/providers/medusa/index.ts +21 -1
  23. package/src/providers/shopify/auth.ts +6 -0
  24. package/src/resilience/README.md +70 -48
  25. package/src/core/errors.js +0 -161
  26. package/src/core/fetch-utils.js +0 -240
  27. package/src/core/logger.js +0 -63
  28. package/src/metadata/channel-scope-validation.js +0 -136
  29. package/src/metadata/channel-scope.js +0 -174
  30. package/src/metadata/index.js +0 -31
  31. package/src/providers/medusa/auth.js +0 -601
  32. package/src/providers/medusa/index.js +0 -2485
  33. package/src/providers/medusa/payment.js +0 -265
  34. package/src/providers/medusa/transforms.js +0 -505
  35. package/src/types/capabilities.types.js +0 -141
  36. package/src/types/provider.js +0 -9
  37. package/src/utils/payment-utils.js +0 -152
@@ -1,265 +0,0 @@
1
- "use strict";
2
- /**
3
- * @thorprovider/adapters v2.0
4
- * Medusa Payment Provider Implementation
5
- *
6
- * Transforms Medusa payment providers to Thor's unified PaymentMethod format.
7
- * Handles region-specific payment provider queries.
8
- */
9
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
10
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
11
- return new (P || (P = Promise))(function (resolve, reject) {
12
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
13
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
14
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
15
- step((generator = generator.apply(thisArg, _arguments || [])).next());
16
- });
17
- };
18
- var __generator = (this && this.__generator) || function (thisArg, body) {
19
- 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);
20
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
21
- function verb(n) { return function (v) { return step([n, v]); }; }
22
- function step(op) {
23
- if (f) throw new TypeError("Generator is already executing.");
24
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
25
- 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;
26
- if (y = 0, t) op = [op[0] & 2, t.value];
27
- switch (op[0]) {
28
- case 0: case 1: t = op; break;
29
- case 4: _.label++; return { value: op[1], done: false };
30
- case 5: _.label++; y = op[1]; op = [0]; continue;
31
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
32
- default:
33
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
34
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
35
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
36
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
37
- if (t[2]) _.ops.pop();
38
- _.trys.pop(); continue;
39
- }
40
- op = body.call(thisArg, _);
41
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
42
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
43
- }
44
- };
45
- Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.transformPaymentMethod = transformPaymentMethod;
47
- exports.getPaymentMethods = getPaymentMethods;
48
- var errors_1 = require("../../core/errors");
49
- /**
50
- * Map of Medusa provider names to display names
51
- */
52
- var PROVIDER_DISPLAY_NAMES = {
53
- stripe: 'Stripe',
54
- paypal: 'PayPal',
55
- system: 'Cash on Delivery',
56
- manual: 'Manual Payment',
57
- bancontact: 'Bancontact',
58
- blik: 'BLIK',
59
- giropay: 'giropay',
60
- ideal: 'iDEAL',
61
- przelewy24: 'Przelewy24',
62
- promptpay: 'PromptPay',
63
- oxxo: 'OXXO',
64
- };
65
- /**
66
- * Infer payment method type from Medusa provider ID
67
- *
68
- * Medusa format: pp_{provider}_{method}
69
- * Examples:
70
- * - pp_stripe_stripe → 'card'
71
- * - pp_paypal_paypal → 'wallet'
72
- * - pp_system_default → 'cash_on_delivery'
73
- * - pp_stripe_bancontact → 'bank_transfer'
74
- *
75
- * @param providerId - Medusa provider ID
76
- * @returns Normalized payment method type
77
- */
78
- function inferPaymentType(providerId) {
79
- var id = providerId.toLowerCase();
80
- // Card payments
81
- if (id.includes('stripe') || id.includes('card')) {
82
- return 'card';
83
- }
84
- // Digital wallets
85
- if (id.includes('paypal') ||
86
- id.includes('wallet') ||
87
- id.includes('apple') ||
88
- id.includes('google')) {
89
- return 'wallet';
90
- }
91
- // Bank transfers
92
- if (id.includes('bancontact') ||
93
- id.includes('ideal') ||
94
- id.includes('giropay') ||
95
- id.includes('sepa') ||
96
- id.includes('ach') ||
97
- id.includes('bank')) {
98
- return 'bank_transfer';
99
- }
100
- // Buy now pay later
101
- if (id.includes('klarna') ||
102
- id.includes('affirm') ||
103
- id.includes('afterpay') ||
104
- id.includes('przelewy24')) {
105
- return 'buy_now_pay_later';
106
- }
107
- // Cash on delivery / Manual
108
- if (id.includes('system') ||
109
- id.includes('manual') ||
110
- id.includes('cod') ||
111
- id.includes('default')) {
112
- return 'cash_on_delivery';
113
- }
114
- // Crypto
115
- if (id.includes('crypto') || id.includes('bitcoin')) {
116
- return 'crypto';
117
- }
118
- return 'other';
119
- }
120
- /**
121
- * Parse Medusa provider ID to extract provider and method names
122
- *
123
- * @param providerId - Medusa provider ID (e.g., "pp_stripe_stripe")
124
- * @returns Object with provider and method names
125
- */
126
- function parseProviderId(providerId) {
127
- // Medusa format: pp_{provider}_{method}
128
- var match = providerId.match(/^pp_([^_]+)_(.+)$/);
129
- if (match) {
130
- return {
131
- provider: match[1],
132
- method: match[2],
133
- };
134
- }
135
- // Fallback for non-standard formats
136
- return {
137
- provider: 'unknown',
138
- method: providerId,
139
- };
140
- }
141
- /**
142
- * Get display name for a provider
143
- */
144
- function getDisplayName(providerName) {
145
- return PROVIDER_DISPLAY_NAMES[providerName.toLowerCase()] ||
146
- providerName.charAt(0).toUpperCase() + providerName.slice(1);
147
- }
148
- /**
149
- * Transform Medusa payment provider to Thor payment method
150
- *
151
- * @param medusaProvider - Medusa payment provider from API
152
- * @returns Normalized Thor PaymentMethod
153
- */
154
- function transformPaymentMethod(medusaProvider) {
155
- var providerId = medusaProvider.id;
156
- var _a = parseProviderId(providerId), provider = _a.provider, method = _a.method;
157
- var type = inferPaymentType(providerId);
158
- // Determine features based on provider type
159
- var isStripe = provider === 'stripe';
160
- var isSystemDefault = provider === 'system' || method === 'default';
161
- var isPayPal = provider === 'paypal';
162
- return {
163
- id: providerId,
164
- name: getDisplayName(provider),
165
- type: type,
166
- isEnabled: true, // Medusa returns only enabled providers by default
167
- requiresSetup: !isSystemDefault, // System/manual payment requires no setup
168
- metadata: {
169
- providerId: provider,
170
- methodId: method,
171
- originalData: medusaProvider,
172
- _backendMapping: {
173
- backend: 'medusa',
174
- originalId: providerId,
175
- normalizedType: type,
176
- },
177
- },
178
- features: {
179
- // Stripe supports saving payment methods
180
- saveForLater: isStripe,
181
- // Stripe requires 3DS for SCA compliance
182
- requires3DS: isStripe,
183
- // Most providers support refunds except COD/manual
184
- supportsRefunds: !isSystemDefault,
185
- // Stripe has minimum amounts (varies by currency)
186
- minAmount: isStripe ? 50 : undefined, // 50 cents minimum for Stripe
187
- // PayPal has maximum transaction limits
188
- maxAmount: isPayPal ? 1000000 : undefined, // $10,000 for PayPal
189
- },
190
- };
191
- }
192
- /**
193
- * Get payment methods for Medusa
194
- *
195
- * Queries Medusa API for available payment providers in a region.
196
- * Medusa requires either regionId or cartId to determine available providers.
197
- *
198
- * @param sdk - Medusa SDK instance
199
- * @param options - Payment methods query options
200
- * @returns Array of normalized payment methods
201
- *
202
- * @throws {Error} If neither regionId nor cartId is provided
203
- * @throws {ProviderAPIError} If API request fails
204
- */
205
- function getPaymentMethods(sdk, options) {
206
- return __awaiter(this, void 0, void 0, function () {
207
- var regionId, cart, error_1, err, response, methods, error_2, err;
208
- return __generator(this, function (_a) {
209
- switch (_a.label) {
210
- case 0:
211
- regionId = options.regionId;
212
- if (!(options.cartId && !regionId)) return [3 /*break*/, 4];
213
- _a.label = 1;
214
- case 1:
215
- _a.trys.push([1, 3, , 4]);
216
- return [4 /*yield*/, sdk.store.cart.retrieve(options.cartId)];
217
- case 2:
218
- cart = (_a.sent()).cart;
219
- regionId = cart.region_id;
220
- return [3 /*break*/, 4];
221
- case 3:
222
- error_1 = _a.sent();
223
- err = error_1;
224
- throw new errors_1.ProviderAPIError('Failed to fetch cart to determine region', 'medusa', err === null || err === void 0 ? void 0 : err.status);
225
- case 4:
226
- // Validate that we have a region ID
227
- if (!regionId) {
228
- throw new Error('Either regionId or cartId is required for Medusa payment methods. ' +
229
- 'Medusa requires a region to determine available payment providers.');
230
- }
231
- _a.label = 5;
232
- case 5:
233
- _a.trys.push([5, 7, , 8]);
234
- return [4 /*yield*/, sdk.store.payment.listPaymentProviders({
235
- region_id: regionId,
236
- })];
237
- case 6:
238
- response = _a.sent();
239
- methods = response.payment_providers.map(transformPaymentMethod);
240
- // Apply filters
241
- if (options.enabledOnly !== false) { // Default to true
242
- methods = methods.filter(function (m) { return m.isEnabled; });
243
- }
244
- // Filter by currency if provided
245
- if (options.currencyCode) {
246
- methods = methods.filter(function (m) {
247
- var _a;
248
- // If method specifies supported currencies, check them
249
- if ((_a = m.features) === null || _a === void 0 ? void 0 : _a.supportedCurrencies) {
250
- return m.features.supportedCurrencies.includes(options.currencyCode);
251
- }
252
- // Otherwise assume all currencies supported
253
- return true;
254
- });
255
- }
256
- return [2 /*return*/, methods];
257
- case 7:
258
- error_2 = _a.sent();
259
- err = error_2;
260
- throw new errors_1.ProviderAPIError('Failed to fetch payment providers from Medusa', 'medusa', err === null || err === void 0 ? void 0 : err.status);
261
- case 8: return [2 /*return*/];
262
- }
263
- });
264
- });
265
- }