-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstripe-service.ts
269 lines (240 loc) · 10 KB
/
stripe-service.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the module page of the stripe-service.
*
* @module stripe
*/
import Stripe from 'stripe';
import { Dinero } from 'dinero.js';
import log4js, { Logger } from 'log4js';
import User from '../entity/user/user';
import StripeDeposit from '../entity/stripe/stripe-deposit';
import DineroTransformer from '../entity/transformer/dinero-transformer';
import StripePaymentIntentStatus, { StripePaymentIntentState } from '../entity/stripe/stripe-payment-intent-status';
import {
StripeDepositResponse,
StripePaymentIntentStatusResponse,
StripePaymentIntentResponse,
} from '../controller/response/stripe-response';
import TransferService from './transfer-service';
import { EntityManager, IsNull } from 'typeorm';
import { parseUserToBaseResponse } from '../helpers/revision-to-response';
import BalanceResponse from '../controller/response/balance-response';
import { StripeRequest } from '../controller/request/stripe-request';
import StripePaymentIntent from '../entity/stripe/stripe-payment-intent';
import { asNumber } from '../helpers/validators';
import WithManager from '../database/with-manager';
export const STRIPE_API_VERSION = '2024-06-20';
export default class StripeService extends WithManager {
private stripe: Stripe;
private logger: Logger;
constructor(manager?: EntityManager) {
super(manager);
this.stripe = new Stripe(process.env.STRIPE_PRIVATE_KEY, {
apiVersion: STRIPE_API_VERSION,
});
this.logger = log4js.getLogger('StripeController');
}
/**
* Topup should be at least 10 euros or the user's negative balance.
* @param balance
* @param request
*/
public static validateStripeRequestMinimumAmount(balance: BalanceResponse, request: StripeRequest): boolean {
const MIN_TOPUP = asNumber(process.env.MIN_TOPUP) || 1000;
// Check if top-up is enough
if (request.amount.amount >= MIN_TOPUP) return true;
return request.amount.amount === -1 * balance.amount.amount;
}
/**
* Topup should be at most 150 euros minus user's positive balance or user's negative balance.
* @param balance
* @param request
*/
public static validateStripeRequestMaximumAmount(balance: BalanceResponse, request: StripeRequest): boolean {
const MAX_BALANCE = asNumber(process.env.MAX_BALANCE) || 15000;
// Check if top-up will not exceed max balance
return MAX_BALANCE >= (balance.amount.amount + request.amount.amount);
}
private static asStripePaymentIntentStatusResponse(status: StripePaymentIntentStatus): StripePaymentIntentStatusResponse {
return {
id: status.id,
createdAt: status.createdAt.toISOString(),
updatedAt: status.updatedAt.toISOString(),
version: status.version,
state: status.state,
};
}
public static asStripeDepositResponse(deposit: StripeDeposit): StripeDepositResponse {
return {
id: deposit.id,
createdAt: deposit.createdAt.toISOString(),
updatedAt: deposit.updatedAt.toISOString(),
version: deposit.version,
stripeId: deposit.stripePaymentIntent.stripeId,
depositStatus: deposit.stripePaymentIntent.paymentIntentStatuses.map((s) => this.asStripePaymentIntentStatusResponse(s)),
amount: deposit.stripePaymentIntent.amount.toObject(),
to: parseUserToBaseResponse(deposit.to, true),
};
}
public static async getProcessingStripeDepositsFromUser(userId: number): Promise<StripeDepositResponse[]> {
const deposits = await StripeDeposit.find({
where: {
to: {
id: userId,
},
transfer: IsNull(),
stripePaymentIntent: {
paymentIntentStatuses: {
state: StripePaymentIntentState.PROCESSING,
},
},
},
relations: ['to'],
});
return deposits.filter((d) => !d.stripePaymentIntent.paymentIntentStatuses.some(
(s) => s.state === StripePaymentIntentState.SUCCEEDED
|| s.state === StripePaymentIntentState.FAILED))
.map((d) => this.asStripeDepositResponse(d));
}
public static async getStripeDeposit(id: number, relations: string[] = []): Promise<StripeDeposit> {
return StripeDeposit.findOne({
where: { id },
relations: ['stripePaymentIntent', 'stripePaymentIntent.paymentIntentStatuses'].concat(relations),
});
}
/**
* Get a payment intent with the given ID, if it exists
* @param stripeId
*/
public async getPaymentIntent(stripeId: string): Promise<StripePaymentIntent | null> {
return this.manager.getRepository(StripePaymentIntent).findOne({ where: { stripeId } });
}
/**
* Create a payment intent and save it to the database
* @param user User that wants to deposit some money into their account
* @param amount The amount to be deposited
*/
public async createStripePaymentIntent(
user: User, amount: Dinero,
): Promise<StripePaymentIntentResponse> {
const paymentIntent = await this.stripe.paymentIntents.create({
amount: DineroTransformer.Instance.to(amount),
currency: amount.getCurrency(),
automatic_payment_methods: { enabled: true },
description: `SudoSOS deposit of ${amount.getCurrency()} ${(amount.getAmount() / 100).toFixed(2)} for ${User.fullName(user)}.`,
metadata: {
'service': process.env.NAME ?? 'sudosos-unknown',
'userId': user.id,
},
});
const stripePaymentIntent = await this.manager.getRepository(StripePaymentIntent).save({
stripeId: paymentIntent.id,
amount,
paymentIntentStatuses: [],
});
const stripeDeposit = await this.manager.getRepository(StripeDeposit).save({
stripePaymentIntent,
to: user,
});
return {
id: stripeDeposit.id,
createdAt: stripeDeposit.createdAt.toISOString(),
updatedAt: stripeDeposit.updatedAt.toISOString(),
stripeId: stripePaymentIntent.stripeId,
clientSecret: paymentIntent.client_secret,
};
}
/**
* Validate a Stripe webhook event
* @param body
* @param signature
*/
public async constructWebhookEvent(
body: any, signature: string | string[],
): Promise<Stripe.Event> {
return this.stripe.webhooks.constructEvent(body, signature, process.env.STRIPE_WEBHOOK_SECRET);
}
/**
* Create a new deposit status
* @param paymentIntentId
* @param state
*/
public async createNewPaymentIntentStatus(
paymentIntentId: number, state: StripePaymentIntentState,
): Promise<StripePaymentIntentStatus> {
const paymentIntent = await this.manager.getRepository(StripePaymentIntent)
.findOne({ where: { id: paymentIntentId }, relations: { deposit: true } });
const states = paymentIntent.paymentIntentStatuses?.map((status) => status.state) ?? [];
if (states.includes(state)) throw new Error(`Status ${state} already exists.`);
if (state === StripePaymentIntentState.SUCCEEDED && states.includes(StripePaymentIntentState.FAILED)) {
throw new Error('Cannot create status SUCCEEDED, because FAILED already exists');
}
if (state === StripePaymentIntentState.FAILED && states.includes(StripePaymentIntentState.SUCCEEDED)) {
throw new Error('Cannot create status FAILED, because SUCCEEDED already exists');
}
const depositStatus = await this.manager.getRepository(StripePaymentIntentStatus).save({ stripePaymentIntent: paymentIntent, state });
// If payment has succeeded, create the transfer
if (state === StripePaymentIntentState.SUCCEEDED && paymentIntent.deposit) {
paymentIntent.deposit.transfer = await new TransferService(this.manager).createTransfer({
amount: paymentIntent.amount.toObject(),
toId: paymentIntent.deposit.to.id,
description: paymentIntent.stripeId,
fromId: undefined,
});
await this.manager.save(paymentIntent.deposit);
}
return depositStatus;
}
/**
* Handle the event by making the appropriate database additions
* @param event {Stripe.Event} Event received from Stripe webhook
*/
public async handleWebhookEvent(event: Stripe.Event) {
try {
const eventPaymentIntent = event.data.object as Stripe.PaymentIntent;
const paymentIntent = await StripePaymentIntent.findOne({
where: { stripeId: eventPaymentIntent.id },
relations: { deposit: { transfer: true }, paymentIntentStatuses: true },
});
switch (event.type) {
case 'payment_intent.created':
await this.createNewPaymentIntentStatus(paymentIntent.id, StripePaymentIntentState.CREATED);
break;
case 'payment_intent.processing':
await this.createNewPaymentIntentStatus(paymentIntent.id, StripePaymentIntentState.PROCESSING);
break;
case 'payment_intent.succeeded':
await this.createNewPaymentIntentStatus(paymentIntent.id, StripePaymentIntentState.SUCCEEDED);
break;
case 'payment_intent.payment_failed':
case 'payment_intent.canceled':
await this.createNewPaymentIntentStatus(paymentIntent.id, StripePaymentIntentState.FAILED);
break;
default:
this.logger.warn('Tried to process event', event.type, 'but processing method is not defined');
}
this.logger.trace(`Successfully processed event "${event.type}" for payment intent "${eventPaymentIntent.id}" (ID: ${paymentIntent.id})`);
} catch (error) {
this.logger.error('Could not process Stripe webhook event with ID', event.id, error);
}
}
}