本文转自:http://docs.nopcommerce.com/display/nc/How+to+code+my+own+payment+method

Payment methods are implemented as plugins in nopCommerce. We recommend you read How to write a nopCommerce plugin before you start coding a new payment method. It will explain to you what the required steps are for creating a plugin.

So actually a payment method is an ordinary plugin which implements an IPaymentMethod interface (Nop.Services.Payments namespace). As you already guessed IPaymentMethod interface is used for creating payment method plugins. It contains some methods which are specific only for payment methods such as ProcessPayment() or GetAdditionalHandlingFee(). So add a new payment plugin project (class library) to solution and let's get started.

Controllers, views, models.

First thing you need to do is create a controller. This controller is responsible for responding to requests made against an ASP.NET MVC website.

  1. When implementing a new payment method, this controller should be derived from a special BaseNopPaymentController abstract class. Some of the payment methods ask customer for some data, such as credit card information. That's why a BasePaymentController class requires you to implement two action methods used to parse and validate customer input during checkout.

    1. ValidatePaymentForm is used in the public store to validate customer input. It returns a list of warnings (for example, a customer did not enter his credit card name). If your payment method does not ask the customer to enter additional information, then the ValidatePaymentForm should return an empty list.

              [NonAction]
      public override IList ValidatePaymentForm(FormCollection form)
              {
                   var warnings = new List();
                   return warnings;
              } 
    2. GetPaymentInfo method is used in the public store to parse customer input, such as credit card information. This method returns a ProcessPaymentRequest object with parsed customer input (for example, credit card information). If your payment method does not ask the customer to enter additional information, then GetPaymentInfo will return an empty ProcessPaymentRequest object.
              [NonAction]
      public override ProcessPaymentRequest GetPaymentInfo(FormCollection form)
      {
      var paymentInfo = new ProcessPaymentRequest();
                  return paymentInfo;
      }
       
  2. Then implement Configure action methods used for plugin configuration (by a store owner in admin area). This method and an appropriate view will define how a store owner sees configuration options in admin panel (System > Configuration > Payment methods).
  3. And the last step is to create PaymentInfo action method. This method and an appropriate view will define how your customers will see the payment information page during checkout.

Payment processing.

Now you need to create a class which implements IPaymentMethod interface. This is the class that will be doing all the actual work of communicating with your payment gateway. When someone creates an order either the ProcessPayment or PostProcessPayment methods of your class will be called. Here is how CashOnDeliveryPaymentProcessor class is defined ("Cash on delivery" payment method):

    ///      
    /// CashOnDelivery payment processor     
    ///
public class CashOnDeliveryPaymentProcessor : BasePlugin, IPaymentMethod
{...

IPaymentMethod interface has several methods and properties which are required to implement.

  • ProcessPayment. This method is always invoked right before a customer places an order. Use it when you need to process a payment before an order is stored into database. For example, capture or authorize credit card. Usually this method is used when a customer is not redirected to third-party site for completing a payment and all payments are handled on your site (for example, PayPal Direct).
  • PostProcessPayment. This method is invoked right after a customer places an order. Usually this method is used when you need to redirect a customer to a third-party site for completing a payment (for example, PayPal Standard).
  • HidePaymentMethod. You can put any logic here. For example, hide this payment method if all products in the cart are downloadable. Or hide this payment method if current customer is from certain country
  • GetAdditionalHandlingFee. You can return any additional handling fees which will be added to an order total.
  • 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 in ProcessPayment or PostProcessPayment method (described above), and then just capture it. In this case a Capture button will be visible on the order details page in admin area. Note that an order should be already authorized and SupportCapture property should returntrue.
  • Refund. This method allows you make a refund. In this case a Refund button will be visible on the order details page in admin area. Note that an order should be paid, and SupportRefund or SupportPartiallyRefund property should return true.
  • Void. This method allows you void an authorized but not captured payment. In this case a Void button will be visible on the order details page in admin area. Note that an order should be authorized and SupportVoid 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,
            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 }
    };
    }
  • GetPaymentInfoRoute. This method should return a route information of appropriate PublicInfo method of the previously created controller. For example,
            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 }
    };
    }
  • GetControllerType. This method should return a type of the previously created controller.
  • 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. And Button 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).

Conclusion

Hopefully this will get you started with adding a new payment method.

[转]NopCommerce How to code my own payment method的更多相关文章

  1. Magento 0元订单 支付方式 -- Magento 0 Subtotal Payment Method

    需求 现有购物网站支持2种支付方式,但是考虑到会出现如下情况: 在一个优惠活动中,假如有一些订单的总金额为0, 那么这些订单就不必跳转到支付网关,现有支付方式无法处理此种情况. 分析 当custome ...

  2. QA:Failed to deploy artifacts from/to snapshots XX Failed to transfer file Return code is: 405, ReasonPhrase:Method Not Allowed.

    QA: Failed to deploy artifacts from/to snapshots XX Failed to transfer file Return code is: 405, Rea ...

  3. volley(2) 参数code : or_barcode, pr_ismsd:false , method:GET

    1. 来自于WHCombineBatchFragment.java /** * 当编辑框里面的内容完成的时候,自动的,同时获取服务器的批量数 */private void barcodeEnterEv ...

  4. ambari-server启动报错500 status code received on GET method for API:/api/v1/stacks/HDP/versions/2.4/recommendations Error message : Server Error解决办法(图文详解)

    问题详情 来源是,我在Ambari集群里,安装Hue. 给Ambari集群里安装可视化分析利器工具Hue步骤(图文详解 所遇到的这个问题. 然后,去ambari-server的log日志,查看,如下 ...

  5. NopCommerce功能与特点介绍

    [本文转自]http://www.cnblogs.com/nopcommerce-b2c/p/3758676.html 前两节我们主要介绍了NopCommerce下载与安装和NopCommerce中文 ...

  6. 03.NopCommerce功能与特点介绍

    前两节我们主要介绍了NopCommerce下载与安装和NopCommerce中文语言包,让大家体验一下NopCommerce.这次我们主要来介绍NopCommerce的功能与特点. NopCommer ...

  7. NopCommerce——源代码的组织,以及系统的架构

    近来使用NopCommerce进行开发,仿照源码的Demo也能做出看上去还蛮高端大气上档次的系统出来,现下准备深入学习学习.首先从官方的Documentation开始看起,先来一篇官网文章的翻译(园里 ...

  8. Code First :使用Entity. Framework编程(6) ----转发 收藏

    Chapter6 Controlling Database Location,Creation Process, and Seed Data 第6章 控制数据库位置,创建过程和种子数据 In prev ...

  9. Code First :使用Entity. Framework编程(7) ----转发 收藏

    第7章 高级概念 The Code First modeling functionality that you have seen so far should be enough to get you ...

随机推荐

  1. Oracle - ORA-12505, TNS:listener does not currently know of SID given in connect descriptor 解决

    java.sql.SQLException: Listener refused the connection with the following error: ORA-12505, TNS:list ...

  2. Java Swing快速构建窗体应用程序

    以前接触java感觉其在桌面开发上,总是不太方便,没有一个好的拖拽界面布局工具,可以快速构建窗体. 最近学习了一下NetBeans IDE 8.1,感觉其窗体设计工具还是很不错的 , 就尝试一下做了一 ...

  3. ThinkPHP3.2 G函数代码及 使用方法

    ThinkPHP3.2 G函数代码及 使用方法 代码: // 内存是否可调用 define('MEMORY_LIMIT_ON',function_exists('memory_get_usage')) ...

  4. Hybrid框架UI重构之路:一、师其长技以自强

    这两年在支撑公司的Hybrid框架的运维发展,让人确认这种移动开发方式确实是一条不错的路.混合应用这种开发方式降低开发难度,极大的提高开发效率,最重要的一点效果可以接近原生应用.框架的本身是需要持续不 ...

  5. Sublime Text 3汉化中文版

    Sublime Text 3汉化中文版是Sublime Text2的升级版.Sublime Text 是一款流行的文本编辑器软件,有点类似于TextMate,跨平台,可运行在Linux,Windows ...

  6. FOR ALL ENTRIES IN 与 INNER JOIN 写在一个SQL上影响效率

    SELECT likp~vbeln likp~lfart lips~werks likp~kunnr INTO CORRESPONDING FIELDS OF TABLE it_likps FROM ...

  7. 为Autodesk Viewer添加自定义工具条

    如果你参加过我们近期的活动,你就会频繁的听到我们现在正在做的Autodesk Viewer大模型浏览器,这是一个不需要下载任何插件,基于WebGL技术的浏览器,可以支持几十种数据格式.同时viewer ...

  8. Linux内核--C语言中内嵌汇编 asm __volatile__

    在内嵌汇编中,可以将C语言表达式指定为汇编指令的操作数,而且不用去管如何将C语言表达式的值读入哪个寄存器,以及如何将计算结果写回C 变量,你只要告诉程序中C语言表达式与汇编指令操作数之间的对应关系即可 ...

  9. <转>关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系

    转自:http://www.cnblogs.com/cywin888/p/3263027.html 刚接触iOS开发的人难免会对苹果的各种证书.配置文件等不甚了解,可能你按照网上的教程一步一步的成功申 ...

  10. 利用RxJava获取手机已安装的App的图片、应用名称和版本号

    先上效果图: 获取手机已安装的App列表利用Android系统API就可以办到,这里为什么要引入RxJava?现在我们假设一下有下面几个需求: 1.我们不需要所有的App,只需要用户安装的第三方App ...