Skip to content

Commit

Permalink
Merge pull request #16 from Jcardif/feature-stk
Browse files Browse the repository at this point in the history
fix bug when fetching daraja client
  • Loading branch information
Jcardif authored Jan 23, 2023
2 parents 8587545 + c5adcae commit d4228d3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 39 deletions.
5 changes: 4 additions & 1 deletion Daraja.App/Daraja.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="MpesaDarajaSDK" Version="1.0.14" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MpesaDaraja\MpesaDaraja.csproj" />
</ItemGroup>

</Project>
33 changes: 24 additions & 9 deletions Daraja.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ static async Task Main(string[] args)
var grantType = config["GrantType"];
var passKey = config["PassKey"];

if (endpoint != null && consumerKey != null && consumerSecret != null && passKey != null)
{
var gateway = new DarajaGateway(endpoint, consumerKey, consumerSecret, passKey);
if (endpoint == null || consumerKey == null || consumerSecret == null || passKey == null)
return;

var darajaClient = await gateway.GetDarajaClientAsync();
var gateway = new DarajaGateway(consumerKey, consumerSecret, passKey, false);

var darajaClient = await gateway.GetDarajaClientAsync(false);

if (darajaClient == null)
return;

await MakeStkPush(gateway, darajaClient);

if (darajaClient != null)
await MakeStkPush(gateway, darajaClient);
}
}

private static async Task MakeStkPush(DarajaGateway darajaGateway, DarajaClient darajaClient)
Expand All @@ -54,9 +57,21 @@ private static async Task MakeStkPush(DarajaGateway darajaGateway, DarajaClient

stkData.Password = darajaGateway.GetStkPushPassword(stkData.BusinessShortCode, stkData.Timestamp);

var result = await darajaClient.SendStkPushAsync(stkData);
var pushResponse = await darajaClient.SendStkPushAsync(stkData);

Console.WriteLine(JsonConvert.SerializeObject(pushResponse, Formatting.Indented));
if(pushResponse is null ) { return;}

var isCompleted = false;
PushQueryResponse? pushQueryResponse = new PushQueryResponse();

while (!isCompleted)
{
(isCompleted, pushQueryResponse) = await darajaClient.QueryStkPushStatus(pushResponse, stkData);
Console.WriteLine("The transaction is being processed");
}

Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
Console.WriteLine(JsonConvert.SerializeObject(pushQueryResponse, Formatting.Indented));
}
}
}
4 changes: 2 additions & 2 deletions MpesaDaraja/Services/DarajaGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class DarajaGateway : IDarajaGateway
public DarajaGateway( string consumerKey, string consumerSecret, string passKey, bool inProduction, string grantType= "client_credentials")
{
EndPoint = inProduction
? "https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
: "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest";
? $"https://api.safaricom.co.ke/oauth/v1/generate"
: $"https://sandbox.safaricom.co.ke/oauth/v1/generate";
GrantType=grantType;
ConsumerKey=consumerKey;
ConsumerSecret=consumerSecret;
Expand Down
61 changes: 34 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,46 @@ using MpesaDaraja.Services;
The package need to be configured with your [Daraja App Credentials](https://developer.safaricom.co.ke/MyApps) (Consumer Secret, consumerKey and your pass key)

```C#
var gateway = new DarajaGateway(endpoint, consumerKey consumerSecret, passKey);
var gateway = new DarajaGateway(consumerKey, consumerSecret, passKey, false);

var darajaClient = await gateway.GetDarajaClientAsync();
var darajaClient = await gateway.GetDarajaClientAsync(false);
```
The ```DarajaGateway``` authenticates with the daraja api to get you a timebound access token which is used to create the ```Darajalient``` which you use to accesss the various [Daraja APIs](https://developer.safaricom.co.ke/APIs)

# Make an STK Push (M-Pesa Express)
To make an online payment on behalf of the customer:
- create an ```STKData``` object
```C#
var stkData = new StkData
{
BusinessShortCode = 174379,
Timestamp = "20230116043457",
TransactionType = "CustomerPayBillOnline",
Amount = 1,
PartyA = receiver,
PartyB = 174379,
PhoneNumber = receiver,
CallBackUrl = new Uri("https://mydomain.com/path"),
AccountReference = "CompanyXLTD",
TransactionDesc = "Payment of X"
};
```
- Get the password that is used= for encrypting the request sent

```C#
stkData.Password = darajaGateway.GetStkPushPassword(stkData.BusinessShortCode, stkData.Timestamp);
```
- Make the STK Push
```C#
var result = await darajaClient.SendStkPushAsync(stkData);
```

Create an ```STKData``` object

```C#
var stkData = new StkData
{
BusinessShortCode = 174379,
Timestamp = "20230116043457",
TransactionType = "CustomerPayBillOnline",
Amount = 1,
PartyA = receiver,
PartyB = 174379,
PhoneNumber = receiver,
CallBackUrl = new Uri("https://mydomain.com/path"),
AccountReference = "CompanyXLTD",
TransactionDesc = "Payment of X"
};
```
Get the password that is used= for encrypting the request sent

```C#
stkData.Password = darajaGateway.GetStkPushPassword(stkData.BusinessShortCode, stkData.Timestamp);
```
Make the STK Push
```C#
var result = await darajaClient.SendStkPushAsync(stkData);
```
Query the stus of the stk push
```C#
var (isCompleted, pushQueryResponse) = await darajaClient.QueryStkPushStatus(pushResponse, stkData);
```


# Find this repository useful? :heart:
Support it by joining [stargazers](https://github.com/Jcardif/MpesaDaraja/stargazers) for this repository.
Expand Down

0 comments on commit d4228d3

Please sign in to comment.