Class: Effective::Providers::MonerisCharge

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/providers/moneris_charge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order:, purchased_url: nil, declined_url: nil) ⇒ MonerisCharge

Returns a new instance of MonerisCharge.



9
10
11
12
13
14
15
# File 'app/models/effective/providers/moneris_charge.rb', line 9

def initialize(order:, purchased_url: nil, declined_url: nil)
  @order = order
  @purchased_url = purchased_url
  @declined_url = declined_url

  moneris_preload!
end

Instance Attribute Details

#declined_urlObject

Returns the value of attribute declined_url.



6
7
8
# File 'app/models/effective/providers/moneris_charge.rb', line 6

def declined_url
  @declined_url
end

#hpp_idObject

return values



7
8
9
# File 'app/models/effective/providers/moneris_charge.rb', line 7

def hpp_id
  @hpp_id
end

#orderObject

Returns the value of attribute order.



6
7
8
# File 'app/models/effective/providers/moneris_charge.rb', line 6

def order
  @order
end

#purchased_urlObject

Returns the value of attribute purchased_url.



6
7
8
# File 'app/models/effective/providers/moneris_charge.rb', line 6

def purchased_url
  @purchased_url
end

#ticketObject

return values



7
8
9
# File 'app/models/effective/providers/moneris_charge.rb', line 7

def ticket
  @ticket
end

Instance Method Details

#moneris_preload!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 'app/models/effective/providers/moneris_charge.rb', line 21

def moneris_preload!
  # Make the moneris preload request
  uri = URI.parse(EffectiveOrders.moneris[:hpp_url])
  params = moneris_preload_payload.to_query
  headers = {}

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  body = http.post(uri.path, params, headers).body
  doc = ::Nokogiri::XML(body)

  # Parse preload request
  moneris = [:hpp_id, :ticket, :order_id, :response_code].inject({}) do |h, key|
    h[key] = doc.xpath("//#{key}").children.first.to_s; h
  end

  # Transaction Response Code: < 50: data successfully loaded, >= 50: data not loaded
  moneris[:response_code] = (moneris[:response_code].to_i rescue 50)

  raise 'data not loaded' unless moneris[:response_code] < 50

  # Our return value
  @hpp_id = moneris[:hpp_id]
  @ticket = moneris[:ticket]
end

#moneris_preload_payloadObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/effective/providers/moneris_charge.rb', line 48

def moneris_preload_payload
  payload = {
    ps_store_id: EffectiveOrders.moneris[:ps_store_id],
    hpp_key: EffectiveOrders.moneris[:hpp_key],
    hpp_preload: '',
    charge_total: ('%.2f' % (order.total / 100.0)),

    # Optional
    order_id: order_id,
    lang: 'en-ca',
    email: order.user.email,

    rvar_purchased_url: purchased_url,
    rvar_declined_url: declined_url
  }.compact

  if order.tax.present?
    payload[:gst] = ('%.2f' % (order.tax / 100.0))
  end

  if order.billing_name.present?
    payload[:bill_first_name] = order.billing_name.split(' ')[0]
    payload[:bill_last_name] = order.billing_name.split(' ')[1..-1].join(' ')
  end

  if order.billing_address.present?
    address = order.billing_address
    payload[:bill_address_one] = address.address1
    payload[:bill_city] = address.city
    payload[:bill_state_or_province] = address.state
    payload[:bill_postal_code] = address.postal_code
    payload[:bill_country] = address.country
  end

  if order.shipping_address.present?
    address = order.shipping_address
    payload[:ship_address_one] = address.address1
    payload[:ship_city] = address.city
    payload[:ship_state_or_province] = address.state
    payload[:ship_postal_code] = address.postal_code
    payload[:ship_country] = address.country
  end

  order.order_items.each_with_index do |item, index|
    payload["id#{index}"] = index
    payload["description#{index}"] = item.title
    payload["quantity#{index}"] = item.quantity
    payload["price#{index}"] = ('%.2f' % (item.price / 100.0))
    payload["subtotal#{index}"] = ('%.2f' % (item.subtotal / 100.0))
  end

  payload
end

#present?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/models/effective/providers/moneris_charge.rb', line 17

def present?
  ticket.present? && hpp_id.present?
end