Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit 3309011

Browse files
✨ Support for invoices
1 parent cf7d5a7 commit 3309011

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

src/helpers/stripe.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export const createStripeCustomer = async (
1818
organizationId: number,
1919
customer: Stripe.customers.ICustomerCardSourceCreationOptions
2020
) => {
21-
const created = await stripe.customers.create(customer);
21+
const created = await stripe.customers.create({
22+
...customer,
23+
metadata: { organizationId }
24+
});
2225
await updateOrganization(organizationId, { stripeCustomerId: created.id });
2326
return created;
2427
};
@@ -32,3 +35,11 @@ export const updateStripeCustomer = async (
3235
) => {
3336
return await stripe.customers.update(id, customer);
3437
};
38+
39+
/**
40+
* Get the details of a customer
41+
* @param id - Stripe customer ID
42+
*/
43+
export const getStripeInvoices = async (id: string) => {
44+
return await stripe.invoices.list({ customer: id });
45+
};

src/rest/organization.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import { can } from "../helpers/authorization";
2222
import {
2323
getStripeCustomer,
2424
createStripeCustomer,
25-
updateStripeCustomer
25+
updateStripeCustomer,
26+
getStripeInvoices
2627
} from "../helpers/stripe";
2728
import { customers } from "stripe";
2829

@@ -147,3 +148,16 @@ export const updateOrganizationBillingForUser = async (
147148
}
148149
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
149150
};
151+
152+
export const getOrganizationInvoicesForUser = async (
153+
userId: number,
154+
organizationId: number
155+
) => {
156+
if (await can(userId, Authorizations.READ, "organization", organizationId)) {
157+
const organization = await getOrganization(organizationId);
158+
if (organization.stripeCustomerId)
159+
return await getStripeInvoices(organization.stripeCustomerId);
160+
throw new Error(ErrorCode.STRIPE_NO_CUSTOMER);
161+
}
162+
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
163+
};

src/routes/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {
2121
routeOrganizationDelete,
2222
routeOrganizationGet,
2323
routeOrganizationBillingGet,
24-
routeOrganizationBillingUpdate
24+
routeOrganizationBillingUpdate,
25+
routeOrganizationInvoicesGet
2526
} from "./organizations";
2627
import { authHandler } from "../helpers/middleware";
2728
import {
@@ -146,6 +147,11 @@ const routesOrganization = (app: Application) => {
146147
authHandler,
147148
asyncHandler(routeOrganizationBillingUpdate)
148149
);
150+
app.get(
151+
"/organizations/:id/invoices",
152+
authHandler,
153+
asyncHandler(routeOrganizationInvoicesGet)
154+
);
149155
};
150156

151157
const routesMembership = (app: Application) => {

src/routes/organizations.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
deleteOrganizationForUser,
66
getOrganizationForUser,
77
getOrganizationBillingForUser,
8-
updateOrganizationBillingForUser
8+
updateOrganizationBillingForUser,
9+
getOrganizationInvoicesForUser
910
} from "../rest/organization";
1011
import { ErrorCode } from "../interfaces/enum";
1112

@@ -52,11 +53,9 @@ export const routeOrganizationBillingGet = async (
5253
req: Request,
5354
res: Response
5455
) => {
55-
const billing = await getOrganizationBillingForUser(
56-
res.locals.token.id,
57-
req.params.id
56+
res.json(
57+
await getOrganizationBillingForUser(res.locals.token.id, req.params.id)
5858
);
59-
res.json(billing);
6059
};
6160

6261
export const routeOrganizationBillingUpdate = async (
@@ -71,3 +70,12 @@ export const routeOrganizationBillingUpdate = async (
7170
);
7271
res.json({ updated: true });
7372
};
73+
74+
export const routeOrganizationInvoicesGet = async (
75+
req: Request,
76+
res: Response
77+
) => {
78+
res.json(
79+
await getOrganizationInvoicesForUser(res.locals.token.id, req.params.id)
80+
);
81+
};

0 commit comments

Comments
 (0)