---
title: "使用ActiveMerchant支持支付宝，财付通和快钱"
author: "花花公子"
date: "2008-03-09T14:46:00Z"
tags: ["Git", "Gmail", "Rails", "Ruby", "百度"]
slug: "activemerchant"
draft: false
lastmod: "2008-03-09T14:46:00Z"
id: 169522
url: "https://masterwujiang.iteye.com/blog/169522"
---

\[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"] &lt;% payment\_service\_for 1000, 'paypalemail@mystore.com', :amount =&gt; 50.00, :currency =&gt; 'CAD', :service =&gt; :paypal, :html =&gt; { :id =&gt; 'payment-form' } do |service| %&gt; &lt;% service.customer :first\_name =&gt; 'Cody', :last\_name =&gt; 'Fauser', :phone =&gt; '(555)555-5555', :email =&gt; 'codyfauser@gmail.com' %&gt; &lt;% service.billing\_address :city =&gt; 'Ottawa', :address1 =&gt; '21 Snowy Brook Lane', :address2 =&gt; 'Apt. 36', :state =&gt; 'ON', :country =&gt; 'CA', :zip =&gt; 'K1J1E5' %&gt; &lt;% service.invoice '#1000' %&gt; &lt;% service.shipping '0.00' %&gt; &lt;% service.tax '0.00' %&gt; &lt;% service.notify\_url url\_for(:only\_path =&gt; false, :action =&gt; 'notify') %&gt; &lt;% service.return\_url url\_for(:only\_path =&gt; false, :action =&gt; 'done') %&gt; &lt;% service.cancel\_return\_url 'http://mystore.com' %&gt; &lt;% service.sign %&gt; &lt;% end %&gt; \[/code] helper.rb中的工作也很重要， \[code="ruby"] mapping :tax, 'tax\_no' \[/code] 说明 service.tax '0.00' 会生成 这样一个表单字段。 \[code="ruby"] mapping :customer, :email =&gt; 'c\_email', :address =&gt; 'c\_address' \[/code] 说明 service.customer :email =&gt; 'alipay@iteye.com', :address =&gt; '上海' 会生成 和 两个字段。 对于不会用于生成Hash签名，基本不会变的字段，可以在helper的intialize方法中生成 \[code="ruby"] def initialize(order, account, options = {}) super add\_field('bank\_type', 0) end =&gt; '' \[/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 &lt; 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]