【发布时间】: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
我明白了:
我们非常欢迎任何帮助。谢谢!
【问题讨论】:
标签: ruby-on-rails paypal express-checkout activemerchant