gpt4 book ai didi

ruby-on-rails - Rails3 中的 Paypal Express Checkout

转载 作者:数据小太阳 更新时间:2023-10-29 08:17:40 25 4
gpt4 key购买 nike

本期是关于:ActiveMerchant + PaypalExpressCheckout + Rails 3.2

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

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

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

--- 我的 Controller :

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.) 在您想要结帐按钮的 View 中添加:

# '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.) 在您想要结帐的 View 的 Controller 显示操作中添加:

def show
...
@cart = current_cart
end

就是这样!这是一个没有“真正的”购物车的 PaypalExpressCheckout,因为我在没有使用订单项的情况下构建了这个购物车。但是您可以按照 Railscast #141 Paypal Basics http://railscasts.com/episodes/141-paypal-basics 向它添加一个订单项。

关于ruby-on-rails - Rails3 中的 Paypal Express Checkout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11317833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com