Skip to content

Commit

Permalink
fix: reading callback url from env (#3)
Browse files Browse the repository at this point in the history
* fix: reading callback url from env

* refacor: added fail option when evironment variables are missing

---------

Co-authored-by: Amos Machora<81857018+AmosMachora@users.noreply.github.com>
  • Loading branch information
amosmachora authored Jun 25, 2024
1 parent 30086fb commit c95a580
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/api/mpesa/stk-push/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { payments } from "@/app/db/schema";
import { db } from "@/app/db/drizzle-client";
import { stkPushRequest } from "@/daraja/stk-push";
import { allowedOrigins, corsOptions } from "@/utils/cors";
import { MPESA_APP_BASE_URL } from "@/config/env";

type RequestBody = {
accountReference: string;
Expand All @@ -18,7 +19,7 @@ export const POST = async (request: NextRequest) => {
const requestBody: RequestBody = {
accountReference,
amount,
callbackURL: `https://billing.kenyahmis.org/api/mpesa/stk-push-callback`,
callbackURL: `${MPESA_APP_BASE_URL}/api/mpesa/stk-push-callback`,
phoneNumber,
transactionDesc: "HMIS Payment",
};
Expand Down
12 changes: 12 additions & 0 deletions config/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const MPESA_APP_BASE_URL = assertValue(
process.env.MPESA_APP_BASE_URL,
"Missing environment variable: MPESA_APP_BASE_URL"
);

function assertValue<T>(v: T | undefined, errorMessage: string): T {
if (v === undefined) {
throw new Error(errorMessage);
}

return v;
}

0 comments on commit c95a580

Please sign in to comment.