ITeye博客

使用ActiveMerchant支持支付宝,财付通和快钱

花花公子 · 2008-03-09 14:46 · rails · Git · 百度 · Ruby · Rails · Gmail · Markdown

[color=red]2008-10-11更新,支持支付宝notify方法。[/color][url]http://www.iteye.com/post/693987[/url] ActiveMerchant 很好很强大,但是网上介绍的都是信用卡支付的方式,对于国内这种类Paypal的支付方式很少。 这种支付方式在ActiveMerchant中称之为integration模式,可能是由于编写起来并不难,介绍它的文档很少,我编写的时候是完全按照paypal的代码写的。 [url]http://code.google.com/p/activemerchant/issues/detail?id=114[/url] 是我提交的Patch,以支付宝为例,在使用前需要指定 ActiveMerchant::Billing::Integrations::Alipay::KEY和Alipay::ACCOUNT,具体这两个值放在哪里,根据个人喜好,直接放在插件中,或者放在environment.rb中,或者放在lib文件夹下require进来都可以。 使用的时候,需要在页面中使用下面这样的代码(参见 [url]http://activemerchant.rubyforge.org/classes/ActiveMerchant/Billing/Integrations/ActionViewHelper.html[/url]) [code="ruby"] <% payment_service_for 1000, 'paypalemail@mystore.com', :amount => 50.00, :currency => 'CAD', :service => :paypal, :html => { :id => 'payment-form' } do |service| %> <% service.customer :first_name => 'Cody', :last_name => 'Fauser', :phone => '(555)555-5555', :email => 'codyfauser@gmail.com' %> <% service.billing_address :city => 'Ottawa', :address1 => '21 Snowy Brook Lane', :address2 => 'Apt. 36', :state => 'ON', :country => 'CA', :zip => 'K1J1E5' %> <% service.invoice '#1000' %> <% service.shipping '0.00' %> <% service.tax '0.00' %> <% service.notify_url url_for(:only_path => false, :action => 'notify') %> <% service.return_url url_for(:only_path => false, :action => 'done') %> <% service.cancel_return_url 'http://mystore.com' %> <% service.sign %> <% end %> [/code] helper.rb中的工作也很重要, [code="ruby"] mapping :tax, 'tax_no' [/code] 说明 service.tax '0.00' 会生成 这样一个表单字段。 [code="ruby"] mapping :customer, :email => 'c_email', :address => 'c_address' [/code] 说明 service.customer :email => 'alipay@iteye.com', :address => '上海' 会生成 和 两个字段。 对于不会用于生成Hash签名,基本不会变的字段,可以在helper的intialize方法中生成 [code="ruby"] def initialize(order, account, options = {}) super add_field('bank_type', 0) end => '' [/code] service.sign方法,是我为生成签名添加的。在这个方法里最后使用 add_field 'sign', sign把hash值填入到表单里。(好像只有国内的支付接口需要计算hash值) [code="ruby"] # in lib/active_merchant/billing/integrations/tenpay.rb def sign add_field('sign', Digest::MD5.hexdigest("cmdno=#{cmdno}&date=#{date}&bargainor_id=#{account}" + "&transaction_id=#{transaction_id}&sp_billno=#{order}&total_fee=#{amount}" + "&fee_type=#{currency}&return_url=#{return_url}&attach=#{attach}&key=#{KEY}")) end [/code] 返回方式有两种,notify和return。可以理解为异步和同步方式,一般notify方法还需要acknowlege,也就是通知对方收到。目前支付宝的notify模式尚未支持,若要使用需要参照paypal的实现。这里先看return模式。 Return类只需要两个方法 success? 和 message,使用的时候也很简单 [code="ruby"] def OrderController < ApplicationController include ActiveMerchant::Billing::Integrations def alipay_return r = Alipay::Return.new(request.query_string) unless @result = r.success? logger.warn(r.message) end end [/code]

评论(15)

  1. #15 楼 · qichunren · 2009-01-05 16:21

    我看了一下网银支付,比这个支付宝简单多了,而且只要手续费1%, 而支付宝签订的话,最起码是600元。

  2. #14 楼 · boyjunqiang · 2008-11-01 10:02

    最好做一个改进,就是在每个类的initialize方法中加入@param ||= param 这样所有的rails版本就都支持了

  3. #13 楼 · boyjunqiang · 2008-11-01 10:00

    楼主的rails版本还是1.2.3吧,我看所有的param都写成了@param,这个在2.0以上的rails就不支持了

  4. #12 楼 · crax.sun · 2008-10-24 12:19

    patch的时候找不到tenpay/helper.rb?

    是不是我哪里弄错了呢?

  5. #11 楼 · lvflying · 2008-10-20 19:37

    Hi,

    很感谢你的这个patch, 我在 www.logoke.com 项目中用到了你这个,省了很多事情。 我按照paypal里的方法, 在notification class 中添加了一个acknowledge method, 在你的基础上又做了一个patch, 放在你那个之后。

  6. #10 楼 · 花花公子 · 2008-10-11 22:30

    这两天有空把前一段时间的修改重新做了一个patch,见附件。 lighthouse上也可以下载 http://jadedpixel.lighthouseapp.com/projects/11599-active-merchant/tickets/8-patch-additon-integrations-support-alipay-99bill-and-tenpay-all-in-china 怎样提交gateway支持请看这篇 http://github.com/Shopify/active_merchant/wikis/contributing

    patch使用方法: 在active_merchant目录下(建议最新的从github.com上取下的版本) git apply active_merchant_alipay_bill99_and_tenpay.diff

  7. #9 楼 · 李盈斐 · 2008-10-07 13:15

    cquaker writes:

    支付宝和paypal比缺少很多东西,目前比较头疼的是没有批量打款的接口。

    支付宝有批量打款的接口,我在项目中用了

  8. #8 楼 · cquaker · 2008-09-26 10:13

    花花公子 writes:

    [quote=right now]我不太懂,我想请教下,这个支持网上银行的支付吗[/quote] 否,网上支付不等于网上银行。等百付宝出来的,谁按照这个方式写一个百付宝支持的。

    有个头疼的地方就是国内很多支付网关没有沙盒模式,往往都需要先签约后使用,支付宝就是一个例子,不知道百度的这个如何?

  9. #7 楼 · cquaker · 2008-09-26 10:10

    花花公子 writes:

    [quote=cquaker]还有更新吗?最近在用alipay,说不定可以一起把代码完善一下 看到todo里面写着 It need two more works:

    1.Add notifications support to alipay.

    2.Complete the test [/quote] notifications支持已经加好了,其实很简单。测试还没有加,不过我有一个计算出支付宝需要的url的方法可以提供。

    旧的代码还有不完善的地方,一直没愿意提交,争取十一前提交吧。

    其实我这个东西就是按照PayPal的方式写的,写好以后发现支付宝就是学的PayPal。但是支付宝没有test模式,所以为了测试支付宝花了我0.01*20个大洋。

    支付宝和paypal比缺少很多东西,目前比较头疼的是没有批量打款的接口。

  10. #6 楼 · cquaker · 2008-09-26 09:50
  11. #5 楼 · 花花公子 · 2008-09-25 16:50

    right now writes:

    我不太懂,我想请教下,这个支持网上银行的支付吗

    否,网上支付不等于网上银行。等百付宝出来的,谁按照这个方式写一个百付宝支持的。

  12. #4 楼 · 0701 · 2008-09-25 16:04

    我不太懂,我想请教下,这个支持网上银行的支付吗

  13. #3 楼 · 花花公子 · 2008-09-25 16:01

    cquaker writes:

    还有更新吗?最近在用alipay,说不定可以一起把代码完善一下 看到todo里面写着 It need two more works:

    1.Add notifications support to alipay.

    2.Complete the test

    notifications支持已经加好了,其实很简单。测试还没有加,不过我有一个计算出支付宝需要的url的方法可以提供。

    旧的代码还有不完善的地方,一直没愿意提交,争取十一前提交吧。

    其实我这个东西就是按照PayPal的方式写的,写好以后发现支付宝就是学的PayPal。但是支付宝没有test模式,所以为了测试支付宝花了我0.01*20个大洋。

  14. #2 楼 · cquaker · 2008-09-25 14:33

    还有更新吗?最近在用alipay,说不定可以一起把代码完善一下 看到todo里面写着 It need two more works:

    1.Add notifications support to alipay.

    2.Complete the test

  15. #1 楼 · boyjunqiang · 2008-09-02 11:35

    还有人这样用过吗?

评论功能维护中,当前仅展示历史评论。