-
-
Notifications
You must be signed in to change notification settings - Fork 260
/
payment-reconciliator.ts
executable file
·164 lines (142 loc) · 4.68 KB
/
payment-reconciliator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env ./node_modules/.bin/babel-node
import '../../server/env';
import OrderStatus from '../../server/constants/order_status';
import { TransactionKind } from '../../server/constants/transaction-kind';
import logger from '../../server/lib/logger';
import { parseToBoolean } from '../../server/lib/utils';
import models, { Op, sequelize } from '../../server/models';
import { paypalRequestV2 } from '../../server/paymentProviders/paypal/api';
const exitWithUsage = () => {
console.error('Usage: ./scripts/paypal/payment-reconciliator.ts check|fix|list-hosts HOST_SLUG');
process.exit(1);
};
const START_DATE = new Date(process.env.START_DATE || '2020-01-01');
const END_DATE = new Date(process.env.END_DATE || new Date());
/**
* A generator to paginate the fetch of orders to avoid loading too much at once
*/
async function* getPaypalPaymentOrdersIterator(host, orderWhere) {
const limit = 100;
let offset = 0;
let orders;
do {
orders = await models.Order.findAll({
offset,
limit,
order: [['createdAt', 'DESC']],
where: {
...orderWhere,
createdAt: {
[Op.gte]: START_DATE,
[Op.lte]: END_DATE,
},
},
include: [
{
association: 'paymentMethod',
required: true,
where: {
service: 'paypal',
type: 'payment',
},
},
{
model: models.Transaction,
required: true,
where: { kind: TransactionKind.CONTRIBUTION, type: 'CREDIT', HostCollectiveId: host.id },
},
],
});
const nbOrders = orders.length;
while (orders.length) {
yield orders.shift();
}
if (!nbOrders || nbOrders % limit !== 0) {
break;
} else {
offset += nbOrders;
console.log(`(%) Processed ${nbOrders} orders, offset ${offset}`);
}
} while (true);
}
const checkOrder = async (host, order) => {
const transactions = order.Transactions;
const creditTransaction = transactions.find(t => t.kind === TransactionKind.CONTRIBUTION && t.type === 'CREDIT');
const captureId = creditTransaction.data?.capture?.id;
if (!captureId) {
logger.warn(`Order ${order.id} has no capture id: ${JSON.stringify(creditTransaction.data)}`);
return false;
}
const captureDetails = await paypalRequestV2(`payments/captures/${captureId}`, host, 'GET');
if (!captureDetails) {
logger.warn(`Order ${order.id} has no capture details matching capture ${captureId}`);
return false;
} else if (captureDetails.status !== 'COMPLETED') {
logger.warn(`Order ${order.id} has capture ${captureId} with status ${captureDetails.status}`);
return false;
}
return true;
};
const markOrderAsError = async order => {
logger.info(`Marking Order ${order.id} as error`);
await models.Transaction.update({ deletedAt: new Date() }, { where: { OrderId: order.id } });
await order.update({ status: OrderStatus.ERROR });
};
const findOrdersWithErroneousStatus = async (hostSlug, fix = false) => {
if (!hostSlug) {
return exitWithUsage();
}
const host = await models.Collective.findBySlug(hostSlug);
const orderIterator = getPaypalPaymentOrdersIterator(host, { status: 'PAID' });
let orderItem = await orderIterator.next();
let hasError = false;
while (!orderItem.done) {
const isValid = await checkOrder(host, orderItem.value);
hasError = hasError || !isValid;
if (fix && !isValid && !parseToBoolean(process.env.DRY)) {
await markOrderAsError(orderItem.value);
}
orderItem = await orderIterator.next();
}
if (!hasError) {
logger.info('No error found for PayPal orders');
}
};
const printAllHostsWithPaypalAccounts = async () => {
const hosts = await models.Collective.findAll({
where: { isHostAccount: true },
group: [sequelize.col('Collective.id')],
include: [
{
association: 'ConnectedAccounts',
required: true,
attributes: [],
where: { service: 'paypal', clientId: { [Op.not]: null }, token: { [Op.not]: null } },
},
],
});
const hostsLabelLists = hosts.map(host => `${host.slug} (#${host.id})`);
console.log(`Hosts with PayPal: ${hostsLabelLists.join(', ')}`);
};
const main = async () => {
const command = process.argv[2];
if (!command) {
return exitWithUsage();
}
switch (command) {
case 'check':
return findOrdersWithErroneousStatus(process.argv[3]);
case 'fix':
return findOrdersWithErroneousStatus(process.argv[3], true);
case 'list-hosts':
return printAllHostsWithPaypalAccounts();
default:
throw new Error(`Unknown command: ${command}`);
}
};
main()
.then(() => process.exit())
.catch(e => {
console.error(e);
process.exit(1);
});