我正在尝试使用带有多个项目的PayPal ExpressCheckout按钮但没有成功。我正在使用NetBeans IDE,rails 4和MySQL db.Here是我到目前为止所做的:
In my production.rb file I have:
Rails.application.configure做 配置/ 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
我明白了:
任何帮助都将受到欢迎。谢谢!
答案 0 :(得分: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按钮的人有用。谢谢你的帮助!