Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added payment attributes for xml generation

  • Loading branch information...
commit 6aff219451ddaa0fb6fa8f49e94b085adb884a61 1 parent 0418a3c
Stefano Diem Benatti teonimesic authored
2  Guardfile
@@ -2,4 +2,6 @@ guard 'rspec', version: 2 do
2 2 watch(%r{^spec/.+_spec\.rb})
3 3 watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
4 4 watch('spec/spec_helper.rb') { "spec" }
  5 + watch('lib/pag_seguro.rb') { "spec" }
  6 + watch('lib/checkout.xml.haml') { "spec/pag_seguro/checkout_xml_spec.rb" }
5 7 end
15 lib/pag_seguro.rb
... ... @@ -1,6 +1,21 @@
1 1 $: << "lib/pag_seguro"
2 2
  3 +# Core Ruby Libraries
  4 +require "net/https"
  5 +require "uri"
  6 +
  7 +# Third party gems
  8 +require "active_model"
  9 +require "nokogiri"
  10 +require "haml"
  11 +
  12 +# PagSeguro classes
  13 +require "item"
3 14 require "payment"
  15 +require "sender"
  16 +require "shipping"
  17 +
  18 +# Version
4 19 require "version"
5 20
6 21 module PagSeguro
59 lib/pag_seguro/checkout.xml.haml
... ... @@ -0,0 +1,59 @@
  1 +<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2 +%checkout
  3 + - if payment.id.present?
  4 + %reference= payment.id
  5 + - if payment.extra_amount.present?
  6 + %extraAmount= payment.extra_amount
  7 + - if payment.redirect_url.present?
  8 + %redirectUrl= payment.redirect_url
  9 + - if payment.max_uses.present?
  10 + %maxUses= payment.max_uses
  11 + - if payment. max_age.present?
  12 + %maxAge= payment.max_age
  13 +
  14 + %currency BRL
  15 +
  16 + %items
  17 + - items.each do |item|
  18 + %item
  19 + %id= item.id
  20 + %description= item.description
  21 + %amount= item.amount
  22 + %quantity= item.quantity
  23 + - if item.shipping_cost.present?
  24 + %shippingCost= item.shipping_cost
  25 + - if item.weight.present?
  26 + %weight= item.weight
  27 +
  28 + - if sender.present?
  29 + %sender
  30 + - if sender.name.present?
  31 + %name= sender.name
  32 + - if sender.email.present?
  33 + %email= sender.email
  34 + %phone
  35 + - if sender.phone_ddd.present?
  36 + %areaCode= sender.phone_ddd
  37 + - if sender.phone_number.present?
  38 + %number= sender.phone_number
  39 +
  40 + - if shipping.present?
  41 + %shipping
  42 + - if shipping.type.present?
  43 + %type= shipping.type
  44 + %address
  45 + %country BRA
  46 + - if shipping.state.present?
  47 + %state= shipping.state
  48 + - if shipping.city.present?
  49 + %city= shipping.city
  50 + - if shipping.postal_code.present?
  51 + %postalCode= shipping.postal_code
  52 + - if shipping.district.present?
  53 + %district= shipping.district
  54 + - if shipping.street.present?
  55 + %street= shipping.street
  56 + - if shipping.number.present?
  57 + %number= shipping.number
  58 + - if shipping.complement.present?
  59 + %complement= shipping.complement
31 lib/pag_seguro/item.rb
... ... @@ -0,0 +1,31 @@
  1 +module PagSeguro
  2 + class Item
  3 + include ActiveModel::Validations
  4 +
  5 + attr_accessor :id, :description, :amount, :quantity, :shipping_cost, :weight
  6 +
  7 + validates_presence_of :id, :description, :amount, :quantity
  8 + validates_format_of :amount, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot"
  9 + validates_format_of :shipping_cost, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot"
  10 + validates_format_of :weight, with: /^\d+$/, message: " must be an integer"
  11 + validate :quantity_amount
  12 +
  13 + def initialize(attributes = {})
  14 + @id = attributes[:id]
  15 + @description = attributes[:description]
  16 + @amount = attributes[:amount]
  17 + @quantity = attributes[:quantity]
  18 + @shipping_cost = attributes[:shipping_cost]
  19 + @weight = attributes[:weight]
  20 + end
  21 +
  22 + def description
  23 + @description.present? && @description.size > 100 ? @description[0..99] : @description
  24 + end
  25 +
  26 + protected
  27 + def quantity_amount
  28 + errors.add(:quantity, " must be a number between 1 and 999") if @quantity.present? && (@quantity == "0" || @quantity.to_s !~ /^\d{1,3}$/)
  29 + end
  30 + end
  31 +end
38 lib/pag_seguro/payment.rb
... ... @@ -1,7 +1,41 @@
1   -require "net/https"
2   -
3 1 module PagSeguro
4 2 class Payment
  3 + include ActiveModel::Validations
  4 +
5 5 BASE_URL = 'ws.pagseguro.uol.com.br/v2/checkout'
  6 +
  7 + attr_accessor :id, :email, :token, :items, :sender, :shipping, :extra_amount, :redirect_url, :max_uses, :max_age
  8 +
  9 + validates_presence_of :email, :token
  10 + validates_format_of :extra_amount, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot", allow_blank: true
  11 + validates_format_of :redirect_url, with: URI::regexp(%w(http https)), message: " must give a correct url for redirection", allow_blank: true
  12 + validate :max_uses_number, :max_age_number
  13 +
  14 + def initialize(email = nil, token = nil, options = {})
  15 + @email = email unless email.nil?
  16 + @token = token unless token.nil?
  17 + @id = options[:id]
  18 + @sender = options[:sender] || Sender.new
  19 + @shipping = options[:shipping] || Shipping.new
  20 + @items = options[:items] || []
  21 + @extra_amount = options[:extra_amount]
  22 + @redirect_url = options[:redirect_url]
  23 + @max_uses = options[:max_uses]
  24 + @max_age = options[:max_age]
  25 + end
  26 +
  27 + def checkout_xml
  28 + xml_content = File.open( File.dirname(__FILE__) + "/checkout.xml.haml" ).read
  29 + Haml::Engine.new(xml_content).render(nil, items: @items, payment: self, sender: @sender, shipping: @shipping)
  30 + end
  31 +
  32 + protected
  33 + def max_uses_number
  34 + errors.add(:max_uses, " must be an integer greater than 0") if @max_uses.present? && @max_uses.to_i <= 0
  35 + end
  36 +
  37 + def max_age_number
  38 + errors.add(:max_age, " must be an integer grater or equal to 30") if @max_age.present? && @max_age.to_i < 30
  39 + end
6 40 end
7 41 end
32 lib/pag_seguro/sender.rb
... ... @@ -0,0 +1,32 @@
  1 +module PagSeguro
  2 + class Sender
  3 + attr_accessor :name, :email, :phone_ddd, :phone_number
  4 +
  5 + def initialize(options = {})
  6 + @name = options[:name]
  7 + @email = options[:email]
  8 + @phone_ddd = options[:phone_ddd]
  9 + @phone_number = options[:phone_number]
  10 + end
  11 +
  12 + def email
  13 + valid_email? ? @email : nil
  14 + end
  15 +
  16 + def valid_email?
  17 + @email =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i && @email.size <= 60
  18 + end
  19 +
  20 + def name
  21 + ( @name.present? && @name.size > 50 ) ? @name[0..49] : @name
  22 + end
  23 +
  24 + def phone_ddd
  25 + @phone_ddd if @phone_ddd =~ /^\d{2}$/
  26 + end
  27 +
  28 + def phone_number
  29 + @phone_number if @phone_number =~/^\d{8}$/
  30 + end
  31 + end
  32 +end
28 lib/pag_seguro/shipping.rb
... ... @@ -0,0 +1,28 @@
  1 +module PagSeguro
  2 + class Shipping
  3 + include ActiveModel::Validations
  4 +
  5 + PAC = 1
  6 + SEDEX = 2
  7 + NOT_IDENTIFIED = 3
  8 +
  9 + validates_format_of :postal_code, with: /^\d{8}$/, message: " must be an integer with 8 digits", allow_blank: true
  10 +
  11 + attr_accessor :type, :state, :city, :postal_code, :district, :street, :number, :complement
  12 +
  13 + def initialize(attributes = {})
  14 + @type = attributes[:type]
  15 + @state = attributes[:state]
  16 + @city = attributes[:city]
  17 + @postal_code = attributes[:postal_code]
  18 + @district = attributes[:district]
  19 + @street = attributes[:street]
  20 + @number = attributes[:number]
  21 + @complement = attributes[:complement]
  22 + end
  23 +
  24 + def postal_code
  25 + @postal_code if @postal_code.present? && @postal_code.to_s.size == 8
  26 + end
  27 + end
  28 +end
2  lib/pag_seguro/version.rb
... ... @@ -1,3 +1,3 @@
1 1 module PagSeguro
2   - VERSION = "0.0.1"
  2 + VERSION = "0.1.0"
3 3 end
12 pag_seguro.gemspec
@@ -3,18 +3,22 @@ $:.push File.expand_path("../lib", __FILE__)
3 3 require "pag_seguro/version"
4 4
5 5 Gem::Specification.new do |s|
  6 + s.platform = Gem::Platform::RUBY
6 7 s.name = "pag_seguro"
7 8 s.version = PagSeguro::VERSION
8 9 s.authors = ["Stefano Diem Benatti"]
9 10 s.email = ["stefano.diem@gmail.com"]
10   - s.homepage = ""
  11 + s.homepage = "http://github.com/heavenstudio/pag_seguro"
11 12 s.summary = %q{A ruby gem to handle PagSeguro's API version 2}
12   - s.description = %q{A ruby gem to handle PagSeguro's API version 2}
13   -
14   - s.rubyforge_project = "pag_seguro"
  13 + s.required_ruby_version = '>= 1.9.2'
15 14
16 15 s.files = `git ls-files`.split("\n")
17 16 s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18 17 s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19 18 s.require_paths = ["lib"]
  19 + s.has_rdoc = false
  20 +
  21 + s.add_dependency('activemodel')
  22 + s.add_dependency('haml')
  23 + s.add_dependency('nokogiri')
20 24 end
176 spec/pag_seguro/checkout_xml_spec.rb
... ... @@ -0,0 +1,176 @@
  1 +# encoding: utf-8
  2 +require 'spec_helper'
  3 +
  4 +items = [
  5 + PagSeguro::Item.new(id: 25, description: "A Bic Pen", amount: "1.50", quantity: "4", shipping_cost: "1.00", weight: 10),
  6 + PagSeguro::Item.new(id: 73, description: "A Book", amount: "38.23", quantity: "1", shipping_cost: "12.00", weight: 300),
  7 + PagSeguro::Item.new(id: 95, description: "A Towel", amount: "69.35", quantity: "2", weight: 400),
  8 + PagSeguro::Item.new(id: 17, description: "A pipe", amount: "3.00", quantity: "89")
  9 +]
  10 +
  11 +sender_info = {name: "Stefano Diem Benatti", email: "stefano@heavenstudio.com.br", phone_ddd: "11", phone_number: "93430994"}
  12 +
  13 +shipping_info = {type: PagSeguro::Shipping::SEDEX, state: "SP", city: "São Paulo", postal_code: "05363000",
  14 + district: "Jd. PoliPoli", street: "Av. Otacilio Tomanik", number: "775", complement: "apto. 92"}
  15 +
  16 +
  17 +describe PagSeguro::Payment do
  18 + context "checkout_xml" do
  19 + before { @payment = PagSeguro::Payment.new }
  20 +
  21 + it "should be a valid xml" do
  22 + lambda { Nokogiri::XML(@payment.checkout_xml) { |config| config.options = Nokogiri::XML::ParseOptions::STRICT } }.should_not raise_error
  23 + end
  24 +
  25 + it "should have encoding UTF-8" do
  26 + @payment.checkout_xml.should match(/^<\?xml.+encoding="UTF-8".+\?>$/)
  27 + end
  28 +
  29 + it "should have currency BRL" do
  30 + Nokogiri::XML(@payment.checkout_xml).css("checkout currency").first.content.should == "BRL"
  31 + end
  32 +
  33 + context "items" do
  34 + before do
  35 + @payment.items = items
  36 + @xml = Nokogiri::XML(@payment.checkout_xml)
  37 + end
  38 +
  39 + it "should have 4 items" do
  40 + @xml.css("checkout items item").size.should == 4
  41 + end
  42 +
  43 + it "should show all ids" do
  44 + @xml.css("checkout items item id").map(&:content).should == ["25","73","95","17"]
  45 + end
  46 +
  47 + it "should show all descriptions" do
  48 + @xml.css("checkout items item description").map(&:content).should == ["A Bic Pen","A Book","A Towel","A pipe"]
  49 + end
  50 +
  51 + it "should show all amounts" do
  52 + @xml.css("checkout items item amount").map(&:content).should == ["1.50","38.23","69.35","3.00"]
  53 + end
  54 +
  55 + it "should show all quantities" do
  56 + @xml.css("checkout items item quantity").map(&:content).should == ["4","1","2","89"]
  57 + end
  58 +
  59 + it "should show all shipping_costs" do
  60 + @xml.css("checkout items item shippingCost").map(&:content).should == ["1.00","12.00"]
  61 + end
  62 +
  63 + it "should show all weights" do
  64 + @xml.css("checkout items item weight").map(&:content).should == ["10","300","400"]
  65 + end
  66 + end
  67 +
  68 + context "sender info" do
  69 + before do
  70 + @xml_without_sender_info = Nokogiri::XML(@payment.checkout_xml)
  71 + @payment.sender = PagSeguro::Sender.new(sender_info)
  72 + @xml = Nokogiri::XML(@payment.checkout_xml)
  73 + end
  74 +
  75 + it "should have sender name" do
  76 + @xml_without_sender_info.css("checkout sender name").should be_empty
  77 + @xml.css("checkout sender name").first.content.should == "Stefano Diem Benatti"
  78 + end
  79 +
  80 + it "should have sender email" do
  81 + @xml_without_sender_info.css("checkout sender email").should be_empty
  82 + @xml.css("checkout sender email").first.content.should == "stefano@heavenstudio.com.br"
  83 + end
  84 +
  85 + it "should have sender phone ddd" do
  86 + @xml_without_sender_info.css("checkout sender phone areaCode").should be_empty
  87 + @xml.css("checkout sender phone areaCode").first.content.should == "11"
  88 + end
  89 +
  90 + it "should have sender phone number" do
  91 + @xml_without_sender_info.css("checkout sender phone number").should be_empty
  92 + @xml.css("checkout sender phone number").first.content.should == "93430994"
  93 + end
  94 + end
  95 +
  96 + context "shipping" do
  97 + before do
  98 + @xml_without_shipping_info = Nokogiri::XML(@payment.checkout_xml)
  99 + @payment.shipping = PagSeguro::Shipping.new(shipping_info)
  100 + @xml = Nokogiri::XML(@payment.checkout_xml)
  101 + end
  102 +
  103 + it "should have shipping type" do
  104 + @xml_without_shipping_info.css("checkout shipping type").should be_empty
  105 + @xml.css("checkout shipping type").first.content.to_i.should == PagSeguro::Shipping::SEDEX
  106 + end
  107 +
  108 + it "should have state" do
  109 + @xml_without_shipping_info.css("checkout shipping address state").should be_empty
  110 + @xml.css("checkout shipping address state").first.content.should == "SP"
  111 + end
  112 +
  113 + it "should have city" do
  114 + @xml_without_shipping_info.css("checkout shipping address city").should be_empty
  115 + @xml.css("checkout shipping address city").first.content.should == "São Paulo"
  116 + end
  117 +
  118 + it "should have posta code" do
  119 + @xml_without_shipping_info.css("checkout shipping address postalCode").should be_empty
  120 + @xml.css("checkout shipping address postalCode").first.content.should == "05363000"
  121 + end
  122 +
  123 + it "should have district" do
  124 + @xml_without_shipping_info.css("checkout shipping address district").should be_empty
  125 + @xml.css("checkout shipping address district").first.content.should == "Jd. PoliPoli"
  126 + end
  127 +
  128 + it "should have street" do
  129 + @xml_without_shipping_info.css("checkout shipping address street").should be_empty
  130 + @xml.css("checkout shipping address street").first.content.should == "Av. Otacilio Tomanik"
  131 + end
  132 +
  133 + it "should have number" do
  134 + @xml_without_shipping_info.css("checkout shipping address number").should be_empty
  135 + @xml.css("checkout shipping address number").first.content.should == "775"
  136 + end
  137 +
  138 + it "should have complement" do
  139 + @xml_without_shipping_info.css("checkout shipping address complement").should be_empty
  140 + @xml.css("checkout shipping address complement").first.content.should == "apto. 92"
  141 + end
  142 + end
  143 +
  144 + context "payment settings" do
  145 + it "should not show id unless specified" do
  146 + Nokogiri::XML(@payment.checkout_xml).css("checkout reference").should be_empty
  147 + @payment.id = 305
  148 + Nokogiri::XML(@payment.checkout_xml).css("checkout reference").first.content.should == "305"
  149 + end
  150 +
  151 + it "should not show extra amount unless specified" do
  152 + Nokogiri::XML(@payment.checkout_xml).css("checkout extraAmount").should be_empty
  153 + @payment.extra_amount = "10.50"
  154 + Nokogiri::XML(@payment.checkout_xml).css("checkout extraAmount").first.content.should == "10.50"
  155 + end
  156 +
  157 + it "should not show redirect url unless specified" do
  158 + Nokogiri::XML(@payment.checkout_xml).css("checkout redirectUrl").should be_empty
  159 + @payment.redirect_url = "http://heavenstudio.com.br"
  160 + Nokogiri::XML(@payment.checkout_xml).css("checkout redirectUrl").first.content.should == "http://heavenstudio.com.br"
  161 + end
  162 +
  163 + it "should not show max uses unless specified" do
  164 + Nokogiri::XML(@payment.checkout_xml).css("checkout maxUses").should be_empty
  165 + @payment.max_uses = "10"
  166 + Nokogiri::XML(@payment.checkout_xml).css("checkout maxUses").first.content.should == "10"
  167 + end
  168 +
  169 + it "should not show max age unless specified" do
  170 + Nokogiri::XML(@payment.checkout_xml).css("checkout maxAge").should be_empty
  171 + @payment.max_age = "5000"
  172 + Nokogiri::XML(@payment.checkout_xml).css("checkout maxAge").first.content.should == "5000"
  173 + end
  174 + end
  175 + end
  176 +end
79 spec/pag_seguro/item_spec.rb
... ... @@ -0,0 +1,79 @@
  1 +require "spec_helper"
  2 +
  3 +valid_attributes = {
  4 + id: 1,
  5 + description: "descrevendo um item",
  6 + amount: "100.50",
  7 + quantity: 1,
  8 + shipping_cost: "10.50",
  9 + weight: 300
  10 +}
  11 +
  12 +describe PagSeguro::Item do
  13 + context "instance" do
  14 + before { @item = PagSeguro::Item.new }
  15 +
  16 + it { @item.should have_attribute_accessor(:id) }
  17 + it { @item.should have_attribute_accessor(:description) }
  18 + it { @item.should have_attribute_accessor(:amount) }
  19 + it { @item.should have_attribute_accessor(:quantity) }
  20 + it { @item.should have_attribute_accessor(:shipping_cost) }
  21 + it { @item.should have_attribute_accessor(:weight) }
  22 +
  23 + it "should be valid with valid attributes" do
  24 + PagSeguro::Item.new(valid_attributes).should be_valid
  25 + end
  26 +
  27 + it "should not be valid without an id" do
  28 + PagSeguro::Item.new(valid_attributes.except(:id)).should_not be_valid
  29 + end
  30 +
  31 + it "should not be valid without a description" do
  32 + PagSeguro::Item.new(valid_attributes.except(:description)).should_not be_valid
  33 + end
  34 +
  35 + it "should trim description to 100 characters if it has more than 100 characters" do
  36 + item = PagSeguro::Item.new(valid_attributes)
  37 + item.description = "-" * 100
  38 + item.description.size.should == 100
  39 + item.should be_valid
  40 + item.description = "-" * 101
  41 + item.description.size.should == 100
  42 + item.should be_valid
  43 + end
  44 +
  45 + it "should not be valid without an amount" do
  46 + PagSeguro::Item.new(valid_attributes.except(:amount)).should_not be_valid
  47 + end
  48 +
  49 + it "should not allow invalid amount formats" do
  50 + PagSeguro::Item.new(valid_attributes.merge(amount: "10")).should_not be_valid
  51 + PagSeguro::Item.new(valid_attributes.merge(amount: "10,50")).should_not be_valid
  52 + PagSeguro::Item.new(valid_attributes.merge(amount: "R$ 10.50")).should_not be_valid
  53 + PagSeguro::Item.new(valid_attributes.merge(amount: "-10.50")).should_not be_valid
  54 + end
  55 +
  56 + it "should not be valid without a quantity" do
  57 + PagSeguro::Item.new(valid_attributes.except(:quantity)).should_not be_valid
  58 + end
  59 +
  60 + it "should not allow invalid quantities" do
  61 + PagSeguro::Item.new(valid_attributes.merge(quantity: "1000")).should_not be_valid
  62 + PagSeguro::Item.new(valid_attributes.merge(quantity: "0")).should_not be_valid
  63 + PagSeguro::Item.new(valid_attributes.merge(quantity: "-1")).should_not be_valid
  64 + end
  65 +
  66 + it "should not allow invalid shipping_cost formats" do
  67 + PagSeguro::Item.new(valid_attributes.merge(shipping_cost: "10")).should_not be_valid
  68 + PagSeguro::Item.new(valid_attributes.merge(shipping_cost: "10,50")).should_not be_valid
  69 + PagSeguro::Item.new(valid_attributes.merge(shipping_cost: "R$ 10.50")).should_not be_valid
  70 + PagSeguro::Item.new(valid_attributes.merge(shipping_cost: "-10.50")).should_not be_valid
  71 + end
  72 +
  73 + it "should not allow non integer values for weight" do
  74 + PagSeguro::Item.new(valid_attributes.merge(weight: "-10")).should_not be_valid
  75 + PagSeguro::Item.new(valid_attributes.merge(weight: "10.5")).should_not be_valid
  76 + PagSeguro::Item.new(valid_attributes.merge(weight: "10,5")).should_not be_valid
  77 + end
  78 + end
  79 +end
79 spec/pag_seguro/payment_spec.rb
@@ -4,4 +4,83 @@
4 4 it "should have a base url to 'ws.pagseguro.uol.com.br/v2/checkout'" do
5 5 PagSeguro::Payment::BASE_URL.should == 'ws.pagseguro.uol.com.br/v2/checkout'
6 6 end
  7 +
  8 + context "instance" do
  9 + context "accessors" do
  10 + before { @payment = PagSeguro::Payment.new }
  11 +
  12 + it { @payment.should have_attribute_accessor(:id) }
  13 + it { @payment.should have_attribute_accessor(:items) }
  14 + it { @payment.should have_attribute_accessor(:sender) }
  15 + it { @payment.should have_attribute_accessor(:shipping) }
  16 + it { @payment.should have_attribute_accessor(:email) }
  17 + it { @payment.should have_attribute_accessor(:token) }
  18 + it { @payment.should have_attribute_accessor(:extra_amount) }
  19 + it { @payment.should have_attribute_accessor(:redirect_url) }
  20 + it { @payment.should have_attribute_accessor(:max_uses) }
  21 + it { @payment.should have_attribute_accessor(:max_age) }
  22 +
  23 + it "should have items" do
  24 + @payment.items.should be_instance_of(Array)
  25 + @payment.items.should be_empty
  26 + end
  27 +
  28 + it "should have a sender" do
  29 + @payment.sender.should be_instance_of(PagSeguro::Sender)
  30 + end
  31 + end
  32 +
  33 + it "should allow to set email and token initialization" do
  34 + payment = PagSeguro::Payment.new("mymail", "mytoken")
  35 + payment.email.should == "mymail"
  36 + payment.token.should == "mytoken"
  37 + end
  38 +
  39 + context "validation" do
  40 + before { @payment = PagSeguro::Payment.new("mymail", "mytoken") }
  41 + it "should be valid with valid attributes" do
  42 + @payment.should be_valid
  43 + end
  44 +
  45 + it "should not be valid without email" do
  46 + @payment.email = nil
  47 + @payment.should_not be_valid
  48 + end
  49 +
  50 + it "should not be valid without token" do
  51 + @payment.token = nil
  52 + @payment.should_not be_valid
  53 + end
  54 +
  55 + it "should not be valid with invalid extra amount format" do
  56 + @payment.extra_amount = "10,50"
  57 + @payment.should_not be_valid
  58 + @payment.extra_amount = "R$ 10.50"
  59 + @payment.should_not be_valid
  60 + @payment.extra_amount = "10.50"
  61 + @payment.should be_valid
  62 + end
  63 +
  64 + it "should not allow invalid urls" do
  65 + @payment.redirect_url = "httd://something"
  66 + @payment.should_not be_valid
  67 + @payment.redirect_url = "http://heavenstudio.com.br"
  68 + @payment.should be_valid
  69 + end
  70 +
  71 + it "should allow an invalid number of uses" do
  72 + @payment.max_uses = "0"
  73 + @payment.should_not be_valid
  74 + @payment.max_uses = "10"
  75 + @payment.should be_valid
  76 + end
  77 +
  78 + it "should allow an invalid second time" do
  79 + @payment.max_age = "29"
  80 + @payment.should_not be_valid
  81 + @payment.max_age = "30"
  82 + @payment.should be_valid
  83 + end
  84 + end
  85 + end
7 86 end
60 spec/pag_seguro/sender_spec.rb
... ... @@ -0,0 +1,60 @@
  1 +require "spec_helper"
  2 +
  3 +describe PagSeguro::Sender do
  4 + context "instance" do
  5 + before { @sender = PagSeguro::Sender.new }
  6 +
  7 + it "should have an email accessor" do
  8 + @sender.should have_attribute_accessor(:email)
  9 + end
  10 +
  11 + it "should have a name accessor" do
  12 + @sender.should have_attribute_accessor(:name)
  13 + end
  14 +
  15 + it "should have a phone_ddd accessor" do
  16 + @sender.should have_attribute_accessor(:phone_ddd)
  17 + end
  18 +
  19 + it "should have a phone_number" do
  20 + @sender.should have_attribute_accessor(:phone_number)
  21 + end
  22 +
  23 + it "should be able to initialize with all attributes" do
  24 + sender = PagSeguro::Sender.new(name: "Stefano Diem Benatti", email: "stefano@heavenstudio.com.br", phone_ddd: "11", phone_number: "93430994")
  25 + sender.name.should == "Stefano Diem Benatti"
  26 + sender.email.should == "stefano@heavenstudio.com.br"
  27 + sender.phone_ddd.should == "11"
  28 + sender.phone_number.should == "93430994"
  29 + end
  30 +
  31 + it "should tell valid e-mail appart" do
  32 + @sender.email = "nothing"
  33 + @sender.should_not be_a_valid_email
  34 + @sender.email = ("a" * 50) + "waytoolongemail@mail.com"
  35 + @sender.should_not be_a_valid_email
  36 + @sender.email = "stefano@heavenstudio.com.br"
  37 + @sender.should be_a_valid_email
  38 + end
  39 +
  40 + it "should not show invalid e-mail" do
  41 + @sender.email = "nothing"
  42 + @sender.email.should be_nil
  43 + end
  44 +
  45 + it "should crop the name if it is too big" do
  46 + @sender.name = "a" * 50 + "b" * 10
  47 + @sender.name.should == "a" * 50
  48 + end
  49 +
  50 + it "should not show invalid phone ddd's" do
  51 + @sender.phone_ddd = "111"
  52 + @sender.phone_ddd.should be_nil
  53 + end
  54 +
  55 + it "should not show invalid phone number" do
  56 + @sender.phone_number = "1234567"
  57 + @sender.phone_number.should be_nil
  58 + end
  59 + end
  60 +end
36 spec/pag_seguro/shipping_spec.rb
... ... @@ -0,0 +1,36 @@
  1 +# encoding: utf-8
  2 +require "spec_helper"
  3 +
  4 +valid_attributes = {
  5 + type: PagSeguro::Shipping::SEDEX,
  6 + state: "SP",
  7 + city: "São Paulo",
  8 + postal_code: "05363000",
  9 + district: "Jd. PoliPoli",
  10 + street: "Av. Otacilio Tomanik",
  11 + number: "775",
  12 + complement: "apto. 92"
  13 +}
  14 +
  15 +describe PagSeguro::Shipping do
  16 + context "instance" do
  17 + before { @shipping = PagSeguro::Shipping.new }
  18 + it { @shipping.should have_attribute_accessor(:type) }
  19 + it { @shipping.should have_attribute_accessor(:state) }
  20 + it { @shipping.should have_attribute_accessor(:city) }
  21 + it { @shipping.should have_attribute_accessor(:postal_code) }
  22 + it { @shipping.should have_attribute_accessor(:district) }
  23 + it { @shipping.should have_attribute_accessor(:street) }
  24 + it { @shipping.should have_attribute_accessor(:number) }
  25 + it { @shipping.should have_attribute_accessor(:complement) }
  26 + end
  27 +
  28 + it "should be able to initialize all attributes" do
  29 + PagSeguro::Shipping.new(valid_attributes).should be_valid
  30 + end
  31 +
  32 + it "should not show postal code unless valid" do
  33 + PagSeguro::Shipping.new(valid_attributes).postal_code.should == "05363000"
  34 + PagSeguro::Shipping.new(valid_attributes.merge(postal_code: 1234567)).postal_code.should be_blank
  35 + end
  36 +end
26 spec/spec_helper.rb
... ... @@ -1,4 +1,28 @@
1 1 require 'simplecov'
2 2 SimpleCov.start
3 3
4   -require File.dirname(__FILE__) + "/../lib/pag_seguro"
  4 +require File.dirname(__FILE__) + "/../lib/pag_seguro"
  5 +
  6 +class HaveAttributeAccessor
  7 + def initialize(attribute)
  8 + @attribute = attribute
  9 + end
  10 +
  11 + def matches?(target)
  12 + @target = target
  13 + @target.respond_to?(:"#{@attribute}").should == true
  14 + @target.respond_to?(:"#{@attribute}=").should == true
  15 + end
  16 +
  17 + def failure_message
  18 + "expected #{@target.inspect} to have '#{@expected}' attribute accessor"
  19 + end
  20 +
  21 + def negative_failure_message
  22 + "expected #{@target.inspect} not to have '#{@expected}' attribute accessor"
  23 + end
  24 +end
  25 +
  26 +def have_attribute_accessor(attribute)
  27 + HaveAttributeAccessor.new(attribute)
  28 +end

0 comments on commit 6aff219

Please sign in to comment.
Something went wrong with that request. Please try again.