|
@@ -0,0 +1,68 @@
|
|
|
+using System.Net.Http;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using Microsoft.Extensions.Configuration;
|
|
|
+using MTWorkHR.Application.Services.Interfaces;
|
|
|
+using Newtonsoft.Json;
|
|
|
+
|
|
|
+public class AmazonPayService: IAmazonPayService
|
|
|
+{
|
|
|
+ private readonly HttpClient _httpClient;
|
|
|
+ private readonly string _merchantId;
|
|
|
+ private readonly string _accessKey;
|
|
|
+ private readonly string _secretKey;
|
|
|
+ private readonly string _region;
|
|
|
+ private readonly bool _sandbox;
|
|
|
+
|
|
|
+ public AmazonPayService(IConfiguration configuration, HttpClient httpClient)
|
|
|
+ {
|
|
|
+ _httpClient = httpClient;
|
|
|
+ _merchantId = configuration["AmazonPay:MerchantId"];
|
|
|
+ _accessKey = configuration["AmazonPay:AccessKey"];
|
|
|
+ _secretKey = configuration["AmazonPay:SecretKey"];
|
|
|
+ _region = configuration["AmazonPay:Region"];
|
|
|
+ _sandbox = configuration.GetValue<bool>("AmazonPay:Sandbox");
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<string> CreateCheckoutSessionAsync(decimal amount, string currencyCode)
|
|
|
+ {
|
|
|
+ var baseUrl = _sandbox ? "https://pay-api.amazon.com/sandbox" : "https://pay-api.amazon.com";
|
|
|
+ var endpoint = $"{baseUrl}/{_region}/checkout-sessions";
|
|
|
+
|
|
|
+ var payload = new
|
|
|
+ {
|
|
|
+ checkoutReviewReturnUrl = "https://yourwebsite.com/review",
|
|
|
+ checkoutResultReturnUrl = "https://yourwebsite.com/result",
|
|
|
+ chargeAmount = new
|
|
|
+ {
|
|
|
+ amount = amount.ToString("F2"),
|
|
|
+ currencyCode = currencyCode
|
|
|
+ },
|
|
|
+ merchantMetadata = new
|
|
|
+ {
|
|
|
+ merchantReferenceId = Guid.NewGuid().ToString(),
|
|
|
+ merchantStoreName = "Your Store Name"
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ var content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json");
|
|
|
+
|
|
|
+ // Add required headers
|
|
|
+ _httpClient.DefaultRequestHeaders.Clear();
|
|
|
+ _httpClient.DefaultRequestHeaders.Add("x-amz-pay-idempotency-key", Guid.NewGuid().ToString());
|
|
|
+ _httpClient.DefaultRequestHeaders.Add("x-amz-pay-authtoken", GenerateAuthToken());
|
|
|
+
|
|
|
+ var response = await _httpClient.PostAsync(endpoint, content);
|
|
|
+ response.EnsureSuccessStatusCode();
|
|
|
+
|
|
|
+ var responseBody = await response.Content.ReadAsStringAsync();
|
|
|
+ return responseBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ private string GenerateAuthToken()
|
|
|
+ {
|
|
|
+ // Implement Amazon Pay's signature calculation logic here.
|
|
|
+ // Refer to Amazon Pay's documentation for details: https://developer.amazon.com/docs/amazon-pay-api/signature.html
|
|
|
+ return "YOUR_AUTH_TOKEN";
|
|
|
+ }
|
|
|
+}
|