Skip to content

bluethumbart/afterpay-ruby

Repository files navigation

Build Status Coverage Status Gem Version

Afterpay Ruby

Based on the API docs

Afterpay Ruby is a Ruby wrapper for Afterpay API. It provides simple DSL and serialization to Afterpay's API attribute.

Installation

Add this line to your application's Gemfile:

gem 'afterpay-ruby'

And then execute:

$ bundle

Or install it yourself as:

$ gem install afterpay-ruby

Usage

You need to configure Afterpay using your Merchant ID and secret.

For Rails, put this in your initializer.

Afterpay.configure do |config|
  config.app_id = <app_id>
  config.secret = <secret>

  # Sets the environment for Afterpay
  # defaults to sandbox
  # config.env = "sandbox" # "live"

  # Sets the user agent header for Afterpay requests
  # Refer https://docs.afterpay.com/au-online-api-v1.html#configuration
  # config.user_agent_header = {pluginOrModuleOrClientLibrary}/{pluginVersion} ({platform}/{platformVersion}; Merchant/{merchantId}) { merchantUrl }
  # Example
  # config.user_agent_header = "Afterpay Module / 1.0.0 (rails/ 5.1.2; Merchant/#{ merchant_id }) #{ merchant_website_url }"

end

Creating an Order

api docs

Order accepts a Consumer and an array of Item object which are required.

order = Afterpay::Order.create(
  total: <Money>,
  consumer: <Afterpay::Consumer>,
  items: [<Afterpay::Item>],
  success_url: <String>,
  cancel_url: <String>,
  reference: <String>,
  tax: <Money | optional>,
  shipping: <Money | optional>,
  discounts: [<Afterpay::Discount | optional>],
  billing_address: <Afterpay::Address | optional>,
  shipping_address: <Afterpay::Address | optional>,
)

# OR

order = Afterpay::Order.new(
  ...
)
order.create


order.success?
=> true
order.token
=> xxxxxxx

# Error

order.success?
=> false
order.error
=> Afterpay::Error:0x00007f8a63953888
  @id="48da0bce49d39625",
  @code="invalid_object",
  @message="merchant.redirectConfirmUrl must be a valid URL, merchant.redirectConfirmUrl may not be empty" >

Executing Payment

payment = Afterpay::Payment.execute(
  token: token <String>,
  reference: "checkout-1" <String>
)
=> <Afterpay::Payment ...>

payment.success?
=> true
payment.order
=> <Afterpay::Order ...>
payment.status
=> APPROVED

Consumer Object

api docs

Consumer contains the details of the payee which will be serialized to API's format.

Afterpay::Consumer.new(
  email: <String>,
  phone: <String>,
  first_name: <String>,
  last_name: <String>,
)

Item Object

api docs

Item holds the details of purchace per item.

Afterpay::Item.new(
  name: <String>,
  price: <Money>,
  sku: <String | optional>,
  quantity: <Number | defaults to 1>,
)

Discount Object

api docs

Discount Applied to the Order

Afterpay::Discount.new(
  name: <String>,
  amount: <Money>
)

Address Object

api docs

Item holds the details of purchace per item.

Afterpay::Address.new(
  name: <String>,
  line_1: <String>,
  line_2: <String | optional>,
  suburb: <String | optional>,
  state: <String>,
  postcode: <String | Number>,
  country: <String | optional>,
  phone: <String>
)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec 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/bluethumbart/afterpay-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

You will need to copy .env.sample to .env for running Afterpay console. This will not be checked into git.

License

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

Code of Conduct

Everyone interacting in the Afterpay project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •