Class: Affirm::Client
- Inherits:
-
Object
- Object
- Affirm::Client
- Defined in:
- lib/affirm/client.rb
Instance Method Summary collapse
-
#authorize(checkout_id) ⇒ Struct::Transaction
Authorize the transaction.
-
#capture(transaction_id) ⇒ Struct::Transaction::Event
Captures the transaction.
-
#get_checkout(checkout_id) ⇒ Struct::Checkout
Fetch the checkout by checkout_id.
-
#http_call(http_method, api_method, id = nil, body = {}) ⇒ Hash
Executes a request, validates and returns the response.
-
#initialize ⇒ Client
constructor
A new instance of Client.
-
#read_transaction(transaction_id, expand = nil) ⇒ Struct::Transaction
Get the transaction by transaction_id.
-
#refund(transaction_id, amount = nil) ⇒ Struct::Transaction::Event
Refunds the transaction.
-
#start_checkout_direct(payload) ⇒ Struct::Checkout::Response
Start the ‘direct’ checkout with Affirm.
-
#start_checkout_store(payload) ⇒ Struct::Checkout::Response
Start the ‘store’ checkout with Affirm.
- #user_agent_string ⇒ Object
-
#void(transaction_id) ⇒ Struct::Transaction::Event
Void the transaction.
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
3 4 5 6 7 8 |
# File 'lib/affirm/client.rb', line 3 def initialize @version_strings = [] add_version_string "Affirm/" << VERSION add_version_string "Ruby/" << RUBY_VERSION add_version_string OpenSSL::OPENSSL_VERSION.split(" ").slice(0, 2).join "/" end |
Instance Method Details
#authorize(checkout_id) ⇒ Struct::Transaction
Authorize the transaction
61 62 63 64 |
# File 'lib/affirm/client.rb', line 61 def (checkout_id) response = http_call(:post, :transactions, nil, {transaction_id: checkout_id}) Struct::Transaction.new(response) end |
#capture(transaction_id) ⇒ Struct::Transaction::Event
Captures the transaction
74 75 76 77 |
# File 'lib/affirm/client.rb', line 74 def capture(transaction_id) response = http_call(:post, "transactions/#{transaction_id}/capture") Struct::Transaction::Event.new(response) end |
#get_checkout(checkout_id) ⇒ Struct::Checkout
Fetch the checkout by checkout_id
48 49 50 51 |
# File 'lib/affirm/client.rb', line 48 def get_checkout(checkout_id) response = http_call(:get, :checkout, checkout_id) Struct::Checkout.new(response) end |
#http_call(http_method, api_method, id = nil, body = {}) ⇒ Hash
Executes a request, validates and returns the response.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/affirm/client.rb', line 136 def http_call(http_method, api_method, id = nil, body = {}) version = "v2" # The Transaction api is actually the new API and replaces # the Charge api. However the api endpoint for Transactions # is set under a v1 version path. version = "v1" if (api_method == :transactions) || ((api_method.class == String) && api_method.start_with?("transactions")) path = "/api/#{version}/#{api_method}/#{id}".chomp("/") api_endpoint = Affirm.config.endpoint uri = URI.parse(api_endpoint) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.set_debug_output($stdout) if ENV["DEBUG"] case http_method when :get request = Net::HTTP::Get.new(path) when :post body.delete_if { |_k, v| v.nil? } request = Net::HTTP::Post.new(path) request.body = body.to_json else raise RequestError, "Invalid HTTP Method: #{http_method.to_s.upcase}" end request.basic_auth Affirm.config.public_api_key, Affirm.config.private_api_key request["Accept"] = "application/json" request["Content-Type"] = "application/json" request["User-Agent"] = user_agent_string begin response = http.request(request) rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e raise RequestError, e. end http_code = response.code.to_i case http_code when 200, 201 JSON.parse(response.body) when 400, 403 # 400 - Bad request raise RequestError, response when 401 # 401 - Unauthorized raise AuthenticationError, response when 404 # 404 - Not found raise NotFoundError, response when 500, 502, 503, 504 # 500, 502, 503, 504 - Server Errors raise Error, response end end |
#read_transaction(transaction_id, expand = nil) ⇒ Struct::Transaction
Get the transaction by transaction_id
88 89 90 91 92 93 |
# File 'lib/affirm/client.rb', line 88 def read_transaction(transaction_id, = nil) transaction_path = "transactions/#{transaction_id}?expand=#{expand}" if transaction_path = "transactions/#{transaction_id}" unless response = http_call(:get, transaction_path) Struct::Transaction.new(response) end |
#refund(transaction_id, amount = nil) ⇒ Struct::Transaction::Event
Refunds the transaction
117 118 119 120 121 122 123 124 |
# File 'lib/affirm/client.rb', line 117 def refund(transaction_id, amount = nil) response = if amount http_call(:post, "transactions/#{transaction_id}/refund", nil, {amount: amount}) else http_call(:post, "transactions/#{transaction_id}/refund") end Struct::Transaction::Event.new(response) end |
#start_checkout_direct(payload) ⇒ Struct::Checkout::Response
Start the ‘direct’ checkout with Affirm.
35 36 37 38 |
# File 'lib/affirm/client.rb', line 35 def start_checkout_direct(payload) response = http_call(:post, "checkout/direct", nil, payload) Struct::Checkout::Response.new(response) end |
#start_checkout_store(payload) ⇒ Struct::Checkout::Response
Start the ‘store’ checkout with Affirm.
22 23 24 25 |
# File 'lib/affirm/client.rb', line 22 def start_checkout_store(payload) response = http_call(:post, "checkout/store", nil, payload) Struct::Checkout::Response.new(response) end |
#user_agent_string ⇒ Object
10 11 12 |
# File 'lib/affirm/client.rb', line 10 def user_agent_string @version_strings.join(" ") end |
#void(transaction_id) ⇒ Struct::Transaction::Event
Void the transaction
103 104 105 106 |
# File 'lib/affirm/client.rb', line 103 def void(transaction_id) response = http_call(:post, "transactions/#{transaction_id}/void") Struct::Transaction::Event.new(response) end |