Class: Clearsale::Order

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

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.build_address(builder, address) ⇒ Object



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

def self.build_address(builder, address)
  builder.tag!('Address') do |b|
    builder.tag!('Street', address.street_name)
    builder.tag!('Number', address.number)
    builder.tag!('Comp', address.complement)
    builder.tag!('County', address.neighborhood)
    builder.tag!('City', address.city)
    builder.tag!('State', address.state)
    builder.tag!('ZipCode', address.postal_code)
  end
end

.build_item(builder, order_item) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/clearsale/order.rb', line 111

def self.build_item(builder, order_item)
  builder.tag!('Item') do |b|
    b.tag!('ID', order_item.product.id)
    b.tag!('Name', order_item.product.name)
    b.tag!('ItemValue', order_item.price)
    b.tag!('Qty', order_item.quantity)
    b.tag!('CategoryID', order_item.product.category.id) if order_item.product.category.try(:id).present?
    b.tag!('CategoryName', order_item.product.category.name) if order_item.product.category.try(:name).present?
  end
end

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



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

def self.build_order(builder, order, payment, user)
  builder.tag!('ID', order.id)
  builder.tag!('Date', order.created_at.strftime("%Y-%m-%dT%H:%M:%S"))
  builder.tag!('Email', user.email)
  builder.tag!('TotalItens', order.total_items)
  builder.tag!('TotalOrder', order.total_order)
  builder.tag!('QtyInstallments', order.installments)
  builder.tag!('QtyItems', order.items_count)
  builder.tag!('IP', user.)
  builder.tag!('CollectionData') do |b|
    build_user_data(b, user, order.billing_address)
  end
  builder.tag!('ShippingData') do |b|
    build_user_data(b, user, order.shipping_address)
  end

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

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

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/clearsale/order.rb', line 88

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

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

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

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

    b.tag!('CardNumber', payment.card_number)
    b.tag!('CardBin', payment.card_number[0..5])
    b.tag!('CardType', CARD_TYPE_MAP.fetch(payment.acquirer.to_sym, 4)) # Failover is 'outros'
    b.tag!('CardExpirationDate', payment.card_expiration)
    b.tag!('Name', payment.customer_name)
    b.tag!('LegalDocument', user.cpf.gsub(/[\.\-]*/, '').strip)

    build_address(b, order.billing_address)
  end
end

.build_phone(builder, user) ⇒ Object



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

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

    builder.tag!('Phone') do |b|
      b.tag!('Type', 0) # Undefined
      b.tag!('DDD', stripped_phone[0..1])
      b.tag!('Number', stripped_phone[2..-1])
    end
  end
end

.build_user_data(builder, user, billing_address) ⇒ Object



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

def self.build_user_data(builder, user, billing_address)
  builder.tag!('ID', user.id)
  builder.tag!('Type', 1) # Pessoa FĂ­sica
  builder.tag!('LegalDocument1', user.cpf.gsub(/[\.\-]*/, '').strip)
  builder.tag!('Name', user.full_name)
  builder.tag!('BirthDate', user.birthdate.to_time.strftime("%Y-%m-%dT%H:%M:%S")) if user.birthdate.present?
  builder.tag!('Email', user.email)
  builder.tag!('Genre', user.gender.downcase)
  build_address(builder, billing_address)
  builder.tag!('Phones') do |b|
    build_phone(b, user)
  end
end

.to_xml(order, payment, user) ⇒ Object



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

def self.to_xml(order, payment, user)
  builder = Builder::XmlMarkup.new
  xml = builder.tag!("ClearSale") do |b|
    b.tag!('Orders') do |b|
      b.tag!('Order') do |b|
        build_order(b, order, payment, user)
      end
    end
  end.to_s
  xml
end