PayPal Express Checkout 与多件商品在 Rails 中的活跃商家

Posted

技术标签:

【中文标题】PayPal Express Checkout 与多件商品在 Rails 中的活跃商家【英文标题】:PayPal Express Checkout with multiple items with active merchant in rails 【发布时间】:2016-01-23 21:11:19 【问题描述】:

我正在尝试对多个项目使用 PayPal ExpressCheckout 按钮,但没有成功。我正在使用 NetBeans IDE、rails 4 和 mysql db。这是我到目前为止所做的:

In my production.rb file I have:

Rails.application.configure 做 config/application.rb.

config.after_initialize do
    ActiveMerchant::Billing::Base.mode = :test
    paypal_options = 
        :login => "xxxx",  
        :password => "xxxx ", 
        :signature => "xxxx "       
    

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

In my transaction.rb model I have:

def valid_purchase

if express_token.blank?
    standard_purchase
    else
    express_token
 end


  def express_purchase

  #    price_in_cents = total
  response = EXPRESS_GATEWAY.purchase(total, express_purchase_options)

  if response.success?
    self.status = "processed"
    else
    errors.add(:transactions, "---- #response.message.")  
  end
    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
    self.ship_to_first_name = details.params["first_name"]
    self.ship_to_last_name = details.params["last_name"]

    end
    end



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

And in my transaction_controller.rb I have:

  def express_checkout
    order_items =[]
    postage_rate=nil
    item = Hash.new
    @order = Order.find(session[:order_id])
    @receipts = Receipt.where(:order_id=>@order.id)
    @receipts.each do |r|
    postage_rate = r.postage_rate * 100
    end

    @cart = Cart.find(@order.cart_id)
    @cart.cart_items.each do |i|
        @product = Product.find(i.product_id)

        item = 
            name: @product.product_name,
            quantity: i.amount,
            description: "ORDER_ID: #@order.id",
            amount: @product.selling_price * 100 ,
            shipping: postage_rate/@cart.cart_items.size
        
        order_items << item
     end
    price_in_cents = (@order.total_p_pr * 100).round(2)

    options = 
        :ip  => request.remote_ip,
        :return_url        => url_for(:action=>:new, :only_path => false),
        :cancel_return_url => catalogs_traders_url,
        :currency => "USD",
        :allow_guest_checkout=> true,
        :items => order_items  # this line outputs: [:name=>"owl potty", :quantity=>1, :description=>"ORDER_ID: 249",  :amount=>2808.0, :shipping=>332.0,     :name=>"a bag", :quantity=>1, :description=>"ORDER_ID: 249", :amount=>1260.0, :shipping=>332.0, :name=>"bracelet", :quantity=>1, :description=>"ORDER_ID: 249", :amount=>120.0, :shipping=>332.0, :name=>"beautiful woman", :quantity=>1, :description=>"ORDER_ID: 249", :amount=>74352.0, :shipping=>332.0]

    

    #passing the cost of the order
    response = EXPRESS_GATEWAY.setup_purchase(price_in_cents,options )
    redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
  end

   def new

    @transaction = Transaction.new(:express_token => params[:token])

   end

我明白了:

我们非常欢迎任何帮助。谢谢!

【问题讨论】:

【参考方案1】:

我非常非常小心地红了这个帖子

setting tax amount in Active Merchant / PayPal Express Checkout 我明白我的错误。这是我更正后的 transaction_controller:

# to redirect to PayPay site
def express_checkout
  pr = nil
  tp = nil
  items =[] 
  postage_r=[]
  total_p = []

    order_items =[]
    postage_rate=nil
    item = Hash.new
    @order = Order.find(session[:order_id])
    @receipts = Receipt.where(:order_id=>@order.id)
    @receipts.each do |r|
        total_p << r.total_price
        postage_r << r.postage_rate

    end
    tp = total_p.inject|sum,x| sum + x 
    pr = postage_r.inject|sum,x| sum + x 
    @cart = Cart.find(@order.cart_id)
    @cart.cart_items.each do |i|
    @product = Product.find(i.product_id)

        item = 
            name: @product.product_name,
            quantity: i.amount,
            description: "ORDER_ID: #@order.id",
            amount: @product.selling_price * 100 ,

        
        order_items << item
    end


    price_in_cents = (@order.total_p_pr * 100).round(2)

    options = 
      :subtotal => tp * 100,
       :shipping => pr * 100,
       :handling => 0,
       :tax => 0,
       :ip  => request.remote_ip,
        :return_url        => url_for(:action=>:new, :only_path => false),
        :cancel_return_url => catalogs_traders_url,
        :currency => "USD",
        :allow_guest_checkout=> true,
        :items => order_items
    

    #passing the cost of the order
    response = EXPRESS_GATEWAY.setup_purchase(price_in_cents,options )

    redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
end

成功了。我希望我的帖子对想要集成 Express Checkout 按钮的人有用。谢谢你的帮助!

【讨论】:

以上是关于PayPal Express Checkout 与多件商品在 Rails 中的活跃商家的主要内容,如果未能解决你的问题,请参考以下文章

如何将 PayPal Express Checkout 与 ASP.NET 网页站点(“Razor”)集成

Paypal Express Checkout:应用订单折扣

PayPal Rest API Express Checkout 工作流程

Paypal Sandbox (express-checkout) 返回内部错误

php 更改YITH PayPal Express Checkout for WooCommerce的Set Express Checkout Request参数

PayPal Express Checkout 通行证 transactionID