Skip to content

Commit

Permalink
Merge pull request #64 from justinelut/master
Browse files Browse the repository at this point in the history
master
  • Loading branch information
justinelut authored Aug 9, 2023
2 parents 60a4c09 + f6ba2da commit 4fc0316
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 46 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
"contributions": [
"code"
]
},
{
"login": "OdidaProtas",
"name": "Dre",
"avatar_url": "https://avatars.githubusercontent.com/u/53041037?v=4",
"profile": "https://protas.dreamfeel.me/",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://me.verixr.com"><img src="https://avatars.githubusercontent.com/u/51832166?v=4?s=100" width="100px;" alt="Justine Gichana"/><br /><sub><b>Justine Gichana</b></sub></a><br /><a href="https://github.com/justinelut/mpesapay/commits?author=justinelut" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/hamisirizwan"><img src="https://avatars.githubusercontent.com/u/72559592?v=4?s=100" width="100px;" alt="Dev_riz"/><br /><sub><b>Dev_riz</b></sub></a><br /><a href="https://github.com/justinelut/mpesapay/commits?author=hamisirizwan" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://verixr.com"><img src="https://avatars.githubusercontent.com/u/51832166?v=4?s=100" width="100px;" alt="Justine Gichana"/><br /><sub><b>Justine Gichana</b></sub></a><br /><a href="https://github.com/justinelut/mpesapay/commits?author=justinelut" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://protas.dreamfeel.me/"><img src="https://avatars.githubusercontent.com/u/53041037?v=4?s=100" width="100px;" alt="Dre"/><br /><sub><b>Dre</b></sub></a><br /><a href="https://github.com/justinelut/mpesapay/commits?author=OdidaProtas" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
119 changes: 74 additions & 45 deletions src/transactions/mpesapay.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
import generateAccessToken from '../utils/accesstoken';
import B2C from './businesstocustomer';
import MpesaExpress from './mpesaexpress';
import checkTransactionStatus from './transactionstatus';
import getAccountBalance from './accountbalance';
import StkPushQuery from './stkpushQuery';
import generateAccessToken from "../utils/accesstoken";
import B2C from "./businesstocustomer";
import MpesaExpress from "./mpesaexpress";
import checkTransactionStatus from "./transactionstatus";
import getAccountBalance from "./accountbalance";
import StkPushQuery from "./stkpushQuery";

type TMpesaPay = {
consumerKey: string;
consumerSecret: string;
businessShortCode: string;
passkey: string;
accountReference: string;
transactionDesc: string;
partyA: string;
b2cSecurityCredential: string;
initiatorName: string;
environment: "sandbox" | "live";
transactionType: "paybill" | "till";
};

type TStkPush = {
amount: string;
phoneNumber: string;
callbackUrl: string;
};

type TTransactionStatus = {
transactionId: string;
callbackUrl: string;
};

type TB2C = {
PaymentAmount: number;
receiversPhonenumber: number;
remarks: string;
callbackUrl: string;
};

class MpesaPay {
private consumerKey: string;
Expand All @@ -18,19 +50,19 @@ class MpesaPay {
private environment: string;
private transactionType: string;

constructor(
consumerKey: string,
consumerSecret: string,
businessShortCode: string,
passkey: string,
accountReference: string,
transactionDesc: string,
partyA: string,
b2cSecurityCredential: string,
initiatorName: string,
environment: 'sandbox' | 'live' = 'sandbox',
transactionType: 'paybill' | 'till' = 'paybill',
) {
constructor({
consumerKey,
consumerSecret,
businessShortCode,
passkey,
accountReference,
transactionDesc,
partyA,
b2cSecurityCredential,
initiatorName,
environment,
transactionType,
}: TMpesaPay) {
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
this.businessShortCode = businessShortCode;
Expand All @@ -40,11 +72,11 @@ class MpesaPay {
this.partyA = partyA;
this.b2cSecurityCredential = b2cSecurityCredential;
this.initiatorName = initiatorName;
this.environment =
environment === 'live'
? 'https://api.safaricom.co.ke'
: 'https://sandbox.safaricom.co.ke';
this.transactionType = transactionType
this.environment =
environment === "live"
? "https://api.safaricom.co.ke"
: "https://sandbox.safaricom.co.ke";
this.transactionType = transactionType;
}

//stkpush method for directly initiating a pop to the specified phonenumber with the amount to payed
Expand All @@ -58,11 +90,11 @@ class MpesaPay {
);
}

public async stkPush(
amount: string,
phoneNumber: string,
callbackUrl: string
): Promise<any> {
public async stkPush({
amount,
phoneNumber,
callbackUrl,
}: TStkPush): Promise<any> {
const accessToken = await this.AccessToken();
return await MpesaExpress(
amount,
Expand All @@ -78,9 +110,7 @@ class MpesaPay {
);
}

public async stkPushQuery(
CheckoutRequestID: string,
): Promise<any> {
public async stkPushQuery(CheckoutRequestID: string): Promise<any> {
const accessToken = await this.AccessToken();
return await StkPushQuery(
this.businessShortCode,
Expand All @@ -91,10 +121,10 @@ class MpesaPay {
);
}

public async transactionStatus(
transactionId: string,
callbackUrl: string
): Promise<any> {
public async transactionStatus({
transactionId,
callbackUrl,
}: TTransactionStatus): Promise<any> {
const accessToken = await this.AccessToken();
return await checkTransactionStatus(
transactionId,
Expand All @@ -107,12 +137,12 @@ class MpesaPay {
);
}

public async business2Customer(
PaymentAmount: number,
receiversPhonenumber: number,
remarks: string,
callbackUrl: string
): Promise<any> {
public async business2Customer({
PaymentAmount,
receiversPhonenumber,
remarks,
callbackUrl,
}: TB2C): Promise<any> {
const accessToken = await this.AccessToken();
return await B2C(
PaymentAmount,
Expand All @@ -139,6 +169,5 @@ class MpesaPay {
}
}


export default MpesaPay
export {MpesaPay}
export default MpesaPay;
export { MpesaPay };

0 comments on commit 4fc0316

Please sign in to comment.