我正在整合Paypal Express付款。我正在改编标准结帐,并且正在使用主动商人级商品。设置应类似于response=gateway.setup_purchase(price, details)
,其中详细信息是PayPal所需的所有参数。快速网关需要一个return_url和cancel_return_url。
当我尝试通过“提交”按钮执行付款时,我得到:
Order xx failed with error message undefined method `checkout_thank_you_url' for #<Order:0x1081e1bb8>
在我的订单模型中,我包括以下部分:
#app/models/order.rb
def process
process_with_active_merchant
save!
self.status == 'processed'
end
private
def process_with_active_merchant
ActiveMerchant::Billing::Base.mode = :test
gateway = ActiveMerchant::Billing::PaypalExpressGateway.new(
:login => 'sandbox-account',
:password => 'sandbox-password',
:signature => "sandbox-secret"
params = {
:order_id => self.id,
:email => email,
:address => { :address1 => ship_to_address,
:city => ship_to_city,
:country => ship_to_country,
:zip => ship_to_postal_code
} ,
:description => 'Books',
:ip => customer_ip,
:return_url => checkout_thank_you_url(@order), # return here if payment success
:cancel_return_url => checkout_index_url(@order) # return here if payment failed
}
response = gateway.setup_purchase((@order.total * 100).round(2), params)
if response.success?
self.status = 'processed'
else
self.error_message = response.message
self.status = 'failed'
end
end
该方法在结帐控制器中调用
def place_order
@order = Order.new(params[:order])
@order.customer_ip = request.remote_ip
populate_order
...
@order.process
...
end
def thank_you
...
end
我该如何使用它?预先谢谢你!
我想我必须指定控制器和动作,但是在使用时:
:return_url => url_for(:controller => :checkout, :action => "thank_you")
我得到:
Order 98 failed with error message undefined method `url_for' for #<Order:0x1065471d8>
答案 0 :(得分:1)
在Oder模型中,包括Rails用来生成URL的模块。
在您的订单类定义中添加以下代码:
class Order
include Rails.application.routes.url_helpers
# ...
end
这些帮助器已经包含在Controller类中,因此默认情况下,控制器中(例如视图模板)可以使用checkout_thank_you_url
之类的方法。
您必须将该模块包含在要使用路由方法的任何其他类中。