Class: Braspag::Connection
- Inherits:
-
Object
- Object
- Braspag::Connection
- Defined in:
- lib/cbraspag/core/order.rb,
lib/cbraspag/payment/eft.rb,
lib/cbraspag/payment/billet.rb,
lib/cbraspag/core/connection.rb,
lib/cbraspag/payment/credit_card.rb
Defined Under Namespace
Classes: InvalidEnvironment, InvalidMerchantId
Constant Summary collapse
- MAPPING_EFT =
{ :merchant_id => "Id_Loja", :order_id => "VENDAID", :customer_name => "nome", :customer_id => "CPF", :amount => "VALOR", :payment_method => "CODPAGAMENTO", :installments => "PARCELAS", :has_interest => "TIPOPARCELADO" }
- PRODUCTION_URL =
"https://transaction.pagador.com.br"
- HOMOLOGATION_URL =
"https://homologacao.pagador.com.br"
- PROTECTED_CARD_PRODUCTION_URL =
"https://cartaoprotegido.braspag.com.br/Services"
- PROTECTED_CARD_HOMOLOGATION_URL =
"https://homologacao.braspag.com.br/services/testenvironment"
- PROTECTED_CARD_MAPPING =
{ :request_id => "RequestId", :merchant_id => "MerchantKey", :customer_name => "CustomerName", :holder => "CardHolder", :card_number => "CardNumber", :expiration => "CardExpiration" }
- JUST_CLICK_MAPPING =
{ :request_id => "RequestId", :merchant_id => "MerchantKey", :customer_name => "CustomerName", :order_id => "OrderId", :amount => "Amount", :payment_method => "PaymentMethod", :number_installments => "NumberInstallments", :payment_type => "PaymentType", :just_click_key => "JustClickKey", :security_code => "SecurityCode" }
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#merchant_id ⇒ Object
readonly
Returns the value of attribute merchant_id.
-
#proxy_address ⇒ Object
readonly
Returns the value of attribute proxy_address.
Class Method Summary collapse
Instance Method Summary collapse
-
#archive(credit_card, customer, request_id) ⇒ Object
saves credit card in Braspag PCI Compliant.
- #authorize(order, credit_card) ⇒ Object
- #capture(order) ⇒ Object
- #convert_to(method) ⇒ Object
- #generate_billet(order, billet) ⇒ Object
-
#get_recurrency(credit_card) ⇒ Object
request the credit card info in Braspag PCI Compliant.
- #homologation? ⇒ Boolean
-
#initialize(params = {}) ⇒ Connection
constructor
A new instance of Connection.
- #post(method, *args) ⇒ Object
- #production? ⇒ Boolean
- #purchase(order, credit_card) ⇒ Object
- #recurrency(order, credit_card, request_id) ⇒ Object
- #url_for(method_name) ⇒ Object
- #void(order, partial = nil) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Connection
Returns a new instance of Connection.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/cbraspag/core/connection.rb', line 14 def initialize(params = {}) merchant_id = params[:merchant_id] env = params[:environment] raise InvalidMerchantId unless merchant_id =~ /\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\}/i raise InvalidEnvironment unless (env == :homologation || env == :production) @merchant_id = merchant_id @env = env @logger = params[:logger] @proxy_address = params[:proxy_address] end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
12 13 14 |
# File 'lib/cbraspag/core/connection.rb', line 12 def env @env end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
12 13 14 |
# File 'lib/cbraspag/core/connection.rb', line 12 def logger @logger end |
#merchant_id ⇒ Object (readonly)
Returns the value of attribute merchant_id.
12 13 14 |
# File 'lib/cbraspag/core/connection.rb', line 12 def merchant_id @merchant_id end |
#proxy_address ⇒ Object (readonly)
Returns the value of attribute proxy_address.
12 13 14 |
# File 'lib/cbraspag/core/connection.rb', line 12 def proxy_address @proxy_address end |
Class Method Details
.generate_eft(order, eft) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cbraspag/payment/eft.rb', line 16 def self.generate_eft(order, eft) return ::Response.new connection = Braspag::Connection.instance params[:merchant_id] = connection.merchant_id params = self.normalize_params(params) self.check_params(params) data = {} MAPPING.each do |k, v| case k when :payment_method data[v] = PAYMENT_METHODS[params[:payment_method]] when :amount data[v] = Utils.convert_decimal_to_string(params[:amount]) else data[v] = params[k] || "" end end html = "<form id=\"form_tef_#{params[:order_id]}\" name=\"form_tef_#{params[:order_id]}\" action=\"#{self.action_url}\" method=\"post\">" if crypto_strategy.nil? data.each do |key, value| html << "<input type=\"text\" name=\"#{key}\" value=\"#{value}\" />" end else data.delete("Id_Loja") html << "<input type=\"text\" name=\"Id_Loja\" value=\"#{params[:merchant_id]}\" />" html << "<input type=\"text\" name=\"crypt\" value=\"#{crypto_strategy.encrypt(data)}\" />" end html << "</form><script type=\"text/javascript\" charset=\"utf-8\">document.forms[\"form_tef_#{params[:order_id]}\"].submit();</script>" html end |
.get(order) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cbraspag/core/order.rb', line 3 def self.get(order) #check if order.is valid for get return ::Response response = Braspag::Poster.new(self, self.url_for(:info)).do_post(:info, { :loja => self.merchant_id, :numeroPedido => order.id.to_s }) response = Utils::convert_to_map(response.body, { :authorization => "CodigoAutorizacao", :error_code => "CodigoErro", :error_message => "MensagemErro", :payment_method => "CodigoPagamento", :payment_method_name => "FormaPagamento", :installments => "NumeroParcelas", :status => "Status", :amount => "Valor", :cancelled_at => "DataCancelamento", :paid_at => "DataPagamento", :order_date => "DataPedido", :transaction_id => "TransId", :tid => "BraspagTid" }) # raise InvalidData if response[:authorization].nil? self.get_billet(order) self.get_credit_card(order) response end |
.normalize_params(params) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cbraspag/payment/eft.rb', line 55 def self.normalize_params(params) params = super params[:installments] = params[:installments].to_i unless params[:installments].nil? params[:installments] ||= 1 params[:has_interest] = params[:has_interest] == true ? "1" : "0" params end |
Instance Method Details
#archive(credit_card, customer, request_id) ⇒ Object
saves credit card in Braspag PCI Compliant
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/cbraspag/payment/credit_card.rb', line 68 def archive(credit_card, customer, request_id) return ::Response self.check_protected_card_params(params) data = { 'saveCreditCardRequestWS' => {} } PROTECTED_CARD_MAPPING.each do |k, v| data['saveCreditCardRequestWS'][v] = params[k] || "" end client = Savon::Client.new(self.save_protected_card_url) response = client.request(:web, :save_credit_card) do soap.body = data end response.to_hash[:save_credit_card_response][:save_credit_card_result] end |
#authorize(order, credit_card) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/cbraspag/payment/credit_card.rb', line 9 def (order, credit_card) response = self.post(:authorize, order, credit_card) status = (response[:status] == "0" || response[:status] == "1") Response.new(status, response[:message], response, :test => homologation?, :authorization => response[:number]) end |
#capture(order) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cbraspag/payment/credit_card.rb', line 21 def capture(order) response = self.post(:capture, order) status = (response[:status] == "0") Response.new(status, response[:message], response, :test => homologation?, :authorization => response[:number]) end |
#convert_to(method) ⇒ Object
72 73 74 |
# File 'lib/cbraspag/core/connection.rb', line 72 def convert_to(method) {:merchant_id => self.merchant_id} end |
#generate_billet(order, billet) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/cbraspag/payment/billet.rb', line 5 def generate_billet(order, billet) response = self.post(:generate_billet, order, billet) status = (response[:status] == "0") Response.new(status, response[:message], response, :test => homologation?, :authorization => response[:number]) end |
#get_recurrency(credit_card) ⇒ Object
request the credit card info in Braspag PCI Compliant
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/cbraspag/payment/credit_card.rb', line 90 def get_recurrency(credit_card) return ::Response raise InvalidJustClickKey unless valid_just_click_key?(just_click_key) data = { 'getCreditCardRequestWS' => {:loja => connection.merchant_id, :justClickKey => just_click_key} } request = ::HTTPI::Request.new(self.get_protected_card_url) request.body = { 'getCreditCardRequestWS' => {:loja => connection.merchant_id, :justClickKey => just_click_key} } response = ::HTTPI.post(request) response = Utils::convert_to_map(response.body, { :holder => "CardHolder", :card_number => "CardNumber", :expiration => "CardExpiration", :masked_card_number => "MaskedCardNumber" }) raise UnknownError if response[:card_number].nil? response end |
#homologation? ⇒ Boolean
30 31 32 |
# File 'lib/cbraspag/core/connection.rb', line 30 def homologation? @env == :homologation end |
#post(method, *args) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cbraspag/core/connection.rb', line 76 def post(method, *args) data = convert_to(method) args.each do |field| data.merge!(field.convert_to(method)) end data = Converter::to(method, data) response = Braspag::Poster.new(self, self.url_for(method)) .do_post(method, data) response = Converter::from(method, response.body) args.each do |field| field.populate!(method, response) end response end |
#production? ⇒ Boolean
26 27 28 |
# File 'lib/cbraspag/core/connection.rb', line 26 def production? @env == :production end |
#purchase(order, credit_card) ⇒ Object
3 4 5 6 7 |
# File 'lib/cbraspag/payment/credit_card.rb', line 3 def purchase(order, credit_card) resp = self.(order, credit_card) resp = self.capture(order) if resp.success? resp end |
#recurrency(order, credit_card, request_id) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/cbraspag/payment/credit_card.rb', line 113 def recurrency(order, credit_card, request_id) return ::Response self.check_just_click_shop_params(params) order_id = params[:order_id] raise InvalidOrderId unless self.valid_order_id?(order_id) data = { 'justClickShopRequestWS' => {} } JUST_CLICK_MAPPING.each do |k, v| case k when :payment_method data['justClickShopRequestWS'][v] = Braspag::Connection.instance.homologation? ? PAYMENT_METHODS[:braspag] : PAYMENT_METHODS[params[:payment_method]] else data['justClickShopRequestWS'][v] = params[k] || "" end end client = Savon::Client.new(self.just_click_shop_url) response = client.request(:web, :just_click_shop) do soap.body = data end response.to_hash[:just_click_shop_response][:just_click_shop_result] end |
#url_for(method_name) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/cbraspag/core/connection.rb', line 34 def url_for(method_name) braspag_url = production? ? PRODUCTION_URL : HOMOLOGATION_URL protected_card_url = production? ? PROTECTED_CARD_PRODUCTION_URL : PROTECTED_CARD_HOMOLOGATION_URL braspag_info_url = if production? braspag_url + "/webservices/pagador/pedido.asmx" else braspag_url + "/pagador/webservice/pedido.asmx" end case method_name when :authorize braspag_url + "/webservices/pagador/Pagador.asmx/Authorize" when :void braspag_url + "/webservices/pagador/Pagador.asmx/VoidTransaction" when :capture braspag_url + "/webservices/pagador/Pagador.asmx/Capture" when :generate_billet braspag_url + "/webservices/pagador/Boleto.asmx/CreateBoleto" when :generate_eft braspag_url + "/pagador/passthru.asp" when :info_billet braspag_info_url + "/GetDadosBoleto" when :info_card braspag_info_url + "/GetDadosCartao" when :info braspag_info_url + "/GetDadosPedido" when :encrypt braspag_url + "/BraspagGeneralService/BraspagGeneralService.asmx" when :archive_card protected_card_url + "/CartaoProtegido.asmx?wsdl" when :get_card protected_card_url + "/CartaoProtegido.asmx/GetCreditCard" when :recurrency protected_card_url + "/CartaoProtegido.asmx?wsdl" end end |