Class: ActiveMerchant::Billing::Affirm
- Inherits:
-
Gateway
- Object
- Gateway
- ActiveMerchant::Billing::Affirm
- Defined in:
- lib/active_merchant/billing/affirm.rb
Instance Method Summary collapse
- #authorize(money, affirm_source, options = {}) ⇒ Object
- #capture(money, charge_source, options = {}) ⇒ Object
- #commit(method, url, parameters = nil, options = {}, ret_charge = false) ⇒ Object
- #credit(money, charge_source, options = {}) ⇒ Object
- #get_checkout(checkout_token) ⇒ Object
- #headers ⇒ Object
-
#initialize(options = {}) ⇒ Affirm
constructor
A new instance of Affirm.
- #json_error(raw_response) ⇒ Object
- #parse(body) ⇒ Object
- #post_data(params) ⇒ Object
-
#purchase(money, affirm_source, options = {}) ⇒ Object
To create a charge on a card or a token, call.
- #refund(money, charge_source, options = {}) ⇒ Object
- #response_error(raw_response) ⇒ Object
- #root_api_url ⇒ Object
- #root_url ⇒ Object
- #set_charge(charge_id) ⇒ Object
- #void(charge_source, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Affirm
Returns a new instance of Affirm.
8 9 10 11 12 13 |
# File 'lib/active_merchant/billing/affirm.rb', line 8 def initialize( = {}) requires!(, :api_key, :secret_key, :server) @api_key = [:api_key] @secret_key = [:secret_key] super end |
Instance Method Details
#authorize(money, affirm_source, options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_merchant/billing/affirm.rb', line 19 def (money, affirm_source, = {}) result = commit(:post, "", {"checkout_token"=>affirm_source.token}, , true) return result unless result.success? if amount(money).to_i != result.params["amount"].to_i return Response.new(false, "Auth amount does not match charge amount", result.params ) elsif result.params["pending"].to_s != "true" return Response.new(false, "There was an error authorizing this Charge", result.params ) end result end |
#capture(money, charge_source, options = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/active_merchant/billing/affirm.rb', line 50 def capture(money, charge_source, = {}) post = {:amount => amount(money)} set_charge(charge_source) result = commit(:post, "#{@charge_id}/capture", post, ) return result unless result.success? if amount(money).to_i != result.params["amount"].to_i return Response.new(false, "Capture amount does not match charge amount", result.params ) end result end |
#commit(method, url, parameters = nil, options = {}, ret_charge = false) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/active_merchant/billing/affirm.rb', line 136 def commit(method, url, parameters=nil, = {}, ret_charge=false) raw_response = response = nil success = false begin raw_response = ssl_request(method, root_url + url, post_data(parameters), headers) response = parse(raw_response) success = !response.key?("status_code") && (!ret_charge || response.key?("id")) rescue ResponseError => e raw_response = e.response.body response = response_error(raw_response) rescue JSON::ParserError response = json_error(raw_response) end if success && ret_charge @charge_id = response["id"] end Response.new(success, success ? "Transaction approved" : response["message"], response, :authorization => @charge_id, ) end |
#credit(money, charge_source, options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/active_merchant/billing/affirm.rb', line 76 def credit(money, charge_source, = {}) set_charge(charge_source) return Response.new(true , "Credited Zero amount", {}, :authorization => @charge_id, ) unless money > 0 refund(money, charge_source, ) end |
#get_checkout(checkout_token) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/active_merchant/billing/affirm.rb', line 129 def get_checkout(checkout_token) _url = root_api_url + "checkout/#{checkout_token}" _raw_response = ssl_request :get, _url, nil, headers parse(_raw_response) end |
#headers ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/active_merchant/billing/affirm.rb', line 94 def headers { "Content-Type" => "application/json", "Authorization" => "Basic " + Base64.encode64(@api_key.to_s + ":" + @secret_key.to_s).gsub(/\n/, '').strip, "User-Agent" => "Affirm/v1 ActiveMerchantBindings", } end |
#json_error(raw_response) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/active_merchant/billing/affirm.rb', line 119 def json_error(raw_response) msg = 'Invalid response. Please contact affirm if you continue to receive this message.' msg += " (The raw response returned by the API was #{raw_response.inspect})" { "error" => { "message" => msg } } end |
#parse(body) ⇒ Object
102 103 104 |
# File 'lib/active_merchant/billing/affirm.rb', line 102 def parse(body) JSON.parse(body) end |
#post_data(params) ⇒ Object
106 107 108 109 |
# File 'lib/active_merchant/billing/affirm.rb', line 106 def post_data(params) return nil unless params params.to_json end |
#purchase(money, affirm_source, options = {}) ⇒ Object
To create a charge on a card or a token, call
purchase(money, card_hash_or_token, { ... })
To create a charge on a customer, call
purchase(money, nil, { :customer => id, ... })
44 45 46 47 48 |
# File 'lib/active_merchant/billing/affirm.rb', line 44 def purchase(money, affirm_source, = {}) result = (money, affirm_source, ) return result unless result.success? capture(money, @charge_id, ) end |
#refund(money, charge_source, options = {}) ⇒ Object
70 71 72 73 74 |
# File 'lib/active_merchant/billing/affirm.rb', line 70 def refund(money, charge_source, = {}) post = {:amount => amount(money)} set_charge(charge_source) commit(:post, "#{@charge_id}/refund", post, ) end |
#response_error(raw_response) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/active_merchant/billing/affirm.rb', line 111 def response_error(raw_response) begin parse(raw_response) rescue JSON::ParserError json_error(raw_response) end end |
#root_api_url ⇒ Object
90 91 92 |
# File 'lib/active_merchant/billing/affirm.rb', line 90 def root_api_url "https://#{@options[:server]}/api/v2/" end |
#root_url ⇒ Object
86 87 88 |
# File 'lib/active_merchant/billing/affirm.rb', line 86 def root_url "#{root_api_url}charges/" end |
#set_charge(charge_id) ⇒ Object
15 16 17 |
# File 'lib/active_merchant/billing/affirm.rb', line 15 def set_charge(charge_id) @charge_id = charge_id end |
#void(charge_source, options = {}) ⇒ Object
65 66 67 68 |
# File 'lib/active_merchant/billing/affirm.rb', line 65 def void(charge_source, = {}) set_charge(charge_source) commit(:post, "#{@charge_id}/void", {}, ) end |