module Payment class Base def self.charge_from_card(payment, customer, card, pre_auth, saved_card = true) amount = pre_auth ? 0 : payment[:amount] event_type = pre_auth ? "pre_authorization" : "credit_card_payment" request, response = default_gateway.charge_from_card(payment, customer, card, pre_auth, saved_card) {event_type: event_type, amount: amount, currency: payment[:currency], response: response, request: request, external_id: payment[:id], gateway: default_gateway.to_s, refunded: false, success: response[:success]} end def self.cancel_transaction(refundable, pre_auth) event_type = pre_auth ? "cancel_pre_auth" : "cancel_transaction" amount = pre_auth ? 0 : -(refundable[:amount]) request, response = default_gateway.cancel_transaction(refundable) request.merge!({amount: -refundable[:amount]}) if pre_auth {event_type: event_type, amount: amount, currency: refundable[:currency], "request" => request, "response" => response, external_id: refundable[:id] , gateway: default_gateway.to_s, success: response[:success]} end def self.refund_money(refundable) request, response = default_gateway.refund_money(refundable) {event_type: "credit_card_refund", amount: -(refundable[:amount]), currency: refundable[:currency], "request" => request, "response" => response, external_id: refundable[:id], gateway: default_gateway.to_s, success: response[:success]} end def self.query_saved_cards(customer) _, response = default_gateway.query_saved_cards(customer) response end def self.delete_card(tokens) success = true tokens.each do |adapter, token| _, response = adapter.constantize.delete_card(token) success = success && response[:success] # rubocop:disable Style/SelfAssignment end success end def self.currency_exchange(from, to, amount) _, response = default_gateway.currency_exchange(from, to, amount) response end def self.query_bin_number(bin) _, response = default_gateway.query_bin_number(bin) response end def self.query_card_type_with_bin_number(bin) _, response = default_gateway.query_bin_number(bin) response[:success] ? (response[:bin]["cardBrand"] == "VISA" ? "visa" : "mastercard") : "other" # rubocop:disable Style/NestedTernaryOperator end def self.default_gateway(user = nil) return Payment::TestGateway if Rails.env.test? case user&.country when "TR" Payment::Paratika when "US", "UK" Payment::Stripe when "CN" Payment::AliPay when "RU" Payment::YandexPay else Payment.adapter end end end end