我正在尝试使用 paypal-express gem 来实现 paypal express 付款。但是,我很困惑。
这是事务控制器中创建方法的代码
elsif params[:purchase][:payment_method] == "Paypal"
#paypal payment
request = Paypal::Express::Request.new(
:username => "xxxxxxxxxxxxxxxxx",
:password => "xxxxxxxxxxxxxxxx",
:signature => "xxxxxxxxxxxxxxxx"
)
payment_request = Paypal::Payment::Request.new(
:currency_code => "#{@current_user.currency}", # if nil, PayPal use USD as default
:description => "Purchase of SMS", # item description
:quantity => 1, # item quantity
:amount => "#{@total}", # item value
)
response = request.setup(
payment_request,
success_url,
cancel_url
)
transaction.tracking_id = response.token
transaction.save
response.redirect_uri
以及成功方法
def success
token = params[:token]
payer_id = params[:payer_id]
request = Paypal::Express::Request.new(
:username => "xxxxxxxxxxxxxxx",
:password => "xxxxxxxxxxxxxxx",
:signature => "xxxxxxxxxxxxxxx"
)
response = request.details(token)
transaction = Transaction.find_by_tracking_id(token)
transaction.merchant_reference = payer_id
transaction.status = "Completed"
transaction.save
user = User.find(transaction.user_id)
user.balance = (user.balance+response.amount.to_i)
user.save()
response = request.checkout!(
token,
payer_id,
payment_request
)
flash[:notice] = 'Payment Transaction Completed'
user_home_path(@current_user)
end
和取消方法
def cancel
flash[:warn] = 'Payment Request Canceled'
user_home_path(@current_user)
end
如何在 create 方法中发出正确的请求,获取令牌作为响应并将其存储,以便能够在成功方法中使用它来搜索特定事务。
DoExpressCheckoutPayment
另外,我怎样才能在成功方法中执行正确的方法。
先感谢您。