· 5 years ago · Aug 05, 2020, 09:48 AM
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Net.Http;
5using System.Net.Http.Headers;
6using System.Net.WebSockets;
7using System.Text;
8using System.Threading.Tasks;
9using Microsoft.Extensions.Configuration;
10using Newtonsoft.Json;
11using TopRateTransfer.API.HTTP_ResonseType;
12using TopRateTransfer.API.Interfaces;
13using TopRateTransfer.API.Models;
14using TopRateTransfer.API.Response;
15
16namespace TopRateTransfer.API.Infrastructures
17{
18 public class PaymentService : IPaymentService
19 {
20 private readonly IConfiguration config;
21 private readonly HttpClient apiClient;
22
23 public PaymentService(IConfiguration _config)
24 {
25 this.config = _config;
26 var secretKey = config.GetSection("PaystackSettings:liveSecretKey").Value;
27 apiClient = new HttpClient();
28 apiClient.DefaultRequestHeaders.Accept.Clear();
29 apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
30 //apiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer " + secretKey);
31 apiClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Bearer " + secretKey);
32
33 }
34
35 // public async Task<bool> ReceivePaymentFromCustomer(CustomerTransactions transaction)
36 // {
37 // bool transactionStatus = false;
38 // string urlToReceiveMoney = "https://poliapi.apac.paywithpoli.com/api/v2/Transaction/Initiate";
39 // var requestData = new
40 // {
41 // Amount = transaction.totalAmountCharged,
42 // CurrencyCode = transaction.currencyToSend,
43 // SuccessURL = "http://topratetransfer.23esm.com/Success"
44 // };
45 //
46 // var json = JsonConvert.SerializeObject(requestData);
47 // var data = new StringContent(json, Encoding.UTF8, "application/json");
48 //
49 // var responseMsg = apiClient.PostAsync(urlToReceiveMoney, data).Result;
50 //
51 // var responseInString = responseMsg.Content.ReadAsStringAsync().Result;
52 // var responseResult = JsonConvert.DeserializeObject<PoliChargeMoneyResponse>(responseInString);
53 //
54 // if (responseResult.Success)
55 // {
56 // transactionStatus = true;
57 // }
58 //
59 // return transactionStatus;
60 //
61 // }
62
63 public async Task<bool> MakePayment(string email, CustomerTransactions transaction)
64 {
65 bool transactionStatus = false;
66 try
67 {
68
69 // Please Use Live Key in Production
70 // Create transfer Recipient
71 string url_transfer_recipient = "https://api.paystack.co/transferrecipient";
72
73 var requestData = new
74 {
75 name = transaction.receiversAccountName,
76 account_number = transaction.receiversAccountNumber,
77 description = "Transfer Payment for " + transaction.amountToSend,
78 bank_code = transaction.receiversBankCode,
79 currency = "NGN",
80 type = "nuban"
81 };
82
83 var responseMessage = apiClient.PostAsJsonAsync(url_transfer_recipient, requestData).Result;
84 var responseStr = responseMessage.Content.ReadAsStringAsync().Result;
85 var response = JsonConvert.DeserializeObject<TransferRecipientResponse>(responseStr, new JsonSerializerSettings() { Culture = CultureInfo.InvariantCulture });
86
87 if (response != null)
88 {
89 string url_initiate_transfer = "https://api.paystack.co/transfer";
90
91 int amountInKobo = (int)transaction.amountToReceive * 100;
92 string reasonForTranfer = "Payout For the Transfer Transaction";
93
94 var transferData = new
95 {
96 source = "balance",
97 reason = reasonForTranfer,
98 amount = amountInKobo,
99 currency = transaction.currencyToReceive,
100 recipient = response.Data.RecipientCode,
101 reference = transaction.referenceNumber
102 };
103
104 var resp = apiClient.PostAsJsonAsync(url_initiate_transfer, transferData).Result;
105 var responseInStr = resp.Content.ReadAsStringAsync().Result;
106 var result = JsonConvert.DeserializeObject<TransferRecipientResponse>(responseInStr, new JsonSerializerSettings() { Culture = CultureInfo.InvariantCulture });
107
108 if (result.Status)
109 {
110 transactionStatus = true;
111 }
112 else
113 {
114 transactionStatus = false;
115 }
116 return transactionStatus;
117 }
118 }
119 catch(Exception e)
120 {
121 Console.WriteLine(e.Message);
122 return transactionStatus;
123
124 }
125
126 return transactionStatus;
127 }
128
129
130
131 public async Task<MyBank> getAllBanks(string countryName = "")
132 {
133 string url = "https://api.paystack.co/bank?country=" + countryName;
134 HttpResponseMessage result = await apiClient.GetAsync(url);
135 result.EnsureSuccessStatusCode();
136 var resStr = await result.Content.ReadAsStringAsync();
137 MyBank allBanks = JsonConvert.DeserializeObject<MyBank>(resStr);
138 return allBanks;
139 }
140
141 public async Task<List<Country>> getAllCountries()
142 {
143 throw new System.NotImplementedException();
144 }
145
146 public async Task<List<Transactionlist>> GetAllTransactions()
147 {
148 string getlink = "https://api.paystack.co/transferrecipient";
149 var responseMessage = apiClient.GetAsync(getlink).Result;
150 var responseStr = responseMessage.Content.ReadAsStringAsync().Result;
151 var response = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Transactionlist>>(responseStr, new JsonSerializerSettings() { Culture = CultureInfo.InvariantCulture });
152 if (response != null)
153 {
154 return response;
155 }
156 else
157 {
158 return null;
159 }
160 }
161 }
162}