lib/gateway/Gateway.rb in stone_ecommerce-1.0.3 vs lib/gateway/Gateway.rb in stone_ecommerce-1.2.3
- old
+ new
@@ -1,441 +1,621 @@
require_relative '../../lib/stone_ecommerce'
+module Gateway
-class Gateway
+ class Gateway
+ extend Gem::Deprecate
- attr_reader :serviceEnvironment
+ attr_reader :serviceEnvironment
- attr_reader :merchantKey
+ attr_reader :merchantKey
- def initialize(environment=:staging, merchantKey)
- @serviceEnvironment = environment
- @merchantKey = merchantKey
- @@SERVICE_HEADERS = {:MerchantKey => "#{@merchantKey}", :Accept => 'application/json', :"Content-Type" => 'application/json'}
- end
+ def initialize(environment=:sandbox, merchantKey)
+ @serviceEnvironment = environment
+ @merchantKey = merchantKey
+ @@SERVICE_HEADERS = {:MerchantKey => "#{@merchantKey}", :Accept => 'application/json', :"Content-Type" => 'application/json'}
+ end
- # URL de producao
- @@SERVICE_URL_PRODUCTION = 'https://transaction.stone.com.br'
+ # URL de producao
+ @@SERVICE_URL_PRODUCTION = 'https://transaction.stone.com.br'
- # URL de homologacao
- @@SERVICE_URL_STAGING = ''
+ # URL de homologacao
+ @@SERVICE_URL_STAGING = ''
- # URL de sandbox
- @@SERVICE_URL_SANDBOX = ''
+ # URL de sandbox
+ @@SERVICE_URL_SANDBOX = 'https://transaction.stone.com.br'
- # URL do postnotification de producao
- @@SERVICE_URL_NOTIFICATION_PRODUCTION = ''
+ # URL do postnotification de producao
+ @@SERVICE_URL_NOTIFICATION_PRODUCTION = ''
- # URL do postnotification de sandbox
- @@SERVICE_URL_NOTIFICATION_SANDBOX = ''
+ # URL do postnotification de sandbox
+ @@SERVICE_URL_NOTIFICATION_SANDBOX = ''
- # permite que o integrador adicione uma busca por transacoes utilizando alguns criterios
- def Query(querySaleRequestEnum, key)
- # try, tenta fazer o request
- begin
- # se for homologacao faz a chamada por aqui
- if @serviceEnvironment == :staging
- getRequest(@@SERVICE_URL_STAGING + '/Sale/Query/' + querySaleRequestEnum + '=' + key)
+ # permite que o integrador adicione uma busca por transacoes utilizando alguns criterios
+ def Query(querySaleRequestEnum, key)
+ # try, tenta fazer o request
+ begin
+ # se for homologacao faz a chamada por aqui
+ if @serviceEnvironment == :staging
+ getRequest(@@SERVICE_URL_STAGING + '/Sale/Query/' + querySaleRequestEnum + '=' + key)
- # se for producao, faz a chamada por aqui
- elsif @serviceEnvironment == :production
- getRequest(@@SERVICE_URL_PRODUCTION + '/Sale/Query/' + querySaleRequestEnum + '=' + key)
+ # se for producao, faz a chamada por aqui
+ elsif @serviceEnvironment == :production
+ getRequest(@@SERVICE_URL_PRODUCTION + '/Sale/Query/' + querySaleRequestEnum + '=' + key)
- # se for sandbox
- elsif @serviceEnvironment == :sandbox
- getRequest(@@SERVICE_URL_SANDBOX + '/Sale/Query/' + querySaleRequestEnum + '=' + key)
- end
+ # se for sandbox
+ elsif @serviceEnvironment == :sandbox
+ getRequest(@@SERVICE_URL_SANDBOX + '/Sale/Query/' + querySaleRequestEnum + '=' + key)
+ end
- # se der algum erro, trata aqui
- rescue Exception => e
- return e.message
+ # se der algum erro, trata aqui
+ rescue Exception => e
+ return e.message
+ end
end
- end
- # criar uma transacao na plataforma One utilizando um ou mais meios de pagamento
- def CreateSale(createSaleRequest)
+ # criar uma transacao na plataforma One utilizando um ou mais meios de pagamento
+ def CreateSale(createSaleRequest)
- saleHash = createSaleRequest.to_json
+ saleHash = createSaleRequest.to_json
- saleHash['BoletoTransactionCollection'] = []
+ saleHash['BoletoTransactionCollection'] = []
- saleHash['CreditCardTransactionCollection'] = []
+ saleHash['CreditCardTransactionCollection'] = []
- saleHash['ShoppingCartCollection'] = []
+ saleHash['ShoppingCartCollection'] = []
- begin
- # transforma a colecao de boleto em json
- if createSaleRequest.BoletoTransactionCollection.any? == false || createSaleRequest.BoletoTransactionCollection.nil?
- saleHash['BoletoTransactionCollection'] = nil
+ begin
+ # transforma a colecao de boleto em json
+ if createSaleRequest.BoletoTransactionCollection.any? == false || createSaleRequest.BoletoTransactionCollection.nil?
+ saleHash['BoletoTransactionCollection'] = nil
- else
- createSaleRequest.BoletoTransactionCollection.each_with_index do |boleto, index|
- b = boleto.to_json
- saleHash['BoletoTransactionCollection'] << b
+ else
+ createSaleRequest.BoletoTransactionCollection.each_with_index do |boleto, index|
+ b = boleto.to_json
+ saleHash['BoletoTransactionCollection'] << b
- if boleto.Options.to_json.any?
- boleto_options = boleto.Options.to_json
- saleHash['BoletoTransactionCollection'][index]['Options'] = boleto_options
- else
- saleHash['BoletoTransactionCollection'][index]['Options'] = nil
- end
+ if boleto.Options.to_json.any?
+ boleto_options = boleto.Options.to_json
+ saleHash['BoletoTransactionCollection'][index]['Options'] = boleto_options
+ else
+ saleHash['BoletoTransactionCollection'][index]['Options'] = nil
+ end
- if boleto.BillingAddress.to_json.any?
- boleto_billing_address = boleto.BillingAddress.to_json
- saleHash['BoletoTransactionCollection'][index]['BillingAddress'] = boleto_billing_address
- else
- saleHash['BoletoTransactionCollection'][index]['BillingAddress'] = nil
+ if boleto.BillingAddress.to_json.any?
+ boleto_billing_address = boleto.BillingAddress.to_json
+ saleHash['BoletoTransactionCollection'][index]['BillingAddress'] = boleto_billing_address
+ else
+ saleHash['BoletoTransactionCollection'][index]['BillingAddress'] = nil
+ end
end
end
- end
- # transforma a colecao de cartao de credito em json
- if createSaleRequest.CreditCardTransactionCollection.any? == false || createSaleRequest.CreditCardTransactionCollection.nil?
- saleHash['CreditCardTransactionCollection'] = nil
- else
- createSaleRequest.CreditCardTransactionCollection.each_with_index do |creditCard, index|
- c = creditCard.to_json
- saleHash['CreditCardTransactionCollection'] << c
+ # transforma a colecao de cartao de credito em json
+ if createSaleRequest.CreditCardTransactionCollection.any? == false || createSaleRequest.CreditCardTransactionCollection.nil?
+ saleHash['CreditCardTransactionCollection'] = nil
+ else
+ createSaleRequest.CreditCardTransactionCollection.each_with_index do |creditCard, index|
+ c = creditCard.to_json
+ saleHash['CreditCardTransactionCollection'] << c
- if creditCard.Options.to_json.any?
- credit_card_options = creditCard.Options.to_json
- saleHash['CreditCardTransactionCollection'][index]['Options'] = credit_card_options
- else
- saleHash['CreditCardTransactionCollection'][index]['Options'] = nil
- end
+ if creditCard.Options.to_json.any?
+ credit_card_options = creditCard.Options.to_json
+ saleHash['CreditCardTransactionCollection'][index]['Options'] = credit_card_options
+ else
+ saleHash['CreditCardTransactionCollection'][index]['Options'] = nil
+ end
- if creditCard.Recurrency.to_json.any?
- credit_card_recurrency = creditCard.Recurrency.to_json
- saleHash['CreditCardTransactionCollection'][index]['Recurrency'] = credit_card_recurrency
- else
- saleHash['CreditCardTransactionCollection'][index]['Recurrency'] = nil
+ if creditCard.Recurrency.to_json.any?
+ credit_card_recurrency = creditCard.Recurrency.to_json
+ saleHash['CreditCardTransactionCollection'][index]['Recurrency'] = credit_card_recurrency
+ else
+ saleHash['CreditCardTransactionCollection'][index]['Recurrency'] = nil
+ end
+
+ if creditCard.CreditCard.to_json.any?
+ credit_card_item = creditCard.CreditCard.to_json
+ saleHash['CreditCardTransactionCollection'][index]['CreditCard'] = credit_card_item
+
+ if creditCard.CreditCard.BillingAddress.to_json.any?
+ credit_card_billing_address = creditCard.CreditCard.BillingAddress.to_json
+ saleHash['CreditCardTransactionCollection'][index]['CreditCard']['BillingAddress'] = credit_card_billing_address
+ else
+ saleHash['CreditCardTransactionCollection'][index]['CreditCard']['BillingAddress'] = nil
+ end
+
+ else
+ saleHash['CreditCardTransactionCollection'][index]['CreditCard'] = nil
+ end
end
+ end
- if creditCard.CreditCard.to_json.any?
- credit_card_item = creditCard.CreditCard.to_json
- saleHash['CreditCardTransactionCollection'][index]['CreditCard'] = credit_card_item
+ # transforma a colecao de shoppingcart em json
+ if createSaleRequest.ShoppingCartCollection.any? == false || createSaleRequest.ShoppingCartCollection.nil?
+ saleHash['ShoppingCartCollection'] = nil
+ else
+ createSaleRequest.ShoppingCartCollection.each_with_index do |shoppingCart, index|
+ s = shoppingCart.to_json
+ saleHash['ShoppingCartCollection'] << s
- if creditCard.CreditCard.BillingAddress.to_json.any?
- credit_card_billing_address = creditCard.CreditCard.BillingAddress.to_json
- saleHash['CreditCardTransactionCollection'][index]['CreditCard']['BillingAddress'] = credit_card_billing_address
+ if shoppingCart.DeliveryAddress.to_json.any?
+ delivery_address = shoppingCart.DeliveryAddress.to_json
+ saleHash['ShoppingCartCollection'][index]['DeliveryAddress'] = delivery_address
else
- saleHash['CreditCardTransactionCollection'][index]['CreditCard']['BillingAddress'] = nil
+ saleHash['ShoppingCartCollection'][index]['DeliveryAddress'] = nil
end
- else
- saleHash['CreditCardTransactionCollection'][index]['CreditCard'] = nil
+ if shoppingCart.ShoppingCartItemCollection.any?
+ shoppingCart.ShoppingCartItemCollection.each_with_index do |cartItem, cartIndex|
+ item = cartItem.to_json
+ saleHash['ShoppingCartCollection'][index]['ShoppingCartItemCollection'][cartIndex] = item
+ end
+ else
+ saleHash['ShoppingCartCollection'][index]['ShoppingCartItemCollection'] = nil
+ end
end
end
- end
- # transforma a colecao de shoppingcart em json
- if createSaleRequest.ShoppingCartCollection.any? == false || createSaleRequest.ShoppingCartCollection.nil?
- saleHash['ShoppingCartCollection'] = nil
- else
- createSaleRequest.ShoppingCartCollection.each_with_index do |shoppingCart, index|
- s = shoppingCart.to_json
- saleHash['ShoppingCartCollection'] << s
+ # transforma objeto options em json
+ if createSaleRequest.Options.to_json.any?
+ o = createSaleRequest.Options.to_json
+ saleHash['Options'] = o
+ else
+ saleHash['Options'] = nil
+ end
- if shoppingCart.DeliveryAddress.to_json.any?
- delivery_address = shoppingCart.DeliveryAddress.to_json
- saleHash['ShoppingCartCollection'][index]['DeliveryAddress'] = delivery_address
- else
- saleHash['ShoppingCartCollection'][index]['DeliveryAddress'] = nil
- end
+ # transforma objeto order em json
+ if createSaleRequest.Order.to_json.any?
+ order = createSaleRequest.Order.to_json
+ saleHash['Order'] = order
+ else
+ saleHash['Order'] = nil
+ end
- if shoppingCart.ShoppingCartItemCollection.any?
- shoppingCart.ShoppingCartItemCollection.each_with_index do |cartItem, cartIndex|
- item = cartItem.to_json
- saleHash['ShoppingCartCollection'][index]['ShoppingCartItemCollection'][cartIndex] = item
- end
+ # transforma objeto merchant em json
+ if createSaleRequest.Merchant.to_json.any?
+ merchant = createSaleRequest.Merchant.to_json
+ saleHash['Merchant'] = merchant
+ else
+ saleHash['Merchant'] = nil
+ end
+
+ # transforma objeto request data em json
+ if createSaleRequest.RequestData.to_json.any?
+ request_data = createSaleRequest.RequestData.to_json
+ saleHash['RequestData'] = request_data
+ else
+ saleHash['RequestData'] = nil
+ end
+
+ # transforma o objeto Buyer em json
+ if createSaleRequest.Buyer.AddressCollection.any?
+ b = createSaleRequest.Buyer.to_json
+ saleHash['Buyer'] = b
+
+ saleHash['Buyer']['AddressCollection'] = []
+ createSaleRequest.Buyer.AddressCollection.each do |address|
+ a = address.to_json
+ saleHash['Buyer']['AddressCollection'] << a
+ end
+ else
+ buyer_hash = createSaleRequest.Buyer.to_json
+ buyer_hash.delete('AddressCollection')
+ if buyer_hash.blank? == false
+ b = createSaleRequest.Buyer.to_json
+ saleHash['Buyer'] = b
+ saleHash['Buyer']['AddressCollection'] = nil
else
- saleHash['ShoppingCartCollection'][index]['ShoppingCartItemCollection'] = nil
+ saleHash['Buyer'] = nil
end
end
- end
- # transforma objeto options em json
- if createSaleRequest.Options.to_json.any?
- o = createSaleRequest.Options.to_json
- saleHash['Options'] = o
- else
- saleHash['Options'] = nil
+ rescue Exception => e
+ return e.message
end
- # transforma objeto order em json
- if createSaleRequest.Order.to_json.any?
- order = createSaleRequest.Order.to_json
- saleHash['Order'] = order
- else
- saleHash['Order'] = nil
+ if @serviceEnvironment == :staging
+ url = @@SERVICE_URL_STAGING + '/Sale/'
+ elsif @serviceEnvironment == :production
+ url = @@SERVICE_URL_PRODUCTION + '/Sale/'
+ elsif @serviceEnvironment == :sandbox
+ url = @@SERVICE_URL_SANDBOX + '/Sale/'
end
+ postRequest(saleHash.to_json, url)
+ end
- # transforma objeto merchant em json
- if createSaleRequest.Merchant.to_json.any?
- merchant = createSaleRequest.Merchant.to_json
- saleHash['Merchant'] = merchant
- else
- saleHash['Merchant'] = nil
+ # permite forcar a retentativa manualmente de uma transacao (podendo ser tambem uma recorrencia) nao autorizada
+ def Retry(retrySaleRequest)
+ saleHash = retrySaleRequest.to_json
+ saleHash['RetrySaleCreditCardTransactionCollection'] = []
+
+ begin
+ if retrySaleRequest.RetrySaleCreditCardTransactionCollection != nil
+ retrySaleRequest.RetrySaleCreditCardTransactionCollection.each do |retrySale|
+ r = retrySale.to_json
+ saleHash['RetrySaleCreditCardTransactionCollection'] << r
+ end
+ end
+ if retrySaleRequest.Options.to_json.any?
+ retry_options = retrySaleRequest.Options.to_json
+ saleHash['Options'] = retry_options
+ else
+ saleHash['Options'] = nil
+ end
+ rescue Exception => e
+ return e.message
end
+ if @serviceEnvironment == :staging
+ url = @@SERVICE_URL_STAGING + '/Sale/Retry'
+ elsif @serviceEnvironment == :production
+ url = @@SERVICE_URL_PRODUCTION + '/Sale/Retry'
+ elsif @serviceEnvironment == :sandbox
+ url = @@SERVICE_URL_SANDBOX + '/Sale/Retry'
+ end
+ postRequest(saleHash.to_json, url)
+ end
- # transforma objeto request data em json
- if createSaleRequest.RequestData.to_json.any?
- request_data = createSaleRequest.RequestData.to_json
- saleHash['RequestData'] = request_data
- else
- saleHash['RequestData'] = nil
+ # eh uma forma de desfazer uma transação com cartao de credito mesmo a transacao sendo capturada
+ def Cancel(cancelSaleRequest)
+ saleHash = cancelSaleRequest.to_json
+ saleHash['CreditCardTransactionCollection'] = []
+
+ begin
+ if cancelSaleRequest.CreditCardTransactionCollection != nil
+ cancelSaleRequest.CreditCardTransactionCollection.each do |creditCard|
+ c = creditCard.to_json
+ saleHash['CreditCardTransactionCollection'] << c
+ end
+ end
+ rescue Exception => e
+ return e.message
end
+ if @serviceEnvironment == :staging
+ url = @@SERVICE_URL_STAGING + '/Sale/Cancel'
+ elsif @serviceEnvironment == :production
+ url = @@SERVICE_URL_PRODUCTION + '/Sale/Cancel'
+ elsif @serviceEnvironment == :sandbox
+ url = @@SERVICE_URL_SANDBOX + '/Sale/Cancel'
+ end
+ postRequest(saleHash.to_json, url)
+ end
- # transforma o objeto Buyer em json
- if createSaleRequest.Buyer.AddressCollection.any?
- b = createSaleRequest.Buyer.to_json
- saleHash['Buyer'] = b
+ # confirmacao de uma transacao de cartao de credito que ja fora autorizada
+ def Capture(captureRequest)
+ saleHash = captureRequest.to_json
+ saleHash['CreditCardTransactionCollection'] = []
- saleHash['Buyer']['AddressCollection'] = []
- createSaleRequest.Buyer.AddressCollection.each do |address|
- a = address.to_json
- saleHash['Buyer']['AddressCollection'] << a
+ begin
+ if captureRequest.CreditCardTransactionCollection != nil
+ captureRequest.CreditCardTransactionCollection.each do |creditCard|
+ c = creditCard.to_json
+ saleHash['CreditCardTransactionCollection'] << c
+ end
end
- else
- buyer_hash = createSaleRequest.Buyer.to_json
- buyer_hash.delete('AddressCollection')
- if buyer_hash.blank? == false
- b = createSaleRequest.Buyer.to_json
- saleHash['Buyer'] = b
- saleHash['Buyer']['AddressCollection'] = nil
- else
- saleHash['Buyer'] = nil
- end
+ rescue Exception => e
+ return e.message
end
+ if @serviceEnvironment == :staging
+ url = @@SERVICE_URL_STAGING + '/Sale/Capture'
+ elsif @serviceEnvironment == :production
+ url = @@SERVICE_URL_PRODUCTION + '/Sale/Capture'
+ elsif @serviceEnvironment == :sandbox
+ url = @@SERVICE_URL_SANDBOX + '/Sale/Capture'
+ end
+ postRequest(saleHash.to_json, url)
+ end
- rescue Exception => e
- return e.message
+ # faz um parse do xml de post notificaton
+ def ParseXmlToNotification(xml)
+ begin
+ response = PostNotification.ParseNotification(xml)
+ rescue Exception => err
+ return err.message
+ end
+
+ return response
end
- if @serviceEnvironment == :staging
- url = @@SERVICE_URL_STAGING + '/Sale/'
- elsif @serviceEnvironment == :production
- url = @@SERVICE_URL_PRODUCTION + '/Sale/'
- elsif @serviceEnvironment == :sandbox
- url = @@SERVICE_URL_SANDBOX + '/Sale/'
+ # faz uma requisicao e retorna uma string com o transaction report file
+ def TransactionReportFile(date)
+ begin
+ if @serviceEnvironment == :staging
+ url = @@SERVICE_URL_NOTIFICATION_PRODUCTION + date.strftime("%Y%m%d")
+ elsif @serviceEnvironment == :production
+ url = @@SERVICE_URL_NOTIFICATION_PRODUCTION + date.strftime("%Y%m%d")
+ elsif @serviceEnvironment == :sandbox
+ url = @@SERVICE_URL_NOTIFICATION_SANDBOX + date.strftime("%Y%m%d")
+ end
+
+ response = getReportFile(url)
+
+ rescue RestClient::ExceptionWithResponse => err
+ return err.response
+ end
+ return response
end
- postRequest(saleHash.to_json, url)
- end
- # permite forcar a retentativa manualmente de uma transacao (podendo ser tambem uma recorrencia) nao autorizada
- def Retry(retrySaleRequest)
- saleHash = retrySaleRequest.to_json
- saleHash['RetrySaleCreditCardTransactionCollection'] = []
+ # faz o download do transaction report file e salva no computador em .txt
+ def TransactionReportFileDownloader(date, file_name, path)
+ begin
+ path = path + file_name + '.txt'
- begin
- if retrySaleRequest.RetrySaleCreditCardTransactionCollection != nil
- retrySaleRequest.RetrySaleCreditCardTransactionCollection.each do |retrySale|
- r = retrySale.to_json
- saleHash['RetrySaleCreditCardTransactionCollection'] << r
- end
+ response = TransactionReportFile(date)
+
+ File.write(path, response)
+ rescue RestClient::ExceptionWithResponse => err
+ return err.response
end
- if retrySaleRequest.Options.to_json.any?
- retry_options = retrySaleRequest.Options.to_json
- saleHash['Options'] = retry_options
- else
- saleHash['Options'] = nil
+ end
+
+ # faz o parse da string recebida do transaction report file e retorna um hash
+ def TransactionReportFileParser(file_to_parse)
+ transaction_report_file = TransactionReportFile.new
+ begin
+ response = transaction_report_file.TransactionReportFileParser(file_to_parse)
+ rescue Exception => err
+ return err
end
- rescue Exception => e
- return e.message
+ return response
end
- if @serviceEnvironment == :staging
- url = @@SERVICE_URL_STAGING + '/Sale/Retry'
- elsif @serviceEnvironment == :production
- url = @@SERVICE_URL_PRODUCTION + '/Sale/Retry'
- elsif @serviceEnvironment == :sandbox
- url = @@SERVICE_URL_SANDBOX + '/Sale/Retry'
+
+ # DEPRECATED
+ def InstantBuyKey(instant_buy_key)
+ GetCreditCard(instant_buy_key)
end
- postRequest(saleHash.to_json, url)
- end
+ deprecate :InstantBuyKey, :GetCreditCard, 2016, 04
- # eh uma forma de desfazer uma transação com cartao de credito mesmo a transacao sendo capturada
- def Cancel(cancelSaleRequest)
- saleHash = cancelSaleRequest.to_json
- saleHash['CreditCardTransactionCollection'] = []
+ # DEPRECATED
+ def BuyerKey(buyer_key)
+ GetCreditCardWithBuyerKey(buyer_key)
+ end
+ deprecate :BuyerKey, :GetCreditCardWithBuyerKey, 2016, 04
- begin
- if cancelSaleRequest.CreditCardTransactionCollection != nil
- cancelSaleRequest.CreditCardTransactionCollection.each do |creditCard|
- c = creditCard.to_json
- saleHash['CreditCardTransactionCollection'] << c
+ # faz um get credit card com buyer key
+ def GetCreditCardWithBuyerKey(buyer_key)
+ # try, tenta fazer o request
+ begin
+
+ # se for homologacao faz a chamada por aqui
+ if @serviceEnvironment == :staging
+ response = getRequest(@@SERVICE_URL_STAGING + '/CreditCard/BuyerKey=' + buyer_key)
+
+ # se for producao, faz a chamada por aqui
+ elsif @serviceEnvironment == :production
+ response = getRequest(@@SERVICE_URL_PRODUCTION + '/CreditCard/BuyerKey=' + buyer_key)
+
+ # se for sandbox, faz a chamada por aqui
+ elsif @serviceEnvironment == :sandbox
+ response = getRequest(@@SERVICE_URL_SANDBOX + '/CreditCard/BuyerKey=' + buyer_key)
end
+
+ # se der algum erro, trata aqui
+ rescue Exception => e
+ return e.message
end
- rescue Exception => e
- return e.message
+
+ # se nao houver erros, retorna o objeto
+ response
end
- if @serviceEnvironment == :staging
- url = @@SERVICE_URL_STAGING + '/Sale/Cancel'
- elsif @serviceEnvironment == :production
- url = @@SERVICE_URL_PRODUCTION + '/Sale/Cancel'
- elsif @serviceEnvironment == :sandbox
- url = @@SERVICE_URL_SANDBOX + '/Sale/Cancel'
- end
- postRequest(saleHash.to_json, url)
- end
- # confirmacao de uma transacao de cartao de credito que ja fora autorizada
- def Capture(captureRequest)
- saleHash = captureRequest.to_json
- saleHash['CreditCardTransactionCollection'] = []
+ # Faz um get no cartão de crédito
+ def GetCreditCard(instant_buy_key)
+ # try, tenta fazer o request
+ begin
+ # se for homologacao faz a chamada por aqui
+ if @serviceEnvironment == :staging
+ getRequest(@@SERVICE_URL_STAGING + '/CreditCard/' + instant_buy_key)
- begin
- if captureRequest.CreditCardTransactionCollection != nil
- captureRequest.CreditCardTransactionCollection.each do |creditCard|
- c = creditCard.to_json
- saleHash['CreditCardTransactionCollection'] << c
+ # se for producao, faz a chamada por aqui
+ elsif @serviceEnvironment == :production
+ getRequest(@@SERVICE_URL_PRODUCTION + '/CreditCard/' + instant_buy_key)
+
+ # se for sandbox
+ elsif @serviceEnvironment == :sandbox
+ getRequest(@@SERVICE_URL_SANDBOX + '/CreditCard/' + instant_buy_key)
end
+
+ # se der algum erro, trata aqui
+ rescue Exception => e
+ return e.message
end
- rescue Exception => e
- return e.message
end
- if @serviceEnvironment == :staging
- url = @@SERVICE_URL_STAGING + '/Sale/Capture'
- elsif @serviceEnvironment == :production
- url = @@SERVICE_URL_PRODUCTION + '/Sale/Capture'
- elsif @serviceEnvironment == :sandbox
- url = @@SERVICE_URL_SANDBOX + '/Sale/Capture'
- end
- postRequest(saleHash.to_json, url)
- end
- # faz um parse do xml de post notificaton
- def ParseXmlToNotification(xml)
- begin
- response = PostNotification.ParseNotification(xml)
- rescue Exception => err
- return err.message
- end
+ # Cria um cartão de crédito
+ def CreateCreditCard(create_instant_buy_data)
+ sale_hash = create_instant_buy_data.to_json
+ begin
+ if create_instant_buy_data.BillingAddress.to_json.any?
+ sale_hash['BillingAddress'] = create_instant_buy_data.BillingAddress.to_json
+ else
+ sale_hash['BillingAddress'] = nil
+ end
- return response
- end
+ # se for homologacao faz a chamada por aqui
+ if @serviceEnvironment == :staging
+ url = @@SERVICE_URL_STAGING + '/CreditCard/'
- # faz uma requisicao e retorna uma string com o transaction report file
- def TransactionReportFile(date)
- begin
- if @serviceEnvironment == :staging
- url = @@SERVICE_URL_NOTIFICATION_PRODUCTION + date.strftime("%Y%m%d")
- elsif @serviceEnvironment == :production
- url = @@SERVICE_URL_NOTIFICATION_PRODUCTION + date.strftime("%Y%m%d")
- elsif @serviceEnvironment == :sandbox
- url = @@SERVICE_URL_NOTIFICATION_SANDBOX + date.strftime("%Y%m%d")
- end
+ # se for producao, faz a chamada por aqui
+ elsif @serviceEnvironment == :production
+ url = @@SERVICE_URL_PRODUCTION + '/CreditCard/'
- response = getReportFile(url)
+ # se for sandbox, faz a chamada por aqui
+ elsif @serviceEnvironment == :sandbox
+ url = @@SERVICE_URL_SANDBOX + '/CreditCard/'
+ end
- rescue RestClient::ExceptionWithResponse => err
- return err.response
+ response = postRequest(sale_hash.to_json, url)
+
+ # se der algum erro, trata aqui
+ rescue Exception => e
+ return e.message
+ end
+ # se nao houver erros, retorna o objeto
+ response
end
- return response
- end
- # faz o download do transaction report file e salva no computador em .txt
- def TransactionReportFileDownloader(date, file_name, path)
- begin
- path = path + file_name + '.txt'
+ # Atualiza o cartão de crédito
+ def UpdateCreditCard(instant_buy_key, update_instant_buy_data_request)
+ begin
+ sale_hash = update_instant_buy_data_request.to_json
- response = TransactionReportFile(date)
+ # se for homologacao faz a chamada por aqui
+ if @serviceEnvironment == :staging
+ url = @@SERVICE_URL_STAGING + '/CreditCard/' + instant_buy_key
- File.write(path, response)
- rescue RestClient::ExceptionWithResponse => err
- return err.response
+ # se for producao, faz a chamada por aqui
+ elsif @serviceEnvironment == :production
+ url = @@SERVICE_URL_PRODUCTION + '/CreditCard/' + instant_buy_key
+
+ # se for sandbox, faz a chamada por aqui
+ elsif @serviceEnvironment == :sandbox
+ url = @@SERVICE_URL_SANDBOX + '/CreditCard/' + instant_buy_key
+ end
+
+ response = patchRequest(sale_hash.to_json, url)
+ # se der algum erro, trata aqui
+ rescue Exception => e
+ return e.message
+ end
+ # se nao houver erros, retorna o objeto
+ response
end
- end
- # faz o parse da string recebida do transaction report file e retorna um hash
- def TransactionReportFileParser(file_to_parse)
- transaction_report_file = TransactionReportFile.new
- begin
- response = transaction_report_file.TransactionReportFileParser(file_to_parse)
- rescue Exception => err
- return err
+ # Deleta um cartão de crédito
+ def DeleteCreditCard(instant_buy_key)
+ # try, tenta fazer o request
+ begin
+ # se for homologacao faz a chamada por aqui
+ if @serviceEnvironment == :staging
+ deleteRequest(@@SERVICE_URL_STAGING + '/CreditCard/' + instant_buy_key)
+
+ # se for producao, faz a chamada por aqui
+ elsif @serviceEnvironment == :production
+ deleteRequest(@@SERVICE_URL_PRODUCTION + '/CreditCard/' + instant_buy_key)
+
+ # se for sandbox
+ elsif @serviceEnvironment == :sandbox
+ deleteRequest(@@SERVICE_URL_SANDBOX + '/CreditCard/' + instant_buy_key)
+ end
+
+ # se der algum erro, trata aqui
+ rescue Exception => e
+ return e.message
+ end
end
- return response
- end
- # faz uma requisicao com instant buy key
- def InstantBuyKey(instant_buy_key)
- # try, tenta fazer o request
- begin
+ # Busca um buyer
+ def GetBuyer(buyer_key)
+ # try, tenta fazer o request
+ begin
- # se for homologacao faz a chamada por aqui
- if @serviceEnvironment == :staging
- getRequest(@@SERVICE_URL_STAGING + '/CreditCard/' + instant_buy_key)
+ # se for homologacao faz a chamada por aqui
+ if @serviceEnvironment == :staging
+ response = getRequest(@@SERVICE_URL_STAGING + '/Buyer/' + buyer_key)
- # se for producao, faz a chamada por aqui
- elsif @serviceEnvironment == :production
- getRequest(@@SERVICE_URL_PRODUCTION + '/CreditCard/' + instant_buy_key)
+ # se for producao, faz a chamada por aqui
+ elsif @serviceEnvironment == :production
+ response = getRequest(@@SERVICE_URL_PRODUCTION + '/Buyer/' + buyer_key)
- # se for sandbox
- elsif @serviceEnvironment == :sandbox
- getRequest(@@SERVICE_URL_SANDBOX + '/CreditCard/' + instant_buy_key)
+ # se for sandbox, faz a chamada por aqui
+ elsif @serviceEnvironment == :sandbox
+ response = getRequest(@@SERVICE_URL_SANDBOX + '/Buyer/' + buyer_key)
+ end
+
+ # se der algum erro, trata aqui
+ rescue Exception => e
+ return e.message
end
- # se der algum erro, trata aqui
- rescue Exception => e
- return e.message
+ # se nao houver erros, retorna o objeto
+ response
end
- end
- # faz uma requisicao com buyer key
- def BuyerKey(buyer_key)
- # try, tenta fazer o request
- begin
+ # Cria um buyer
+ def CreateBuyer(buyer_request)
+ sale_hash = buyer_request.to_json
+ sale_hash['AddressCollection'] = []
+ begin
+ if buyer_request.AddressCollection.any? == false || buyer_request.AddressCollection.nil?
+ sale_hash['AddressCollection'] = nil
+ else
+ buyer_request.AddressCollection.each do |address|
+ a = address.to_json
+ sale_hash['AddressCollection'] << a
+ end
+ end
- # se for homologacao faz a chamada por aqui
- if @serviceEnvironment == :staging
- response = getRequest(@@SERVICE_URL_STAGING + '/CreditCard/' + buyer_key + '/BuyerKey')
+ # se for homologacao faz a chamada por aqui
+ if @serviceEnvironment == :staging
+ url = @@SERVICE_URL_STAGING + '/Buyer/'
- # se for producao, faz a chamada por aqui
- elsif @serviceEnvironment == :production
- response = getRequest(@@SERVICE_URL_PRODUCTION + '/CreditCard/' + buyer_key + '/BuyerKey')
+ # se for producao, faz a chamada por aqui
+ elsif @serviceEnvironment == :production
+ url = @@SERVICE_URL_PRODUCTION + '/Buyer/'
- # se for sandbox, faz a chamada por aqui
- elsif @serviceEnvironment == :sandbox
- response = getRequest(@@SERVICE_URL_SANDBOX + '/CreditCard/' + buyer_key + '/BuyerKey')
+ # se for sandbox, faz a chamada por aqui
+ elsif @serviceEnvironment == :sandbox
+ url = @@SERVICE_URL_SANDBOX + '/Buyer/'
+ end
+
+ response = postRequest(sale_hash.to_json, url)
+ rescue Exception => e
+ return e.message
end
+ response
+ end
- # se der algum erro, trata aqui
- rescue Exception => e
- return e.message
+ # funcao de post generica
+ def postRequest(payload, url)
+ response = nil
+ begin
+ response = RestClient.post(url, payload, headers=@@SERVICE_HEADERS)
+ rescue RestClient::ExceptionWithResponse => err
+ return err.response
+ end
+ json_response = JSON.load response
+ json_response
end
- # se nao houver erros, retorna o objeto
- response
- end
+ # funcao patch generica
+ def patchRequest(payload, url)
+ begin
+ response = RestClient.patch(url, payload, headers=@@SERVICE_HEADERS)
+ rescue RestClient::ExceptionWithResponse => err
+ return err.response
+ end
+ json_response = JSON.load response
+ json_response
+ end
- # funcao de post generica
- def postRequest(payload, url)
- response = nil
- begin
- response = RestClient.post(url, payload, headers=@@SERVICE_HEADERS)
- rescue RestClient::ExceptionWithResponse => err
- return err.response
+ # funcao de delete generica
+ def deleteRequest(url)
+ begin
+ response = RestClient.delete(url, headers=@@SERVICE_HEADERS)
+ rescue RestClient::ExceptionWithResponse => err
+ return err.response
+ end
+ json_response = JSON.load response
+ json_response
end
- json_response = JSON.load response
- json_response
- end
- # funcao get generica
- def getRequest(url)
- response = nil
- begin
- response = RestClient.get(url, headers=@@SERVICE_HEADERS)
- rescue RestClient::ExceptionWithResponse => err
- return err.response
+ # funcao get generica
+ def getRequest(url)
+ response = nil
+ begin
+ response = RestClient.get(url, headers=@@SERVICE_HEADERS)
+ rescue RestClient::ExceptionWithResponse => err
+ return err.response
+ end
+ json_response = JSON.load response
+ json_response
end
- json_response = JSON.load response
- json_response
- end
- def getReportFile(url)
- begin
- response = RestClient.get(url, headers={:MerchantKey => "#{@merchantKey}"})
- rescue RestClient::ExceptionWithResponse => err
- return err.response
+ def getReportFile(url)
+ begin
+ response = RestClient.get(url, headers={:MerchantKey => "#{@merchantKey}"})
+ rescue RestClient::ExceptionWithResponse => err
+ return err.response
+ end
+ return response
end
- return response
end
end