Skip to content

nebulab/afterpay-ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ruby SDK for the Afterpay API

CircleCI codecov

Afterpay API documentation

Installation

Add this line to your application's Gemfile:

gem 'afterpay'

And then execute:

$ bundle

Or install it yourself as:

$ gem install afterpay

Configuration

Merchant ID and Secret Key are required to work with Afterpay API

Afterpay.configure do |c|
  c.merchant_id = 'your_merchant_id'
  c.secret_key  = 'your_secret_key'
  c.server      = 'us_or_au_server' # Default is 'https://global-api.afterpay.com/'
  c.user_agent  = 'MyAfterpayModule/1.0.0 (E-Commerce Platform Name/1.0.0; PHP/7.0.0; Merchant/600032000) https://merchant.example.com' # Default is nil
end

Usage

# Retrieving the configuration
configuration = Afterpay::API::Configuration::Retrieve.call

# Creating an order based on different components
amount = Afterpay::Components::Money.new(
  amount:   '10.00',
  currency: 'USD'
)

consumer = Afterpay::Components::Consumer.new(
  phone_number: '2120000000',
  given_names:  'Joe',
  surname:      'Consumer',
  email:        '[email protected]'
)

merchant = Afterpay::Components::Merchant.new(
  redirect_confirm_url: 'https://www.merchant.com/confirm',
  redirect_cancel_url:  'https://www.merchant.com/cancel'
)

order = Afterpay::Components::Order.new(
  amount:   amount,
  consumer: consumer,
  merchant: merchant
)

Afterpay::API::Order::Create.call(
  order: order
) # => <Hashie::Mash expires="2018-12-06T18:51:27.710Z" token="some_token">

# Returned token can be used to capture the payment
payment = Afterpay::Components::Payment.new(
  token: token
)

Afterpay::API::Payment::Capture.call(
  payment: payment
) # => <Hashie::Mash created="2018-10-06T18:45:00.819Z" events=#<Hashie::Array []> id="afterpay_order_id" ... status="APPROVED" token="some_token" originalAmount=#<Hashie::Mash amount="10.00" currency="USD">>

# Returned unique Afterpay's Order ID can be used to make a refund
refund_amount = Afterpay::Components::Money.new(
  amount:   '5.00',
  currency: 'USD'
)

refund = Afterpay::Components::Refund.new(
  amount: refund_amount
)

Afterpay::API::Payment::Refund.call(
  order_id: order_id,
  refund:   refund
) # => <Hashie::Mash amount=#<Hashie::Mash amount="5.00" currency="USD"> refundId="some_refund_id" refundedAt="2018-12-06T18:50:50.844Z">

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nebulab/afterpay-ruby.

License

The gem is available as open source under the terms of the Apache License, Version 2.0.