Installation
Add this line to your application's Gemfile:
gem 'pagaris'
And then execute:
$ bundle
Or install it yourself as:
$ gem install pagaris
Usage
Configuration
Warning: Never store your credentials in plaintext. We recommend using environment variables.
Note: The private key you use in
Pagaris#private_key=
determines the current environment for all calls (i.e. sandbox or production calls).
# Only if it's not already required for you (e.g. Rails)
require 'pagaris'
Pagaris.application_id = ENV['PAGARIS_APPLICATION_ID']
Pagaris.private_key = ENV['PAGARIS_PRIVATE_KEY']
Creating an Order
# First way: `Pagaris::Order::create`
begin
order = Pagaris::Order.create(
amount: 4_999.99,
metadata: { internal_id: 192 }, # Optional
products: [{ sku: '123', quantity: 3 }], # Optional
redirect_url: 'https://some.url/for/redirect' # Optional, overrides app
)
order.id # => 'a0248c87-2f0f-4e4e-a913-893d54046321'
order.amount # => 4999.99
order.fee # => 100 (for example)
order.payout_amount # => 4899.99
order.status # => 'created'
order. # => { internal_id: 192 }
order.products # => [{ sku: '123', quantity: 3 }]
order.created_at # => 2020-05-04 16:03:51 -0500 (Time)
order.updated_at # => 2020-05-04 16:03:51 -0500 (Time)
order.url # => 'https://pagaris.com/some_url_to_redirect_your_buyer_to'
rescue Exception => e
# See the 'Exceptions' section
end
# Second way: Calling `Order#create` after initializing it
order = Order.new(amount: 4_999.99)
order.redirect_url = 'http://something.com'
begin
order.create
rescue Exception => e
# See the 'Exceptions' section
end
Getting an Order
order = Order.get('caff2436-b41c-41f5-b7e9-7259d158c95a') # => <Pagaris::Order>
orders.id # => 'a0248c87-2f0f-4e4e-a913-893d54046321'
Getting all Orders
orders = Order.all # => [<Pagaris::Order>, <Pagaris::Order>, ...]
orders.first&.id # => 'a0248c87-2f0f-4e4e-a913-893d54046321'
Confirming an Order
order = Order.get('caff2436-b41c-41f5-b7e9-7259d158c95a')
order.status # => 'approved'
order.confirm
order.status # => 'confirmed'
Cancelling an Order
order = Order.get('dcf647cb-39f2-4ef2-a07f-555a1d5be7e3')
order.status # => 'approved'
order.cancel
order.status # => 'cancelled'
Exceptions
Any of the previous operations can raise one of the following exceptions. You can call #response
on them to inspect what caused the error
Pagaris::Errors::UnauthorizedError
- Credentials were not configured correctly or the request is impossible (e.g. trying to confirm an Order which can't be confirmed)Pagaris::Errors::NotFoundError
- When fetching or trying to update an Order with an incorrectid
(note the environment in which they were created)Pagaris::Errors::UnprocessableEntityError
- When the parameters are invalid (e.g. creating an Order without an amount, or with an amount less than the minimum amount or greater than the maximum amount).Pagaris::Errors::UnexpectedResponseError
Pagaris::Errors::ServerError
Example of handling exceptions
begin
order = Order.create(amount: 100)
rescue Pagaris::Errors::UnprocessableEntityError => e
e.response # => HTTParty::Response
e.response&.body # => {"error":{"status":422,"detail":"Some detail"}}
end
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
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.