簡體   English   中英

在Rails3中的Paypal快速結賬

[英]Paypal Express Checkout in Rails3

這個問題是關於: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

得到“頁面/索引”

這是要點: https//gist.github.com/11be6cef6a97632343b9

任何人都可以指點我最近的教程或幫我弄清楚我在做錯了什么?

最簡單的方法是做如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM