@medusajs/payment 0.0.2 → 0.0.3-next-20240425093236
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.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/scripts/bin/run-seed.js +0 -0
- package/dist/services/payment-module.d.ts +1 -0
- package/dist/services/payment-module.js +29 -17
- package/dist/services/payment-provider.d.ts +1 -0
- package/dist/services/payment-provider.js +11 -0
- package/dist/types/index.d.ts +18 -1
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -20,3 +20,4 @@ Object.defineProperty(exports, "revertMigration", { enumerable: true, get: funct
|
|
|
20
20
|
Object.defineProperty(exports, "runMigrations", { enumerable: true, get: function () { return module_definition_1.runMigrations; } });
|
|
21
21
|
exports.default = module_definition_1.moduleDefinition;
|
|
22
22
|
__exportStar(require("./initialize"), exports);
|
|
23
|
+
__exportStar(require("./types"), exports);
|
|
File without changes
|
|
@@ -60,5 +60,6 @@ export default class PaymentModuleService<TPaymentCollection extends PaymentColl
|
|
|
60
60
|
cancelPayment(paymentId: string, sharedContext?: Context): Promise<PaymentDTO>;
|
|
61
61
|
processEvent(eventData: ProviderWebhookPayload, sharedContext?: Context): Promise<void>;
|
|
62
62
|
listPaymentProviders(filters?: FilterablePaymentProviderProps, config?: FindConfig<PaymentProviderDTO>, sharedContext?: Context): Promise<PaymentProviderDTO[]>;
|
|
63
|
+
listAndCountPaymentProviders(filters?: FilterablePaymentProviderProps, config?: FindConfig<PaymentProviderDTO>, sharedContext?: Context): Promise<[PaymentProviderDTO[], number]>;
|
|
63
64
|
}
|
|
64
65
|
export {};
|
|
@@ -11,14 +11,10 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
|
-
};
|
|
17
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
15
|
const types_1 = require("@medusajs/types");
|
|
19
16
|
const utils_1 = require("@medusajs/utils");
|
|
20
17
|
const _models_1 = require("../models");
|
|
21
|
-
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
22
18
|
const joiner_config_1 = require("../joiner-config");
|
|
23
19
|
const generateMethodForModels = [
|
|
24
20
|
_models_1.PaymentCollection,
|
|
@@ -230,13 +226,12 @@ class PaymentModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
|
230
226
|
return await this.retrievePayment(data.payment_id, { relations: ["captures"] }, sharedContext);
|
|
231
227
|
}
|
|
232
228
|
const capturedAmount = payment.captures.reduce((captureAmount, next) => {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
if (newCaptureAmount.gt(remainingToCapture)) {
|
|
229
|
+
return utils_1.MathBN.add(captureAmount, next.raw_amount);
|
|
230
|
+
}, utils_1.MathBN.convert(0));
|
|
231
|
+
const authorizedAmount = new utils_1.BigNumber(payment.raw_amount);
|
|
232
|
+
const newCaptureAmount = new utils_1.BigNumber(data.amount);
|
|
233
|
+
const remainingToCapture = utils_1.MathBN.sub(authorizedAmount, capturedAmount);
|
|
234
|
+
if (utils_1.MathBN.gt(newCaptureAmount, remainingToCapture)) {
|
|
240
235
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `You cannot capture more than the authorized amount substracted by what is already captured.`);
|
|
241
236
|
}
|
|
242
237
|
const paymentData = await this.paymentProviderService_.capturePayment({
|
|
@@ -250,7 +245,8 @@ class PaymentModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
|
250
245
|
}, sharedContext);
|
|
251
246
|
await this.paymentService_.update({ id: payment.id, data: paymentData }, sharedContext);
|
|
252
247
|
// When the entire authorized amount has been captured, we mark it fully capture by setting the captured_at field
|
|
253
|
-
|
|
248
|
+
const totalCaptured = utils_1.MathBN.convert(utils_1.MathBN.add(capturedAmount, newCaptureAmount));
|
|
249
|
+
if (utils_1.MathBN.gte(totalCaptured, authorizedAmount)) {
|
|
254
250
|
await this.paymentService_.update({ id: payment.id, captured_at: new Date() }, sharedContext);
|
|
255
251
|
}
|
|
256
252
|
return await this.retrievePayment(payment.id, { relations: ["captures"] }, sharedContext);
|
|
@@ -264,11 +260,11 @@ class PaymentModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
|
264
260
|
data.amount = payment.amount;
|
|
265
261
|
}
|
|
266
262
|
const capturedAmount = payment.captures.reduce((captureAmount, next) => {
|
|
267
|
-
const amountAsBigNumber = new
|
|
268
|
-
return
|
|
269
|
-
},
|
|
270
|
-
const refundAmount =
|
|
271
|
-
if (
|
|
263
|
+
const amountAsBigNumber = new utils_1.BigNumber(next.raw_amount);
|
|
264
|
+
return utils_1.MathBN.add(captureAmount, amountAsBigNumber);
|
|
265
|
+
}, utils_1.MathBN.convert(0));
|
|
266
|
+
const refundAmount = new utils_1.BigNumber(data.amount);
|
|
267
|
+
if (utils_1.MathBN.lt(capturedAmount, refundAmount)) {
|
|
272
268
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `You cannot refund more than what is captured on the payment.`);
|
|
273
269
|
}
|
|
274
270
|
const paymentData = await this.paymentProviderService_.refundPayment({
|
|
@@ -323,6 +319,15 @@ class PaymentModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
|
323
319
|
populate: true,
|
|
324
320
|
});
|
|
325
321
|
}
|
|
322
|
+
async listAndCountPaymentProviders(filters = {}, config = {}, sharedContext) {
|
|
323
|
+
const [providers, count] = await this.paymentProviderService_.listAndCount(filters, config, sharedContext);
|
|
324
|
+
return [
|
|
325
|
+
await this.baseRepository_.serialize(providers, {
|
|
326
|
+
populate: true,
|
|
327
|
+
}),
|
|
328
|
+
count,
|
|
329
|
+
];
|
|
330
|
+
}
|
|
326
331
|
}
|
|
327
332
|
exports.default = PaymentModuleService;
|
|
328
333
|
__decorate([
|
|
@@ -444,3 +449,10 @@ __decorate([
|
|
|
444
449
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
445
450
|
__metadata("design:returntype", Promise)
|
|
446
451
|
], PaymentModuleService.prototype, "listPaymentProviders", null);
|
|
452
|
+
__decorate([
|
|
453
|
+
(0, utils_1.InjectManager)("baseRepository_"),
|
|
454
|
+
__param(2, (0, utils_1.MedusaContext)()),
|
|
455
|
+
__metadata("design:type", Function),
|
|
456
|
+
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
457
|
+
__metadata("design:returntype", Promise)
|
|
458
|
+
], PaymentModuleService.prototype, "listAndCountPaymentProviders", null);
|
|
@@ -11,6 +11,7 @@ export default class PaymentProviderService {
|
|
|
11
11
|
constructor(container: InjectedDependencies, moduleDeclaration: InternalModuleDeclaration);
|
|
12
12
|
create(data: CreatePaymentProviderDTO[], sharedContext?: Context): Promise<PaymentProvider[]>;
|
|
13
13
|
list(filters: FilterablePaymentProviderProps, config: FindConfig<PaymentProviderDTO>, sharedContext?: Context): Promise<PaymentProvider[]>;
|
|
14
|
+
listAndCount(filters: FilterablePaymentProviderProps, config: FindConfig<PaymentProviderDTO>, sharedContext?: Context): Promise<[PaymentProvider[], number]>;
|
|
14
15
|
retrieveProvider(providerId: string): IPaymentProvider;
|
|
15
16
|
createSession(providerId: string, sessionInput: CreatePaymentProviderSession): Promise<PaymentProviderSessionResponse["data"]>;
|
|
16
17
|
updateSession(providerId: string, sessionInput: UpdatePaymentProviderSession): Promise<Record<string, unknown> | undefined>;
|
|
@@ -28,6 +28,10 @@ class PaymentProviderService {
|
|
|
28
28
|
const queryOptions = utils_1.ModulesSdkUtils.buildQuery(filters, config);
|
|
29
29
|
return await this.paymentProviderRepository_.find(queryOptions, sharedContext);
|
|
30
30
|
}
|
|
31
|
+
async listAndCount(filters, config, sharedContext) {
|
|
32
|
+
const queryOptions = utils_1.ModulesSdkUtils.buildQuery(filters, config);
|
|
33
|
+
return await this.paymentProviderRepository_.findAndCount(queryOptions, sharedContext);
|
|
34
|
+
}
|
|
31
35
|
retrieveProvider(providerId) {
|
|
32
36
|
try {
|
|
33
37
|
return this.container_[providerId];
|
|
@@ -118,3 +122,10 @@ __decorate([
|
|
|
118
122
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
119
123
|
__metadata("design:returntype", Promise)
|
|
120
124
|
], PaymentProviderService.prototype, "list", null);
|
|
125
|
+
__decorate([
|
|
126
|
+
(0, utils_1.InjectManager)("paymentProviderRepository_"),
|
|
127
|
+
__param(2, (0, utils_1.MedusaContext)()),
|
|
128
|
+
__metadata("design:type", Function),
|
|
129
|
+
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
130
|
+
__metadata("design:returntype", Promise)
|
|
131
|
+
], PaymentProviderService.prototype, "listAndCount", null);
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,21 @@
|
|
|
1
|
-
import { Logger } from "@medusajs/types";
|
|
1
|
+
import { Logger, ModuleProviderExports, ModuleServiceInitializeOptions } from "@medusajs/types";
|
|
2
2
|
export type InitializeModuleInjectableDependencies = {
|
|
3
3
|
logger?: Logger;
|
|
4
4
|
};
|
|
5
|
+
export type PaymentModuleOptions = Partial<ModuleServiceInitializeOptions> & {
|
|
6
|
+
/**
|
|
7
|
+
* Providers to be registered
|
|
8
|
+
*/
|
|
9
|
+
providers?: {
|
|
10
|
+
/**
|
|
11
|
+
* The module provider to be registered
|
|
12
|
+
*/
|
|
13
|
+
resolve: string | ModuleProviderExports;
|
|
14
|
+
options: {
|
|
15
|
+
/**
|
|
16
|
+
* key value pair of the provider name and the configuration to be passed to the provider constructor
|
|
17
|
+
*/
|
|
18
|
+
config: Record<string, unknown>;
|
|
19
|
+
};
|
|
20
|
+
}[];
|
|
21
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medusajs/payment",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3-next-20240425093236",
|
|
4
4
|
"description": "Medusa Payment module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"watch:test": "tsc --build tsconfig.spec.json --watch",
|
|
29
29
|
"prepublishOnly": "cross-env NODE_ENV=production tsc --build && tsc-alias -p tsconfig.json",
|
|
30
30
|
"build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json",
|
|
31
|
-
"test": "jest --runInBand --bail --forceExit -- src
|
|
31
|
+
"test": "jest --runInBand --bail --passWithNoTests --forceExit -- src",
|
|
32
32
|
"test:integration": "jest --forceExit -- integration-tests/**/__tests__/**/*.ts",
|
|
33
33
|
"migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate",
|
|
34
34
|
"migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"@mikro-orm/cli": "5.9.7",
|
|
41
41
|
"cross-env": "^5.2.1",
|
|
42
42
|
"jest": "^29.6.3",
|
|
43
|
-
"medusa-test-utils": "
|
|
43
|
+
"medusa-test-utils": "1.1.44-next-20240425093236",
|
|
44
44
|
"rimraf": "^3.0.2",
|
|
45
45
|
"ts-jest": "^29.1.1",
|
|
46
46
|
"ts-node": "^10.9.1",
|
|
@@ -48,9 +48,9 @@
|
|
|
48
48
|
"typescript": "^5.1.6"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@medusajs/modules-sdk": "
|
|
52
|
-
"@medusajs/types": "
|
|
53
|
-
"@medusajs/utils": "
|
|
51
|
+
"@medusajs/modules-sdk": "1.12.11-next-20240425093236",
|
|
52
|
+
"@medusajs/types": "1.12.0-next-20240425093236",
|
|
53
|
+
"@medusajs/utils": "1.12.0-next-20240425093236",
|
|
54
54
|
"@mikro-orm/core": "5.9.7",
|
|
55
55
|
"@mikro-orm/migrations": "5.9.7",
|
|
56
56
|
"@mikro-orm/postgresql": "5.9.7",
|