2011年9月5日月曜日

Mac ports からhomebrewへ移行

homebrewはmac portsと競合するらしいので、先にmac portsをアンインストールする。

へんなところでつまずくと嫌なので、念のためmac portsでインストールしたものをテキストファイルで保存
#port installed

アンインストール
# sudo port -f uninstall installed
# sudo rm -rf /opt/local/


homebrewのインストール
#ruby -e "$(curl -fsSL https://gist.github.com/raw/323731/install_homebrew.rb)"

homebrewはgitで更新するらしいのでgitをインストール
#brew install git

homebrewのアップデート
#brew update

あとは個別に必要なものをインストール

2011年9月4日日曜日

railsでpaypalを使う方法

rails2.3.5の場合です。
ライブラリが用意してあるので、基本的に簡単なのですが、ちょっとはまったので。

まずはアカウントを用意。
sandboxに登録する。
「Create a preconfigured account」で、BuyerとSellerのアカウントをひとつずつ作る。
Buyerの方には残高を入れておくことを忘れないように。

次にsandbox test siteにsellerのアカウントでログイン。
ProfileからAPI Accessで「API Username」「API Password」「Signature」を取得。
これで準備は完了です。

ライブラリのインストール。
#sudo gem install activemerchant -v 1.10
わざわざ1.10にしているのは、それ以上だとrails2.3.11以上にしか対応していないから。

#app/controller/payments_controller.rb

class PaymentsController < ApplicationController
include ActiveMerchant::Billing
def index

end

def confirm
redirect_to :action => 'index' unless params[:token]

details_response = gateway.details_for(params[:token])

if !details_response.success?
@message = details_response.message
render :action => 'error'
return
end

@address = details_response.address
end

def complete


purchase = gateway.purchase(100,
:ip => request.remote_ip,
:payer_id => params[:payer_id],
:token => params[:token]
)

if !purchase.success?
@message = purchase.message
render :action => 'error'
return
end
end


def checkout
setup_response = gateway.setup_purchase(100,
:ip => request.remote_ip,
:return_url => url_for(:action => 'confirm', :only_path => false),
:cancel_return_url => url_for(:action => 'index', :only_path => false)
)
redirect_to gateway.redirect_url_for(setup_response.token)
end

private
def gateway
@gateway ||= PaypalExpressGateway.new(
:login => 'API Username',
:password => 'API Password',
:signature => 'Signature'
)
end

end


#app/paments/index.html.erb


Purchase Sprockets


Thank you for your decision to purchase our sprockets.


Your order total is 100円



<%= link_to image_tag('https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif'), :action => 'checkout' %>




#app/paments/confirm.html.erb

Please Confirm Your Payment Details












Name<%= @address['name'] %>
Company<%= @address['company'] %>
Address 1<%= @address['address1'] %>
Address 2<%= @address['address2'] %>
City<%= @address['city'] %>
State<%= @address['state'] %>
Country<%= @address['country'] %>
Zip<%= @address['zip'] %>


<% form_tag :action => 'complete', :token => params[:token], :payer_id => params[:PayerID] do %>
<%= submit_tag 'Complete Payment' %>
<% end %>


#app/paments/confirm.html.erb

Thank you for your purchase



#app/paments/confirm.html.erb

Error


Unfortunately an error occurred: <%= @message %>


The confirm.html.erb templates displays the customer's PayPal address and asks them if they want to complete the payment. Displaying the review page represents Integration Point 3a from the Integration guide. This is the customer's opportunity to review the details of their order, change shipping methods, etc. It looks like this:


#config/enviroment.rb
config.gem "activemerchant"
を追記

#config/enviroments/development.rb
config.after_initialize do
ActiveMerchant::Billing::Base.mode = :test
ActiveMerchant::Billing::PaypalExpressGateway.default_currency = "JPY"
end
を追記

これでpaypalのテストサイトで課金のテストができます。
本番サーバでやるなら、本番サーバ用にアカウントを用意して、
#config/enviroments/production.rb
config.after_initialize do
ActiveMerchant::Billing::Base.mode = :production
ActiveMerchant::Billing::PaypalExpressGateway.default_currency = "JPY"
end

で、行けるはず。
試してませんが。。。

テスト環境と本番環境でかなり動作が違うらしい。
日本じゃpaypalアカウント持っている人少ないから、クレジットカードの欄を優先させたいんだけど、どうやるんだろう?
本番環境なら優先されるのかな?

あと、定期的な支払い請求もできるらしいけど、それもどれかわからず・・・