Skip to content

bonitour/braspag-rest

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BraspagRest

Build Status Maintainability Test Coverage

Gem to use Braspag gateway in his REST version.

Installation

Add this line to your application's Gemfile:

gem 'braspag-rest'

And then execute:

$ bundle

Or install it yourself as:

$ gem install braspag-rest

Usage

Configuration

The gem looks for a default config/braspag-rest.yml configuration file, with environment (RACK_ENV or RAILS_ENV) settings for the Braspag API.

# config/braspag-rest.yml
development:
  url: 'https://apisandbox.cieloecommerce.cielo.com.br'
  query_url: 'https://apiquerysandbox.cieloecommerce.cielo.com.br'
  oauth2_url: 'https://authsandbox.braspag.com.br/oauth2/token'
  client_id: 'YOUR CLIENT ID'
  client_secret: 'YOUR SECRET'
  request_timeout: 60

If you want to use a different file or manually set which environment should be used, you can create an initializer file and use the BraspagRest.config method to set your config.

# config/initializers/braspag-rest.rb
BraspagRest.config do |config|
  config.config_file_path = 'config/path/here.yml'
  config.enviroment = 'production'
end

You can use ERB blocks to interpolate values from your environment variables into the configuration if you are using something like dotenv to handle configuration values that shouldn't be present in the source code.

# config/braspag-rest.yml
development:
  url: 'https://apisandbox.cieloecommerce.cielo.com.br'
  query_url: 'https://apiquerysandbox.cieloecommerce.cielo.com.br'
  oauth2_url: 'https://authsandbox.braspag.com.br/oauth2/token'
  client_id: 'YOUR CLIENT ID'
  client_secret: 'YOUR SECRET'
  request_timeout: 60
production:
  url: <%= ENV['BRASPAG_URL'] %>
  query_url: <%= ENV['BRASPAG_QUERY_URL'] %>
  oauth2_url: <%= ENV['BRASPAG_OAUTH2_URL'] %>
  client_id: <%= ENV['BRASPAG_CLIENT_ID'] %>
  client_secret: <%= ENV['BRASPAG_CLIENT_SECRET'] %>
  request_timeout: <%= ENV['BRASPAG_REQUEST_TIMEOUT'] %>

Authorize an order

sale = BraspagRest::Sale.new(
  order_id: '123456',
  request_id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
  customer: {
    name: 'Comprador Teste'
  },
  payment: {
    type: 'CreditCard',
    amount: 15700,
    provider: 'Simulado',
    installments: 1,
    credit_card: {
      number: '0000000000000001',
      holder: 'Teste Holder',
      expiration_date: '12/2021',
      security_code: '123',
      brand: 'Visa'
    }
  }
)

sale.save

Authorize an order support fraud analysis fields

sale = BraspagRest::Sale.new(
  order_id: '201811201418',
  request_id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
  customer: {
    name: 'Comprador Teste Accept'
  },
  payment: {
    type: 'CreditCard',
    amount: 15700,
    provider: 'Simulado',
    installments: 1,
    credit_card: {
      number: '4905045123937354',
      holder: 'Elijah Rodriguez',
      expiration_date: '05/2022',
      security_code: '368',
      brand: 'Visa'
    },
    fraud_analysis: {
      sequence: 'AuthorizeFirst',
      sequence_criteria: 'Always',
      capture_on_low_risk: false,
      void_on_high_risk: false,
      browser: {
        cookies_accepted: false,
        email: 'compradorteste@live.com',
        host_name: 'Teste',
        ip_address: '202.190.150.350',
        type: 'Chrome'
      },
      cart: {
        is_gift: false,
        returns_accepted: true,
        items: [
          {
            gift_category: 'Undefined',
            host_hedge: 'Off',
            non_sensical_hedge: 'Off',
            obscenities_hedge: 'Off',
            phone_hedge: 'Off',
            name: 'ItemTeste',
            quantity: 1,
            sku: '201411170235134521346',
            unit_price: 123,
            risk: 'High',
            time_hedge: 'Normal',
            type: 'AdultContent',
            velocity_hedge: 'High'
          }
        ]
      },
      merchant_defined_fields: [
        {
          id: 9,
          value: 'web'
        }
      ],
      shipping: {
        addressee: 'Sr Comprador Teste',
        method: 'LowCost',
        phone: '21114740'
      },
      travel: {
        departure_time: '2010-01-02',
        journey_type: 'Ida',
        route: 'MAO-RJO',
        legs: [{
          destination: 'GYN',
          origin: 'VCP'
        }]
      }
    }
  }
)

sale.save

And to create a protected credit card, you should set the credit card saved as true:

credit_card = BraspagRest::CreditCard.new
credit_card.number = '0000000000000001'
credit_card.holder = 'Teste Holder'
credit_card.expiration_date = '12/2021'
credit_card.security_code = '123'
credit_card.brand = 'Visa'
credit_card.saved = true

Find a sale

sale = BraspagRest::Sale.find('REQUEST_ID', 'PAYMENT_ID')
sale.customer.name
=> "Maria"

Find sales for a store, aka: MerchantOrderId

sales = BraspagRest::Sale.find_by_order_id('REQUEST_ID', 'MERCHANT_ORDER_ID')
sales.map { |sale| sale.customer.name }
=> ["Maria", "Joana"]

Split a sale

split1 = BraspagRest::SplitPayment.new(subordinate_merchant_id: 'a4133798-9fac-4592-b040-d62d8239bd97', amount: 4000, fares: BraspagRest::Fare.new({mdr: 2, fee: 30}))
split2 = BraspagRest::SplitPayment.new(subordinate_merchant_id: '20943d1a-153f-42b6-93b8-07b9db000651', amount: 6000, fares: BraspagRest::Fare.new({mdr: 2, fee: 30}))

sale.payment.split([split1, split2])

Cancel a sale

sale = BraspagRest::Sale.find('REQUEST_ID', 'PAYMENT_ID')
sale.cancel

sale.voided_amount
=> 30017

sale.voided_date
=> "2017-09-11 16:53:03"

sale.payment.refunds
=> [{:amount=>30017, :status=>3, :received_date=>"2017-09-11T16:53:03.407"}]

Capture a sale

sale = BraspagRest::Sale.find('REQUEST_ID', 'PAYMENT_ID')
sale.capture

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Dinda-com-br/braspag-rest.

License

The gem is available as open source under the terms of the MIT License.

About

Gem to use Braspag gateway in his REST version

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 99.9%
  • Shell 0.1%