ruby-on-rails - 如何在 rails 中通过 paypal 智能按钮集成实现 Paypal 支付
问题描述
我通过使用SmartButtons并在服务器端创建订单,在我的 rails 应用程序中实现了 PayPal 结帐 API 。
我使用了payouts-ruby-sdk gem,我的代码如下:-
index.html.erb
<!-- Set up a container element for the button -->
<div id="paypal-button-container"></div>
<!-- Include the PayPal JavaScript SDK -->
<script src="https://www.paypal.com/sdk/js?client-id=xyz¤cy=USD"></script>
<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Call your server to set up the transaction
createOrder: function(data, actions) {
return fetch('/orders', {
method: 'post'
}).then(function(res) {
return res.json();
}).then(function(orderData) {
return orderData.orderID;
});
},
// Call your server to finalize the transaction
onApprove: function(data, actions) {
return fetch('/orders/' + data.orderID + '/capture', {
method: 'post'
}).then(function(res) {
return res.json();
}).then(function(orderData) {
// Three cases to handle:
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
// (2) Other non-recoverable errors -> Show a failure message
// (3) Successful transaction -> Show a success / thank you message
// Your server defines the structure of 'orderData', which may differ
var errorDetail = Array.isArray(orderData.details) && orderData.details[0];
if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
// Recoverable state, see: "Handle Funding Failures"
// https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
return actions.restart();
}
if (errorDetail) {
var msg = 'Sorry, your transaction could not be processed.';
if (errorDetail.description) msg += '\n\n' + errorDetail.description;
if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
// Show a failure message
return alert(msg);
}
// Show a success message to the buyer
alert('Transaction completed');
});
}
}).render('#paypal-button-container');
</script>
订单控制器.rb
class OrdersController < ApplicationController
skip_before_action :verify_authenticity_token
def index
end
def create
# Creating Access Token for Sandbox
client_id = 'xyz'
client_secret = 'abc'
# Creating an environment
environment = PayPal::SandboxEnvironment.new(client_id, client_secret)
client = PayPal::PayPalHttpClient.new(environment)
request = PayPalCheckoutSdk::Orders::OrdersCreateRequest::new
request.request_body({
intent: "CAPTURE",
purchase_units: [
{
amount: {
currency_code: "USD",
value: "10.00"
}
}
]
})
begin
# Call API with your client and get a response for your call
# debugger
response = client.execute(request)
puts response.result.id
render json: {success: true, orderID: response.result.id}
rescue PayPalHttp::HttpError => ioe
# Something went wrong server-side
puts ioe.status_code
puts ioe.headers["debug_id"]
end
end
def execute_payment
client_id = 'xyz'
client_secret = 'abc'
# Creating an environment
environment = PayPal::SandboxEnvironment.new(client_id, client_secret)
client = PayPal::PayPalHttpClient.new(environment)
request = PayPalCheckoutSdk::Orders::OrdersCaptureRequest::new(session[:orderID])
begin
# Call API with your client and get a response for your call
response = client.execute(request)
# If call returns body in response, you can get the deserialized version from the result attribute of the response
order = response.result
puts order
rescue PayPalHttp::HttpError => ioe
# Something went wrong server-side
puts ioe.status_code
puts ioe.headers["debug_id"]
end
end
end
现在我想实现 Paypal 的 Payouts API,我知道paypal-ruby-sdk可用于它,但我很困惑在哪里安装此代码以及如何将其与前端集成。有任何想法吗?提前致谢 :)
解决方案
您上面的代码是 Checkout 代码,用于前端 (JavaScript) 和后端 (Ruby)。
付款与 Checkout 无关,无论是前端 Checkout 还是后端 Checkout。
支付是严格的后端 API 操作,您可以在其中将资金从您的帐户发送到另一个帐户。
付款不会连接到任何前端 UI。如果需要,您可以构建自己的 UI 来触发付款。大概您知道要从您的帐户向谁汇款,以及应该触发此操作的流程。
推荐阅读
- html - 如何配置 CentOS-vServer 以支持在端口 8080 上运行 Spring Boot 的域名上的 REST 调用
- mysql - 您如何检查并检查组是否包含 2 个或更多给定值?
- linux - 监听端口并将 https 解密到标准输出
- java - 如何在 oracle 存储过程中返回自己的类型数据?
- fortran - 对大整数求幂时得到错误的结果
- java - Spring批处理类加载器冲突问题
- swift - 如果自定义搜索栏中的字符串在 swift 中不匹配,想要在 tableview 中显示消息
- c++ - 基于范围的 for 和其他增量
- reactjs - Wrapper Material-UI Components 将类名更改为“jss”
- object - 如何为自定义对象列表声明嵌套字段类型?