我已经尽我所能,需要帮助(请!)。我正在尝试为我的点对点市场中的项目设置付款。我想我已经正确地设置了一切,但当我按下我创建的支付按钮时,它不会重定向到PayPal,相反,它似乎只是刷新项目页面。我认为这是路由错误,但我并不完全肯定。
以下是我的项目模型中的内容:
class Item < ActiveRecord::Base
belongs_to :user
belongs_to :category
default_scope -> { order('created_at DESC') }
def recipients
[
{ email: '<biz email address>', amount: '1.00', primary: false },
{ email: 'item.user.booth.paypal_email', amount: '5.00', primary: true }
]
end
end在项目控制器中:
class ItemsController < ApplicationController
include ActiveMerchant::Billing::Integrations
before_action :logged_in_user, only: [:edit, :update, :new, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
def show
@item = Item.find(params[:id])
end
def gateway
@gateway ||= ActiveMerchant::Billing::PaypalAdaptivePayment.new \
login: '<login email>',
password: '<password>',
signature: '<signature>',
appid: 'APP-80W284485P519543T'
response = gateway.setup_purchase \
return_url: root_url,
cancel_url: item_path,
#ipn_notification_url: <notification URL>,
receiver_list: recipients
redirect_to gateway.redirect_url_for(response['payKey'])
end
end 该按钮的链接位于views/items/show中:
<%= link_to "Buy Now with PayPal", @gateway, class: "btn btn-primary col-md-8", id: "paypal-buy-btn"%>我还想知道错误是否可能是因为我没有在show操作中定义@gateway,但当我尝试这样做时,我也得到了错误。
有人能帮我解开这个难题吗?谢谢你的帮助。
发布于 2015-01-30 03:01:33
除了我的评论之外,这里还有几件事,我将编辑这个答案,或者稍后删除它,如果它实际上没有帮助。只需要回答,而不是在小评论框中键入。
首先,您的视图会刷新,因为@gateway为nil,所以您将链接到nil,它什么也不做。您需要为gateway方法定义一个路由,如下所示:
resources :items do
post :gateway, on: :member
end现在您已经定义了gateway_item_path(item),所以您将改为链接到它:
<%= link_to "Buy Now with PayPal", gateway_item_path(@item), method: :post, ... %>现在,每当您单击该链接时,您的gateway方法都会被击中。在网关方法中,您需要找到Item,如图所示:
@item = Item.find(params[:id])在设置网关时,您需要将项目传递给取消url,以便返回到该特定项目,因此将其更改为:
item_url(@item)
并将您的收件人更改为@item.recipients,因为您已经在模型上定义了该方法。
让我知道它进行得怎么样!
https://stackoverflow.com/questions/28031925
复制相似问题