-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdac6e6
commit c28c294
Showing
12 changed files
with
237 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import axios from "axios"; | ||
import { BASE_URL, CONSUMER_KEY, CONSUMER_SECRET } from "./env"; | ||
import cache from "memory-cache"; | ||
import { AccessTokenResponse } from "../types"; | ||
|
||
export const generateAccessToken = async (): Promise<AccessTokenResponse> => { | ||
const credentials = `${CONSUMER_KEY}:${CONSUMER_SECRET}`; | ||
const encodedCredentials = btoa(credentials); | ||
|
||
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`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${encodedCredentials}`, | ||
"Access-Control-Allow-Origin": "*", | ||
}, | ||
} | ||
); | ||
|
||
cache.put("act", res.data, res.data.expires_in); | ||
|
||
return res.data; | ||
} catch (err: any) { | ||
throw new Error( | ||
`Error occurred with status code ${err.response?.status}, ${err.response?.statusText}` | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export const ENVIRONMENT = assertValue( | ||
process.env.NODE_ENV, | ||
"Missing environment variable: NODE_ENV" | ||
); | ||
|
||
export const BASE_URL = | ||
ENVIRONMENT === "production" | ||
? "https://api.safaricom.co.ke" | ||
: "https://sandbox.safaricom.co.ke"; | ||
|
||
export const CONSUMER_KEY = assertValue( | ||
process.env.MPESA_CONSUMER_KEY, | ||
"Missing environment variable: MPESA_CONSUMER_KEY" | ||
); | ||
|
||
export const CONSUMER_SECRET = assertValue( | ||
process.env.MPESA_CONSUMER_SECRET, | ||
"Missing environment variable: MPESA_CONSUMER_SECRET" | ||
); | ||
|
||
export const BUSINESS_SHORT_CODE = assertValue( | ||
process.env.BUSINESS_SHORT_CODE, | ||
"Missing environment variable: BUSINESS_SHORT_CODE" | ||
); | ||
|
||
export const PRODUCTION_PASS_KEY = | ||
ENVIRONMENT === "production" | ||
? assertValue( | ||
process.env.PRODUCTION_PASS_KEY, | ||
"Missing environment variable: PRODUCTION_PASS_KEY" | ||
) | ||
: null; | ||
|
||
function assertValue<T>(v: T | undefined, errorMessage: string): T { | ||
if (v === undefined) { | ||
throw new Error(errorMessage); | ||
} | ||
|
||
return v; | ||
} |
Oops, something went wrong.