NopCommerce如何编写支付方式代码

如何编写支付方式代码

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。正是这个类实现了和支付网关的通讯。当某个人创建了一个订单时,就调用这个类的ProcessPayment PostProcessPayment 方法。下面的代码演示了类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 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 return true.

·    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,

·    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. 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).

结论

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

 

接口名称:支付宝手机网站支付接口(alipay.wap.create.direct.pay.by.user)     代码版本:3.3 开发语言:ASP 版 权:支付宝()网络技术有限公司   制 作 者:支付宝技术部技术支持组 联系方式:https://support.open.alipay.com/alipay/support/index.htm 免责声明:DEMO仅供参考,实际开发中需要结合具体场景修改使用。 ───────────────────────────────── ─────── 代码文件结构 ─────── alipay.wap.create.direct.pay.by.user-CSHARP-UTF-8 │ ├class┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈类文件夹 │ │ │ ├alipay_core.asp┈┈┈┈┈┈┈┈┈┈┈┈支付宝接口公用函数文件 │ │ │ ├alipay_md5.asp ┈┈┈┈┈┈┈┈┈┈┈┈MD5签名函数文件 │ │ │ ├alipay_notify.asp┈┈┈┈┈┈┈┈┈┈┈支付宝通知处理类文件 │ │ │ ├alipay_submit.asp┈┈┈┈┈┈┈┈┈┈┈支付宝各接口请求提交类文件 │ │ │ └alipay_config.asp┈┈┈┈┈┈┈┈┈┈┈基础配置文件 │ ├log┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈日志文件夹 │ ├alipayapi.asp┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈支付宝接口入口文件 │ ├index.asp┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈支付宝调试入口页面 │ ├notify_url.asp ┈┈┈┈┈┈┈┈┈┈┈┈┈┈服务器异步通知页面文件 │ ├return_url.asp ┈┈┈┈┈┈┈┈┈┈┈┈┈┈页面跳转同步通知文件 │ └readme.txt ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈使用说明文本 ※注意※ 需要配置的文件是: alipay_config.asp alipayapi.asp notify_url.asp return_url.asp ───────── 类文件函数结构 ───────── alipay_core.asp Function CreateLinkstring(sPara) 功能:把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 输入:Array sPara 需要拼接的数组 输出:String 拼接完成以后的字符串 Function CreateLinkstringUrlEncode(sPara) 功能:把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并且对其做URLENCODE编码 输入:Array sPara 需要拼接的数组 输出:String 拼接完成以后的字符串 Function FilterPara(sPara) 功能:除去数组中的空值和签名参数 输入:Array sPara 签名参数组 输出:Array 去掉空值与签名参数后的新签名参数组 Function SortPara(sPara) 功能:对数组排序 输入:Array sPara 排序前的数组 输出:Array 排序后的数组 Function Md5Sign(prestr, key, input_charset) 功能:MD5签名 输入:String prestr 需要签名的字符串 String key 私钥 String input_charset 编码格式 输出:String 签名结果 Function Md5Verify(prestr, sign, key, input_charset) 功能:MD5签名 输入:String prestr 需要签名的字符串 String sign 签名结果 String key 私钥 String input_charset 编码格式 输出:String 签名结果 Function LogResult(sWord) 功能:写日志,方便测试(看网站需求,也可以改成存入数据库) 输入:String sWord 要写入日志里的文本内容 Function GetDateTimeFormat() 功能:获取当前时间 格式:年[4位]-月[2位]-日[2位] 小时[2位 24小时制]:分[2位]:秒[2位],如:2007-10-01 13:13:1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值