app/models/solidus/gateway/culqi_gateway.rb in solidus_culqi-0.0.1 vs app/models/solidus/gateway/culqi_gateway.rb in solidus_culqi-0.0.2

- old
+ new

@@ -1,28 +1,19 @@ module Solidus - class Gateway::CulqiGateway < ::Spree::Gateway + class Gateway::CulqiGateway < SolidusCulqi::Gateway.parent_class + extend SolidusCulqi::Gateway + preference :public_key, :string preference :secret_key, :string + partial_method 'culqi' + provider_method self.class + def default_currency "PEN" end - if SolidusSupport.solidus_gem_version < Gem::Version.new('2.3.x') - def method_type - 'culqi' - end - else - def partial_name - 'culqi' - end - end - - def provider_class - self.class - end - def authorize(amount, creditcard, gateway_options) init_culqi commit(amount, creditcard, gateway_options, false) end @@ -35,27 +26,45 @@ init_culqi charge = Culqi::Charge.capture(response_code) parse_response(charge) end - def credit(amount, creditcard, _gateway_options) + def credit(amount, _creditcard, response_code, _gateway_options) init_culqi # Culqi only accepts 'duplicado','fraudulento' o 'solicitud_comprador' # like reason's value refund = Culqi::Refund.create( amount: amount, - charge_id: creditcard, + charge_id: response_code, reason: "solicitud_comprador" ) parse_response(refund) end - def void(creditcard, gateway_options) + def void(response_code, creditcard, gateway_options) amount = gateway_options[:subtotal].to_i - credit(amount, creditcard, gateway_options) + credit(amount, creditcard, response_code, gateway_options) end + def payment_profiles_supported? + true + end + + def create_profile(payment) + return unless payment.source.gateway_customer_profile_id.nil? + init_culqi + customer = get_customer(payment) + token = payment.source.gateway_payment_profile_id + card_token = generate_card(customer, token) + unless customer.nil? || card_token.nil? + payment.source.update({ + gateway_customer_profile_id: customer, + gateway_payment_profile_id: card_token + }) + end + end + private def init_culqi Culqi.public_key = preferred_public_key Culqi.secret_key = preferred_secret_key @@ -82,8 +91,43 @@ res[:object] != "error", res[:merchant_message], res, authorization: res["id"] ) + end + + def find_customer(email) + res = Culqi::Customer.list + customers = JSON.parse(res)["data"] + customers.select { |customer| customer["email"] == email }.first + end + + def get_customer(payment) + address = payment.order.bill_address + email = payment.order.email + customer = find_customer(email) + customer = generate_customer(address, email) if customer.nil? + customer["id"] + end + + def generate_customer(address, email) + culqi_customer = Culqi::Customer.create( + address: address.address1, + address_city: address.state.name, + country_code: address.country.iso, + email: email, + first_name: address.first_name, + last_name: address.last_name, + phone_number: address.phone.tr("/() -/", "") + ) + JSON.parse(culqi_customer) + end + + def generate_card(customer, token) + card = Culqi::Card.create( + customer_id: customer, + token_id: token + ) + JSON.parse(card)["id"] end end end