Ric*_*ndx 2 ruby paypal ruby-on-rails activemerchant
这个问题是关于:ActiveMerchant + PaypalExpressCheckout + Rails 3.2
我一直在尝试在我的Rails 3.2应用程序上构建Paypal Express Checkout.那里的大部分教程都已经过时,所以我按照了几篇然后阅读了Paypal Express Checkout集成指南.我已经设置了我的Sandobx和我的paypal信息.
当我尝试通过点击我的"立即购买"链接来处理付款时:
<%= link_to image_tag('http://img36.imageshack.us/img36/249/buttonpaypal.png'),
action: 'checkout', controller: 'orders'%>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
This transaction is invalid. Please return to the recipient's website to complete
you transaction using their regular checkout flow.
Return to merchant
At this time, we are unable to process your request. Please return to and try
another option.
Run Code Online (Sandbox Code Playgroud)
---我的控制器:
class OrdersController < ApplicationController
include ActiveMerchant::Billing
def checkout
setup_response = ::GATEWAY.setup_purchase(2000,
:ip => request.remote_ip,
:return_url => url_for('confirm'),
:cancel_return_url => url_for(root_path)
)
redirect_to ::GATEWAY.redirect_url_for(setup_response.token)
end
end
Run Code Online (Sandbox Code Playgroud)
---我的初始化器ActiveMerchant.rb:
ActiveMerchant::Billing::Base.mode = :test
::GATEWAY = ActiveMerchant::Billing::PaypalExpressGateway.new(
:login => "I_PUT_MY_EMAIL_HERE",
:password => "I_PUT_MY_PASS_HERE",
:signature => "I_PUT_MY_SIGNATURE_HERE",
:allow_guest_checkout => true
)
Run Code Online (Sandbox Code Playgroud)
---我的路线:routes.rb:
resources :orders do
# Im not sure why 'get :checkout' by itself doesn't work.
get :checkout, :on => :new
get :confirm
get :complete
end
Run Code Online (Sandbox Code Playgroud)
得到"页面/索引"
这是要点:https://gist.github.com/11be6cef6a97632343b9
任何人都可以指点我最近的教程或帮我弄清楚我在做错了什么?
最简单的方法是做如下:
1.)您必须创建一个paypal测试帐户.
2.)创建购物车型号:
$ rails g model Cart purchased_at:datetime
Run Code Online (Sandbox Code Playgroud)
3.)在您的购物车型号中:
class Cart < ActiveRecord::Base
def paypal_url(return_url)
values = {
# get it form your http://sandbox.paypal.com account
:business => 'ENTER_THE_SELLER_PAYPAL_EMAIL_ADDRESS',
:cmd => '_cart',
:upload => 1,
:return => return_url,
:invoice => id
}
# These values set up the details for the item on paypal.
values.merge!({
# The amount is in cents
"amount_1" => ENTER_AN_AMOUNT_HERE,
"item_name_1" => ENTER_THE_ITEM_NAME_HERE,
"item_number_1" => 1,
"quantity_1" => 1
})
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
end
end
Run Code Online (Sandbox Code Playgroud)
4.)在appllication_controller.rb文件中添加此项
def current_cart
session[:cart_id] ||= Cart.create!.id
@current_cart ||= Cart.find(session[:cart_id])
end
Run Code Online (Sandbox Code Playgroud)
5.)在您想要结帐按钮的视图上添加以下内容:
# 'products_url' is just the url where you would like to redirect
# the user after the transaction
<%= link_to 'Buy with PAYPAL', @cart.paypal_url(products_url) %>
Run Code Online (Sandbox Code Playgroud)
6.)在控制器上显示您想要结帐的视图的操作添加:
def show
...
@cart = current_cart
end
Run Code Online (Sandbox Code Playgroud)
而已!这是一个没有"真正"购物车的PaypalExpressCheckout,因为我在不使用订单项的情况下构建了此购物车.但你可以在Railscast#141 Paypal基础知识之后添加一个行项目http://railscasts.com/episodes/141-paypal-basics
| 归档时间: |
|
| 查看次数: |
4225 次 |
| 最近记录: |