fix(api): /charge-stripe-card responses (#54530)

Co-authored-by: Naomi <nhcarrigan@gmail.com>
This commit is contained in:
Oliver Eyton-Williams
2024-04-25 21:22:43 +02:00
committed by GitHub
parent 04d6cfcce0
commit 0b65ec502b
3 changed files with 54 additions and 37 deletions
+16 -8
View File
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {
createSuperRequest,
devLogin,
@@ -77,9 +76,11 @@ describe('Donate', () => {
);
expect(response.body).toEqual({
type: 'UserActionRequired',
message: 'Payment requires user action',
client_secret: 'superSecret'
error: {
type: 'UserActionRequired',
message: 'Payment requires user action',
client_secret: 'superSecret'
}
});
expect(response.status).toBe(402);
});
@@ -93,8 +94,10 @@ describe('Donate', () => {
);
expect(response.body).toEqual({
type: 'PaymentMethodRequired',
message: 'Card has been declined'
error: {
type: 'PaymentMethodRequired',
message: 'Card has been declined'
}
});
expect(response.status).toBe(402);
});
@@ -111,6 +114,12 @@ describe('Donate', () => {
const failResponse = await superPost('/donate/charge-stripe-card').send(
chargeStripeCardReqBody
);
expect(failResponse.body).toEqual({
error: {
type: 'AlreadyDonatingError',
message: 'User is already donating.'
}
});
expect(failResponse.status).toBe(400);
});
@@ -121,8 +130,7 @@ describe('Donate', () => {
);
expect(response.status).toBe(500);
expect(response.body).toEqual({
type: 'danger',
message: 'Donation failed due to a server error.'
error: 'Donation failed due to a server error.'
});
});
});
+25 -21
View File
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {
Type,
type FastifyPluginCallbackTypebox
@@ -106,10 +105,12 @@ export const donateRoutes: FastifyPluginCallbackTypebox = (
if (user.isDonating) {
void reply.code(400);
return {
type: 'info',
message: 'User is already donating.'
} as const;
return reply.send({
error: {
type: 'AlreadyDonatingError',
message: 'User is already donating.'
}
});
}
// Create Stripe Customer
@@ -141,18 +142,22 @@ export const donateRoutes: FastifyPluginCallbackTypebox = (
});
if (status === 'requires_source_action') {
void reply.code(402);
return {
type: 'UserActionRequired',
message: 'Payment requires user action',
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
client_secret
} as const;
return reply.send({
error: {
type: 'UserActionRequired',
message: 'Payment requires user action',
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
client_secret
}
});
} else if (status === 'requires_source') {
void reply.code(402);
return {
type: 'PaymentMethodRequired',
message: 'Card has been declined'
} as const;
return reply.send({
error: {
type: 'PaymentMethodRequired',
message: 'Card has been declined'
}
});
}
// update record in database
@@ -182,17 +187,16 @@ export const donateRoutes: FastifyPluginCallbackTypebox = (
}
});
return {
return reply.send({
type: 'success',
isDonating: true
} as const;
});
} catch (error) {
fastify.log.error(error);
void reply.code(500);
return {
type: 'danger',
message: 'Donation failed due to a server error.'
} as const;
return reply.send({
error: 'Donation failed due to a server error.'
});
}
}
);
+13 -8
View File
@@ -12,18 +12,23 @@ export const chargeStripeCard = {
type: Type.Literal('success')
}),
400: Type.Object({
message: Type.String(),
type: Type.Literal('info')
error: Type.Object({
message: Type.String(),
type: Type.Literal('AlreadyDonatingError')
})
}),
402: Type.Object({
message: Type.String(),
type: Type.String(),
// eslint-disable-next-line @typescript-eslint/naming-convention
client_secret: Type.Optional(Type.String())
error: Type.Object({
message: Type.String(),
type: Type.Union([
Type.Literal('UserActionRequired'),
Type.Literal('PaymentMethodRequired')
]),
client_secret: Type.Optional(Type.String())
})
}),
500: Type.Object({
message: Type.String(),
type: Type.Literal('danger')
error: Type.Literal('Donation failed due to a server error.')
})
}
};