Class: BigCharger

Inherits:
Object
  • Object
show all
Defined in:
lib/bigcharger.rb,
lib/bigcharger/ops.rb,
lib/bigcharger/utils.rb,
lib/bigcharger/config.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

SOAP_NAMESPACE =
'http://schemas.xmlsoap.org/soap/envelope/'
SERVICE_NAMESPACE =
'https://www.eway.com.au/gateway/managedpayment'
ENDPOINT =
'https://www.eway.com.au/gateway/ManagedPaymentService/managedCreditCardPayment.asmx'
TEST_ENDPOINT =
'https://www.eway.com.au/gateway/ManagedPaymentService/test/managedCreditCardPayment.asmx'
CUSTOMER_REQUEST_FIELDS =

List of fields (with significant ordering as per WSDL) for customer requests

[
  'Title',
  'FirstName',
  'LastName',
  'Address',
  'Suburb',
  'State',
  'Company',
  'PostCode',
  'Country',
  'Email',
  'Fax',
  'Phone',
  'Mobile',
  'CustomerRef',
  'JobDesc',
  'Comments',
  'URL',
  'CCNumber',
  'CCNameOnCard',
  'CCExpiryMonth',
  'CCExpiryYear'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(customer_id, username, password, test_mode = false, logger = Logger.new('/dev/null')) ⇒ BigCharger

Returns a new instance of BigCharger.


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bigcharger.rb', line 8

def initialize(customer_id, username, password, test_mode = false, logger = Logger.new('/dev/null'))
  @credentials = { 
    :customer_id => customer_id,
    :username => username,
    :password => password
  }
  @client = Curl::Easy.new
  @endpoint = test_mode ? TEST_ENDPOINT : ENDPOINT
  @logger = logger
  set_request_defaults
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger


6
7
8
# File 'lib/bigcharger.rb', line 6

def logger
  @logger
end

Instance Method Details

#create_customer(customer_fields = {}) ⇒ String

Creates a new managed customer on the eWAY server.

Parameters:

  • customer_fields (defaults to: {})

    Title, FirstName, LastName, Address, Suburb, State, Company, PostCode, Country, Email, Fax, Phone, Mobile, CustomerRef, JobDesc, Comments, URL, CCNumber, CCNameOnCard, CCExpiryMonth, CCExpiryYear

Returns:

  • (String)

    Managed customer ID of new customer.

Raises:


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bigcharger/ops.rb', line 11

def create_customer(customer_fields = {})
  log_operation(:create_customer, customer_fields)
  raise Error, 'customer_fields is not a Hash' unless customer_fields.class == Hash
  envelope = wrap_in_envelope do |xml|
    xml['man'].CreateCustomer {
      CUSTOMER_REQUEST_FIELDS.each do |field|
        xml['man'].send(field, customer_fields[field]) if customer_fields[field]
      end
    }
  end
  response = post(envelope, 'CreateCustomer')
  result = response.xpath('//man:CreateCustomerResult', { 'man' => SERVICE_NAMESPACE }).first
  return result ? result.text : false
end

#process_payment(managed_customer_id, amount, invoice_ref = nil, invoice_desc = nil) ⇒ Hash

Processes a payment of a given amount for the specified customer.

Parameters:

  • managed_customer_id (#to_s)
  • amount (Integer)

    The amount of the payment, in cents. (e.g. 1000 = $10)

  • invoice_ref (defaults to: nil)

    An optional field that you can use to identify the corresponding invoice within your system.

  • invoice_desc (defaults to: nil)

    An optional field to describe the subject of the payment.

Returns:

  • (Hash)

    ewayTrxnError, ewayTrxnStatus, ewayTrxnNumber, ewayReturnAmount, ewayAuthCode


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bigcharger/ops.rb', line 36

def process_payment(managed_customer_id, amount, invoice_ref = nil, invoice_desc = nil)
  log_operation(:process_payment, managed_customer_id, amount, invoice_ref, invoice_desc)
  envelope = wrap_in_envelope do |xml|
    xml['man'].ProcessPayment {
      xml['man'].managedCustomerID managed_customer_id
      xml['man'].amount amount
      xml['man'].invoiceReference invoice_ref if invoice_ref
      xml['man'].invoiceDescription invoice_desc if invoice_desc
    }
  end
  response = post(envelope, 'ProcessPayment')
  result = response.xpath('//man:ProcessPaymentResponse/man:ewayResponse', { 'man' => SERVICE_NAMESPACE }).first
  return result ? node_to_hash(result) : false
end

#process_payment_with_cvn(managed_customer_id, amount, cvn = nil, invoice_ref = nil, invoice_desc = nil) ⇒ Object

Identical to #process_payment except that it accepts a CVN/CVV for greater security.


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bigcharger/ops.rb', line 53

def process_payment_with_cvn(managed_customer_id, amount, cvn = nil, invoice_ref = nil, invoice_desc = nil)
  log_operation(:process_payment_with_cvn, managed_customer_id, amount, cvn, invoice_ref, invoice_desc)
  envelope = wrap_in_envelope do |xml|
    xml['man'].ProcessPaymentWithCVN {
      xml['man'].managedCustomerID managed_customer_id
      xml['man'].amount amount
      xml['man'].invoiceReference invoice_ref if invoice_ref
      xml['man'].invoiceDescription invoice_desc if invoice_desc
      xml['man'].cvn cvn if cvn
    }
  end
  response = post(envelope, 'ProcessPaymentWithCVN')
  result = response.xpath('//man:ProcessPaymentWithCVNResponse/man:ewayResponse', { 'man' => SERVICE_NAMESPACE }).first
  return result ? node_to_hash(result) : false
end

#query_customer(managed_customer_id) ⇒ Hash

Returns the eWAY record for a specific managed customer ID.

Parameters:

  • managed_customer_id (#to_s)

Returns:

  • (Hash)

    ManagedCustomerID, CustomerRef, CustomerTitle, CustomerFirstName, CustomerLastName, CustomerCompany, CustomerJobDesc, CustomerEmail, CustomerAddress, CustomerSuburb, CustomerState, CustomerPostCode, CustomerCountry, CustomerPhone1, CustomerPhone2, CustomerFax, CustomerURL, CustomerComments


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

def query_customer(managed_customer_id)
  log_operation(:query_customer, managed_customer_id)
  envelope = wrap_in_envelope do |xml|
    xml['man'].QueryCustomer {
      xml['man'].managedCustomerID managed_customer_id
    }
  end
  response = post(envelope, 'QueryCustomer')
  result = response.xpath('//man:QueryCustomerResult', { 'man' => SERVICE_NAMESPACE }).first
  return result ? node_to_hash(result) : false
end

#query_customer_by_reference(customer_ref) ⇒ Hash

Returns the eWAY customer record with the specified customer reference.

Parameters:

  • customer_ref (#to_s)

Returns:

  • (Hash)

    ManagedCustomerID, CustomerRef, CustomerTitle, CustomerFirstName, CustomerLastName, CustomerCompany, CustomerJobDesc, CustomerEmail, CustomerAddress, CustomerSuburb, CustomerState, CustomerPostCode, CustomerCountry, CustomerPhone1, CustomerPhone2, CustomerFax, CustomerURL, CustomerComments


96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bigcharger/ops.rb', line 96

def query_customer_by_reference(customer_ref)
  log_operation(:query_customer_by_reference, customer_ref)
  envelope = wrap_in_envelope do |xml|
    xml['man'].QueryCustomerByReference {
      xml['man'].CustomerReference customer_ref
    }
  end
  response = post(envelope, 'QueryCustomerByReference')
  result = response.xpath('//man:QueryCustomerByReferenceResult', { 'man' => SERVICE_NAMESPACE }).first
  return result ? node_to_hash(result) : false
end

#query_payment(managed_customer_id) ⇒ Array of Hashes

Returns all the payment records associated with a specified managed customer ID.

Parameters:

  • customer_ref (#to_s)

Returns:

  • (Array of Hashes)

    TotalAmount, Result, ResponseText, TransactionDate, ewayTrxnNumber


114
115
116
117
118
119
120
121
122
123
124
# File 'lib/bigcharger/ops.rb', line 114

def query_payment(managed_customer_id)
  log_operation(:query_payment, managed_customer_id)
  envelope = wrap_in_envelope do |xml|
    xml['man'].QueryPayment {
      xml['man'].managedCustomerID managed_customer_id
    }
  end
  response = post(envelope, 'QueryPayment')
  result = response.xpath('//man:QueryPaymentResult', { 'man' => SERVICE_NAMESPACE }).first
  return result ? node_collection_to_array(result) : false
end

#update_customer(managed_customer_id, customer_fields = {}) ⇒ Boolean

Update details of an eWAY customer record.

Parameters:

  • managed_customer_id (#to_s)
  • customer_fields (defaults to: {})

    Title, FirstName, LastName, Address, Suburb, State, Company, PostCode, Country, Email, Fax, Phone, Mobile, CustomerRef, JobDesc, Comments, URL, CCNumber, CCNameOnCard, CCExpiryMonth, CCExpiryYear

Returns:

  • (Boolean)

    Success or otherwise

Raises:


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bigcharger/ops.rb', line 133

def update_customer(managed_customer_id, customer_fields = {})
  log_operation(:update_customer, managed_customer_id, customer_fields)
  raise Error, 'customer_fields is not a Hash' unless customer_fields.class == Hash
  envelope = wrap_in_envelope do |xml|
    xml['man'].UpdateCustomer {
      xml['man'].managedCustomerID managed_customer_id
      CUSTOMER_REQUEST_FIELDS.each do |field|
        xml['man'].send(field, customer_fields[field]) if customer_fields[field]
      end
    }
  end
  response = post(envelope, 'UpdateCustomer')
  result = response.xpath('//man:UpdateCustomerResult', { 'man' => SERVICE_NAMESPACE }).first
  return result ? result.text == 'true' : false
end