From 6e10c135da61862a935a5405733eba9d0180d0ce Mon Sep 17 00:00:00 2001 From: "ADEBISI Foluso A. (uM)" Date: Mon, 25 Mar 2019 23:16:16 +0100 Subject: [PATCH] * Improve ChargeAuthorization 'Reference' flow * Implements CheckAuthorization/RequestReauthorization for Transations --- .../Apis/Transactions/ChargeAuthorization.cs | 9 +--- .../Apis/Transactions/CheckAuthorization.cs | 39 +++++++++++++++ .../Apis/Transactions/ITransactionsApi.cs | 11 +++-- src/main/Apis/Transactions/Reauthorization.cs | 39 +++++++++++++++ src/main/Apis/Transactions/TransactionsApi.cs | 49 ++++++++++++++++--- 5 files changed, 130 insertions(+), 17 deletions(-) create mode 100644 src/main/Apis/Transactions/CheckAuthorization.cs create mode 100644 src/main/Apis/Transactions/Reauthorization.cs diff --git a/src/main/Apis/Transactions/ChargeAuthorization.cs b/src/main/Apis/Transactions/ChargeAuthorization.cs index 6f68d7a..13df284 100644 --- a/src/main/Apis/Transactions/ChargeAuthorization.cs +++ b/src/main/Apis/Transactions/ChargeAuthorization.cs @@ -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; } @@ -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 diff --git a/src/main/Apis/Transactions/CheckAuthorization.cs b/src/main/Apis/Transactions/CheckAuthorization.cs new file mode 100644 index 0000000..6184965 --- /dev/null +++ b/src/main/Apis/Transactions/CheckAuthorization.cs @@ -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; } + } +} \ No newline at end of file diff --git a/src/main/Apis/Transactions/ITransactionsApi.cs b/src/main/Apis/Transactions/ITransactionsApi.cs index e4112df..04a6364 100644 --- a/src/main/Apis/Transactions/ITransactionsApi.cs +++ b/src/main/Apis/Transactions/ITransactionsApi.cs @@ -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); } } \ No newline at end of file diff --git a/src/main/Apis/Transactions/Reauthorization.cs b/src/main/Apis/Transactions/Reauthorization.cs new file mode 100644 index 0000000..62869e7 --- /dev/null +++ b/src/main/Apis/Transactions/Reauthorization.cs @@ -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; } + } +} \ No newline at end of file diff --git a/src/main/Apis/Transactions/TransactionsApi.cs b/src/main/Apis/Transactions/TransactionsApi.cs index a086b2a..20806c0 100644 --- a/src/main/Apis/Transactions/TransactionsApi.cs +++ b/src/main/Apis/Transactions/TransactionsApi.cs @@ -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("transaction/initialize", request); } @@ -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( + 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( "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( + "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( + "transaction/check_authorization", request + ); } } \ No newline at end of file