Skip to content

Commit

Permalink
TSUP problems
Browse files Browse the repository at this point in the history
  • Loading branch information
amosmachora committed Oct 5, 2023
1 parent 5f3b713 commit b6cc671
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 95 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
dist
33 changes: 33 additions & 0 deletions hooks/useInitializeApp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import axios from "axios";
import { InitOptions, InitializeAppResponse } from "../types";
import { useReactDaraja } from "./useReactDaraja";

export const useInitializeApp = (
initOptions: InitOptions
): (() => Promise<InitializeAppResponse>) => {
const { consumerKey, consumerSecret } = initOptions;
const credentials = `${consumerKey}:${consumerSecret}`;
const encodedCredentials = btoa(credentials);

const { baseURL } = useReactDaraja();

const initApp = async (): Promise<InitializeAppResponse> => {
try {
const res = await axios.get(
`${baseURL}/oauth/v1/generate?grant_type=client_credentials`,
{
headers: {
Authorization: `Bearer ${encodedCredentials}`,
},
}
);
return res.data;
} catch (err: any) {
throw new Error(
`Error occurred with status code ${err.response?.status}, ${err.response?.statusText}`
);
}
};

return initApp;
};
17 changes: 8 additions & 9 deletions hooks/useReactDaraja.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { createContext, useContext, useEffect, useState } from "react";
import { InitializeApp } from "..";
import { BusinessShortCode, ContextData, STKPushBody } from "../types";
import { useInitializeApp } from "./useInitializeApp";

export const darajaAPIProvider = createContext<ContextData>({
accessToken: null,
Expand Down Expand Up @@ -32,21 +32,20 @@ export const ReactDarajaProvider = ({
? "https://sandbox.safaricom.co.ke"
: "https://api.safaricom.co.ke";

const initFunction = useInitializeApp({
consumerKey,
consumerSecret,
});

useEffect(() => {
let intervalId: NodeJS.Timeout;

const init = async () => {
const { access_token, expires_in } = await InitializeApp({
consumerKey,
consumerSecret,
});
const { access_token, expires_in } = await initFunction();
setAccessToken(access_token);

intervalId = setInterval(async () => {
const { access_token } = await InitializeApp({
consumerKey,
consumerSecret,
});
const { access_token } = await initFunction();
setAccessToken(access_token);
}, parseInt(expires_in));
};
Expand Down
13 changes: 10 additions & 3 deletions hooks/useSTKPush.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import { useReactDaraja } from "./useReactDaraja";
export const useSTKPush = (
body: Omit<
STKPushBody,
"BusinessShortCode" | "PartyB" | "Timestamp" | "Password"
| "BusinessShortCode"
| "PartyB"
| "Timestamp"
| "Password"
| "PhoneNumber"
| "PartyA"
>
): (() => Promise<STKPushResponse>) => {
): ((phoneNumber: string) => Promise<STKPushResponse>) => {
const { accessToken, businessShortCode, baseURL, mode, productionPassKey } =
useReactDaraja();

const stkPush = async () => {
const stkPush = async (phoneNumber: string) => {
try {
const timestamp = generateTimestamp();
const password =
Expand All @@ -26,6 +31,8 @@ export const useSTKPush = (
PartyB: businessShortCode!,
Timestamp: timestamp,
Password: password,
PartyA: phoneNumber,
PhoneNumber: phoneNumber,
};

const res: STKPushResponse = await axios.post(
Expand Down
27 changes: 0 additions & 27 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,6 @@ import {
TransactionStatusResponse,
} from "./types";
import { useReactDaraja } from "./hooks/useReactDaraja";
import { generatePassword, generateTimestamp } from "./util/utils";

export const InitializeApp = async (
initOptions: InitOptions
): Promise<InitializeAppResponse> => {
const { consumerKey, consumerSecret } = initOptions;
const credentials = `${consumerKey}:${consumerSecret}`;
const encodedCredentials = btoa(credentials);

const { baseURL } = useReactDaraja();

try {
const res = await axios.get(
`${baseURL}/oauth/v1/generate?grant_type=client_credentials`,
{
headers: {
Authorization: `Bearer ${encodedCredentials}`,
},
}
);
return res.data;
} catch (err: any) {
throw new Error(
`Error occurred with status code ${err.response?.status}, ${err.response?.statusText}`
);
}
};

/**
* Use this API to check the status of a Lipa Na M-Pesa Online Payment.
Expand Down
Loading

0 comments on commit b6cc671

Please sign in to comment.