2

本期内容是:ActiveMerchant + PaypalExpressCheckout + Rails 3.2

我一直在尝试在我的 Rails 3.2 应用程序上构建 Paypal Express Checkout。那里的大多数教程都已经过时了,所以我遵循了一些然后阅读了 Paypal Express Checkout 集成指南。我已经设置了我的 Sandobx 和我的贝宝信息。

当我尝试通过单击我的视图中的“立即购买”链接来处理付款时:

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

获取“页面/索引”

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

谁能给我指出一个“最近的”教程或帮我弄清楚我在这里做错了什么?

4

2 回答 2

6

最简单的方法是执行以下操作:

1.) 您必须创建一个贝宝测试帐户。

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 Basics http://railscasts.com/episodes/141-paypal-basics添加一个 Line Item

于 2012-07-05T20:46:09.100 回答
4

这里有一个最近的教程:http: //spin.atomicobject.com/2011/10/24/integrating-paypal-express-with-rails-3-1-part-1/

于 2012-07-04T05:21:09.217 回答