Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added payment attributes for xml generation
- Loading branch information
1 parent
0418a3c
commit 6aff219
Showing
15 changed files
with
667 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,3 +1,3 @@ | |||
module PagSeguro | module PagSeguro | ||
VERSION = "0.0.1" | VERSION = "0.1.0" | ||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.