@commercetools/connect-payments-sdk 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Payment, PaymentDraft, Transaction } from '@commercetools/platform-sdk';
|
|
2
|
-
import { GetPayment,
|
|
2
|
+
import { GetPayment, PaymentService, PaymentServiceOptions, TransactionData, UpdatePayment } from '../types/payment.type';
|
|
3
3
|
/**
|
|
4
4
|
* This is the default implementation of the PaymentService interface.
|
|
5
5
|
*/
|
|
@@ -10,9 +10,6 @@ export declare class DefaultPaymentService implements PaymentService {
|
|
|
10
10
|
getPayment(opts: GetPayment): Promise<Payment>;
|
|
11
11
|
createPayment(draft: PaymentDraft): Promise<Payment>;
|
|
12
12
|
updatePayment(opts: UpdatePayment): Promise<Payment>;
|
|
13
|
-
validatePaymentCancelAuthorization(opts: PaymentCancelAuthorizationValidation): PaymentModificationValidationResult;
|
|
14
|
-
validatePaymentCharge(opts: PaymentChargeValidation): PaymentModificationValidationResult;
|
|
15
|
-
validatePaymentRefund(opts: PaymentRefundValidation): PaymentModificationValidationResult;
|
|
16
13
|
private consolidateUpdateActions;
|
|
17
14
|
private populateSetInterfaceIdAction;
|
|
18
15
|
private populateChangeTransactionInteractionId;
|
|
@@ -21,6 +18,4 @@ export declare class DefaultPaymentService implements PaymentService {
|
|
|
21
18
|
private populateSetPaymentMethod;
|
|
22
19
|
findMatchingTransactions(payment: Payment, transaction: TransactionData): Transaction[];
|
|
23
20
|
private consolidateTransactionChanges;
|
|
24
|
-
private hasTransactionWithState;
|
|
25
|
-
private calculateTotalAmount;
|
|
26
21
|
}
|
|
@@ -47,78 +47,6 @@ class DefaultPaymentService {
|
|
|
47
47
|
}
|
|
48
48
|
throw err;
|
|
49
49
|
}
|
|
50
|
-
validatePaymentCancelAuthorization(opts) {
|
|
51
|
-
if (!this.hasTransactionWithState(opts.payment, 'Authorization', ['Success'])) {
|
|
52
|
-
return { isValid: false, reason: `No authorization transaction found for resource ${opts.payment.id}.` };
|
|
53
|
-
}
|
|
54
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Success'])) {
|
|
55
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} has already been cancelled.` };
|
|
56
|
-
}
|
|
57
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Pending'])) {
|
|
58
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} is pending to be cancelled.` };
|
|
59
|
-
}
|
|
60
|
-
if (this.hasTransactionWithState(opts.payment, 'Charge', ['Success'])) {
|
|
61
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} has already been charged.` };
|
|
62
|
-
}
|
|
63
|
-
if (this.hasTransactionWithState(opts.payment, 'Charge', ['Pending'])) {
|
|
64
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} is pending to be charged.` };
|
|
65
|
-
}
|
|
66
|
-
return { isValid: true };
|
|
67
|
-
}
|
|
68
|
-
validatePaymentCharge(opts) {
|
|
69
|
-
if (opts.payment.amountPlanned.currencyCode !== opts.amount.currencyCode) {
|
|
70
|
-
return {
|
|
71
|
-
isValid: false,
|
|
72
|
-
reason: `Invalid currency ${opts.amount.currencyCode} for resource ${opts.payment.id}, expected ${opts.payment.amountPlanned.currencyCode}`,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
const totalAuthorized = this.calculateTotalAmount(opts.payment, 'Authorization', opts.amount.currencyCode);
|
|
76
|
-
if (totalAuthorized === 0) {
|
|
77
|
-
return { isValid: false, reason: `No authorization transaction found for resource ${opts.payment.id}.` };
|
|
78
|
-
}
|
|
79
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Success'])) {
|
|
80
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} has already been cancelled.` };
|
|
81
|
-
}
|
|
82
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Pending'])) {
|
|
83
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} is pending to be cancelled.` };
|
|
84
|
-
}
|
|
85
|
-
const totalCaptured = this.calculateTotalAmount(opts.payment, 'Charge', opts.amount.currencyCode);
|
|
86
|
-
const allowedAmount = totalAuthorized - totalCaptured;
|
|
87
|
-
if (opts.amount.centAmount > allowedAmount) {
|
|
88
|
-
return {
|
|
89
|
-
isValid: false,
|
|
90
|
-
reason: `The amount to capture ${opts.amount.centAmount} exceeds the allowed amount [${allowedAmount}]`,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
return { isValid: true };
|
|
94
|
-
}
|
|
95
|
-
validatePaymentRefund(opts) {
|
|
96
|
-
if (opts.payment.amountPlanned.currencyCode !== opts.amount.currencyCode) {
|
|
97
|
-
return {
|
|
98
|
-
isValid: false,
|
|
99
|
-
reason: `Invalid currency ${opts.amount.currencyCode} for resource ${opts.payment.id}, expected ${opts.payment.amountPlanned.currencyCode}`,
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Success'])) {
|
|
103
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} has already been cancelled.` };
|
|
104
|
-
}
|
|
105
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Pending'])) {
|
|
106
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} is pending to be cancelled.` };
|
|
107
|
-
}
|
|
108
|
-
const totalCaptured = this.calculateTotalAmount(opts.payment, 'Charge', opts.amount.currencyCode);
|
|
109
|
-
if (totalCaptured === 0) {
|
|
110
|
-
return { isValid: false, reason: `No charge transaction found for resource ${opts.payment.id}.` };
|
|
111
|
-
}
|
|
112
|
-
const totalRefunded = this.calculateTotalAmount(opts.payment, 'Refund', opts.amount.currencyCode);
|
|
113
|
-
const allowedAmount = totalCaptured - totalRefunded;
|
|
114
|
-
if (opts.amount.centAmount > allowedAmount) {
|
|
115
|
-
return {
|
|
116
|
-
isValid: false,
|
|
117
|
-
reason: `The amount to refund ${opts.amount.centAmount} exceeds the allowed amount [${allowedAmount}]`,
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
return { isValid: true };
|
|
121
|
-
}
|
|
122
50
|
consolidateUpdateActions(payment, updateInfo) {
|
|
123
51
|
const actions = [];
|
|
124
52
|
if (!payment.interfaceId && updateInfo.pspReference) {
|
|
@@ -209,15 +137,5 @@ class DefaultPaymentService {
|
|
|
209
137
|
}
|
|
210
138
|
return actions;
|
|
211
139
|
}
|
|
212
|
-
hasTransactionWithState(payment, type, state) {
|
|
213
|
-
return payment.transactions.some((transaction) => transaction.type === type && state.includes(transaction.state));
|
|
214
|
-
}
|
|
215
|
-
calculateTotalAmount(payment, type, currencyCode) {
|
|
216
|
-
return payment.transactions
|
|
217
|
-
.filter((transaction) => transaction.type === type &&
|
|
218
|
-
transaction.state === 'Success' &&
|
|
219
|
-
transaction.amount.currencyCode === currencyCode)
|
|
220
|
-
.reduce((total, transaction) => total + transaction.amount.centAmount, 0);
|
|
221
|
-
}
|
|
222
140
|
}
|
|
223
141
|
exports.DefaultPaymentService = DefaultPaymentService;
|
|
@@ -30,21 +30,6 @@ export type UpdatePayment = {
|
|
|
30
30
|
transaction?: TransactionData;
|
|
31
31
|
paymentMethod?: string;
|
|
32
32
|
};
|
|
33
|
-
export type PaymentCancelAuthorizationValidation = {
|
|
34
|
-
payment: Payment;
|
|
35
|
-
};
|
|
36
|
-
export type PaymentChargeValidation = {
|
|
37
|
-
payment: Payment;
|
|
38
|
-
amount: Money;
|
|
39
|
-
};
|
|
40
|
-
export type PaymentRefundValidation = {
|
|
41
|
-
payment: Payment;
|
|
42
|
-
amount: Money;
|
|
43
|
-
};
|
|
44
|
-
export type PaymentModificationValidationResult = {
|
|
45
|
-
isValid: boolean;
|
|
46
|
-
reason?: string;
|
|
47
|
-
};
|
|
48
33
|
/**
|
|
49
34
|
* Payment service interface exposes methods to interact with the commercetools platform API.
|
|
50
35
|
*/
|
|
@@ -52,7 +37,4 @@ export interface PaymentService {
|
|
|
52
37
|
getPayment(opts: GetPayment): Promise<Payment>;
|
|
53
38
|
createPayment(draft: PaymentDraft): Promise<Payment>;
|
|
54
39
|
updatePayment(opts: UpdatePayment): Promise<Payment>;
|
|
55
|
-
validatePaymentCancelAuthorization(opts: PaymentCancelAuthorizationValidation): PaymentModificationValidationResult;
|
|
56
|
-
validatePaymentCharge(opts: PaymentChargeValidation): PaymentModificationValidationResult;
|
|
57
|
-
validatePaymentRefund(opts: PaymentRefundValidation): PaymentModificationValidationResult;
|
|
58
40
|
}
|