gpt4 book ai didi

ruby-on-rails - 使用 Ruby on Rails form_for 和 Controller /方法绑定(bind) PayPal 智能按钮?

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:56 26 4
gpt4 key购买 nike

我有智能按钮在沙盒中“工作”,但我想不出任何方法将智能按钮成功附加到创建订单的订单。对于 Stripe Elements,它非常即插即用,因为它在页面上并且是表单本身的一部分,但是对于带有重定向的 PayPal,我似乎想不出办法。

这是否需要 javascript,或者我可以在没有它的情况下执行此操作吗?

表格:

<%= form_for(@order, url: listing_orders_path([@listing, @listing_video]), html: {id: "payment_form-4"} ) do |form| %>

<%= form.label :name, "Your Name", class: "form-label" %>
<%= form.text_field :name, class: "form-control", required: true, placeholder: "John" %>
#stripe code here (not important)
<%= form.submit %>
<div id="paypal-button-container"></div>

<!-- Include the PayPal JavaScript SDK -->
<script src="https://www.paypal.com/sdk/js?client-id=sb&currency=USD"></script>

<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({

// Set up the transaction
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: <%= @listing.listing_video.price %>
}
}]
});
},

// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
// Show a success message to the buyer
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}


}).render('#paypal-button-container');
</script>

在 Controller 中创建方法:

require 'paypal-checkout-sdk'
client_id = Rails.application.credentials[Rails.env.to_sym].dig(:paypal, :client_id)
client_secret = Rails.application.credentials[Rails.env.to_sym].dig(:paypal, :client_secret)
# Creating an environment
environment = PayPal::SandboxEnvironment.new(client_id, client_secret)
client = PayPal::PayPalHttpClient.new(environment)

@amount_paypal = (@listing.listing_video.price || @listing.listing_tweet.price)
request = PayPalCheckoutSdk::Orders::OrdersCreateRequest::new
request.request_body(
{
intent: 'AUTHORIZE',
purchase_units: [
{
amount: {
currency_code: 'USD',
value: "#{@amount_paypal}"
}
}
]
}
)

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
@order.paypal_authorization_token = response.id
rescue BraintreeHttp::HttpError => ioe
# Something went wrong server-side
puts ioe.status_code
puts ioe.headers['debug_id']
end

我如何将 PayPal 智能按钮与表单绑定(bind),以便在付款完成后创建订单(如果成功)?

更新::::::::

创建了一个 PaypalPayments Controller 和模型:

Controller :

  def create
@paypal_payment = PaypalPayment.new
@listing = Listing.find_by(params[:listing_id])

require 'paypal-checkout-sdk'
client_id = "#{Rails.application.credentials[Rails.env.to_sym].dig(:paypal, :client_id)}"
client_secret = "#{Rails.application.credentials[Rails.env.to_sym].dig(:paypal, :client_secret)}"
# Creating an environment
environment = PayPal::SandboxEnvironment.new(client_id, client_secret)
client = PayPal::PayPalHttpClient.new(environment)

@amount_paypal = @listing.listing_video.price
request = PayPalCheckoutSdk::Orders::OrdersCreateRequest::new
@paypal_payment = request.request_body({
intent: "AUTHORIZE",
purchase_units: [
{
amount: {
currency_code: "USD",
value: "#{@amount_paypal}"
}
}
]
})

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
# @order.paypal_authorization_token = response.id
rescue BraintreeHttp::HttpError => ioe
# Something went wrong server-side
puts ioe.status_code
puts ioe.headers["debug_id"]
end

# if @paypal_payment.create
# render json: {success: true}
# else
# render json: {success: false}
# end

end

Javascript 在 View 中:

paypal.Buttons({

createOrder: function() {
return fetch('/paypal_payments', {
method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.orderID;
});
},



onApprove: function(data) {
return fetch('/orders', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
}).then(function(res) {
return res.json();
}).then(function(details) {
alert('Authorization created for ' + details.payer_given_name);
});
},


}).render('#paypal-button-container');

有了这个,paypal 框出现了,但在它加载到 CMD 中后立即消失了:

#<OpenStruct id="1Pxxxxxxx394U", links=[#<OpenStruct href="https://api.sandbox.paypal.com/v2/checkout/orders/1P0xxxxxxx394U", rel="self", method="GET">, #<OpenStruct href="https://www.sandbox.paypal.com/checkoutnow?token=1P07xxxxxxx94U", rel="approve", method="GET">, #<OpenStruct href="https://api.sandbox.paypal.com/v2/checkout/orders/1Pxxxxxxx4U", rel="update", method="PATCH">, #<OpenStruct href="https://api.sandbox.paypal.com/v2/checkout/orders/1P07xxxxxxx394U/authorize", rel="authorize", method="POST">], status="CREATED">
No template found for PaypalPaymentsController#create, rendering head :no_content
Completed 204 No Content in 2335ms (ActiveRecord: 15.8ms)

最佳答案

我没有使用过智能按钮。但是,您不应该在创建操作中有“大量代码”。如果您遵循 MVC 和 rails 约定。看起来您需要一个单独的 Controller 操作来处理与创建操作分开的支付授权。但是如果你能在你的 javascript 中做到这一点,这里是你如何将数据从 paypal javascript 发送回你的 Controller 的例子,这将需要一些工作,但希望它能为你指明正确的方向:

// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
// Show a success message to the buyer
alert('Transaction completed by ' + details.payer.name.given_name + '!');
// here is where you should send info to your controller action via ajax.
$.ajax({
type: "POST",
url: "/orders",
data: data,
success: function(data) {
alert(data); // or whatever you wanna do here
return false;
},
error: function(data) {
alert(data); // or something else
return false;
}
});
});
}

关于ruby-on-rails - 使用 Ruby on Rails form_for 和 Controller /方法绑定(bind) PayPal 智能按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55663445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com