98 lines
3.0 KiB
C#
98 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace _0_Framework.Application.PaymentGateway;
|
|
|
|
public class SepehrPaymentGateway:IPaymentGateway
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private const long TerminalId = 99213700;
|
|
|
|
public SepehrPaymentGateway(IHttpClientFactory httpClient)
|
|
{
|
|
_httpClient = httpClient.CreateClient();
|
|
_httpClient.BaseAddress = new Uri("https://sepehr.shaparak.ir/Rest/V1/PeymentApi/");
|
|
}
|
|
|
|
public async Task<PaymentGatewayResponse> Create(CreatePaymentGatewayRequest command, CancellationToken cancellationToken = default)
|
|
{
|
|
command.ExtraData ??= new Dictionary<string, object>();
|
|
command.ExtraData.Add("financialInvoiceId", command.FinancialInvoiceId);
|
|
var extraData = JsonConvert.SerializeObject(command.ExtraData);
|
|
var res = await _httpClient.PostAsJsonAsync("GetToken", new
|
|
{
|
|
TerminalID = TerminalId,
|
|
Amount = command.Amount,
|
|
InvoiceID = command.TransactionId,
|
|
callbackURL = command.CallBackUrl,
|
|
payload = extraData
|
|
}, cancellationToken: cancellationToken);
|
|
// خواندن محتوای پاسخ
|
|
var content = await res.Content.ReadAsStringAsync(cancellationToken);
|
|
|
|
// تبدیل پاسخ JSON به آبجکت داتنت
|
|
var json = System.Text.Json.JsonDocument.Parse(content);
|
|
|
|
// گرفتن مقدار AccessToken
|
|
var accessToken = json.RootElement.GetProperty("Accesstoken").ToString();
|
|
var status = json.RootElement.GetProperty("Status").ToString();
|
|
|
|
return new PaymentGatewayResponse
|
|
{
|
|
Status = status,
|
|
IsSuccess = status == "0",
|
|
Token = accessToken
|
|
};
|
|
}
|
|
|
|
public string GetStartPayUrl(string token)=>
|
|
$"https://sepehr.shaparak.ir/Payment/Pay?token={token}&terminalId={TerminalId}";
|
|
|
|
public async Task<PaymentGatewayResponse> Verify(VerifyPaymentGateWayRequest command, CancellationToken cancellationToken = default)
|
|
{
|
|
var res = await _httpClient.PostAsJsonAsync("Advice", new
|
|
{
|
|
digitalreceipt = command.DigitalReceipt,
|
|
Tid = TerminalId,
|
|
}, cancellationToken: cancellationToken);
|
|
|
|
// خواندن محتوای پاسخ
|
|
var content = await res.Content.ReadAsStringAsync(cancellationToken);
|
|
|
|
// تبدیل پاسخ JSON به آبجکت داتنت
|
|
var json = System.Text.Json.JsonDocument.Parse(content);
|
|
|
|
|
|
var message = json.RootElement.GetProperty("Message").GetString();
|
|
var status = json.RootElement.GetProperty("Status").GetString();
|
|
return new PaymentGatewayResponse
|
|
{
|
|
Status = status,
|
|
IsSuccess = status.ToLower() == "ok",
|
|
Message = message
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
public Task<PaymentGatewayResponse> CreateSandBox(CreatePaymentGatewayRequest command, CancellationToken cancellationToken = default)
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
|
|
public string GetStartPaySandBoxUrl(string transactionId)
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
|
|
public Task<WalletAmountResponse> GetWalletAmount(CancellationToken cancellationToken)
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
} |