Skip to content

Commit

Permalink
Enforces a minimum top-up for stripe deposits. (#135)
Browse files Browse the repository at this point in the history
* Enforces a minimum top-up for stripe deposits.

* Fixed rules

* Fixed testcases and small mistake in logic
  • Loading branch information
JustSamuel authored Jan 17, 2024
1 parent becfdf2 commit 3f57b0b
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/controller/stripe-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Policy from './policy';
import { RequestWithToken } from '../middleware/token-middleware';
import StripeService from '../service/stripe-service';
import { StripeRequest } from './request/stripe-request';
import BalanceService from '../service/balance-service';

export default class StripeController extends BaseController {
private logger: Logger = log4js.getLogger('StripeController');
Expand Down Expand Up @@ -72,6 +73,13 @@ export default class StripeController extends BaseController {

try {
const amount = Dinero({ ...request.amount } as DineroObject);
const balance = await BalanceService.getBalance(req.token.user.id);
if (!StripeService.validateStripeRequestAmount(balance, request)) {
res.status(422).json({ error: 'Top-up amount is to low' });
return;
}


const result = await this.stripeService.createStripePaymentIntent(req.token.user, amount);
res.status(200).json(result);
} catch (error) {
Expand Down
15 changes: 15 additions & 0 deletions src/service/stripe-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import TransferService from './transfer-service';
import { EntityManager, IsNull } from 'typeorm';
import { parseUserToBaseResponse } from '../helpers/revision-to-response';
import wrapInManager from '../helpers/database';
import BalanceResponse from '../controller/response/balance-response';
import { StripeRequest } from '../controller/request/stripe-request';

export const STRIPE_API_VERSION = '2022-08-01';

Expand All @@ -46,6 +48,19 @@ export default class StripeService {
this.logger = getLogger('StripeController');
}

/**
* Topup should be at least 10 euros or the users negative balance.
* @param balance
* @param request
*/
public static validateStripeRequestAmount(balance: BalanceResponse, request: StripeRequest): boolean {
const MIN_TOPUP = 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;
}

private static asStripeDepositStatusResponse(status: StripeDepositStatus): StripeDepositStatusResponse {
return {
id: status.id,
Expand Down
87 changes: 87 additions & 0 deletions test/unit/service/stripe-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import StripeService, { STRIPE_API_VERSION } from '../../../src/service/stripe-s
import DineroTransformer from '../../../src/entity/transformer/dinero-transformer';
import { StripeDepositState } from '../../../src/entity/deposit/stripe-deposit-status';
import wrapInManager from '../../../src/helpers/database';
import BalanceResponse from '../../../src/controller/response/balance-response';
import { StripeRequest } from '../../../src/controller/request/stripe-request';

describe('StripeService', async (): Promise<void> => {
let shouldSkip: boolean;
Expand Down Expand Up @@ -260,3 +262,88 @@ describe('StripeService', async (): Promise<void> => {
});
});
});

describe('validateStripeRequestAmount', async () => {
it('should accept 10 euros if user is in the positive', () => {
const balance = { amount: {
amount: 1,
currency: 'EUR',
precision: 2,
} } as BalanceResponse;
const request: StripeRequest = {
amount: {
amount: 1000,
currency: 'EUR',
precision: 2,
},
};
const res = StripeService.validateStripeRequestAmount(balance, request);
expect(res).to.be.true;
});
it('should disallow 9 euros if user is in the positive', () => {
const balance = { amount: {
amount: 1,
currency: 'EUR',
precision: 2,
} } as BalanceResponse;
const request: StripeRequest = {
amount: {
amount: 900,
currency: 'EUR',
precision: 2,
},
};
const res = StripeService.validateStripeRequestAmount(balance, request);
expect(res).to.be.false;
});
it('should accept 10 euros if user less than 10 euros in the negative', () => {
const balance = {
amount: {
amount: -800,
currency: 'EUR',
precision: 2,
},
} as BalanceResponse;
const request: StripeRequest = {
amount: {
amount: 1000,
currency: 'EUR',
precision: 2,
},
};
const res = StripeService.validateStripeRequestAmount(balance, request);
expect(res).to.be.true;
});
it('should allow 11 euros if user more than 10 euros in the negative', () => {
const balance = { amount: {
amount: -1800,
currency: 'EUR',
precision: 2,
} } as BalanceResponse;
const request: StripeRequest = {
amount: {
amount: 1100,
currency: 'EUR',
precision: 2,
},
};
const res = StripeService.validateStripeRequestAmount(balance, request);
expect(res).to.be.true;
});
it('should allow 8,33 euros if user is -8,33', () => {
const balance = { amount: {
amount: -833,
currency: 'EUR',
precision: 2,
} } as BalanceResponse;
const request: StripeRequest = {
amount: {
amount: 833,
currency: 'EUR',
precision: 2,
},
};
const res = StripeService.validateStripeRequestAmount(balance, request);
expect(res).to.be.true;
});
});

0 comments on commit 3f57b0b

Please sign in to comment.