繁体   English   中英

路由错误自适应支付导轨

[英]Routing Error Adaptive Payments Rails

我已经竭尽所能,并且需要帮助(请!)。 我正在尝试为我的对等市场中的商品设置付款。 我认为我已经正确设置了所有内容,但是当我按下创建的“付款”按钮时,它不会重定向到PayPal,而是似乎只是刷新了项目页面。 我认为这是一个路由错误,但并非完全肯定。

这是我的Item模型中的内容:

   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 

该按钮的链接位于视图/项目/显示中:

<%= link_to "Buy Now with PayPal", @gateway, class: "btn btn-primary col-md-8", id: "paypal-buy-btn"%>

我也想知道错误是否可能是因为我没有在show动作中定义@gateway,但是当我尝试这样做时,我也会收到错误。

谁能帮我解决这个难题? 谢谢你的帮助。

除了我的评论外,这里还有其他几项内容,如果实际没有帮助,我将编辑此答案或稍后将其删除。 只需回答即可,而不是在小小的注释框中输入内容。

首先,您的视图会刷新,因为@gatewaynil ,因此您正在链接到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 ,就像在show中一样:

@item = Item.find(params[:id])

设置网关时,您需要将项目传递到取消URL,以便它返回到该特定项目,因此请将其更改为:

item_url(@item)

并将您的收件人更改为@item.recipients ,因为您已经在模型上定义了该方法。

让我知道事情的后续!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM