Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
Refactor Use ERB for render templates xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Renato Elias committed Nov 14, 2012
1 parent 3676749 commit 5776fa5
Show file tree
Hide file tree
Showing 11 changed files with 462 additions and 2,926 deletions.
337 changes: 15 additions & 322 deletions coverage/.resultset.json

Large diffs are not rendered by default.

2,550 changes: 354 additions & 2,196 deletions coverage/index.html

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/cbraspag.rb
Expand Up @@ -7,6 +7,7 @@
require 'savon'
require 'bigdecimal'
require 'active_merchant'
require 'erb'

require "cbraspag/version"
require 'cbraspag/core/converter'
Expand All @@ -24,6 +25,7 @@


module Braspag
PATH = File.dirname(__FILE__)

INTEREST = {
:no => 0,
Expand Down
41 changes: 6 additions & 35 deletions lib/cbraspag/crypto/webservice.rb
Expand Up @@ -2,34 +2,17 @@ module Braspag
module Crypto
class Webservice
def encrypt(connection, map)
fields = "\n"
map.each do |key, value|
fields.concat(" <tns:string>#{key}=#{value}</tns:string>\n")
end
data = ERB.new(File.read(Braspag::PATH + '/cbraspag/templates/crypto/encrypt.xml.erb'))

data = <<-STRING
<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header />
<env:Body>
<tns:EncryptRequest xmlns:tns="https://www.pagador.com.br/webservice/BraspagGeneralService">
<tns:merchantId>#{connection.merchant_id}</tns:merchantId>
<tns:request>
#{fields}
</tns:request>
</tns:EncryptRequest>
</env:Body>
</env:Envelope>
STRING
response = Braspag::Poster.new(
connection,
connection.url_for(:encrypt)
).do_post(
:encrypt,
data,
data.result(binding),
{"Content-Type" => "text/xml"}
)

document = Nokogiri::XML(response.body)

raise 'UnknownError' if document.children.empty?
Expand All @@ -44,29 +27,17 @@ def encrypt(connection, map)
end

def decrypt(connection, encripted_text)

data = <<-STRING
<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header />
<env:Body>
<tns:DecryptRequest xmlns:tns="https://www.pagador.com.br/webservice/BraspagGeneralService">
<tns:merchantId>#{connection.merchant_id}</tns:merchantId>
<tns:cryptString>#{encripted_text}</tns:cryptString>
</tns:DecryptRequest>
</env:Body>
</env:Envelope>
STRING
data = ERB.new(File.read(Braspag::PATH + '/cbraspag/templates/crypto/decrypt.xml.erb'))

response = Braspag::Poster.new(
connection,
connection.url_for(:decrypt)
).do_post(
:decrypt,
data,
data.result(binding),
{"Content-Type" => "text/xml"}
)

document = Nokogiri::XML(response.body)
raise 'UnknownError' if document.children.empty?

Expand Down
91 changes: 5 additions & 86 deletions lib/cbraspag/payment/recurrency_credit_card.rb
@@ -1,99 +1,18 @@
module Braspag
class Connection
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"
}


# saves credit card in Braspag PCI Compliant
def archive(credit_card, customer, request_id)

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]

year_normalize = credit_card.year.to_s[-2, 2]

template = ERB.new 'save_credit'
puts template.result(binding)
end

# request the credit card info in Braspag PCI Compliant
def get_recurrency(credit_card)

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

def recurrency(order, credit_card, request_id)

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


end
end
10 changes: 10 additions & 0 deletions lib/cbraspag/templates/crypto/decrypt.xml.erb
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header />
<env:Body>
<tns:DecryptRequest xmlns:tns="https://www.pagador.com.br/webservice/BraspagGeneralService">
<tns:merchantId><%= connection.merchant_id %></tns:merchantId>
<tns:cryptString><%= encripted_text %></tns:cryptString>
</tns:DecryptRequest>
</env:Body>
</env:Envelope>
14 changes: 14 additions & 0 deletions lib/cbraspag/templates/crypto/encrypt.xml.erb
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header />
<env:Body>
<tns:EncryptRequest xmlns:tns="https://www.pagador.com.br/webservice/BraspagGeneralService">
<tns:merchantId><%= connection.merchant_id %></tns:merchantId>
<tns:request>
<% map.each do |key, value| %>
<tns:string><%= key %>=<%= value %></tns:string>
<% end %>
</tns:request>
</tns:EncryptRequest>
</env:Body>
</env:Envelope>
16 changes: 16 additions & 0 deletions lib/cbraspag/templates/justclick/archive.xml.erb
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SaveCreditCard xmlns="http://www.cartaoprotegido.com.br/WebService/">
<saveCreditCardRequestWS>
<MerchantKey><%= merchant_id %></MerchantKey>
<CustomerIdentification><%= customer.cpf%></CustomerIdentification>
<CustomerName><%= customer.name %></CustomerName>
<CardHolder><%= credit_card.holder_name %></CardHolder>
<CardNumber><%= credit_card.number %></CardNumber>
<CardExpiration><%= credit_card.year + credit_card.month %></CardExpiration>
<JustClickAlias><%= creditcard_alias%></JustClickAlias>
</saveCreditCardRequestWS>
</SaveCreditCard>
</soap:Body>
</soap:Envelope>
12 changes: 12 additions & 0 deletions lib/cbraspag/templates/justclick/get_recurrency.xml.erb
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetCreditCard xmlns="http://www.cartaoprotegido.com.br/WebService/">
<getCreditCardRequestWS>
<MerchantKey><%= merchant_id %></MerchantKey>
<JustClickKey>guid</JustClickKey>
<JustClickAlias>string</JustClickAlias>
</getCreditCardRequestWS>
</GetCreditCard>
</soap:Body>
</soap:Envelope>
23 changes: 23 additions & 0 deletions lib/cbraspag/templates/justclick/recurrency.xml.erb
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<JustClickShop xmlns="http://www.cartaoprotegido.com.br/WebService/">
<justClickShopRequestWS>
<MerchantKey>guid</MerchantKey>
<CustomerIdentification>string</CustomerIdentification>
<CustomerName>string</CustomerName>
<OrderId>string</OrderId>
<Amount>int</Amount>
<PaymentMethod>short</PaymentMethod>
<NumberInstallments>short</NumberInstallments>
<PaymentType>short</PaymentType>
<JustClickKey>guid</JustClickKey>
<SecurityCode>string</SecurityCode>
<Country>string</Country>
<Currency>string</Currency>
<MerchantIdPagador>guid</MerchantIdPagador>
<JustClickAlias>string</JustClickAlias>
</justClickShopRequestWS>
</JustClickShop>
</soap:Body>
</soap:Envelope>

0 comments on commit 5776fa5

Please sign in to comment.