在Rails3中的Paypal快速结账

时间:2012-07-03 19:29:00

标签: ruby-on-rails ruby paypal 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'%>

我收到以下错误:

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.

---我的控制器:

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

---我的初始化器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
 )

---我的路线: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

获取“pages / index”

这是要点:https://gist.github.com/11be6cef6a97632343b9

有人能指点我最近的教程还是帮我弄清楚我在做错了什么?

2 个答案:

答案 0 :(得分:6)

最简单的方法是按照以下步骤操作:

1。)您必须创建一个paypal测试帐户。

2.)创建购物车型号:

$ rails g model Cart purchased_at:datetime

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

4.。)在appllication_controller.rb文件中添加此

  def current_cart
     session[:cart_id] ||= Cart.create!.id
     @current_cart ||= Cart.find(session[:cart_id])
   end

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) %>

6。)在控制器上显示你想要结账的视图的动作添加:

def show
  ...
  @cart = current_cart
end

多数民众赞成!这是一个没有“真正”购物车的PaypalExpressCheckout,因为我在不使用订单项的情况下构建了此购物车。但您可以在Railscast#141 Paypal基础http://railscasts.com/episodes/141-paypal-basics

之后添加一个行项目

答案 1 :(得分:4)