Class: Clearsale::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/clearsale/order.rb

Constant Summary collapse

CARD_TYPE_MAP =
{
  :visa       => 3,
  :mastercard => 2
}

Class Method Summary collapse

Class Method Details

.build_address(builder, address) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/clearsale/order.rb', line 62

def self.build_address(builder, address)
  builder.tag!('Endereco') do |b|
    builder.tag!('Logradouro', address.street_name)
    builder.tag!('Complemento', address.complement)
    builder.tag!('Numero', address.number)
    builder.tag!('Bairro', address.neighborhood)
    builder.tag!('Cidade', address.city)
    builder.tag!('UF', address.state)
    builder.tag!('CEP', address.postal_code)
    builder.tag!('Pais', 'Brasil')
  end
end

.build_collection_address(builder, address) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/clearsale/order.rb', line 75

def self.build_collection_address(builder, address)
  builder.tag!('EnderecoCobranca') do |b|
    builder.tag!('Logradouro', address.street_name)
    builder.tag!('Complemento', address.complement)
    builder.tag!('Numero', address.number)
    builder.tag!('Bairro', address.neighborhood)
    builder.tag!('Cidade', address.city)
    builder.tag!('UF', address.state)
    builder.tag!('CEP', address.postal_code)
    builder.tag!('Pais', 'Brasil')
  end
end

.build_item(builder, order_item) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/clearsale/order.rb', line 124

def self.build_item(builder, order_item)
  builder.tag!('Item') do |b|
    product = Object.new(order_item.product)
    category = Object.new(product.category)

    b.tag!('CodigoItem', product.product_id)
    b.tag!('NomeItem', product.name)
    b.tag!('ValorItem', order_item.price)
    b.tag!('Quantidade', order_item.quantity)
    b.tag!('CodigoCategoria', category.category_id) if category.try(:category_id).present?
    b.tag!('NomeCategoria', category.name) if category.try(:name).present?
  end
end

.build_order(builder, order, payment, user) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/clearsale/order.rb', line 21

def self.build_order(builder, order, payment, user)
  builder.tag!('PedidoID', order.order_id)
  builder.tag!('Data', order.created_at.strftime("%Y-%m-%dT%H:%M:%S"))
  builder.tag!('Email', user.email)
  builder.tag!('ValorTotalItens', order.total_items)
  builder.tag!('ValorTotalPedido', order.total_order)
  builder.tag!('QtdParcelas', order.installments)
  builder.tag!('QtdItens', order.items_count)
  builder.tag!('IP', user.)
  builder.tag!('DadosCobranca') do |b|
    build_user_data(b, user, order.billing_address)
  end
  builder.tag!('DadosEntrega') do |b|
    build_user_data(b, user, order.shipping_address)
  end

  builder.tag!('Pagamentos') do |b|
    build_payment_data(b, order, payment, user)
  end

  builder.tag!('Itens') do |b|
    order.order_items.each do |order_item|
      build_item(b, Object.new(order_item))
    end
  end
end

.build_payment_data(builder, order, payment, user) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/clearsale/order.rb', line 100

def self.build_payment_data(builder, order, payment, user)
  builder.tag!('Pagamento') do |b|
    paid_at = order.paid_at || Time.current

    b.tag!('Data', paid_at.strftime("%Y-%m-%dT%H:%M:%S"))
    b.tag!('Valor', payment.amount)

    #is_credit_card
    b.tag!('TipoPagamentoID', 1)

    b.tag!('QtdParcelas', order.installments)

    b.tag!('HashNumeroCartao', payment.card_hash)
    b.tag!('BinCartao', payment.card_number[0..5])
    b.tag!('Cartao4Ultimos', payment.card_number.reverse[0..3])
    b.tag!('TipoCartao', CARD_TYPE_MAP.fetch(payment.acquirer.to_sym, 4)) # Failover is 'outros'
    b.tag!('DataValidadeCartao', payment.card_expiration)
    b.tag!('NomeTitularCartao', payment.customer_name)
    b.tag!('DocumentoLegal1', payment.cpf.gsub(/\D+/, '')) if payment.cpf

    build_collection_address(b, Object.new(order.billing_address))
  end
end

.build_phone(builder, user) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/clearsale/order.rb', line 88

def self.build_phone(builder, user)
  if user.phone.present?
    stripped_phone = user.phone.gsub(/\(*\)*\s*\-*/, '')

    builder.tag!('Telefone') do |b|
      b.tag!('Tipo', 0) # Undefined
      b.tag!('DDD', stripped_phone[0..1])
      b.tag!('Numero', stripped_phone[2..-1])
    end
  end
end

.build_user_data(builder, user, billing_address) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/clearsale/order.rb', line 48

def self.build_user_data(builder, user, billing_address)
  builder.tag!('UsuarioID', user.user_id)
  builder.tag!('TipoUsuario', 1) # Pessoa FĂ­sica
  builder.tag!('DocumentoLegal1', user.cpf.gsub(/[\.\-]*/, '').strip)
  builder.tag!('Nome', user.full_name)
  builder.tag!('Email', user.email)
  builder.tag!('Sexo', user.gender.downcase)
  builder.tag!('Nascimento', user.birthdate.to_time.strftime("%Y-%m-%dT%H:%M:%S")) if user.birthdate.present?
  build_address(builder, Object.new(billing_address))
  builder.tag!('Telefones') do |b|
    build_phone(b, user)
  end
end

.to_xml(order, payment, user) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/clearsale/order.rb', line 10

def self.to_xml(order, payment, user)
  builder = Builder::XmlMarkup.new(:indent => 2, :encoding => 'utf-8')
  xml = builder.tag!("ClearID_Input") do |b|
    builder.tag!('SessionID', order.session_id)
    b.tag!('Pedido') do |b|
      build_order(b, order, payment, user)
    end
  end
  xml.to_s
end