Skip to content

Commit

Permalink
* Improve ChargeAuthorization 'Reference' flow
Browse files Browse the repository at this point in the history
* Implements CheckAuthorization/RequestReauthorization for Transations
  • Loading branch information
adebisi-fa committed Mar 25, 2019
1 parent fb06aaa commit 6e10c13
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 17 deletions.
9 changes: 1 addition & 8 deletions src/main/Apis/Transactions/ChargeAuthorization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ChargeAuthorizationRequest : RequestMetadataExtender
public string Currency { get; set; }

[JsonProperty("amount")]
public string AmountInKobo { get; set; }
public int AmountInKobo { get; set; }

public string Email { get; set; }

Expand All @@ -23,13 +23,6 @@ public class ChargeAuthorizationRequest : RequestMetadataExtender
public int TransactionCharge { get; set; }

public string Bearer { get; set; }

public override void Prepare()
{
base.Prepare();
Reference =
$"{Reference};{Guid.NewGuid().ToString().Substring(0, 8)}";
}
}

public class ChargeAuthorizationResponse : HasRawResponse
Expand Down
39 changes: 39 additions & 0 deletions src/main/Apis/Transactions/CheckAuthorization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using Newtonsoft.Json;

namespace PayStack.Net
{
public class CheckAuthorizationRequest
{

[JsonProperty("authorization_code")]
public string AuthorizationCode { get; set; }

public string Currency { get; set; }

[JsonProperty("amount")]
public int AmountInKobo { get; set; }

public string Email { get; set; }

}

public class CheckAuthorizationResponse : HasRawResponse
{
[JsonProperty("status")]
public bool Status { get; set; }

[JsonProperty("message")]
public string Message { get; set; }

[JsonProperty("data")]
public CheckAuthorizationData Data { get; set; }
}

public class CheckAuthorizationData
{
[JsonProperty("amount")]
public string AmountInKobo { get; set; }
public string Currency { get; set; }
}
}
11 changes: 8 additions & 3 deletions src/main/Apis/Transactions/ITransactionsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ namespace PayStack.Net
{
public interface ITransactionsApi
{
TransactionInitializeResponse Initialize(string email, int amount, string reference = null, bool makeReferenceUnique = false);
TransactionInitializeResponse Initialize(string email, int amountInKobo, string reference = null, bool makeReferenceUnique = false);
TransactionInitializeResponse Initialize(TransactionInitializeRequest request, bool makeReferenceUnique = false);
TransactionVerifyResponse Verify(string reference);
TransactionListResponse List(TransactionListRequest request = null);
TransactionFetchResponse Fetch(string transactionId);
TransactionTimelineResponse Timeline(string transactionIdOrReference);
TransactionTotalsResponse Totals(DateTime? from = null, DateTime? to = null);
ChargeAuthorizationResponse ChargeAuthorization(string authorizationCode, string email, string amount);
ChargeAuthorizationResponse ChargeAuthorization(ChargeAuthorizationRequest request);
ChargeAuthorizationResponse ChargeAuthorization(string authorizationCode, string email, int amountInKobo, string reference = null, bool makeReferenceUnique = false);
ChargeAuthorizationResponse ChargeAuthorization(ChargeAuthorizationRequest request, bool makeReferenceUnique = false);
TransactionExportResponse Export(DateTime? from = null, DateTime? to = null,
bool settled = false, string paymentPage = null);
ReAuthorizationResponse RequestReAuthorization(string authorizationCode, string email, int amountInKobo, string reference = null, bool makeReferenceUnique = false);
ReAuthorizationResponse RequestReAuthorization(ReAuthorizationRequest request, bool makeReferenceUnique = false);

CheckAuthorizationResponse CheckAuthorization(string authorizationCode, string email, int amountInKobo);
CheckAuthorizationResponse CheckAuthorization(CheckAuthorizationRequest request);
}
}
39 changes: 39 additions & 0 deletions src/main/Apis/Transactions/Reauthorization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using Newtonsoft.Json;

namespace PayStack.Net
{
public class ReAuthorizationRequest : RequestMetadataExtender
{
public string Reference { get; set; }

[JsonProperty("authorization_code")]
public string AuthorizationCode { get; set; }

public string Currency { get; set; }

[JsonProperty("amount")]
public int AmountInKobo { get; set; }

public string Email { get; set; }
}

public class ReAuthorizationResponse : HasRawResponse
{
[JsonProperty("status")]
public bool Status { get; set; }

[JsonProperty("message")]
public string Message { get; set; }

[JsonProperty("data")]
public ReAuthorizationData Data { get; set; }
}

public class ReAuthorizationData {
[JsonProperty("reauthorization_url")]
public string ReAuthorizationUrl { get; set; }

public string Reference { get; set; }
}
}
49 changes: 43 additions & 6 deletions src/main/Apis/Transactions/TransactionsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public TransactionInitializeResponse Initialize(string email, int amount, string

public TransactionInitializeResponse Initialize(TransactionInitializeRequest request, bool makeReferenceUnique = false)
{
if (makeReferenceUnique)
if (makeReferenceUnique && request.Reference != null)
request.Reference = $"{request.Reference}-{Guid.NewGuid().ToString().Substring(0, 8)}";
return _api.Post<TransactionInitializeResponse, TransactionInitializeRequest>("transaction/initialize", request);
}
Expand Down Expand Up @@ -46,16 +46,53 @@ public TransactionExportResponse Export(DateTime? from = null, DateTime? to = nu
new TransactionExportRequest { From = from, To = to, Settled = settled, Payment_Page = paymentPage }
);

public ChargeAuthorizationResponse ChargeAuthorization(string authorizationCode, string email, string amount) =>
ChargeAuthorization(new ChargeAuthorizationRequest{
public ChargeAuthorizationResponse ChargeAuthorization(string authorizationCode, string email, int amountInKobo, string reference = null, bool makeReferenceUnique = false) =>
ChargeAuthorization(new ChargeAuthorizationRequest
{
Reference = reference,
AuthorizationCode = authorizationCode,
Email = email,
AmountInKobo = amount
AmountInKobo = amountInKobo
});

public ChargeAuthorizationResponse ChargeAuthorization(ChargeAuthorizationRequest request) =>
_api.Post<ChargeAuthorizationResponse, ChargeAuthorizationRequest>(
public ChargeAuthorizationResponse ChargeAuthorization(ChargeAuthorizationRequest request, bool makeReferenceUnique = false)
{
if (makeReferenceUnique && request.Reference != null)
request.Reference = $"{request.Reference}-{Guid.NewGuid().ToString().Substring(0, 8)}";
return _api.Post<ChargeAuthorizationResponse, ChargeAuthorizationRequest>(
"transaction/charge_authorization", request
);
}

public ReAuthorizationResponse RequestReAuthorization(string authorizationCode, string email, int amountInKobo, string reference = null, bool makeReferenceUnique = false) =>
RequestReAuthorization(new ReAuthorizationRequest
{
AuthorizationCode = authorizationCode,
Email = email,
AmountInKobo = amountInKobo,
Reference = reference
});

public ReAuthorizationResponse RequestReAuthorization(ReAuthorizationRequest request, bool makeReferenceUnique = false)
{
if (makeReferenceUnique && request.Reference != null)
request.Reference = $"{request.Reference}-{Guid.NewGuid().ToString().Substring(0, 8)}";
return _api.Post<ReAuthorizationResponse, ReAuthorizationRequest>(
"transaction/request_reauthorization", request
);
}

public CheckAuthorizationResponse CheckAuthorization(string authorizationCode, string email, int amountInKobo) =>
CheckAuthorization(new CheckAuthorizationRequest
{
AuthorizationCode = authorizationCode,
Email = email,
AmountInKobo = amountInKobo
});

public CheckAuthorizationResponse CheckAuthorization(CheckAuthorizationRequest request) =>
_api.Post<CheckAuthorizationResponse, CheckAuthorizationRequest>(
"transaction/check_authorization", request
);
}
}

0 comments on commit 6e10c13

Please sign in to comment.