Skip to content

Commit

Permalink
Add ruby example
Browse files Browse the repository at this point in the history
  • Loading branch information
guilpejon committed Dec 31, 2022
1 parent 915c8a1 commit 6b32399
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions server/ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

gem 'mercadopago-sdk'
gem 'sinatra'
15 changes: 15 additions & 0 deletions server/ruby/README.md
Original file line number Diff line number Diff line change
@@ -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 `ACCESS_TOKEN` on server-side `server.rb`

### How to run it
- `bundle`
- `ruby server.rb`
- Navigate to http://localhost:8080 on your browser
50 changes: 50 additions & 0 deletions server/ruby/server.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6b32399

Please sign in to comment.