Skip to content

Commit

Permalink
fix: Stripe webhook success event never creating a transfer (#282)
Browse files Browse the repository at this point in the history
* fix: Stripe webhook success event never creating a transfer

* fix: test case not detecting the problem in the first place

* fix: test case
  • Loading branch information
Yoronex authored Aug 25, 2024
1 parent 933c47b commit a8bc690
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/service/stripe-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export default class StripeService {
paymentIntentId: number, state: StripePaymentIntentState,
): Promise<StripePaymentIntentStatus> {
const paymentIntent = await this.manager.getRepository(StripePaymentIntent)
.findOne({ where: { id: paymentIntentId } });
.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.`);
Expand Down
12 changes: 7 additions & 5 deletions test/unit/service/stripe-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('StripeService', async (): Promise<void> => {
});
});

describe('createNewDepositStatus', () => {
describe('createNewPaymentIntentStatus', () => {
const testStatusCreation = async (id: number, state: StripePaymentIntentState) => {
const beforeStripeDeposit = await StripeService.getStripeDeposit(id);

Expand Down Expand Up @@ -146,19 +146,21 @@ describe('StripeService', async (): Promise<void> => {
await testStatusCreation(id, StripePaymentIntentState.PROCESSING);
});
it('should correctly create only one success status', async () => {
const { id } = (ctx.stripeDeposits.filter((d) => d.stripePaymentIntent.paymentIntentStatuses.length === 2))[0];
let deposit = await StripeService.getStripeDeposit(id);
expect(deposit.transfer).to.be.undefined;
const { id } = (ctx.stripeDeposits.filter((d) => d.stripePaymentIntent.paymentIntentStatuses.length === 2 && !d.transfer))[0];
let deposit = await StripeService.getStripeDeposit(id, ['transfer', 'transfer.to', 'to']);
expect(deposit.transfer).to.be.null;

await testStatusCreation(id, StripePaymentIntentState.SUCCEEDED);

deposit = await StripeService.getStripeDeposit(id, ['transfer', 'transfer.to', 'to']);
// Correct transfer should have been created
expect(deposit.transfer).to.not.be.null;
expect(ctx.dineroTransformer.to(deposit.transfer.amountInclVat))
.to.equal(ctx.dineroTransformer.to(deposit.stripePaymentIntent.amount));
expect(deposit.transfer.to.id).to.equal(deposit.to.id);
});
it('should correctly create only one failed status', async () => {
const { id } = (ctx.stripeDeposits.filter((d) => d.stripePaymentIntent.paymentIntentStatuses.length === 2))[1];
const { id } = (ctx.stripeDeposits.filter((d) => d.stripePaymentIntent.paymentIntentStatuses.length === 1))[1];
await testStatusCreation(id, StripePaymentIntentState.FAILED);
});
it('should not create duplicate created status', async () => {
Expand Down

0 comments on commit a8bc690

Please sign in to comment.