簡體   English   中英

PayPal Express Checkout 與 Rails 中活躍商家的多個項目

[英]PayPal Express Checkout with multiple items with active merchant in rails

我正在嘗試對多個項目使用 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

我得到:

在此處輸入圖片說明

任何幫助都將受到歡迎。 謝謝!

我非常非常仔細地紅色這個帖子

在 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

有效。 我希望我的帖子對想要集成快速結賬按鈕的人有用。 謝謝你的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM