Sha256: 3c9f1aa1d7cf0f3a0c9b5eecc209256c6f416ebeac91275863e78cf3a84834a5

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

require "net/http"
require "net/https"
require "json"
require "paymill/version"

module Paymill
  API_BASE    = "api.paymill.de"
  API_VERSION = "v2"

  @@api_key = nil

  autoload :Client,       "paymill/client"
  autoload :Offer,        "paymill/offer"
  autoload :Payment,      "paymill/payment"
  autoload :Subscription, "paymill/subscription"
  autoload :Transaction,  "paymill/transaction"

  module Operations
    autoload :All,    "paymill/operations/all"
    autoload :Create, "paymill/operations/create"
    autoload :Delete, "paymill/operations/delete"
    autoload :Find,   "paymill/operations/find"
  end

  class PaymillError < StandardError
  end

  class AuthenticationError < PaymillError; end
  class APIError            < PaymillError; end

  class << self
    def api_key
      @@api_key
    end

    def api_key=(api_key)
      @@api_key = api_key
    end

    def request(http_method, api_url, data)
      raise AuthenticationError if api_key.nil?

      https              = Net::HTTP.new(API_BASE, Net::HTTP.https_default_port)
      https.use_ssl      = true
      https.verify_mode  = OpenSSL::SSL::VERIFY_PEER
      https.ca_file      = File.join(File.dirname(__FILE__), "data/paymill.crt")
      https.start do |connection|
        url = "/#{API_VERSION}/#{api_url}"
        https_request = case http_method
              when :post
                Net::HTTP::Post.new(url)
              when :delete
                Net::HTTP::Delete.new(url)
              else
                Net::HTTP::Get.new(url)
              end
        https_request.basic_auth(api_key,"")
        https_request.set_form_data(data) if http_method == :post
        @response = https.request(https_request)
      end
      raise AuthenticationError if @response.code.to_i == 401
      raise APIError if @response.code.to_i >= 500

      data = JSON.parse(@response.body)
      raise APIError.new(data["error"]) if data["error"]

      data
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paymill-0.1.0 lib/paymill.rb