Skip to content

Commit

Permalink
fix: remove cache functionality (#6)
Browse files Browse the repository at this point in the history
* fix: remove cache functionality

* fix: remove console.logs

---------

Co-authored-by: Amoh Prince <[email protected]>
  • Loading branch information
amosmachora and amosmachora authored Jun 27, 2024
1 parent 56eda11 commit c5225ce
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 30 deletions.
1 change: 0 additions & 1 deletion app/api/mpesa/check-payment-state/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export const POST = async (req: NextRequest) => {

return response;
} catch (error) {
console.error(error);
return NextResponse.json(
{
message: "An error occurred",
Expand Down
1 change: 0 additions & 1 deletion app/api/mpesa/stk-push/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export const POST = async (request: NextRequest) => {

return response;
} catch (err: any) {
console.error(err);
return NextResponse.json(
{
message: "An error occurred",
Expand Down
2 changes: 0 additions & 2 deletions app/api/stk-push-callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const safaricomOrigins = [
];

export const POST = async (req: NextRequest, res: NextResponse) => {
console.log("request", req);

const received: STKPushSuccessfulCallbackBody = await req.json();

const origin = req.headers.get("origin") ?? "";
Expand Down
10 changes: 0 additions & 10 deletions daraja/access-token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import axios from "axios";
import { BASE_URL } from "../config/env";
import cache from "memory-cache";
import { AccessTokenResponse } from "daraja-kit";
import { MPESA_CONFIG } from "@/config/mpesa-config";

Expand All @@ -11,12 +10,6 @@ export const generateAccessToken = async (
const credentials = `${MPESA_CONSUMER_KEY}:${MPESA_CONSUMER_SECRET}`;
const encodedAuthString = Buffer.from(credentials).toString("base64");

const token: AccessTokenResponse = cache.get("act");

if (token) {
return token;
}

try {
const res = await axios.get(
`${BASE_URL}/oauth/v1/generate?grant_type=client_credentials`,
Expand All @@ -27,11 +20,8 @@ export const generateAccessToken = async (
}
);

cache.put("act", res.data, 3599 * 1000);

return res.data;
} catch (err: any) {
console.error(err);
throw new Error(
`Error occurred with status code ${err.response?.status}, ${err.response?.statusText}`
);
Expand Down
4 changes: 1 addition & 3 deletions daraja/stk-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const stkPushRequest = async (
try {
const timestamp = generateTimestamp();

const password = generatePassword(mpesaConfig);
const password = generatePassword(mpesaConfig, timestamp);

const stkPushBody: STKPushBody = {
BusinessShortCode: MPESA_BUSINESS_SHORT_CODE,
Expand Down Expand Up @@ -70,8 +70,6 @@ export const stkPushRequest = async (

return res.data;
} catch (err: any) {
console.error(err);

throw new Error(
`Error occurred with status code ${err.response?.status}, ${err.response?.statusText}`
);
Expand Down
34 changes: 21 additions & 13 deletions daraja/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@ export function generateTimestamp(): string {
return `${year}${month}${date}${hours}${minutes}${seconds}`;
}

export const generatePassword = (mpesaConfig: MPESA_CONFIG): string => {
export const generatePassword = (
mpesaConfig: MPESA_CONFIG,
timestamp: string
): string => {
const { MPESA_BUSINESS_SHORT_CODE, MPESA_API_PASS_KEY } = mpesaConfig;

const timestamp = generateTimestamp();

const concatenatedString = `${MPESA_BUSINESS_SHORT_CODE}${MPESA_API_PASS_KEY}${timestamp}`;
const encodedString = Buffer.from(concatenatedString).toString("base64");

// Check if the environment is Node.js
if (typeof btoa === "undefined") {
// Node.js environment
const encodedString = Buffer.from(concatenatedString).toString("base64");
return encodedString;
} else {
// Browser environment
const encodedString = btoa(concatenatedString);
return encodedString;
}
return encodedString;
};

// {
// "BusinessShortCode": "174379",
// "Password": "MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMTYwMjE2MTY1NjI3",
// "Password" : 'NzYxNzcyMGM0Njg3Yzc2NDU3NjAzYzU4OGY3NjFiYTZhZGMzYTNjYWRmMjZkODBmYWM3YTNiZjJjYjI1ZjhkYmNmYTIyNTYyMDI0MDYyNzE3MjE0Mw==',
// "Timestamp":"20160216165627",
// "TransactionType": "CustomerPayBillOnline",
// "Amount": "1",
// "PartyA":"254708374149",
// "PartyB":"174379",
// "PhoneNumber":"254708374149",
// "CallBackURL": "https://mydomain.com/pat",
// "AccountReference":"Test",
// "TransactionDesc":"Test"
// }

0 comments on commit c5225ce

Please sign in to comment.