Rails 5中没有令牌通过PayPal Express Checkout

Posted

技术标签:

【中文标题】Rails 5中没有令牌通过PayPal Express Checkout【英文标题】:No Token Passed PayPal Express Checkout in Rails 5 【发布时间】:2017-10-28 09:50:20 【问题描述】:

我正在制作一个带有 PayPal Express Checkout 功能的基本购物车。一切似乎都在工作,直到我点击结帐按钮,然后我得到一个空白页,上面写着“没有令牌通过”。在服务器日志中,似乎没有创建任何令牌,但我不确定为什么。我认为这很可能是创建快速结帐令牌的问题,但我真的不确定我做错了什么。 服务器日志如下所示:

Started GET "/express_checkout" for 10.240.0.204 at 2017-05-27 16:54:43 +0000
Cannot render console from 10.240.0.204! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by OrdersController#express_checkout as html
  Order Load (0.1ms)  SELECT  "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  OrderItem Load (0.1ms)  SELECT "order_items".* FROM "order_items" WHERE "order_items"."order_id" = ?  [["order_id", 1]]
  Product Load (0.2ms)  SELECT  "products".* FROM "products" WHERE "products"."active" = ? AND "products"."id" = ? LIMIT ?  [["active", true], ["id", 1], ["LIMIT", 1]]
Redirected to https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=
Completed 302 Found in 615ms (ActiveRecord: 0.5ms)

这是我的 Order.rb:

class Order < ApplicationRecord
  has_many :order_items
  before_save :update_subtotal

  def subtotal
    order_items.collect  |oi| oi.valid? ? (oi.quantity * oi.unit_price) : 0 .sum
  end

  def purchase
    response = EXPRESS_GATEWAY.purchase(order.total, express_purchase_options)
    order.update_attribute(:purchased_at, Time.now) if response.success?
    response.success?
  end

  def express_token=(token)
      self[:express_token]=token
      if new_record? && !token.blank?
        details = EXPRESS_GATEWAY.details_for(token)
        self.express_payer_id = details.payer_id
      end
  end 

private

  def update_subtotal
    self[:subtotal] = subtotal
  end

  def express_purchase_options
    
      :ip => ip,
      :token => express_token,
      :payer_id => express_payer_id
    
  end
end

我的orders_controller.rb:

class OrdersController < ApplicationController

    def express_checkout
        @order = current_order
        response = EXPRESS_GATEWAY.setup_purchase(@order.subtotal,
        ip: request.remote_ip,
        return_url: 'carts/show',
        cancel_return_url: 'carts/cancel',
        currency: "USD",
        allow_guest_checkout: true,
        items: [name: "Order", description: "Order description", quantity: "1", amount: @order.subtotal]
    )
        redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
    end

def new
    @order = Order.new(:express_token => params[:token])
end

def create
    @order = current_order.create!(order_params)
    @order.ip = request.remote_ip
    if @order.save
        if @order.purchase
            redirect_to order_url(@order)
        else
            render :action => "failure"
        end
    else
        render :action => 'new'
    end
end

private
def order_params
  params.require(:order).permit(:subtotal, :total, :ip, :express_token, :express_payer_id)
end
end

我的 application_controller.rb:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  helper_method :current_order

  def current_order
    if !session[:order_id].nil?
      Order.find(session[:order_id])
    else
      Order.new(order_params)
    end
  end

end

我的 development.rb 中的活跃商家代码:

config.after_initialize do
ActiveMerchant::Billing::Base.mode = :test
paypal_options = 
  login: "merchant_api1.christianorourke.com",
  password: ************,
  signature: ***************************

::EXPRESS_GATEWAY = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
  end

我的路线文件:

Rails.application.routes.draw do

  root "products#index"
  resources :products, only: [:index]
  resource :cart, only: [:show]
  resources :order_items, only: [:create, :update, :destroy]
  get 'express_checkout', to: 'orders#express_checkout'
  resources :orders do 
    collection do
      post 'express_checkout'
    end
  end
end

【问题讨论】:

你检查了吗***.com/questions/29417328/… @fabrizio-bertoglio 是的,但这只会从 10.240.0.204 中删除无法渲染控制台!并且没有修复我试图纠正的 No Token Passed 错误 你不应该在 *** 上分享你的密码,删除它们......如果它们是真实的...... @fabrizio-bertoglio 谢谢伙计,甚至没想过不以某种方式分享它们。幸运的是,它们只是用于沙盒帐户。 我不知道你从哪里得到令牌?从响应?路由是如何组织的? params 从哪里获取令牌? 【参考方案1】:

我刚刚找到这个链接,它解释了如何配置

贝宝快递

https://github.com/nov/paypal-express/wiki

paypal_options = 
  no_shipping: true, # if you want to disable shipping information
  allow_note: false, # if you want to disable notes
  pay_on_paypal: true # if you don't plan on showing your own confirmation step


request = Paypal::Express::Request.new(
  :username   => SET_YOUR_OWN,
  :password   => SET_YOUR_OWN,
  :signature  => SET_YOUR_OWN
)
payment_request = Paypal::Payment::Request.new(
  :currency_code => :JPY,   # if nil, PayPal use USD as default
  :description   => FOO,    # item description
  :quantity      => 1,      # item quantity
  :amount        => 1.00,   # item value
  :custom_fields => 
    CARTBORDERCOLOR: "C00000",
    LOGOIMG: "https://example.com/logo.png"
  
)
response = request.setup(
  payment_request,
  YOUR_SUCCESS_CALLBACK_URL,
  YOUR_CANCEL_CALLBACK_URL,
  paypal_options  # Optional
)
response.redirect_uri

然后你就有了 ActiveMerchant 配置,你可以找到向导听到:

https://duckduckgo.com/?q=rails+paypal+active+merchant+gem&atb=v52-6_b&ia=qa

How to integrate Paypal with Ruby on Rails

https://spin.atomicobject.com/2011/10/24/integrating-paypal-express-with-rails-3-1-part-1/

【讨论】:

@ChristianO'Rourke 祝你好运!

以上是关于Rails 5中没有令牌通过PayPal Express Checkout的主要内容,如果未能解决你的问题,请参考以下文章

不从 PayPal 上的 curl 请求接收令牌

paypal rest sdk - 取消付款只提供令牌

Axios Paypal 令牌问题

我想在 Rails 中实现 PayPal Digital Goods Express Checkout API

如何通过 iOS 中的 API 调用获取 PayPal 刷新令牌

Paypal 响应中的 Receipt_id 为空,我该如何获取?