daraja-pie
Version:
A simple JS API built on top of M-Pesa's daraja
33 lines (27 loc) • 801 B
JavaScript
import axios from "axios";
import { getAccessToken } from "./auth.js";
import { SB_END_POINTS, LIVE_END_POINTS } from "./constants.js";
export async function sendStkPush(details) {
const token = await getAccessToken();
const END_POINT =
process.env.ENVIRONMENT == "production"
? LIVE_END_POINTS.STK_PUSH
: SB_END_POINTS.STK_PUSH;
if (token) {
const config = {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${token}`,
},
};
const body = JSON.stringify(details);
try {
const { data } = await axios.post(END_POINT, body, config);
return data;
} catch (err) {
throw new Error(err);
}
} else {
throw new Error("Failed to get access token");
}
}