Skip to content

Commit

Permalink
Initiate STK Push and querying status
Browse files Browse the repository at this point in the history
  • Loading branch information
Jcardif committed Jan 21, 2023
1 parent 184edd8 commit 5211280
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
11 changes: 7 additions & 4 deletions Daraja.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ static async Task Main(string[] args)
var grantType = config["GrantType"];
var passKey = config["PassKey"];

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

var darajaClient = await gateway.GetDarajaClientAsync();
var darajaClient = await gateway.GetDarajaClientAsync();

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

private static async Task MakeStkPush(DarajaGateway darajaGateway, DarajaClient darajaClient)
Expand Down
2 changes: 1 addition & 1 deletion MpesaDaraja/Interfaces/IDarajaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal interface IDarajaClient
/// <param name="pushResponse">Response from initiating the STK Push</param>
/// <param name="stkData">The <see cref="StkData"/> object used to initiate the stk push</param>
/// <returns></returns>
Task<(bool isCompleted, PushQueryResponse response)> QueryStkPushStatus(PushResponse pushResponse,
Task<(bool isCompleted, PushQueryResponse? pushQueryResponse)> QueryStkPushStatus(PushResponse pushResponse,
StkData stkData);

}
Expand Down
21 changes: 21 additions & 0 deletions MpesaDaraja/Models/PushQueryResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,42 @@ namespace MpesaDaraja.Models
/// </summary>
public class PushQueryResponse
{
/// <summary>
/// This is a Numeric status code that indicates the status of the transaction submission. 0 means successful submission and any other code means an error occurred.
/// </summary>
[JsonProperty("ResponseCode")]
public string? ResponseCode { get; set; }

/// <summary>
/// Response description is an acknowledgment message from the API that gives the status of the request submission usually maps to a specific ResponseCode value.
/// It can be a Success submission message or an error description
/// </summary>
[JsonProperty("ResponseDescription")]
public string? ResponseDescription { get; set; }

/// <summary>
/// This is a global unique Identifier for any submitted payment request.
/// </summary>
[JsonProperty("MerchantRequestID")]
public string? MerchantRequestId { get; set; }

/// <summary>
/// This is a global unique identifier of the processed checkout transaction request.
/// </summary>
[JsonProperty("CheckoutRequestID")]
public string? CheckoutRequestId { get; set; }

/// <summary>
/// This is a numeric status code that indicates the status of the transaction processing.
/// 0 means successful processing and any other code means an error occurred or the transaction failed.
/// </summary>
[JsonProperty("ResultCode")]
public string? ResultCode { get; set; }

/// <summary>
/// Result description is a message from the API that gives the status of the request processing, usually maps to a specific ResultCode value.
/// It can be a Success processing message or an error description message.
/// </summary>
[JsonProperty("ResultDesc")]
public string? ResultDesc { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion MpesaDaraja/Models/PushResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace MpesaDaraja.Models
/// <summary>
/// Result after initiating an stk push
/// </summary>
public class PushResult
public class PushResponse
{
/// <summary>
/// This is a global unique Identifier for any submitted payment request.
Expand Down
6 changes: 6 additions & 0 deletions MpesaDaraja/Models/StkData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace MpesaDaraja.Models
{
/// <summary>
/// Defines the class for initiating online payment on behalf of a customer.
/// </summary>
public class StkData
{
/// <summary>
Expand Down Expand Up @@ -77,6 +80,9 @@ public class StkData
[JsonProperty("TransactionDesc")]
public string? TransactionDesc { get; set; }

/// <summary>
/// Creates an instance of the <see cref="StkData"/> class
/// </summary>
public StkData()
{
Timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
Expand Down

0 comments on commit 5211280

Please sign in to comment.