module RubyEcommClient class Client include ResponseConverter ENDPOINTS_MANAGER = { :development => 'http://bonsai.dev.glbt1.gdg/bonsai/bonsaimanager/service.asmx', :test => 'http://bonsai.dev.glbt1.gdg/bonsai/bonsaimanager/service.asmx', :production => 'http://gdbonsaiXv01.prod.mesa1.gdg/bonsai/bonsaimanager/service.asmx', :qa => 'http://bonsai.test.glbt1.gdg/bonsai/bonsaimanager/service.asmx' } ENDPOINTS_PROFILE = { :development => 'http://gdcomm.dev.glbt1.gdg/WSgdInstantPurchasePublic/Service.asmx', :test => 'http://gdcomm.dev.glbt1.gdg/WSgdInstantPurchasePublic/Service.asmx', :production => 'http://gdcomm.godaddy.com/WSgdInstantPurchasePublic/Service.asmx', :qa => 'http://gdcomm.test.glbt1.gdg/WSgdInstantPurchasePublic/Service.asmx' } ENDPOINTS_PURCHASE = { :development => 'https://bonsaipurchase.dev.glbt1.gdg/BonsaiPurchase/Service.asmx', :test => 'https://bonsaipurchase.dev.glbt1.gdg/BonsaiPurchase/Service.asmx', :production => 'https://bonsaipurchase.prod.mesa1.gdg/BonsaiPurchase/Service.asmx', :qa => 'https://bonsaipurchase.test.glbt1.gdg/BonsaiPurchase/Service.asmx' } HOST_NAME = `hostname`.chomp ID_TYPE = 'ORION' WSDL_MANAGER = 'manager.wsdl' WSDL_PROFILE = 'profile.wsdl' WSDL_PURCHASE = 'purchase.wsdl' attr_accessor :shopper_id, :resource_type, :resource_id def initialize(shopper_id, resource_type, resource_id) raise ArgumentError.new('shopper_id must be specified') if blank?(shopper_id) raise ArgumentError.new('resource_type must be specified') if blank?(resource_type) raise ArgumentError.new('resource_id must be specified') if blank?(resource_id) @shopper_id = shopper_id @resource_type = resource_type @resource_id = resource_id end def self.environment ((defined?(Rails) ? Rails.env : ENV['RAILS_ENV']) || 'development').to_sym end def express_checkout? result = client_profile.call(:shopper_has_instant_purchase_payment, :message => { 'sShopperID' => @shopper_id }) response = convert_value(result.body[:shopper_has_instant_purchase_payment_response]) response[:b_has_instant_purchase_payment] end def account_info(source_tree_id = 0) result = client_manager.call(:get_account_xml, :message => { 'ResourceID' => @resource_id, 'ResourceType' => @resource_type, 'IDType' => ID_TYPE, 'TreeID' => source_tree_id, 'PrivateLabelID' => 1 }) response = convert_response(result.body[:get_account_xml_response]) if response[:result_code] == 0 response[:account_xml][:bonsai][:bonsai] else raise RubyEcommError.new(response[:result_code]) end end def transitions(source_tree_id = 0) transitions = account_info(source_tree_id)[:tree][:transition] transitions.is_a?(Array) ? transitions : [transitions] end def request_change(target_tree_id, client_ip = '127.0.0.1') raise ArgumentError.new('target_tree_id must be specified') if blank?(target_tree_id) if express_checkout? request_change_via_express(target_tree_id, client_ip) else request_change_via_manager(target_tree_id) end end private def blank?(value) value.nil? || value.to_s == '' end def wsdl_path "#{File.expand_path(File.dirname(__FILE__))}/wsdl/" end def shared_globals(globals) globals.convert_response_tags_to TAG_CONVERTER globals.log false globals.logger Rails.logger if defined?(Rails) globals.namespace '#Bonsai' globals.open_timeout 15 globals.read_timeout 15 end def client_manager @client_manager ||= Savon.client do |globals| shared_globals(globals) globals.wsdl "#{wsdl_path}#{WSDL_MANAGER}" globals.endpoint ENDPOINTS_MANAGER[Client.environment] end end def client_profile @client_profile ||= Savon.client do |globals| shared_globals(globals) globals.wsdl "#{wsdl_path}#{WSDL_PROFILE}" globals.endpoint ENDPOINTS_PROFILE[Client.environment] globals.namespace 'http://tempuri.org/' end end def client_purchase @client_purchase ||= Savon.client do |globals| shared_globals(globals) globals.wsdl "#{wsdl_path}#{WSDL_PURCHASE}" globals.endpoint ENDPOINTS_PURCHASE[Client.environment] globals.ssl_verify_mode :none ssl_cert_file = "config/environments/#{Client.environment}/client.crt" globals.ssl_cert_file ssl_cert_file if File.exists?(ssl_cert_file) ssl_cert_key_file = "config/environments/#{Client.environment}/client.key" globals.ssl_cert_key_file ssl_cert_key_file if File.exists?(ssl_cert_key_file) end end def request_change_via_express(target_tree_id, client_ip = '127.0.0.1') raise ArgumentError.new('target_tree_id must be specified') if blank?(target_tree_id) result = client_purchase.call(:purchase_change_account_request, :message => { 'ResourceID' => @resource_id, 'ResourceType' => @resource_type, 'IDType' => ID_TYPE, 'AccountPurchaseChangeXml' => "", 'RenewalPFID' => 0, 'RenewalPeriods' => 0, 'ItemRequestXml' => '' }) response = convert_response(result.body[:purchase_change_account_request_response]) if response[:result_code] == 0 { :used_express_checkout => true, :request_result => response[:purchase_change_account_request_result], :error => response[:error], :order_xml => response[:order_xml] } else raise RubyEcommError.new(response[:result_code], response[:change_account_request_result]) end end def request_change_via_manager(target_tree_id) raise ArgumentError.new('target_tree_id must be specified') if blank?(target_tree_id) result = client_manager.call(:change_account_request, :message => { 'ResourceID' => @resource_id, 'ResourceType' => @resource_type, 'IDType' => ID_TYPE, 'AccountChangeXml' => "", 'RenewalPFID' => 0, 'RenewalPeriods' => 0, 'ItemRequestXml' => '' }) response = convert_response(result.body[:change_account_request_response]) if response[:result_code] == 0 { :used_express_checkout => false, :request_result => response[:change_account_request_result] } else raise RubyEcommError.new(response[:result_code], response[:change_account_request_result]) end end end end