我正在为Nopcommerce网站开发自定义付款方式插件。这是支付处理器类代码:
public class CODBookingPaymentProcessor : BasePlugin, IPaymentMethod
{
private IShoppingCartService _shoppingCartService;
private IOrderService _orderService;
private IHttpContextAccessor _httpContextAccessor;
#region Ctor
public CODBookingPaymentProcessor(IShoppingCartService shoppingCartService,
IOrderService orderService, IHttpContextAccessor httpContextAccessor)
{
this._shoppingCartService = shoppingCartService;
this._orderService = orderService;
this._httpContextAccessor = httpContextAccessor;
}
#endregion
~~~~~~~~~~~~~~~~ SOME CODE ~~~~~~~~~~~~~~~~~~~~~
public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
{
// some code
string url = protocol + host + "/" + "PaymentCODBooking/ProcessInternetPayment";
_httpContextAccessor.HttpContext.Response.Redirect(url);
}
断点即将到来,url
正在形成。但是当在Checkout页面上点击url
按钮时,页面不会重定向到CONFIRM
。它只是留在页面上或有时清空推车。这意味着无需前往支付网关即可创建订单。
重定向也不适用于ConfirmOrder
的CheckoutController
行动。
if (_webHelper.IsRequestBeingRedirected || _webHelper.IsPostBeingDone)
{
//redirection or POST has been done in PostProcessPayment
//return Content("Redirected");
return Redirect("http://localhost:15536/PaymentCODBooking/ProcessInternetPayment");
}
重定向必须是动作结果。例如在控制器的动作中,我们这样写:
return Redirect("http://www.google.com");
没有return
关键字,它不会重定向。
要从插件的控制器重定向,请查看开箱即用的PayPalStandard插件的\ Plugins \ Nop.Plugin.Payments.PayPalStandard \ Controllers \ PaymentPayPalStandardController.cs类中的类似实现。
如果您正在尝试开发插件,那么不要更改nopCommerce源代码会好得多。您可以在插件中执行重定向,不要更改ConfirmOrder
的CheckoutController
动作。将您的代码更改为:
public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
{
// some code
string url = protocol + host + "/" + "PaymentCODBooking/ProcessInternetPayment";
_httpContextAccessor.HttpContext.Response.Redirect(url);
return;
}
你可以在ConfirmOrder
动作中找到这些线。在PostProcessPayment
之后,应用程序将在这里丰富。重定向在这里执行:
if (_webHelper.IsRequestBeingRedirected || _webHelper.IsPostBeingDone)
{
//redirection or POST has been done in PostProcessPayment
return Content("Redirected");
}
谢谢大家的帮助。你的答案给了我一些提示并找到了问题。问题是我忘了设置public PaymentMethodType PaymentMethodType => PaymentMethodType.Redirection;
。它被设置为Standard
导致了这个问题。