NopCommerce如何编写支付方式代码

来源:互联网 发布:139邮箱exchange 端口 编辑:程序博客网 时间:2024/05/22 05:24

如何编写支付方式代码

NopCommerce中,支付方式是通过插件实现的。我们建议你在开始编写支付方式代码前阅读“如何编写插件”。“如何编写插件”解释了编写插件所要求的步骤。

而支付方式不过是实现了IPaymentMethod接口(Nop.Service.Payments名称空间)的普通插件。正如你猜想到的那样,IPaymentMethod接口用于创建支付方式插件。这个接口包含了一些支付方式特有的方法,例如,ProcessPayment()GetAdditionalHandingFee()。在解决方案中增加新的支付插件项目(类库),我们就可以开始了。

 

控制器,视图,模型

首先需要创建控制器。控制器负责响应ASP.NET MVC网站的请求。

1.  实现新的支付方式时,控制器应从一个特定的抽象类BaseNopPaymentController派生。有些支付方式需要客户的一些信息,例如信用卡信息。这就是为什么类BaseNopPaymentController要求你实现两个action方法,分别用于在付款时解析和验证用户输入。

a.  ValidatePaymentForm用于验证用户输入。返回警告信息列表(例如,用户没有输入信用卡名称)。如果支付方式不需要用户输入额外信息,ValidatePaymentForm就应当返回空列表。

        [NonAction]

        public override IList ValidatePaymentForm(FormCollection form)

{            

var warnings = new List();            

return warnings;        

}

b.  GetPaymentInfo方法用于解析用户输入,例如,信用卡信息。该方法返回对象 ProcessPaymentRequest,这个对象解析用户输入(例如,信用卡信息)。如果支付方式不需要用户输入额外信息, GetPaymentInfo就应当返回空的ProcessPaymentRequest对象。

        [NonAction]

        public override ProcessPaymentRequest GetPaymentInfo(FormCollection form)

        {

            var paymentInfo = new ProcessPaymentRequest();

            return paymentInfo;

        }

2.  实现Configure action方法用于插件配置(网站后台)。这个方法和相应的视图实现了管理员在admin panel (System > Configuration > Payment methods)看到的配置选项。

3.  最后一步是创建PaymentInfo action方法。这个方法和相应的视图实现了客户在付款时在付款信息页所看到的内容。

支付处理

现在需要创建一个类实现接口IPaymentMethod。正是这个类实现了和支付网关的通讯。当某个人创建了一个订单时,就调用这个类的ProcessPaymentPostProcessPayment 方法。下面的代码演示了类CashOnDeliveryPaymentProcessor是如何定义的 ("Cash on delivery"支付方式)

    ///      /// CashOnDelivery payment processor     ///

    public class CashOnDeliveryPaymentProcessor : BasePlugin, IPaymentMethod

    {...

IPaymentMethod接口需要实现几个方法和属性:

·    ProcessPayment.这个方法总是在客户下订单之前调用。在订单存储到数据库之前需要处理付款时使用这个方法。例如,信用卡授权时。通常在客户不用重定向到第三方站点完成付款,所有付款操作在本网站完成时使用这个方法 (例如, PayPal Direct)

·    PostProcessPayment.这个方法总是在客户下订单之后调用。通常在客户需要重定向到第三方站点完成付款时使用这个方法 (例如,PayPal Standard)

·    GetAdditionalHandlingFee.返回附加的处理费(增加到订单总金额)。

·    Capture. Some payment gateways allow you to authorize payments before they're captured. It allows store owners to review order details before the payment is actually done. In this case you just authorize a payment inProcessPayment or PostProcessPayment method (described above), and then just capture it. In this case aCapture button will be visible on the order details page in admin area. Note that an order should be already authorized andSupportCapture property should return true.

·    Refund. This method allows you make a refund. In this case aRefund button will be visible on the order details page in admin area. Note that an order should be paid, andSupportRefund or SupportPartiallyRefund property should returntrue.

·    Void. This method allows you void an authorized but not captured payment. In this case aVoid button will be visible on the order details page in admin area. Note that an order should be authorized andSupportVoid property should return true.

·    ProcessRecurringPayment. Use this method to process recurring payments.

·    CancelRecurringPayment. Use this method to cancel recurring payments.

·    CanRePostProcessPayment. Usually this method is used when it redirects a customer to a third-party site for completing a payment. If the third party payment fails this option will allow customers to attempt the order again later without placing a new order.CanRePostProcessPayment should return true to enable this feature.

·    GetConfigurationRoute. As you remember we created a controller in the previous step. This method should return a route information of its Configure method. For example,

·    GetPaymentInfoRoute. This method should return a route information of appropriate PublicInfo method of the previously created controller. For example,

        public void GetConfigurationRoute(out string actionName,

            out string controllerName,

            out RouteValueDictionary routeValues)

        {

            actionName = "Configure";

            controllerName = "PaymentCashOnDelivery";

            routeValues = new RouteValueDictionary()

            {

               { "Namespaces", "Nop.Plugin.Payments.CashOnDelivery.Controllers" },

               { "area", null }

            };

        }

·    GetControllerType. This method should return a type of the previously created controller.

        public void GetPaymentInfoRoute(out string actionName,

            out string controllerName,

            out RouteValueDictionary routeValues)

        {

            actionName = "PaymentInfo";

            controllerName = "PaymentCashOnDelivery";

            routeValues = new RouteValueDictionary()

            {

                { "Namespaces", "Nop.Plugin.Payments.CashOnDelivery.Controllers" },

               { "area", null }

            };

        }

·    SupportCapture, SupportPartiallyRefund, SupportRefund, SupportVoid. These properties indicate whether appropriate methods of your payment method are supported.

·    RecurringPaymentType. This property indicates whether recurring payments are supported.

·    PaymentMethodType. This property indicates payment method type. Currently there are three types.Standard used by payment methods when a customer is not redirected to a third-party site.Redirection is used when a customer is redirected to a third-party site. AndButton is similar to Redirection payment methods. The only difference is used that it's displayed as a button on shopping cart page (for example, Google Checkout).

结论

希望本文有助于你创建新的支付方式。

 

原创粉丝点击