From 056935eb90d77ce20a05fbb61ac5cdb035c94f2b Mon Sep 17 00:00:00 2001 From: Guilherme Ferratti Pejon Date: Sat, 31 Dec 2022 18:17:14 -0300 Subject: [PATCH] Add ruby example --- server/ruby/Gemfile | 4 ++++ server/ruby/README.md | 15 +++++++++++++ server/ruby/server.rb | 50 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 server/ruby/Gemfile create mode 100644 server/ruby/README.md create mode 100644 server/ruby/server.rb diff --git a/server/ruby/Gemfile b/server/ruby/Gemfile new file mode 100644 index 0000000..d132184 --- /dev/null +++ b/server/ruby/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +gem 'mercadopago-sdk' +gem 'sinatra' diff --git a/server/ruby/README.md b/server/ruby/README.md new file mode 100644 index 0000000..5d08717 --- /dev/null +++ b/server/ruby/README.md @@ -0,0 +1,15 @@ +# Checkout payment processing with Checkout Pro + +## Using a Ruby server with Sinatra + +### Requirements +- Ruby 2.3.0 or higher +- Read our [testing instructions](https://www.mercadopago.com/developers/en/guides/online-payments/checkout-pro/test-integration) +- Setup your credentials: + - `PUBLIC_KEY`and `LOCALE` on client-side `index.js` + - Private `YOUR_ACCESS_TOKEN` on server-side `server.rb` + +### How to run it +- `bundle` +- `ruby server.rb` +- Navigate to http://localhost:8080 on your browser diff --git a/server/ruby/server.rb b/server/ruby/server.rb new file mode 100644 index 0000000..485decb --- /dev/null +++ b/server/ruby/server.rb @@ -0,0 +1,50 @@ +require 'mercadopago' +require 'sinatra' + +ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN' +mercadopago = Mercadopago::SDK.new(ACCESS_TOKEN) + +post '/create_preference' do + payload = JSON.parse(request.body.read) + + preference = { + items: [{ + title: payload['title'], + unit_price: payload['price'].to_f, + quantity: payload['quantity'].to_f + }], + back_urls: { + success: 'localhost:8080/feedback', + failure: 'localhost:8080/feedback', + pending: 'localhost:8080/feedback' + }, + auto_return: 'approved' + } + + begin + response = mercadopago.preference.create(preference) + + { id: response[:response]['id'] }.to_json + rescue StandardError => e + puts e + end +end + +get '/feedback' do + params = request.params + { + Payment: params['payment_id'], + Status: params['status'], + MechantOrder: params['merchant_order_id'] + }.to_json +end + +get '/' do + send_file '../../client/index.html' +end + +set :public_folder, '../../client' +set :views, '../../client' +set :static, true +set :static_cache_control, [:public, { max_age: 0 }] +set :port, 8080