Я использую рельсы 5 с ruby 2.3 и gem 'paypal-sdk-rest'
код на мой взгляд выглядит так:
<script>
paypal.Button.render({
env: 'sandbox', // sandbox | production
// Show the buyer a 'Pay Now' button in the checkout flow
commit: true,
// payment() is called when the button is clicked
payment: function() {
// Set up a url on your server to create the payment
var CREATE_URL = '/payment/create/';
// Make a call to your server to set up the payment
return paypal.request.post(CREATE_URL)
.then(function(res) {
return res.paymentID;
});
},
// onAuthorize() is called when the buyer approves the payment
onAuthorize: function(data, actions) {
// Set up a url on your server to execute the payment
var EXECUTE_URL = '/payment/execute/';
// Set up the data you need to pass to your server
var data = {
paymentID: data.paymentID,
payerID: data.payerID
};
// Make a call to your server to execute the payment
return paypal.request.post(EXECUTE_URL, data)
.then(function (res) {
window.alert('Payment Complete!');
});
}
}, '#paypal-button-container');
</script>
Мой код на стороне сервера для PayPal:
require 'paypal-sdk-rest'
include PayPal::SDK::REST
include PayPal::SDK::Core::Logging
class PaymentsController < ApplicationController
skip_before_action :verify_authenticity_token
def create
# Build Payment object
byebug
@payment = Payment.new({
:intent => "sale",
:payer => {
:payment_method => "paypal"},
:redirect_urls => {
:return_url => "http://localhost:3000/payment/execute",
:cancel_url => "http://localhost:3000/"},
:transactions => [{
:item_list => {
:items => [{
:name => "item",
:sku => "item",
:price => ".1",
:currency => "USD",
:quantity => 1}]},
:amount => {
:total => ".1",
:currency => "USD",:details => {"subtotal": "126", "shipping": "0", "tax": "0"}},
:description => "This is the payment transaction description."}]})
if @payment.create
@payment.id # Payment Id
logger.info "Payment[#{@payment.id}]"
logger.info "Redirect: #{@redirect_url}"
render json: {success: true, paymentID: @payment.id}
else
logger.error @payment.error.inspect
@payment.error # Error Hash
render json: {success: false}
end
end
def execute
# byebug
payment = PayPal::SDK::REST::Payment.find(params[:paymentID])
if payment.execute(payer_id: params[:payerID])
render json: {msg: 'Payment Complete'}
else
render json: {msg: payment.error}
end
end
end
config / paypal.yml имеет вид:
test: &default
# Credentials for REST APIs
client_id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
client_secret: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# Mode can be 'live' or 'sandbox'
mode: sandbox
# Credentials for Classic APIs
app_id: APP-80W284485P519543T
username: jb-us-seller_api1.paypal.com
password: WX4WTU3S8MY44S7F
signature: AFcWxV21C7fd0v3bYYYRCpSSRl31A7yDhhsPUU2XhtMoZXsWHFxu-RWy
# # With Certificate
# cert_path: "config/cert_key.pem"
sandbox_email_address: XXXXX-facilitator@XXXXXX.com
# # IP Address
# ip_address: 127.0.0.1
# # HTTP Proxy
# http_proxy: http://proxy-ipaddress:3129/
development:
<<: *default
production:
<<: *default
mode: live
Теперь, пожалуйста, предоставьте процедуру / метод, с помощью которого я передаю аргумент серверу для представления на стороне клиента.