require "countries" require "money" module Spree class Gateway::VeritransGateway < Gateway preference :client_key, :string preference :server_key, :string preference :url_api, :string preference :use_3d_secure, :boolean, :default => false def method_type 'veritrans' end def provider_class ActiveMerchant::Billing::VeritransGateway end def purchase(money, creditcard, gateway_options) money = veritrans_gross_amount(money) provider.charge(create_data_for_auth_or_charge(money, creditcard, gateway_options), { url_api: preferred_url_api, server_key: preferred_server_key }) end def authorize(money, creditcard, gateway_options) money = veritrans_gross_amount(money) provider.charge(create_data_for_auth_or_charge(money, creditcard, gateway_options, preferred_use_3d_secure ? nil : "authorize"), { url_api: preferred_url_api, server_key: preferred_server_key }) end def capture(money, response_code, gateway_options) money = veritrans_gross_amount(money) provider.capture(response_code, money, { url_api: preferred_url_api, server_key: preferred_server_key }) end def void(response_code, gateway_options) provider.cancel(response_code, { url_api: preferred_url_api, server_key: preferred_server_key }) end def cancel(response_code) provider.cancel(response_code, { url_api: preferred_url_api, server_key: preferred_server_key }) end private def veritrans_gross_amount(money) money = money.to_s 2.times { money.chomp!("0") } money.to_i end def create_data_for_auth_or_charge(money, creditcard, gateway_options, type = nil) data = {} data[:payment_type] = "credit_card" data[:transaction_details] = { order_id: gateway_options[:order_id], gross_amount: money } data[:credit_card] = {} data[:credit_card][:token_id] = creditcard.gateway_payment_profile_id if creditcard.gateway_payment_profile_id data[:credit_card][:type] = type unless type.nil? data[:customer_details] = {} data[:customer_details][:first_name] = creditcard.name data[:customer_details][:email] = gateway_options[:email] data[:customer_details][:billing_address] = {} data[:customer_details][:billing_address][:first_name] = gateway_options[:billing_address][:name] data[:customer_details][:billing_address][:last_name] = "" data[:customer_details][:billing_address][:address] = "#{ gateway_options[:billing_address][:address1] } #{ gateway_options[:billing_address][:address2] }" data[:customer_details][:billing_address][:city] = gateway_options[:billing_address][:city] data[:customer_details][:billing_address][:postal_code] = gateway_options[:billing_address][:zip] data[:customer_details][:billing_address][:phone] = gateway_options[:billing_address][:phone] data[:customer_details][:billing_address][:country_code] = get_alpha3_country_code(gateway_options[:billing_address][:country]) data[:customer_details][:shipping_address] = {} data[:customer_details][:shipping_address][:first_name] = gateway_options[:shipping_address][:name] data[:customer_details][:shipping_address][:last_name] = "" data[:customer_details][:shipping_address][:address] = "#{ gateway_options[:shipping_address][:address1] } #{ gateway_options[:billing_address][:address2] }" data[:customer_details][:shipping_address][:city] = gateway_options[:shipping_address][:city] data[:customer_details][:shipping_address][:postal_code] = gateway_options[:shipping_address][:zip] data[:customer_details][:shipping_address][:phone] = gateway_options[:shipping_address][:phone] data[:customer_details][:shipping_address][:country_code] = get_alpha3_country_code(gateway_options[:shipping_address][:country]) return data end def get_alpha3_country_code(country_code) c = ISO3166::Country.new(country_code) c.data["alpha3"] rescue return "" end end end