Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Update IPaymentProvider

KrzysztofPajak6e0ea102752d
diff --git a/src/Business/Grand.Business.Checkout/Services/Payments/PaymentService.cs b/src/Business/Grand.Business.Checkout/Services/Payments/PaymentService.cs
index c1abd43a5..3e209f623 100644
--- a/src/Business/Grand.Business.Checkout/Services/Payments/PaymentService.cs
+++ b/src/Business/Grand.Business.Checkout/Services/Payments/PaymentService.cs
@@ -223,18 +223,18 @@ public class PaymentService : IPaymentService
/// Post redirect payment (used by payment gateways that redirecting to a another URL)
/// </summary>
/// <param name="paymentTransaction">Payment Transaction</param>
- public virtual async Task PostRedirectPayment(PaymentTransaction paymentTransaction)
+ public virtual async Task<string> PostRedirectPayment(PaymentTransaction paymentTransaction)
{
ArgumentNullException.ThrowIfNull(paymentTransaction);
//already paid or order.OrderTotal == 0
if (paymentTransaction.TransactionStatus == TransactionStatus.Paid)
- return;
+ return "";
var paymentMethod = LoadPaymentMethodBySystemName(paymentTransaction.PaymentMethodSystemName);
if (paymentMethod == null)
throw new GrandException("Payment method couldn't be loaded");
- await paymentMethod.PostRedirectPayment(paymentTransaction);
+ return await paymentMethod.PostRedirectPayment(paymentTransaction);
}
/// <summary>
diff --git a/src/Business/Grand.Business.Core/Interfaces/Checkout/Payments/IPaymentProvider.cs b/src/Business/Grand.Business.Core/Interfaces/Checkout/Payments/IPaymentProvider.cs
index 864c940b7..d248ed523 100644
--- a/src/Business/Grand.Business.Core/Interfaces/Checkout/Payments/IPaymentProvider.cs
+++ b/src/Business/Grand.Business.Core/Interfaces/Checkout/Payments/IPaymentProvider.cs
@@ -36,7 +36,7 @@ public interface IPaymentProvider : IProvider
/// Post redirect payment (used by payment gateways that redirecting to a another URL)
/// </summary>
/// <param name="paymentTransaction">Payment transaction</param>
- Task PostRedirectPayment(PaymentTransaction paymentTransaction);
+ Task<string> PostRedirectPayment(PaymentTransaction paymentTransaction);
/// <summary>
/// Returns a value indicating whether payment method should be hidden during checkout
diff --git a/src/Business/Grand.Business.Core/Interfaces/Checkout/Payments/IPaymentService.cs b/src/Business/Grand.Business.Core/Interfaces/Checkout/Payments/IPaymentService.cs
index a9b3b2356..22d8317cb 100644
--- a/src/Business/Grand.Business.Core/Interfaces/Checkout/Payments/IPaymentService.cs
+++ b/src/Business/Grand.Business.Core/Interfaces/Checkout/Payments/IPaymentService.cs
@@ -83,7 +83,7 @@ public interface IPaymentService
/// Post redirect payment (used by payment gateways that redirecting to a another URL)
/// </summary>
/// <param name="paymentTransaction">Payment transaction</param>
- Task PostRedirectPayment(PaymentTransaction paymentTransaction);
+ Task<string> PostRedirectPayment(PaymentTransaction paymentTransaction);
/// <summary>
/// Gets a value indicating whether customers can complete a payment after order is placed but not completed (for
diff --git a/src/Plugins/Payments.BrainTree/BrainTreePaymentProvider.cs b/src/Plugins/Payments.BrainTree/BrainTreePaymentProvider.cs
index 1fa351cfd..8408bd7e6 100644
--- a/src/Plugins/Payments.BrainTree/BrainTreePaymentProvider.cs
+++ b/src/Plugins/Payments.BrainTree/BrainTreePaymentProvider.cs
@@ -193,10 +193,10 @@ public class BrainTreePaymentProvider : IPaymentProvider
/// Post redirect payment (used by payment gateways that redirecting to a another URL)
/// </summary>
/// <param name="paymentTransaction">Payment transaction</param>
- public Task PostRedirectPayment(PaymentTransaction paymentTransaction)
+ public Task<string> PostRedirectPayment(PaymentTransaction paymentTransaction)
{
//nothing
- return Task.CompletedTask;
+ return Task.FromResult(string.Empty);
}
/// <summary>
diff --git a/src/Plugins/Payments.CashOnDelivery/CashOnDeliveryPaymentProvider.cs b/src/Plugins/Payments.CashOnDelivery/CashOnDeliveryPaymentProvider.cs
index f14152467..e1f654cc2 100644
--- a/src/Plugins/Payments.CashOnDelivery/CashOnDeliveryPaymentProvider.cs
+++ b/src/Plugins/Payments.CashOnDelivery/CashOnDeliveryPaymentProvider.cs
@@ -67,10 +67,10 @@ public class CashOnDeliveryPaymentProvider : IPaymentProvider
/// Post redirect payment (used by payment gateways that redirecting to a another URL)
/// </summary>
/// <param name="paymentTransaction">Payment transaction</param>
- public Task PostRedirectPayment(PaymentTransaction paymentTransaction)
+ public Task<string> PostRedirectPayment(PaymentTransaction paymentTransaction)
{
//nothing
- return Task.CompletedTask;
+ return Task.FromResult(string.Empty);
}
public async Task<bool> HidePaymentMethod(IList<ShoppingCartItem> cart)
diff --git a/src/Plugins/Payments.StripeCheckout/StripeCheckoutPaymentProvider.cs b/src/Plugins/Payments.StripeCheckout/StripeCheckoutPaymentProvider.cs
index 88d16533c..e481f27db 100644
--- a/src/Plugins/Payments.StripeCheckout/StripeCheckoutPaymentProvider.cs
+++ b/src/Plugins/Payments.StripeCheckout/StripeCheckoutPaymentProvider.cs
@@ -79,11 +79,10 @@ public class StripeCheckoutPaymentProvider : IPaymentProvider
/// Post process payment (used by payment gateways that require redirecting to a third-party URL)
/// </summary>
/// <param name="paymentTransaction"></param>
- public async Task PostRedirectPayment(PaymentTransaction paymentTransaction)
+ public async Task<string> PostRedirectPayment(PaymentTransaction paymentTransaction)
{
var order = await _orderService.GetOrderByGuid(paymentTransaction.OrderGuid);
- var url = await _stripeCheckoutService.CreateRedirectUrl(order);
- _httpContextAccessor.HttpContext?.Response.Redirect(url);
+ return await _stripeCheckoutService.CreateRedirectUrl(order);
}
/// <summary>
diff --git a/src/Web/Grand.Web/Controllers/CheckoutController.cs b/src/Web/Grand.Web/Controllers/CheckoutController.cs
index 8d0ca564d..ddc99ea58 100644
--- a/src/Web/Grand.Web/Controllers/CheckoutController.cs
+++ b/src/Web/Grand.Web/Controllers/CheckoutController.cs
@@ -781,10 +781,11 @@ public class CheckoutController : BasePublicController
if ((DateTime.UtcNow - order.CreatedOnUtc).TotalMinutes > 5)
return RedirectToRoute("HomePage");
- await _paymentService.PostRedirectPayment(paymentTransaction);
-
- if (IsRequestBeingRedirected || IsPostBeingDone) return Content("Redirected");
-
+ // Get the redirect URL from PostRedirectPayment
+ var redirectUrl = await _paymentService.PostRedirectPayment(paymentTransaction);
+ if (!string.IsNullOrEmpty(redirectUrl))
+ return Redirect(redirectUrl);
+
return RedirectToRoute("CheckoutCompleted", new { orderId = order.Id });
}
catch (Exception exc)
@@ -849,20 +850,6 @@ public class CheckoutController : BasePublicController
return errors;
}
-
- protected virtual bool IsRequestBeingRedirected {
- get {
- var response = HttpContext.Response;
- return new List<int> { 301, 302 }.Contains(response.StatusCode);
- }
- }
-
- protected virtual bool IsPostBeingDone {
- get => HttpContext.Items["grand.IsPOSTBeingDone"] != null &&
- Convert.ToBoolean(HttpContext.Items["grand.IsPOSTBeingDone"]);
- set => HttpContext.Items["grand.IsPOSTBeingDone"] = value;
- }
-
#endregion
#region Private methods
diff --git a/src/Web/Grand.Web/Controllers/OrderController.cs b/src/Web/Grand.Web/Controllers/OrderController.cs
index fea81ed7b..f6257af3d 100644
--- a/src/Web/Grand.Web/Controllers/OrderController.cs
+++ b/src/Web/Grand.Web/Controllers/OrderController.cs
@@ -63,23 +63,6 @@ public class OrderController : BasePublicController
#endregion
- #region Utilities
-
- protected virtual bool IsRequestBeingRedirected {
- get {
- var response = HttpContext.Response;
- return new List<int> { 301, 302 }.Contains(response.StatusCode);
- }
- }
-
- protected virtual bool IsPostBeingDone {
- get => HttpContext.Items["grand.IsPOSTBeingDone"] != null &&
- Convert.ToBoolean(HttpContext.Items["grand.IsPOSTBeingDone"]);
- set => HttpContext.Items["grand.IsPOSTBeingDone"] = value;
- }
-
- #endregion
-
#region Methods
//My account / Orders
@@ -201,14 +184,10 @@ public class OrderController : BasePublicController
if (paymentTransaction == null || !await _paymentService.CanRePostRedirectPayment(paymentTransaction))
return RedirectToRoute("OrderDetails", new { orderId });
- await _paymentService.PostRedirectPayment(paymentTransaction);
-
- if (IsRequestBeingRedirected || IsPostBeingDone)
- //redirection or POST has been done in PostProcessPayment
- return Content("Redirected");
-
- //if no redirection has been done (to a third-party payment page)
- //theoretically it's not possible
+ var redirectUrl = await _paymentService.PostRedirectPayment(paymentTransaction);
+ if (!string.IsNullOrEmpty(redirectUrl))
+ return Redirect(redirectUrl);
+
return RedirectToRoute("OrderDetails", new { orderId });
}