Module: Paymill

Defined in:
lib/paymill.rb,
lib/paymill/base.rb,
lib/paymill/offer.rb,
lib/paymill/client.rb,
lib/paymill/refund.rb,
lib/paymill/payment.rb,
lib/paymill/version.rb,
lib/paymill/webhook.rb,
lib/paymill/transaction.rb,
lib/paymill/subscription.rb,
lib/paymill/operations/all.rb,
lib/paymill/operations/find.rb,
lib/paymill/preauthorization.rb,
lib/paymill/operations/create.rb,
lib/paymill/operations/delete.rb,
lib/paymill/operations/update.rb

Defined Under Namespace

Modules: Operations Classes: APIError, AuthenticationError, Base, Client, Offer, Payment, PaymillError, Preauthorization, Refund, Subscription, Transaction, Webhook

Constant Summary collapse

API_BASE =
"api.paymill.com"
API_VERSION =
"v2"
VERSION =
"0.3.0"
@@api_key =
nil

Class Method Summary collapse

Class Method Details

.api_keyString

Returns the set api key

Returns:

  • (String)

    The api key



40
41
42
# File 'lib/paymill.rb', line 40

def api_key
  @@api_key
end

.api_key=(api_key) ⇒ Object

Sets the api key

Parameters:

  • api_key (String)

    The api key



47
48
49
# File 'lib/paymill.rb', line 47

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

.path_with_params(path, params) ⇒ String

Builds and encodes a URI using given path and params.

Parameters:

  • path (String)

    The path to use

  • params (Array)

    The params to use

Returns:

  • (String)

    The built URI.



99
100
101
102
103
104
105
106
# File 'lib/paymill.rb', line 99

def path_with_params(path, params)
  unless params.empty?
    encoded_params = URI.encode_www_form(params)
    [path, encoded_params].join("?")
  else
    path
  end
end

.request(http_method, api_url, data) ⇒ Array

Makes a request against the Paymill API

Parameters:

  • http_method (Symbol)

    The http method to use, must be one of :get, :post, :put and :delete

  • api_url (String)

    The API url to use

  • data (Hash)

    The data to send, e.g. used when creating new objects.

Returns:

  • (Array)

    The parsed JSON response.

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/paymill.rb', line 57

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|
    if api_url == "refunds"
      url = "/#{API_VERSION}/#{api_url}/#{data[:id]}"
      data.delete(:id)
    else
      url = "/#{API_VERSION}/#{api_url}"
    end
    https_request = case http_method
                    when :post
                      Net::HTTP::Post.new(url)
                    when :put
                      Net::HTTP::Put.new(url)
                    when :delete
                      Net::HTTP::Delete.new(url)
                    else
                      Net::HTTP::Get.new(path_with_params(url, data))
                    end
    https_request.basic_auth(api_key, "")
    https_request.set_form_data(data) if [:post, :put].include? http_method
    @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