Skip to content

Commit

Permalink
fix: sipay payment integration bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
onurkanbakirci committed Dec 31, 2023
1 parent 3654fe1 commit 306691f
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@
));

// Non secure payment
var nonSecurePaymentResponse = await sipayPaymentIntegration.NonSecurePaymentAsync(new NonSecurePaymentRequestModel(
var nonSecurePaymentModel = new NonSecurePaymentRequestModel(
ccHolderName: "John Doe",
ccNo: "123456",
ccNo: "4508034508034509",
expiryMonth: 12,
expiryYear: 2022,
cvv: 123,
expiryYear: 2026,
cvv: 000,
currencyCode: "TRY",
installmentsNumber: 1,
invoiceId: 1,
invoiceDescription: "Invoice description",
name: "John",
surname: "Doe",
total: 100,
items: "items",
cancelUrl: "https://cancelUrl.com",
returnUrl: "https://returnUrl.com",
hashKey: "hashKey",
ip: "",
orderType: 0
));
);
nonSecurePaymentModel.AddItem(new NonSecurePaymentItemRequestModel("name", "100", 1, "desc"));
var nonSecurePaymentResponse = await sipayPaymentIntegration.NonSecurePaymentAsync(nonSecurePaymentModel);

// Secure payment
// 1. Get secure payment hmtl content and show to user
Expand All @@ -58,7 +58,6 @@
items: "items",
cancelUrl: "https://cancelUrl.com",
returnUrl: "https://returnUrl.com",
hashKey: "hashKey",
orderType: 0,
recurringPaymentNumber: 0,
recurringPaymentCycle: "",
Expand All @@ -75,14 +74,12 @@
var securePaymentChargeRequest = await sipayPaymentIntegration.SecurePaymentChargeAsync(new SecurePaymentChargeRequestModel(
invoiceId: "1",
orderId: "1",
status: "1",
hashKey: ""));
status: "1"));


// Cancel payment
var cancelResponse = await sipayPaymentIntegration.CancelAsync(new CancellationRequestModel(
invoiceId: "",
hashKey: "",
refundTransactionId: "",
refundWebhookKey: ""
));
Expand All @@ -91,7 +88,6 @@
var refundResponse = await sipayPaymentIntegration.RefundAsync(new RefundRequestModel(
invoiceId: "",
amount: 100,
hashKey: "",
refundTransactionId: "",
refundWebhookKey: ""
));
1 change: 0 additions & 1 deletion src/Integration.Hub/IntegrationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public async Task<TResponse> InvokeRequestAsync<TRequest, TResponse>(Func<HttpCl
throw new Exception(responseAsString);
return JsonSerializer.Deserialize<TResponse>(responseAsString, _options)!;
}

public async Task<bool> InvokeRequestAsync<TRequest>(Func<HttpClient, StringContent?, Task<HttpResponseMessage>> httpRequest, TRequest requestModel)
where TRequest : IRequestModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
namespace Integration.PaymentGateways.Sipay.Infrastructure.PaymentIntegration.Models.Response;
public class CancellationRequestModel : IRequestModel
{
public CancellationRequestModel(string invoiceId, string hashKey, string refundTransactionId, string? refundWebhookKey = null)
public CancellationRequestModel(string invoiceId, string refundTransactionId, string? refundWebhookKey = null)
{
InvoiceId = invoiceId;
HashKey = hashKey;
RefundTransactionId = refundTransactionId;
RefundWebhookKey = refundWebhookKey;
}
Expand All @@ -17,4 +16,20 @@ public CancellationRequestModel(string invoiceId, string hashKey, string refundT
public string HashKey { get; set; }
public string RefundTransactionId { get; set; }
public string? RefundWebhookKey { get; set; }

public CancellationRequestModel SetMerchantKey(string merchantKey)
{
MerchantKey = merchantKey;
return this;
}
public CancellationRequestModel SetAppSecret(string appSecret)
{
AppSecret = appSecret;
return this;
}
public CancellationRequestModel SetAppId(string appId)
{
AppId = appId;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public CheckInstallmentRequestModel(string creditCard, double amount, string cur
public string? CommissionBy { get; set; }
public bool? IsRecurring { get; set; }
public bool? Is2d { get; set; }
public CheckInstallmentRequestModel SetMerchantKey(string merchantKey)
{
MerchantKey = merchantKey;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Integration.Hub;
using Integration.PaymentGateways.Sipay.Infrastructure.PaymentIntegration.Helpers;

namespace Integration.PaymentGateways.Sipay.Infrastructure.PaymentIntegration.Models.Request;

public class NonSecurePaymentRequestModel : IRequestModel
{
public NonSecurePaymentRequestModel(string ccHolderName, string ccNo, int expiryMonth, int expiryYear, int cvv, string currencyCode, int installmentsNumber, int invoiceId, string invoiceDescription, string name, string surname, int total, string items, string cancelUrl, string returnUrl, string hashKey, string ip, int orderType)
public NonSecurePaymentRequestModel(string ccHolderName, string ccNo, int expiryMonth, int expiryYear, int cvv, string currencyCode, int installmentsNumber, int invoiceId, string invoiceDescription, string name, string surname, int total, string cancelUrl, string returnUrl, string ip, int orderType)
{
CCHolderName = ccHolderName;
CCNo = ccNo;
Expand All @@ -18,14 +19,36 @@ public NonSecurePaymentRequestModel(string ccHolderName, string ccNo, int expiry
Name = name;
Surname = surname;
Total = total;
Items = items;
CancelUrl = cancelUrl;
ReturnUrl = returnUrl;
HashKey = hashKey;
Ip = ip;
OrderType = orderType;
}

public NonSecurePaymentRequestModel AddItem(NonSecurePaymentItemRequestModel nonSecurePaymentItemRequestModel)
{
Items.Add(nonSecurePaymentItemRequestModel);
return this;
}

public NonSecurePaymentRequestModel AddItems(List<NonSecurePaymentItemRequestModel> nonSecurePaymentItemRequestModels)
{
Items.AddRange(nonSecurePaymentItemRequestModels);
return this;
}

public NonSecurePaymentRequestModel SetMerchantKey(string merchantKey)
{
MerchantKey = merchantKey;
return this;
}

public NonSecurePaymentRequestModel SetHashKey(string appSecret)
{
HashKey = HashHelper.GenerateHashKey(Total.ToString(), InstallmentsNumber.ToString(), CurrencyCode, MerchantKey, InvoiceId.ToString(), appSecret);
return this;
}

public string CCHolderName { get; set; }
public string CCNo { get; set; }
public int ExpiryMonth { get; set; }
Expand All @@ -39,7 +62,7 @@ public NonSecurePaymentRequestModel(string ccHolderName, string ccNo, int expiry
public string Surname { get; set; }
public int Total { get; set; }
public string MerchantKey { get; set; }
public string Items { get; set; }
public List<NonSecurePaymentItemRequestModel> Items { get; set; } = new();
public string CancelUrl { get; set; }
public string ReturnUrl { get; set; }
public string HashKey { get; set; }
Expand All @@ -64,4 +87,20 @@ public NonSecurePaymentRequestModel(string ccHolderName, string ccNo, int expiry
public string RecurringPaymentCycle { get; set; }
public string RecurringPaymentInterval { get; set; }
public string RecurringWebHookKey { get; set; }
}
}

public class NonSecurePaymentItemRequestModel : IRequestModel
{
public NonSecurePaymentItemRequestModel(string name, string price, int quantity, string? description = null)
{
Name = name;
Price = price;
Quantity = quantity;
Description = description;
}

public string Name { get; set; }
public string Price { get; set; }
public int Quantity { get; set; }
public string? Description { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
namespace Integration.PaymentGateways.Sipay.Infrastructure.PaymentIntegration.Models.Response;
public class RefundRequestModel : IRequestModel
{
public RefundRequestModel(string invoiceId, double amount, string hashKey, string refundTransactionId, string? refundWebhookKey = null)
public RefundRequestModel(string invoiceId, double amount, string refundTransactionId, string? refundWebhookKey = null)
{
InvoiceId = invoiceId;
Amount = amount;
HashKey = hashKey;
RefundTransactionId = refundTransactionId;
RefundWebhookKey = refundWebhookKey;
}
Expand All @@ -19,4 +18,21 @@ public RefundRequestModel(string invoiceId, double amount, string hashKey, strin
public string HashKey { get; set; }
public string RefundTransactionId { get; set; }
public string? RefundWebhookKey { get; set; }


public RefundRequestModel SetMerchantKey(string merchantKey)
{
MerchantKey = merchantKey;
return this;
}
public RefundRequestModel SetAppSecret(string appSecret)
{
AppSecret = appSecret;
return this;
}
public RefundRequestModel SetAppId(string appId)
{
AppId = appId;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
namespace Integration.PaymentGateways.Sipay.Infrastructure.PaymentIntegration.Models.Request;
public class SecurePaymentChargeRequestModel : IRequestModel
{
public SecurePaymentChargeRequestModel(string invoiceId, string orderId, string status, string hashKey)
public SecurePaymentChargeRequestModel(string invoiceId, string orderId, string status)
{
InvoiceId = invoiceId;
OrderId = orderId;
Status = status;
HashKey = hashKey;
}

public string MerchantKey { get; set; }
public string InvoiceId { get; set; }
public string OrderId { get; set; }
public string Status { get; set; }
public string HashKey { get; set; }
public SecurePaymentChargeRequestModel SetMerchantKey(string merchantKey)
{
MerchantKey = merchantKey;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Reflection.Metadata;
using Integration.Hub;
using Integration.PaymentGateways.Sipay.Infrastructure.PaymentIntegration.Helpers;
namespace Integration.PaymentGateways.Sipay.Infrastructure.PaymentIntegration.Models.Request;
public class SecurePaymentInitialRequestModel : IRequestModel
{
public SecurePaymentInitialRequestModel(string ccHolderName, string ccNo, int expiryMonth, int expiryYear, int cvv, string currencyCode, int installmentsNumber, string invoiceId, string invoiceDescription, string name, string surname, double total, string items, string cancelUrl, string returnUrl, string hashKey, int orderType, int recurringPaymentNumber, string recurringPaymentCycle, string recurringPaymentInterval, string recurringWebHookKey, int maturityPeriod, int paymentFrequency)
public SecurePaymentInitialRequestModel(string ccHolderName, string ccNo, int expiryMonth, int expiryYear, int cvv, string currencyCode, int installmentsNumber, string invoiceId, string invoiceDescription, string name, string surname, double total, string items, string cancelUrl, string returnUrl, int orderType, int recurringPaymentNumber, string recurringPaymentCycle, string recurringPaymentInterval, string recurringWebHookKey, int maturityPeriod, int paymentFrequency)
{
CCHolderName = ccHolderName;
CCNo = ccNo;
Expand All @@ -19,7 +21,6 @@ public SecurePaymentInitialRequestModel(string ccHolderName, string ccNo, int ex
Items = items;
CancelUrl = cancelUrl;
ReturnUrl = returnUrl;
HashKey = hashKey;
OrderType = orderType;
RecurringPaymentNumber = recurringPaymentNumber;
RecurringPaymentCycle = recurringPaymentCycle;
Expand Down Expand Up @@ -70,4 +71,16 @@ public SecurePaymentInitialRequestModel(string ccHolderName, string ccNo, int ex
public string? SaleWebHookKey { get; set; }
public string? PaymentCompletedBy { get; set; }
public string? ResponseMethod { get; set; }

public SecurePaymentInitialRequestModel SetHashKey(string appSecret)
{
HashKey = HashHelper.GenerateHashKey(Total.ToString(), InstallmentsNumber.ToString(), CurrencyCode, MerchantKey, InvoiceId, appSecret);
return this;
}

public SecurePaymentInitialRequestModel SetMerchantKey(string merchantKey)
{
MerchantKey = merchantKey;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public class CheckInstallmentResponseModel : IResponseModel
public string AmountToBePaid { get; set; }
public string CurrencyCode { get; set; }
public int CurrencyId { get; set; }
public string Title { get; set; }
//public string Title { get; set; }//Sipay has problem. This prop can be both string and int
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ public class NonSecurePaymentResponseModel : IResponseModel
{
public int Status { get; set; }
public string OrderNo { get; set; }

Check warning on line 6 in src/PaymentGateways/Integration.PaymentGateways.Sipay/Infrastructure/PaymentIntegration/Models/Response/NonSecurePaymentResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build Lib

Non-nullable property 'OrderNo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public int InvoiceId { get; set; }
public string InvoiceId { get; set; }

Check warning on line 7 in src/PaymentGateways/Integration.PaymentGateways.Sipay/Infrastructure/PaymentIntegration/Models/Response/NonSecurePaymentResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build Lib

Non-nullable property 'InvoiceId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public int StatusCode { get; set; }
public string StatusDescription { get; set; }

Check warning on line 9 in src/PaymentGateways/Integration.PaymentGateways.Sipay/Infrastructure/PaymentIntegration/Models/Response/NonSecurePaymentResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build Lib

Non-nullable property 'StatusDescription' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string PaymentMethod { get; set; }
public int PaymentMethod { get; set; }
public string TransactionType { get; set; }

Check warning on line 11 in src/PaymentGateways/Integration.PaymentGateways.Sipay/Infrastructure/PaymentIntegration/Models/Response/NonSecurePaymentResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build Lib

Non-nullable property 'TransactionType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public int ErrorCode { get; set; }
public string Error { get; set; }

Check warning on line 13 in src/PaymentGateways/Integration.PaymentGateways.Sipay/Infrastructure/PaymentIntegration/Models/Response/NonSecurePaymentResponseModel.cs

View workflow job for this annotation

GitHub Actions / Build Lib

Non-nullable property 'Error' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task<bool> SetTokenAsync()
/// <returns><inheritdoc/></returns>
public async Task<SipayBaseResponseModel<List<CheckInstallmentResponseModel>>> CheckInstallmentsAsync(CheckInstallmentRequestModel checkInstallmentRequestModel)
{
checkInstallmentRequestModel.MerchantKey = _merchantKey;
checkInstallmentRequestModel.SetMerchantKey(_merchantKey);
return await InvokeRequestAsync<CheckInstallmentRequestModel, SipayBaseResponseModel<List<CheckInstallmentResponseModel>>>((client, requestBody) => client.PostAsync(GetCheckInstallmentsUrl(), requestBody), checkInstallmentRequestModel);
}

Expand All @@ -45,7 +45,8 @@ public async Task<SipayBaseResponseModel<List<CheckInstallmentResponseModel>>> C
/// <returns><inheritdoc/></returns>
public async Task<SipayBaseResponseModel<NonSecurePaymentResponseModel>> NonSecurePaymentAsync(NonSecurePaymentRequestModel nonSecurePaymentRequestModel)
{
nonSecurePaymentRequestModel.MerchantKey = _merchantKey;
nonSecurePaymentRequestModel.SetMerchantKey(_merchantKey);
nonSecurePaymentRequestModel.SetHashKey(_appSecret);
return await InvokeRequestAsync<NonSecurePaymentRequestModel, SipayBaseResponseModel<NonSecurePaymentResponseModel>>((client, requestBody) => client.PostAsync(GetNonSecurePaymentUrl(), requestBody), nonSecurePaymentRequestModel);
}

Expand All @@ -56,15 +57,8 @@ public async Task<SipayBaseResponseModel<NonSecurePaymentResponseModel>> NonSecu
/// <returns><inheritdoc/></returns>
public string SecurePaymentInitial(SecurePaymentInitialRequestModel securePaymentInitialRequestModel)
{
securePaymentInitialRequestModel.MerchantKey = _merchantKey;

//Generate hashkey
var hashKey = HashHelper.GenerateHashKey(securePaymentInitialRequestModel.Total.ToString(), securePaymentInitialRequestModel.InstallmentsNumber.ToString(), securePaymentInitialRequestModel.CurrencyCode, securePaymentInitialRequestModel.MerchantKey, securePaymentInitialRequestModel.InvoiceId, _appSecret);

//Add hash key to dto
securePaymentInitialRequestModel.HashKey = hashKey;

//Generate template
securePaymentInitialRequestModel.SetMerchantKey(_merchantKey);
securePaymentInitialRequestModel.SetHashKey(_appSecret);
var template = HTMLHelper.GenerateTemplate("3ds-payment-form", "Resources/HTML", securePaymentInitialRequestModel);
return template;
}
Expand All @@ -76,8 +70,7 @@ public string SecurePaymentInitial(SecurePaymentInitialRequestModel securePaymen
/// <returns><inheritdoc/></returns>
public async Task<SipayBaseResponseModel<SecurePaymentChargeResponseModel>> SecurePaymentChargeAsync(SecurePaymentChargeRequestModel securePaymentChargeRequestModel)
{
securePaymentChargeRequestModel.MerchantKey = _merchantKey;

securePaymentChargeRequestModel.SetMerchantKey(_merchantKey);
return await InvokeRequestAsync<SecurePaymentChargeRequestModel, SipayBaseResponseModel<SecurePaymentChargeResponseModel>>((client, requestBody) => client.PostAsync(GetSecurePaymentChargeUrl(), requestBody), securePaymentChargeRequestModel);
}

Expand All @@ -88,10 +81,9 @@ public async Task<SipayBaseResponseModel<SecurePaymentChargeResponseModel>> Secu
/// <returns><inheritdoc/></returns>
public async Task<SipayBaseResponseModel<CancellationResponseModel>> CancelAsync(CancellationRequestModel cancellationRequestModel)
{
cancellationRequestModel.MerchantKey = _merchantKey;
cancellationRequestModel.AppSecret = _appSecret;
cancellationRequestModel.AppId = _appKey;

cancellationRequestModel.SetMerchantKey(_merchantKey);
cancellationRequestModel.SetAppSecret(_appSecret);
cancellationRequestModel.SetAppId(_appKey);
return await InvokeRequestAsync<CancellationRequestModel, SipayBaseResponseModel<CancellationResponseModel>>((client, requestBody) => client.PostAsync(GetCancelUrl(), requestBody), cancellationRequestModel);
}

Expand All @@ -102,10 +94,9 @@ public async Task<SipayBaseResponseModel<CancellationResponseModel>> CancelAsync
/// <returns><inheritdoc/></returns>
public async Task<SipayBaseResponseModel<RefundResponseModel>> RefundAsync(RefundRequestModel refundRequestModel)
{
refundRequestModel.MerchantKey = _merchantKey;
refundRequestModel.AppSecret = _appSecret;
refundRequestModel.AppId = _appKey;

refundRequestModel.SetMerchantKey(_merchantKey);
refundRequestModel.SetAppSecret(_appSecret);
refundRequestModel.SetAppId(_appKey);
return await InvokeRequestAsync<RefundRequestModel, SipayBaseResponseModel<RefundResponseModel>>((client, requestBody) => client.PostAsync(GetRefundUrl(), requestBody), refundRequestModel);
}
}
Loading

0 comments on commit 306691f

Please sign in to comment.