Sha256: b623a578077a0fc1c547590996a8cda0dd21ca4493e11ed5277ed118428563bc

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

require "versapay/version"

require "versapay/rails_helpers"
require "versapay/webhooks"
require "versapay/debit_agreement"
require "versapay/fund_sources"
require "versapay/transactions"

module Versapay

  class VersapayError < RuntimeError; end

  class InvalidInput < VersapayError; end
  class DuplicateTransaction < InvalidInput; end

  class InvalidWebhookSignature < VersapayError; end
  class Unprocessable < VersapayError; end
  class NotFound < VersapayError; end

  class << self
    attr_accessor :token, :key
  end

  @@token = "PleaseOverrideYourToken"
  @@key = "PleaseOverriedYourKey"

  def self.site
    Rails.env.production? ? "secure.versapay.com" : "demo.versapay.com"
  end


  def self.make_request(method, url, args = {})
    if method == :get then
      RestClient.get("https://#{Versapay.token}:#{Versapay.key}@" + Versapay::site + url, {:params => args}) do |response, request, result, &block|
        case response.code
        when 200
          return JSON.parse(response)
        when 422
          raise Versapay::Unprocessable, response
        when 500
          raise Versapay::NotFound, response
        end
      end
    end

    if method == :post then
      RestClient.post("https://#{Versapay.token}:#{Versapay.key}@" + Versapay::site + url, args.to_json, :content_type => :json, :accept => :json) do |response, request, result, &block|
        case response.code
        when 200,201
          return JSON.parse(response)
        when 412
          result = JSON.parse(response)
          if result.key? "unique_reference"
            raise Versapay::DuplicateTransaction, response
          else
            raise Versapay::InvalidInput, response
          end
        when 422
          raise Versapay::Unprocessable, response
        when 500
          raise Versapay::NotFound, response
        end
      end
    end


  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
versapay-0.1.5 lib/versapay.rb