Skip to content

Commit

Permalink
Added payment attributes for xml generation
Browse files Browse the repository at this point in the history
  • Loading branch information
teonimesic committed Dec 14, 2011
1 parent 0418a3c commit 6aff219
Show file tree
Hide file tree
Showing 15 changed files with 667 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Guardfile
Expand Up @@ -2,4 +2,6 @@ guard 'rspec', version: 2 do
watch(%r{^spec/.+_spec\.rb}) watch(%r{^spec/.+_spec\.rb})
watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" } watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" } watch('spec/spec_helper.rb') { "spec" }
watch('lib/pag_seguro.rb') { "spec" }
watch('lib/checkout.xml.haml') { "spec/pag_seguro/checkout_xml_spec.rb" }
end end
15 changes: 15 additions & 0 deletions lib/pag_seguro.rb
@@ -1,6 +1,21 @@
$: << "lib/pag_seguro" $: << "lib/pag_seguro"


# Core Ruby Libraries
require "net/https"
require "uri"

# Third party gems
require "active_model"
require "nokogiri"
require "haml"

# PagSeguro classes
require "item"
require "payment" require "payment"
require "sender"
require "shipping"

# Version
require "version" require "version"


module PagSeguro module PagSeguro
Expand Down
59 changes: 59 additions & 0 deletions lib/pag_seguro/checkout.xml.haml
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
%checkout
- if payment.id.present?
%reference= payment.id
- if payment.extra_amount.present?
%extraAmount= payment.extra_amount
- if payment.redirect_url.present?
%redirectUrl= payment.redirect_url
- if payment.max_uses.present?
%maxUses= payment.max_uses
- if payment. max_age.present?
%maxAge= payment.max_age

%currency BRL

%items
- items.each do |item|
%item
%id= item.id
%description= item.description
%amount= item.amount
%quantity= item.quantity
- if item.shipping_cost.present?
%shippingCost= item.shipping_cost
- if item.weight.present?
%weight= item.weight

- if sender.present?
%sender
- if sender.name.present?
%name= sender.name
- if sender.email.present?
%email= sender.email
%phone
- if sender.phone_ddd.present?
%areaCode= sender.phone_ddd
- if sender.phone_number.present?
%number= sender.phone_number

- if shipping.present?
%shipping
- if shipping.type.present?
%type= shipping.type
%address
%country BRA
- if shipping.state.present?
%state= shipping.state
- if shipping.city.present?
%city= shipping.city
- if shipping.postal_code.present?
%postalCode= shipping.postal_code
- if shipping.district.present?
%district= shipping.district
- if shipping.street.present?
%street= shipping.street
- if shipping.number.present?
%number= shipping.number
- if shipping.complement.present?
%complement= shipping.complement
31 changes: 31 additions & 0 deletions lib/pag_seguro/item.rb
@@ -0,0 +1,31 @@
module PagSeguro
class Item
include ActiveModel::Validations

attr_accessor :id, :description, :amount, :quantity, :shipping_cost, :weight

validates_presence_of :id, :description, :amount, :quantity
validates_format_of :amount, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot"
validates_format_of :shipping_cost, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot"
validates_format_of :weight, with: /^\d+$/, message: " must be an integer"
validate :quantity_amount

def initialize(attributes = {})
@id = attributes[:id]
@description = attributes[:description]
@amount = attributes[:amount]
@quantity = attributes[:quantity]
@shipping_cost = attributes[:shipping_cost]
@weight = attributes[:weight]
end

def description
@description.present? && @description.size > 100 ? @description[0..99] : @description
end

protected
def quantity_amount
errors.add(:quantity, " must be a number between 1 and 999") if @quantity.present? && (@quantity == "0" || @quantity.to_s !~ /^\d{1,3}$/)
end
end
end
38 changes: 36 additions & 2 deletions lib/pag_seguro/payment.rb
@@ -1,7 +1,41 @@
require "net/https"

module PagSeguro module PagSeguro
class Payment class Payment
include ActiveModel::Validations

BASE_URL = 'ws.pagseguro.uol.com.br/v2/checkout' BASE_URL = 'ws.pagseguro.uol.com.br/v2/checkout'

attr_accessor :id, :email, :token, :items, :sender, :shipping, :extra_amount, :redirect_url, :max_uses, :max_age

validates_presence_of :email, :token
validates_format_of :extra_amount, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot", allow_blank: true
validates_format_of :redirect_url, with: URI::regexp(%w(http https)), message: " must give a correct url for redirection", allow_blank: true
validate :max_uses_number, :max_age_number

def initialize(email = nil, token = nil, options = {})
@email = email unless email.nil?
@token = token unless token.nil?
@id = options[:id]
@sender = options[:sender] || Sender.new
@shipping = options[:shipping] || Shipping.new
@items = options[:items] || []
@extra_amount = options[:extra_amount]
@redirect_url = options[:redirect_url]
@max_uses = options[:max_uses]
@max_age = options[:max_age]
end

def checkout_xml
xml_content = File.open( File.dirname(__FILE__) + "/checkout.xml.haml" ).read
Haml::Engine.new(xml_content).render(nil, items: @items, payment: self, sender: @sender, shipping: @shipping)
end

protected
def max_uses_number
errors.add(:max_uses, " must be an integer greater than 0") if @max_uses.present? && @max_uses.to_i <= 0
end

def max_age_number
errors.add(:max_age, " must be an integer grater or equal to 30") if @max_age.present? && @max_age.to_i < 30
end
end end
end end
32 changes: 32 additions & 0 deletions lib/pag_seguro/sender.rb
@@ -0,0 +1,32 @@
module PagSeguro
class Sender
attr_accessor :name, :email, :phone_ddd, :phone_number

def initialize(options = {})
@name = options[:name]
@email = options[:email]
@phone_ddd = options[:phone_ddd]
@phone_number = options[:phone_number]
end

def email
valid_email? ? @email : nil
end

def valid_email?
@email =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i && @email.size <= 60
end

def name
( @name.present? && @name.size > 50 ) ? @name[0..49] : @name
end

def phone_ddd
@phone_ddd if @phone_ddd =~ /^\d{2}$/
end

def phone_number
@phone_number if @phone_number =~/^\d{8}$/
end
end
end
28 changes: 28 additions & 0 deletions lib/pag_seguro/shipping.rb
@@ -0,0 +1,28 @@
module PagSeguro
class Shipping
include ActiveModel::Validations

PAC = 1
SEDEX = 2
NOT_IDENTIFIED = 3

validates_format_of :postal_code, with: /^\d{8}$/, message: " must be an integer with 8 digits", allow_blank: true

attr_accessor :type, :state, :city, :postal_code, :district, :street, :number, :complement

def initialize(attributes = {})
@type = attributes[:type]
@state = attributes[:state]
@city = attributes[:city]
@postal_code = attributes[:postal_code]
@district = attributes[:district]
@street = attributes[:street]
@number = attributes[:number]
@complement = attributes[:complement]
end

def postal_code
@postal_code if @postal_code.present? && @postal_code.to_s.size == 8
end
end
end
2 changes: 1 addition & 1 deletion lib/pag_seguro/version.rb
@@ -1,3 +1,3 @@
module PagSeguro module PagSeguro
VERSION = "0.0.1" VERSION = "0.1.0"
end end
12 changes: 8 additions & 4 deletions pag_seguro.gemspec
Expand Up @@ -3,18 +3,22 @@ $:.push File.expand_path("../lib", __FILE__)
require "pag_seguro/version" require "pag_seguro/version"


Gem::Specification.new do |s| Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = "pag_seguro" s.name = "pag_seguro"
s.version = PagSeguro::VERSION s.version = PagSeguro::VERSION
s.authors = ["Stefano Diem Benatti"] s.authors = ["Stefano Diem Benatti"]
s.email = ["stefano.diem@gmail.com"] s.email = ["stefano.diem@gmail.com"]
s.homepage = "" s.homepage = "http://github.com/heavenstudio/pag_seguro"
s.summary = %q{A ruby gem to handle PagSeguro's API version 2} s.summary = %q{A ruby gem to handle PagSeguro's API version 2}
s.description = %q{A ruby gem to handle PagSeguro's API version 2} s.required_ruby_version = '>= 1.9.2'

s.rubyforge_project = "pag_seguro"


s.files = `git ls-files`.split("\n") s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.has_rdoc = false

s.add_dependency('activemodel')
s.add_dependency('haml')
s.add_dependency('nokogiri')
end end

0 comments on commit 6aff219

Please sign in to comment.