Rails3 中的 Paypal Express 结帐

Posted

技术标签:

【中文标题】Rails3 中的 Paypal Express 结帐【英文标题】:Paypal Express Checkout in Rails3 【发布时间】:2012-07-04 07:02:36 【问题描述】:

这个问题是关于: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

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

【问题讨论】:

【参考方案1】:

最简单的方法如下:

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 Basics http://railscasts.com/episodes/141-paypal-basics987654321@

之后添加一个 Line Item

【讨论】:

刚刚试过这段代码,它可以工作。虽然“# The amount is in cents”实际上是美元而不是美分。 @Richardsondx :我只需要使用模型方法,效果很好。我怎样才能得到贝宝的回复? @s-s-r:你能澄清你的问题吗?什么模型方法?你打什么电话?将您的代码制作成一个过去的bin并在此处分享链接。 @Richardsondx:哦!我的错。我把你的模型方法用于贝宝结帐。这对我来说是模型方法:D。和你一样,我正在发送请求。我怎样才能得到贝宝订单号的回复? 我认为答案是直接 paypal 方法而不是快速结帐。【参考方案2】:

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

【讨论】:

以上是关于Rails3 中的 Paypal Express 结帐的主要内容,如果未能解决你的问题,请参考以下文章

更改 PayPal Express Checkout 插件中的 paypal 项目名称

删除 PayPal Express Checkout 中的送货地址选项

PHP 支付中的 Paypal API Express Checkout 错误代码 13113

如何将订单详细信息传递到 magento 中的 paypal-express-checkout?

如何使用 Vue js 中的 paypal/stripe 等付款方式收费?我需要像express这样的服务器吗?

Rails:Localhost 中的 PayPal Express Checkout 集成