其中一种方法是编写一个模块或一个包装以下步骤的类。确保您传递了正确的值并且总数加起来。
1.准备网关
paypal_express_params = {
login: ENV['PAYPAL_LOGIN'],
password: ENV['PAYPAL_PASSWORD'],
signature: ENV['PAYPAL_SIGNATURE']
}
gateway = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_express_params)
2。设置购买
setup_hash = {
ip: '170.170.1.1',
items: [{name: 'sample', quantity: 1, amount: 100_00, description: 'desc'}],
subtotal: 10000,
shipping: 0,
handling: 0,
tax: 0,
currency: 'USD',
return_url: 'http://localhost:3000/success',
cancel_return_url: 'http://localhost:3000/failure',
allow_guest_checkout: true
}
response = gateway.setup_purchase(100_00, setup_hash) # 100 USD
3.生成 PayPal URL
url = gateway.redirect_url_for(response.token)
# => https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=XXXXXXXXXXXXXX
之后,您只需在用户完成购买后从 PayPal 获取数据。
有几种方法可以做到这一点,但重要的是:
- 先让它工作
- 确保您的 Rails 代码中没有硬编码密钥和机密
- 将代码包装在类或模块中以进行清理
- 同时阅读 PayPal 文档(不仅仅是 ActiveMerchant)