Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asan Pardakht Rest #309

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Parbad.sln
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Zibal", "Zibal", "{3506EB66
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Parbad.Gateway.Zibal", "src\Parbad.Gateway\Zibal\src\Parbad.Gateway.Zibal.csproj", "{EF684245-CD65-46F8-84E3-4BF1D43E3146}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication1", "WebApplication1\WebApplication1.csproj", "{D3E03864-3D45-40FB-B530-8B7DA40A1B86}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\Parbad.Shared\Parbad.Shared.projitems*{2e124fc2-6fa6-45e1-9912-edca1c19cef7}*SharedItemsImports = 13
Expand Down Expand Up @@ -196,6 +198,10 @@ Global
{EF684245-CD65-46F8-84E3-4BF1D43E3146}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF684245-CD65-46F8-84E3-4BF1D43E3146}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF684245-CD65-46F8-84E3-4BF1D43E3146}.Release|Any CPU.Build.0 = Release|Any CPU
{D3E03864-3D45-40FB-B530-8B7DA40A1B86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D3E03864-3D45-40FB-B530-8B7DA40A1B86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3E03864-3D45-40FB-B530-8B7DA40A1B86}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D3E03864-3D45-40FB-B530-8B7DA40A1B86}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -241,6 +247,7 @@ Global
{D1F5CF0A-BD88-4B12-B58D-E286690D147C} = {05CE4791-3EB8-4DA6-A0C3-4379878BBDAA}
{3506EB66-92DD-4E0A-B0C1-B943FD4071AA} = {B1447D20-66E0-46FB-B0DB-BCBEBA7B337A}
{EF684245-CD65-46F8-84E3-4BF1D43E3146} = {3506EB66-92DD-4E0A-B0C1-B943FD4071AA}
{D3E03864-3D45-40FB-B530-8B7DA40A1B86} = {6BB6AFB4-7951-4435-8C10-95AF86B200F8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CBDDB699-C14A-4235-B0AE-32725230B4ED}
Expand Down
2 changes: 1 addition & 1 deletion src/Parbad.Mvc/src/Parbad.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageId>Parbad.Mvc5</PackageId>
<Product>Parbad.Mvc5</Product>
<VersionPrefix>2.2.0</VersionPrefix>
<TargetFramework>net462</TargetFramework>
<TargetFramework>net472</TargetFramework>
<LangVersion>9</LangVersion>
<Authors>Sina Soltani</Authors>
<Description>ASP.NET MVC 4 and 5 integration for Parbad project.
Expand Down
2 changes: 1 addition & 1 deletion src/Parbad.Owin/src/Parbad.Owin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<PackageId>Parbad.Owin</PackageId>
<Product>Parbad.Owin</Product>
<TargetFramework>net462</TargetFramework>
<TargetFramework>net472</TargetFramework>
<VersionPrefix>1.2.0</VersionPrefix>
<LangVersion>9</LangVersion>
<Authors>Sina Soltani</Authors>
Expand Down
196 changes: 145 additions & 51 deletions src/Parbad/src/Gateway/AsanPardakht/AsanPardakhtGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class AsanPardakhtGateway : GatewayBase<AsanPardakhtGatewayAccount>
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly HttpClient _httpClient;
private readonly IAsanPardakhtCrypto _crypto;
private readonly AsanPardakhtGatewayOptions _gatewayOptions;
private readonly MessagesOptions _messageOptions;

Expand All @@ -31,13 +30,11 @@ public AsanPardakhtGateway(
IHttpContextAccessor httpContextAccessor,
IHttpClientFactory httpClientFactory,
IGatewayAccountProvider<AsanPardakhtGatewayAccount> accountProvider,
IAsanPardakhtCrypto crypto,
IOptions<AsanPardakhtGatewayOptions> gatewayOptions,
IOptions<MessagesOptions> messageOptions) : base(accountProvider)
{
_httpContextAccessor = httpContextAccessor;
_httpClient = httpClientFactory.CreateClient(this);
_crypto = crypto;
_gatewayOptions = gatewayOptions.Value;
_messageOptions = messageOptions.Value;
}
Expand All @@ -49,21 +46,14 @@ public override async Task<IPaymentRequestResult> RequestAsync(Invoice invoice,

var account = await GetAccountAsync(invoice).ConfigureAwaitFalse();

var data = await AsanPardakhtHelper.CreateRequestData(invoice, account, _crypto)
.ConfigureAwaitFalse();

var responseMessage = await _httpClient
.PostXmlAsync(_gatewayOptions.ApiUrl, data, cancellationToken)
.ConfigureAwaitFalse();

var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwaitFalse();

return AsanPardakhtHelper.CreateRequestResult(
response,
return await AsanPardakhtHelper.CreateRequestResult(
_httpClient,
invoice,
account,
_httpContextAccessor.HttpContext,
_gatewayOptions,
_messageOptions);
_messageOptions,
cancellationToken);
}

/// <inheritdoc />
Expand All @@ -73,19 +63,19 @@ public override async Task<IPaymentFetchResult> FetchAsync(InvoiceContext contex

var account = await GetAccountAsync(context.Payment).ConfigureAwaitFalse();

var callbackResult = await AsanPardakhtHelper.CreateCallbackResult(
context,
account,
_httpContextAccessor.HttpContext.Request,
_crypto,
_messageOptions).ConfigureAwaitFalse();
_httpClient.Timeout = TimeSpan.FromSeconds(20);

var result = await AsanPardakhtHelper.GetTransResult(context, _httpClient, account, _gatewayOptions, _messageOptions,
cancellationToken);

if (callbackResult.IsSucceed)
var fetchResult = new PaymentFetchResult
{
return PaymentFetchResult.ReadyForVerifying();
}
Status = result.IsSucceed ? PaymentFetchResultStatus.ReadyForVerifying : PaymentFetchResultStatus.Failed,
Message = result.FailedMessage
};
fetchResult.SetAsanPardakhtOriginalPaymentResult(result.TransModel);

return PaymentFetchResult.Failed(callbackResult.Message);
return fetchResult;
}

/// <inheritdoc />
Expand All @@ -95,50 +85,154 @@ public override async Task<IPaymentVerifyResult> VerifyAsync(InvoiceContext cont

var account = await GetAccountAsync(context.Payment).ConfigureAwaitFalse();

var callbackResult = await AsanPardakhtHelper.CreateCallbackResult(
var transResult = await AsanPardakhtHelper.GetTransResult(
context,
_httpClient,
account,
_httpContextAccessor.HttpContext.Request,
_crypto,
_messageOptions);

if (!callbackResult.IsSucceed)
{
return PaymentVerifyResult.Failed(callbackResult.Message);
}
_gatewayOptions,
_messageOptions,
cancellationToken).ConfigureAwaitFalse();

var data = await AsanPardakhtHelper.CreateVerifyData(callbackResult, account, _crypto)
.ConfigureAwaitFalse();
var paymentVerifyResult = new PaymentVerifyResult();
paymentVerifyResult.SetAsanPardakhtOriginalPaymentResult(transResult.TransModel);

var responseMessage = await _httpClient
.PostXmlAsync(_gatewayOptions.ApiUrl, data, cancellationToken)
.ConfigureAwaitFalse();
if (!transResult.IsSucceed)
{
paymentVerifyResult.Status = PaymentVerifyResultStatus.Failed;
paymentVerifyResult.Message = transResult.FailedMessage;

var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwaitFalse();
return paymentVerifyResult;
}

var verifyResult = AsanPardakhtHelper.CheckVerifyResult(response, callbackResult, _messageOptions);
var verifyResult = await AsanPardakhtHelper.CompletionMethod(
_httpClient,
_gatewayOptions.VerifyUrl,
transResult.TransModel.PayGateTranID,
account,
_gatewayOptions,
_messageOptions,
cancellationToken);

if (!verifyResult.IsSucceed)
{
return verifyResult.Result;
paymentVerifyResult.Status = PaymentVerifyResultStatus.Failed;
paymentVerifyResult.Message = verifyResult.FailedMessage;

return paymentVerifyResult;
}

data = await AsanPardakhtHelper.CreateSettleData(callbackResult, account, _crypto)
.ConfigureAwaitFalse();
var settleResult = await AsanPardakhtHelper.CompletionMethod(
_httpClient,
_gatewayOptions.SettlementUrl,
transResult.TransModel.PayGateTranID,
account,
_gatewayOptions,
_messageOptions,
cancellationToken);

responseMessage = await _httpClient
.PostXmlAsync(_gatewayOptions.ApiUrl, data, cancellationToken)
.ConfigureAwaitFalse();
if (!settleResult.IsSucceed)
{
paymentVerifyResult.Status = PaymentVerifyResultStatus.Failed;
paymentVerifyResult.Message = settleResult.FailedMessage;

return paymentVerifyResult;
}

response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwaitFalse();
paymentVerifyResult.Status = PaymentVerifyResultStatus.Succeed;
paymentVerifyResult.Message = _messageOptions.PaymentSucceed;
paymentVerifyResult.TransactionCode = transResult.TransModel.Rrn;

return AsanPardakhtHelper.CreateSettleResult(response, callbackResult, _messageOptions);
return paymentVerifyResult;
}

/// <inheritdoc />
public override Task<IPaymentRefundResult> RefundAsync(InvoiceContext context, Money amount, CancellationToken cancellationToken = default)
public override async Task<IPaymentRefundResult> RefundAsync(InvoiceContext context, Money amount, CancellationToken cancellationToken = default)
{
return PaymentRefundResult.Failed("RefundNotSupports").ToInterfaceAsync();
if (context == null) throw new ArgumentNullException(nameof(context));

var account = await GetAccountAsync(context.Payment).ConfigureAwaitFalse();

var transResult = await AsanPardakhtHelper.GetTransResult(
context,
_httpClient,
account,
_gatewayOptions,
_messageOptions,
cancellationToken).ConfigureAwaitFalse();

if (!transResult.IsSucceed)
{
return PaymentRefundResult.Failed(transResult.FailedMessage);
}

var refundResult = await AsanPardakhtHelper.CompletionMethod(
_httpClient,
_gatewayOptions.CancelUrl,
transResult.TransModel.PayGateTranID,
account,
_gatewayOptions,
_messageOptions,
cancellationToken);

if (!refundResult.IsSucceed)
{
return PaymentRefundResult.Failed(refundResult.FailedMessage);
}

return PaymentRefundResult.Succeed();
}

///// <inheritdoc />
//public override async Task<IPaymentVerifyResult> VerifyAsync(InvoiceContext context, CancellationToken cancellationToken = default)
//{
// if (context == null) throw new ArgumentNullException(nameof(context));

// var account = await GetAccountAsync(context.Payment).ConfigureAwaitFalse();

// var callbackResult = await AsanPardakhtHelper.CreateCallbackResult(
// context,
// account,
// _httpContextAccessor.HttpContext.Request,
// _crypto,
// _messageOptions);

// if (!callbackResult.IsSucceed)
// {
// return PaymentVerifyResult.Failed(callbackResult.Message);
// }

// var data = await AsanPardakhtHelper.CreateVerifyData(callbackResult, account, _crypto)
// .ConfigureAwaitFalse();

// var responseMessage = await _httpClient
// .PostXmlAsync(_gatewayOptions.ApiUrl, data, cancellationToken)
// .ConfigureAwaitFalse();

// var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwaitFalse();

// var verifyResult = AsanPardakhtHelper.CheckVerifyResult(response, callbackResult, _messageOptions);

// if (!verifyResult.IsSucceed)
// {
// return verifyResult.Result;
// }

// data = await AsanPardakhtHelper.CreateSettleData(callbackResult, account, _crypto)
// .ConfigureAwaitFalse();

// responseMessage = await _httpClient
// .PostXmlAsync(_gatewayOptions.ApiUrl, data, cancellationToken)
// .ConfigureAwaitFalse();

// response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwaitFalse();

// return AsanPardakhtHelper.CreateSettleResult(response, callbackResult, _messageOptions);
//}

///// <inheritdoc />
//public override Task<IPaymentRefundResult> RefundAsync(InvoiceContext context, Money amount, CancellationToken cancellationToken = default)
//{
// return PaymentRefundResult.Failed("RefundNotSupports").ToInterfaceAsync();
//}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@ namespace Parbad.Gateway.AsanPardakht
{
public class AsanPardakhtGatewayAccount : GatewayAccount
{
public string MerchantConfigurationId { get; set; }
public long MerchantConfigurationId { get; set; }

public string UserName { get; set; }

public string Password { get; set; }

/// <summary>
/// The Key for encrypting/decrypting the data. You can ask Asan Pardakht Support to receive your key.
/// </summary>
public string Key { get; set; }

/// <summary>
/// The IV for encrypting/decrypting the data. You can ask Asan Pardakht Support to receive your IV.
/// </summary>
public string IV { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public static IGatewayConfigurationBuilder<AsanPardakhtGateway> AddAsanPardakht(
{
if (builder == null) throw new ArgumentNullException(nameof(builder));

builder.Services.AddSingleton<IAsanPardakhtCrypto, AsanPardakhtCrypto>();

return builder
.AddGateway<AsanPardakhtGateway>()
.WithOptions(options => { })
Expand Down
Loading