Module: EffectiveMonerisCheckoutHelper

Defined in:
app/helpers/effective_moneris_checkout_helper.rb

Constant Summary collapse

SCRUB =
/[^\w\d#,\s]/

Instance Method Summary collapse

Instance Method Details

#moneris_checkout_preload_request(order) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/effective_moneris_checkout_helper.rb', line 4

def moneris_checkout_preload_request(order)
  # Make the Preload Request
  params = {
    # Required
    environment: EffectiveOrders.moneris_checkout.fetch(:environment),

    api_token: EffectiveOrders.moneris_checkout.fetch(:api_token),
    store_id: EffectiveOrders.moneris_checkout.fetch(:store_id),
    checkout_id: EffectiveOrders.moneris_checkout.fetch(:checkout_id),
    action: :preload,
    txn_total: ('%.2f' % (order.total_with_surcharge / 100.0)),

    # Optional
    order_no: order.transaction_id, # Has to be unique. This is order number, billing name and Time.now
    cust_id: order.user_id,
    language: :en,

    contact_details: {
      first_name: moneris_checkout_scrub(order.billing_first_name),
      last_name: moneris_checkout_scrub(order.billing_last_name),
      email: order.email,
    }
  }

  if (address = order.billing_address).present?
    params.merge!(
      billing_details: {
        address_1: moneris_checkout_scrub(address.address1),
        address_2: moneris_checkout_scrub(address.address2),
        city: moneris_checkout_scrub(address.city),
        province: address.state_code,
        country: address.country_code,
        postal_code: address.postal_code
      }
    )
  end

  if (address = order.shipping_address).present?
    params.merge!(
      shipping_details: {
        address_1: moneris_checkout_scrub(address.address1),
        address_2: moneris_checkout_scrub(address.address2),
        city: address.city,
        province: address.state_code,
        country: address.country_code,
        postal_code: address.postal_code
      }
    )
  end

  response = Effective::Http.post(EffectiveOrders.moneris_request_url, params: params)
  preload = response['response'] if response

  raise("moneris preload error #{response}") unless preload && preload['success'].to_s == 'true'

  payload = {
    environment: EffectiveOrders.moneris_checkout.fetch(:environment),
    ticket: preload['ticket']
  }
end

#moneris_checkout_scrub(value) ⇒ Object



65
66
67
68
# File 'app/helpers/effective_moneris_checkout_helper.rb', line 65

def moneris_checkout_scrub(value)
  return value unless value.kind_of?(String)
  value.gsub(SCRUB, '').first(50)
end