rename store endpoints, move stripe config to .env

This commit is contained in:
Christian Fehmer 2023-11-27 16:26:25 +01:00
parent 64af76859c
commit cb5f4db868
No known key found for this signature in database
GPG key ID: 7294582286D5F1F4
5 changed files with 37 additions and 30 deletions

4
.gitignore vendored
View file

@ -115,7 +115,3 @@ content-validation.js
ads.txt
updateContributors.mjs
copyAnticheatToDev.sh
#stripe secrets
stripeConfig*.json

View file

@ -50,7 +50,7 @@ const mockApp = request(app);
const configuration = Configuration.getCachedConfiguration();
describe("store controller test", () => {
describe("createCheckout", () => {
describe("startCheckout", () => {
beforeEach(async () => {
await enablePremiumFeatures(true);
});
@ -69,7 +69,7 @@ describe("store controller test", () => {
//WHEN
const response = await mockApp
.post("/store/checkouts")
.post("/store/startCheckout")
.set("Authorization", "Bearer 123456789")
.send({ items: [{ lookupKey: "prime_monthly" }] })
.expect(200);
@ -101,7 +101,7 @@ describe("store controller test", () => {
//WHEN
const response = await mockApp
.post("/store/checkouts")
.post("/store/startCheckout")
.set("Authorization", "Bearer 123456789")
.send({ items: [{ lookupKey: "prime_monthly" }] })
.expect(200);
@ -139,7 +139,7 @@ describe("store controller test", () => {
//WHEN
const response = await mockApp
.post("/store/checkouts")
.post("/store/startCheckout")
.set("Authorization", "Bearer 123456789")
.send({ items: [{ lookupKey: "prime_monthly" }] })
.expect(200);
@ -170,7 +170,7 @@ describe("store controller test", () => {
//WHEN
await mockApp
.post("/store/checkouts")
.post("/store/startCheckout")
.set("Authorization", "Bearer 123456789")
.send()
.expect(422)
@ -184,7 +184,7 @@ describe("store controller test", () => {
//WHEN
await mockApp
.post("/store/checkouts")
.post("/store/startCheckout")
.set("Authorization", "Bearer 123456789")
.send({ items: [] })
.expect(422)
@ -200,7 +200,7 @@ describe("store controller test", () => {
//WHEN
await mockApp
.post("/store/checkouts")
.post("/store/startCheckout")
.set("Authorization", "Bearer 123456789")
.send({ items: [{}] })
.expect(422)
@ -216,7 +216,7 @@ describe("store controller test", () => {
//WHEN
await mockApp
.post("/store/checkouts")
.post("/store/startCheckout")
.set("Authorization", "Bearer 123456789")
.send({ items: [{ lookupKey: "" }] })
.expect(422)
@ -234,7 +234,7 @@ describe("store controller test", () => {
//WHEN
await mockApp
.post("/store/checkouts")
.post("/store/startCheckout")
.set("Authorization", "Bearer 123456789")
.send({ items: [{ lookupKey: "first" }, { lookupKey: "second" }] })
.expect(422)
@ -253,7 +253,7 @@ describe("store controller test", () => {
//WHEN
await mockApp
.post("/store/checkouts")
.post("/store/startCheckout")
.set("Authorization", "Bearer 123456789")
.send()
.expect(503)
@ -261,7 +261,7 @@ describe("store controller test", () => {
});
});
});
describe("finalizeCheckout", () => {
describe("finishCheckout", () => {
beforeEach(async () => {
await enablePremiumFeatures(true);
userLinkCustomerByUidMock.mockResolvedValue();
@ -294,7 +294,7 @@ describe("store controller test", () => {
//WHEN
await mockApp
.post("/store/checkouts/sessionId")
.post("/store/finishCheckout/sessionId")
.set("Authorization", "Bearer 123456789")
.send()
.expect(200);
@ -319,7 +319,7 @@ describe("store controller test", () => {
//WHEN /THEN
await mockApp
.post("/store/checkouts/theSessionId")
.post("/store/finishCheckout/theSessionId")
.set("Authorization", "Bearer 123456789")
.send()
.expect(400)
@ -335,7 +335,7 @@ describe("store controller test", () => {
//WHEN
await mockApp
.post("/store/checkouts/sessionId")
.post("/store/finishCheckout/sessionId")
.set("Authorization", "Bearer 123456789")
.send()
.expect(500)
@ -352,7 +352,7 @@ describe("store controller test", () => {
//WHEN
await mockApp
.post("/store/checkouts/sessionId")
.post("/store/finishCheckout/sessionId")
.set("Authorization", "Bearer 123456789")
.send()
.expect(500)
@ -368,7 +368,7 @@ describe("store controller test", () => {
//WHEN
await mockApp
.post("/store/checkouts/theSessionId")
.post("/store/finishCheckout/theSessionId")
.set("Authorization", "Bearer 123456789")
.send()
.expect(503)

View file

@ -18,7 +18,7 @@ const validateFeatureEnabled = validateConfiguration({
});
router.post(
"/checkouts",
"/startCheckout",
validateFeatureEnabled,
authenticateRequest(),
validateRequest({
@ -38,7 +38,7 @@ router.post(
asyncHandler(StoreController.createCheckout)
);
router.post(
"/checkouts/:stripeSessionId",
"/finishCheckout/:stripeSessionId",
validateFeatureEnabled,
authenticateRequest(),
validateRequest({

View file

@ -1,7 +1,8 @@
import * as config from "../credentials/stripeConfig.json";
import Stripe from "stripe";
import MonkeyError from "../utils/error";
const stripe = new Stripe(config.apiKey);
const { STRIPE_API_KEY } = process.env;
const stripe =
STRIPE_API_KEY !== undefined ? new Stripe(STRIPE_API_KEY) : undefined;
export type Price = {
id: string;
@ -14,7 +15,7 @@ export type Subscription = Stripe.Subscription;
export async function getPrices(
lookupKeys: Array<string>
): Promise<Array<Price>> {
const result = await stripe.prices.list({
const result = await getService().prices.list({
lookup_keys: lookupKeys,
});
//TODO error handling
@ -24,7 +25,7 @@ export async function getPrices(
export async function createCheckout(
params: SessionCreateParams
): Promise<string> {
const result = await stripe.checkout.sessions.create(params);
const result = await getService().checkout.sessions.create(params);
if (result.url === null) {
throw new MonkeyError(500, "Cannot create checkout session"); //TODO error handling
}
@ -32,13 +33,21 @@ export async function createCheckout(
}
export async function getCheckout(sessionId: string): Promise<Session> {
const session = await stripe.checkout.sessions.retrieve(sessionId);
const session = await getService().checkout.sessions.retrieve(sessionId);
return session;
}
export async function getSubscription(
subscriptionId: string
): Promise<Subscription> {
const subscription = await stripe.subscriptions.retrieve(subscriptionId);
const subscription = await getService().subscriptions.retrieve(
subscriptionId
);
return subscription;
}
function getService(): Stripe {
if (stripe === undefined)
throw new Error("Stripe config missing from environment 'STRIPE_API_KEY'.");
return stripe;
}

View file

@ -9,12 +9,14 @@ export default class Store {
items: [{ lookupKey: item }],
};
return await this.httpClient.post(`${BASE_PATH}/checkouts`, { payload });
return await this.httpClient.post(`${BASE_PATH}/startCheckout`, {
payload,
});
}
async finalizeCheckout(sessionId: string): Ape.EndpointResponse {
return await this.httpClient.post(
`${BASE_PATH}/checkouts/${sessionId}`,
`${BASE_PATH}/finishCheckout/${sessionId}`,
{}
);
}