Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor Use ERB for render templates xml

  • Loading branch information...
commit 5776fa5830fc50ad6bcaf446c8a0f07b1109ae76 1 parent 3676749
@renatoelias renatoelias authored
View
337 coverage/.resultset.json
@@ -11,6 +11,7 @@
1,
1,
1,
+ 1,
null,
1,
1,
@@ -28,6 +29,7 @@
null,
null,
1,
+ 1,
null,
1,
null,
@@ -175,23 +177,23 @@
1,
null,
1,
- 71,
- 71,
- 71,
- 70,
- null,
- 68,
- 68,
- 68,
+ 69,
+ 69,
+ 69,
68,
null,
+ 66,
+ 66,
+ 66,
+ 66,
+ null,
null,
1,
89,
null,
null,
1,
- 23,
+ 21,
null,
null,
1,
@@ -622,9 +624,6 @@
1,
1,
4,
- 4,
- 4,
- null,
null,
4,
null,
@@ -635,20 +634,8 @@
null,
null,
null,
- null,
- null,
- null,
- null,
- null,
4,
null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
4,
null,
4,
@@ -663,20 +650,8 @@
null,
null,
1,
- null,
3,
null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
3,
null,
null,
@@ -825,11 +800,11 @@
null,
null,
1,
- 4,
+ 2,
null,
- 4,
+ 2,
null,
- 4,
+ 2,
null,
null,
null,
@@ -2653,296 +2628,14 @@
1,
null,
1,
- 3,
- 3,
- null,
- 1,
- 1,
- 1,
- null,
- null,
- null,
- null,
- 1,
- null,
- 1,
- null,
- 1,
- 1,
- 1,
- 1,
- null,
- null,
- 1,
1,
- null,
- null,
- null,
- null,
- 1,
- null,
- 1,
- null,
1,
null,
- null,
- null,
1,
- 0,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- 0,
- null,
- null,
- 0,
- 0,
- null,
- null,
- 0,
- null,
- 0,
- null,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- 0,
- null,
- null,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- 0,
- null,
- 0,
- 0,
- null,
- 0,
- null,
- null,
- 0,
- 0,
- null,
- null,
- null,
- 0,
- null,
- null,
- null,
- 0,
- 0,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- 0,
- null,
- null,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- null,
- 0,
- null,
- 0,
- 0,
- null,
- 0,
- null,
- null,
- 0,
- 0,
- null,
- null,
- null,
- 0,
- null,
- null,
- null,
null,
1,
- 0,
- null,
- 0,
- 0,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- 0,
- 0,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- 0,
- 0,
- null,
- null,
- null,
- 0,
- 0,
- null,
- null,
- null,
- 0,
- 0,
- null,
- 0,
- null,
- null,
- 0,
- 0,
- null,
- null,
- null,
- 0,
- 0,
- null,
- 0,
- null,
- null,
- 0,
- 0,
- null,
- null,
- null,
- null,
- null,
- null,
- 0,
- null,
- null,
- null,
null,
1,
- 0,
- 0,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- 0,
- null,
- 0,
- 0,
- 0,
- null,
- 0,
- 0,
- 0,
- null,
- 0,
- null,
- null,
- 0,
- 0,
- null,
- null,
- null,
- 0,
- 0,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- 0,
- null,
- null,
- 0,
- 0,
- 0,
- null,
- null,
- null,
null,
null,
1,
@@ -3034,6 +2727,6 @@
null
]
},
- "timestamp": 1352840553
+ "timestamp": 1352924674
}
}
View
2,550 coverage/index.html
354 additions, 2,196 deletions not shown
View
2  lib/cbraspag.rb
@@ -7,6 +7,7 @@
require 'savon'
require 'bigdecimal'
require 'active_merchant'
+require 'erb'
require "cbraspag/version"
require 'cbraspag/core/converter'
@@ -24,6 +25,7 @@
module Braspag
+ PATH = File.dirname(__FILE__)
INTEREST = {
:no => 0,
View
41 lib/cbraspag/crypto/webservice.rb
@@ -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?
@@ -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?
View
91 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
View
10 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>
View
14 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>
View
16 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>
View
12 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>
View
23 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>
View
292 spec/payment/recurrency_credit_card_spec.rb
@@ -4,293 +4,11 @@
let(:merchant_id) { "{12345678-1234-1234-1234-123456789000}" }
let(:connection) { Braspag::Connection.new(:merchant_id => merchant_id, :environment => :homologation)}
- context ".void" do
- it "should return response" do
- void = {
- :status => "1",
- :message => "BLA"
- }
-
- connection.should_receive(:post).and_return(void)
-
- response = connection.void(mock)
-
- response.success?.should eq(false)
- response.message.should eq(void[:message])
- response.params.should eq({"status"=>"1", "message"=>"BLA"})
- response.test.should eq(true)
- end
-
- it "should return success when status is zero" do
- void = {
- :status => "0",
- :message => "BLA"
- }
-
- connection.should_receive(:post).and_return(void)
-
- response = connection.void(mock)
-
- response.success?.should eq(true)
- end
- end
-
- pending ".save" do
- let(:params) do
- {
- :customer_name => "W" * 21,
- :holder => "Joao Maria Souza",
- :card_number => "9" * 10,
- :expiration => "10/12",
- :order_id => "um order id",
- :request_id => "00000000-0000-0000-0000-000000000044"
- }
- end
-
- let(:params_with_merchant_id) do
- params.merge!(:merchant_id => merchant_id)
- end
-
- let(:save_protected_card_url) { "http://braspag.com/bla" }
-
- let(:savon_double) { double('Savon') }
-
- before do
- @connection.should_receive(:merchant_id)
- end
-
- context "with valid params" do
- let(:valid_hash) do
- {
- :save_credit_card_response => {
- :save_credit_card_result => {
- :just_click_key => 'SAVE-PROTECTED-CARD-TOKEN',
- :success => true
- }
- }
- }
- end
-
- let(:response) do
- double('Response', :to_hash => valid_hash)
- end
-
- before do
- Braspag::ProtectedCreditCard.should_receive(:save_protected_card_url)
- Braspag::ProtectedCreditCard.should_receive(:check_protected_card_params)
- .and_return(true)
- Savon::Client.should_receive(:new).and_return(savon_double)
- savon_double.should_receive(:request).and_return(response)
-
- @response = Braspag::ProtectedCreditCard.save(params)
- end
-
- it "should return a Hash" do
- @response.should be_kind_of Hash
- @response.should == {
- :just_click_key => "SAVE-PROTECTED-CARD-TOKEN",
- :success => true
- }
- end
- end
-
- context "with invalid params" do
- let(:invalid_hash) do
- {
- :save_credit_card_response => {
- :save_credit_card_result => {
- :just_click_key => nil,
- :success => false
- }
- }
- }
- end
-
- let(:response) do
- double('Response', :to_hash => invalid_hash)
- end
-
- before do
- Braspag::ProtectedCreditCard.should_receive(:check_protected_card_params)
- .and_return(true)
- Braspag::ProtectedCreditCard.should_receive(:save_protected_card_url)
- .and_return(save_protected_card_url)
- Savon::Client.should_receive(:new).and_return(savon_double)
- savon_double.should_receive(:request).and_return(response)
-
- @response = Braspag::ProtectedCreditCard.save(params)
- end
-
- it "should return a Hash" do
- @response.should be_kind_of Hash
- @response.should == {
- :just_click_key => nil,
- :success => false
- }
- end
- end
- end
-
- pending ".get" do
- let(:get_protected_card_url) { "http://braspag/bla" }
-
- let(:invalid_xml) do
- <<-EOXML
- <CartaoProtegidoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- xmlns="http://www.pagador.com.br/">
- <CardHolder>Joao Maria Souza</CardHolder>
- <CardNumber></CardNumber>
- <CardExpiration>10/12</CardExpiration>
- <MaskedCardNumber>******9999</MaskedCardNumber>
- </CartaoProtegidoReturn>
- EOXML
- end
-
- let(:valid_xml) do
- <<-EOXML
- <CartaoProtegidoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- xmlns="http://www.pagador.com.br/">
- <CardHolder>Joao Maria Souza</CardHolder>
- <CardNumber>9999999999</CardNumber>
- <CardExpiration>10/12</CardExpiration>
- <MaskedCardNumber>******9999</MaskedCardNumber>
- </CartaoProtegidoReturn>
- EOXML
- end
-
- it "should raise an error when just click key is not valid" do
- Braspag::ProtectedCreditCard.should_receive(:valid_just_click_key?)
- .with("bla")
- .and_return(false)
-
- expect {
- Braspag::ProtectedCreditCard.get "bla"
- }.to raise_error(Braspag::InvalidJustClickKey)
- end
-
- it "should raise an error when Braspag returned an invalid xml as response" do
- FakeWeb.register_uri(:post, get_protected_card_url, :body => invalid_xml)
-
- Braspag::ProtectedCreditCard.should_receive(:get_protected_card_url)
- .and_return(get_protected_card_url)
-
- expect {
- Braspag::ProtectedCreditCard.get("b0b0b0b0-bbbb-4d4d-bd27-f1f1f1ededed")
- }.to raise_error(Braspag::UnknownError)
- end
-
- it "should return a Hash when Braspag returned a valid xml as response" do
- FakeWeb.register_uri(:post, get_protected_card_url, :body => valid_xml)
-
- Braspag::ProtectedCreditCard.should_receive(:get_protected_card_url)
- .and_return(get_protected_card_url)
-
- response = Braspag::ProtectedCreditCard.get("b0b0b0b0-bbbb-4d4d-bd27-f1f1f1ededed")
- response.should be_kind_of Hash
-
- response.should == {
- :holder => "Joao Maria Souza",
- :expiration => "10/12",
- :card_number => "9" * 10,
- :masked_card_number => "*" * 6 + "9" * 4
- }
- end
-
- end
-
- pending ".just_click_shop" do
- context "body" do
- let(:params) { {
- :request_id => "123",
- :customer_name => "Joao Silva",
- :order_id => "999",
- :amount => 10.50,
- :payment_method => :redecard,
- :number_installments => 3,
- :payment_type => "test",
- :just_click_key => "key",
- :security_code => "123"
- } }
-
- class SavonClientTest
- attr_accessor :response
- attr_reader :method
-
- def request(web, method, &block)
- @method = method
- instance_eval &block
-
- @response
- end
-
- def soap
- @soap ||= OpenStruct.new
- end
- end
-
- before :each do
- @savon_client_test = SavonClientTest.new
- @savon_client_test.response = {:just_click_shop_response => {}}
- Savon::Client.stub(:new).with('https://www.cartaoprotegido.com.br/Services/TestEnvironment/CartaoProtegido.asmx?wsdl').and_return(@savon_client_test)
- end
-
- after :each do
- Savon::Client.unstub(:new)
- end
-
- it "should have RequestId" do
- described_class.just_click_shop(params)
- @savon_client_test.soap.body['justClickShopRequestWS']['RequestId'].should eq '123'
- end
-
- it "should have MerchantKey" do
- described_class.just_click_shop(params)
- @savon_client_test.soap.body['justClickShopRequestWS']['MerchantKey'].should eq 'um id qualquer'
- end
-
- it "should have CustomerName" do
- described_class.just_click_shop(params)
- @savon_client_test.soap.body['justClickShopRequestWS']['CustomerName'].should eq 'Joao Silva'
- end
-
- it "should have OrderId" do
- described_class.just_click_shop(params)
- @savon_client_test.soap.body['justClickShopRequestWS']['OrderId'].should eq '999'
- end
-
- it "should have Amount" do
- described_class.just_click_shop(params)
- @savon_client_test.soap.body['justClickShopRequestWS']['Amount'].should eq 10.50
- end
-
- it "should have PaymentMethod" do
- described_class.just_click_shop(params)
- @savon_client_test.soap.body['justClickShopRequestWS']['PaymentMethod'].should eq 20
- end
-
- it "should have PaymentType" do
- described_class.just_click_shop(params)
- @savon_client_test.soap.body['justClickShopRequestWS']['PaymentType'].should eq 'test'
- end
-
- it "should have NumberInstallments" do
- described_class.just_click_shop(params)
- @savon_client_test.soap.body['justClickShopRequestWS']['NumberInstallments'].should eq 3
- end
-
- it "should have JustClickKey" do
- described_class.just_click_shop(params)
- @savon_client_test.soap.body['justClickShopRequestWS']['JustClickKey'].should eq 'key'
- end
-
- it "should have SecurityCode" do
- described_class.just_click_shop(params)
- @savon_client_test.soap.body['justClickShopRequestWS']['SecurityCode'].should eq '123'
- end
- end
- end
+ pending 'archive'
+
+ pending 'get_recurrency'
+
+ pending 'recurrency'
end
describe Braspag::CreditCard do
Please sign in to comment.
Something went wrong with that request. Please try again.