lib/active_merchant/billing/gateways/ebanx.rb in activemerchant-1.73.0 vs lib/active_merchant/billing/gateways/ebanx.rb in activemerchant-1.74.0

- old
+ new

@@ -22,19 +22,21 @@ URL_MAP = { purchase: "direct", authorize: "direct", capture: "capture", refund: "refund", - void: "cancel" + void: "cancel", + store: "token" } HTTP_METHOD = { purchase: :post, authorize: :post, capture: :get, refund: :post, - void: :get + void: :get, + store: :post } def initialize(options={}) requires!(options, :integration_key) super @@ -44,25 +46,26 @@ post = { payment: {} } add_integration_key(post) add_operation(post) add_invoice(post, money, options) add_customer_data(post, payment, options) - add_payment(post, payment) + add_card_or_token(post, payment) add_address(post, options) - add_customer_responsible_person(post, payment, options) if post[:payment][:country] == 'BR' + add_customer_responsible_person(post, payment, options) + commit(:purchase, post) end def authorize(money, payment, options={}) post = { payment: {} } add_integration_key(post) add_operation(post) add_invoice(post, money, options) add_customer_data(post, payment, options) - add_payment(post, payment) + add_card_or_token(post, payment) add_address(post, options) - add_customer_responsible_person(post, payment, options) if post[:payment][:country] == 'BR' + add_customer_responsible_person(post, payment, options) post[:payment][:creditcard][:auto_capture] = false commit(:authorize, post) end @@ -92,10 +95,19 @@ add_authorization(post, authorization) commit(:void, post) end + def store(credit_card, options={}) + post = {} + add_integration_key(post) + add_payment_details(post, credit_card) + post[:country] = customer_country(options) + + commit(:store, post) + end + def verify(credit_card, options={}) MultiResponse.run(:use_first_response) do |r| r.process { authorize(100, credit_card, options) } r.process(:ignore_result) { void(r.authorization, options) } end @@ -125,31 +137,34 @@ def add_authorization(post, authorization) post[:hash] = authorization end def add_customer_data(post, payment, options) - post[:payment][:name] = payment.name + post[:payment][:name] = customer_name(payment, options) post[:payment][:email] = options[:email] || "unspecified@example.com" post[:payment][:document] = options[:document] post[:payment][:birth_date] = options[:birth_date] if options[:birth_date] end - def add_customer_responsible_person(post, payment, options) - post[:payment][:responsible] = {} - post[:payment][:responsible][:name] = payment.name - post[:payment][:responsible][:document] = options[:document] - post[:payment][:responsible][:birth_date] = options[:birth_date] if options[:birth_date] + def add_customer_responsible_person(post, payment, options) + post[:payment][:person_type] = options[:person_type] if options[:person_type] + if options[:person_type] && options[:person_type].downcase == 'business' + post[:payment][:responsible] = {} + post[:payment][:responsible][:name] = customer_name(payment, options) + post[:payment][:responsible][:document] = options[:document] if options[:document] + post[:payment][:responsible][:birth_date] = options[:birth_date] if options[:birth_date] + end end def add_address(post, options) if address = options[:billing_address] || options[:address] post[:payment][:address] = address[:address1].split[1..-1].join(" ") if address[:address1] post[:payment][:street_number] = address[:address1].split.first if address[:address1] post[:payment][:city] = address[:city] post[:payment][:state] = address[:state] post[:payment][:zipcode] = address[:zip] - post[:payment][:country] = address[:country] + post[:payment][:country] = address[:country].downcase post[:payment][:phone_number] = address[:phone] end end def add_invoice(post, money, options) @@ -157,20 +172,36 @@ post[:payment][:currency_code] = (options[:currency] || currency(money)) post[:payment][:merchant_payment_code] = options[:order_id] post[:payment][:instalments] = options[:instalments] || 1 end - def add_payment(post, payment) - post[:payment][:payment_type_code] = CARD_BRAND[payment.brand.to_sym] - post[:payment][:creditcard] = { - card_number: payment.number, - card_name: payment.name, - card_due_date: "#{payment.month}/#{payment.year}", - card_cvv: payment.verification_value - } + def add_card_or_token(post, payment) + if payment.is_a?(String) + payment, brand = payment.split("|") + end + post[:payment][:payment_type_code] = payment.is_a?(String) ? brand : CARD_BRAND[payment.brand.to_sym] + post[:payment][:creditcard] = payment_details(payment) end + def add_payment_details(post, payment) + post[:payment_type_code] = CARD_BRAND[payment.brand.to_sym] + post[:creditcard] = payment_details(payment) + end + + def payment_details(payment) + if payment.is_a?(String) + { token: payment } + else + { + card_number: payment.number, + card_name: payment.name, + card_due_date: "#{payment.month}/#{payment.year}", + card_cvv: payment.verification_value + } + end + end + def parse(body) JSON.parse(body) end def commit(action, parameters) @@ -181,11 +212,11 @@ Response.new( success, message_from(response), response, - authorization: authorization_from(response), + authorization: authorization_from(action, parameters, response), test: test?, error_code: error_code_from(response, success) ) end @@ -194,22 +225,28 @@ response.try(:[], "payment").try(:[], "status") == "CO" elsif action == :authorize response.try(:[], "payment").try(:[], "status") == "PE" elsif action == :void response.try(:[], "payment").try(:[], "status") == "CA" + elsif action == :store + response.try(:[], "status") == "SUCCESS" else false end end def message_from(response) return response["status_message"] if response["status"] == "ERROR" response.try(:[], "payment").try(:[], "transaction_status").try(:[], "description") end - def authorization_from(response) - response.try(:[], "payment").try(:[], "hash") + def authorization_from(action, parameters, response) + if action == :store + response.try(:[], "token") + "|" + CARD_BRAND[parameters[:payment_type_code].to_sym] + else + response.try(:[], "payment").try(:[], "hash") + end end def post_data(action, parameters = {}) return nil if requires_http_get(action) return convert_to_url_form_encoded(parameters) if action == :refund @@ -235,9 +272,24 @@ def error_code_from(response, success) unless success return response["status_code"] if response["status"] == "ERROR" response.try(:[], "payment").try(:[], "transaction_status").try(:[], "code") + end + end + + def customer_country(options) + if country = options[:country] || (options[:billing_address][:country] if options[:billing_address]) + country.downcase + end + end + + def customer_name(payment, options) + address_name = options[:billing_address][:name] if options[:billing_address] && options[:billing_address][:name] + if payment.is_a?(String) + address_name || "Not Provided" + else + payment.name end end end end end