Skip to content

Commit

Permalink
refactor: allow for transaction type and partyB configuration. (#13)
Browse files Browse the repository at this point in the history
refactor: allow for transaction type configuration type configuration and party B configuration
  • Loading branch information
amosmachora authored Sep 2, 2024
1 parent d09e1cf commit cdf4405
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
5 changes: 0 additions & 5 deletions config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ export const BASE_URL =
? "https://api.safaricom.co.ke"
: "https://sandbox.safaricom.co.ke";

export const MPESA_TRANSACTION_TYPE = assertValue(
process.env.MPESA_TRANSACTION_TYPE,
"Missing environment variable: MPESA_TRANSACTION_TYPE"
);

export const MPESA_APP_BASE_URL = assertValue(
process.env.MPESA_APP_BASE_URL,
"Missing environment variable: MPESA_APP_BASE_URL"
Expand Down
4 changes: 3 additions & 1 deletion config/mpesa-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ export type MPESA_CONFIG = {
MPESA_CONSUMER_KEY: string;
MPESA_CONSUMER_SECRET: string;
MPESA_API_PASS_KEY: string;
MPESA_TILL_OR_PAYBILL_NO: string;
MPESA_TRANSACTION_TYPE: "BUY-GOODS" | "PAYBILL";
};

// HASHMAP
const mpesaConfigMap: { [key: string]: MPESA_CONFIG } = assertValue(
configData,
configData as { [key: string]: MPESA_CONFIG },
"MISSING MPESA CONFIGURATION FILE"
);

Expand Down
16 changes: 12 additions & 4 deletions daraja/stk-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export const stkPushRequest = async (
}: STKPushRequestParam,
mpesaConfig: MPESA_CONFIG
) => {
const { MPESA_BUSINESS_SHORT_CODE } = mpesaConfig;
const {
MPESA_BUSINESS_SHORT_CODE,
MPESA_TILL_OR_PAYBILL_NO,
MPESA_TRANSACTION_TYPE,
} = mpesaConfig;

try {
const timestamp = generateTimestamp();
Expand All @@ -43,19 +47,23 @@ export const stkPushRequest = async (

const stkPushBody: STKPushBody = {
BusinessShortCode: MPESA_BUSINESS_SHORT_CODE,
PartyB: MPESA_BUSINESS_SHORT_CODE,
PartyB: MPESA_TILL_OR_PAYBILL_NO,
Timestamp: timestamp,
Password: password,
PartyA: phoneNumber,
PhoneNumber: phoneNumber,
Amount: ENVIRONMENT === "production" ? amount : "1",
CallBackURL: callbackURL,
TransactionDesc: transactionDesc,
TransactionType: process.env
.MPESA_TRANSACTION_TYPE as unknown as TransactionType,
TransactionType:
MPESA_TRANSACTION_TYPE === "BUY-GOODS"
? "CustomerBuyGoodsOnline"
: "CustomerPayBillOnline",
AccountReference: accountReference,
};

console.log("stk-push-body", stkPushBody);

const accessTokenResponse = await generateAccessToken(mpesaConfig);

const res = await axios.post<STKPushResponse>(
Expand Down

0 comments on commit cdf4405

Please sign in to comment.