Class: ATPAY::Payment

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/atpay/payment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePayment

Returns a new instance of Payment.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/atpay/payment.rb', line 55

def initialize
  @username = ATPAY.username
  @password = ATPAY.password
  @host = ATPAY.host
  @ip_address = '127.0.0.1'
  @error = @first_name = @last_name = @email = @address = @address2 = @city = @state = @zip = @phone = @name_on_card = @number = @credit_card_type = @cvv = @expiration_date = @free_text = @recurring_installment_interval_method_additional_info = @account_id = @sub_account_id = @service_expiry_method = @service_expiry_method_additional_info = @request = @response = @transaction_id = @org_options = @dynamic_tags = @xml = @order_id = ''
  @amount = @base_amount = @first_installment_amount = @initial_pre_auth_amount = @first_installment_interval = @recurring_amount = 0
  @recurring_payment = @success = false
  @recurring_installment_interval_method = 'Daily'
  errors
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



43
44
45
# File 'lib/atpay/payment.rb', line 43

def 
  @account_id
end

#addressObject

Returns the value of attribute address.



17
18
19
# File 'lib/atpay/payment.rb', line 17

def address
  @address
end

#address2Object

Returns the value of attribute address2.



18
19
20
# File 'lib/atpay/payment.rb', line 18

def address2
  @address2
end

#amountObject

charge info



31
32
33
# File 'lib/atpay/payment.rb', line 31

def amount
  @amount
end

#atpay_errorObject

internal



53
54
55
# File 'lib/atpay/payment.rb', line 53

def atpay_error
  @atpay_error
end

#base_amountObject

Returns the value of attribute base_amount.



36
37
38
# File 'lib/atpay/payment.rb', line 36

def base_amount
  @base_amount
end

#cityObject

Returns the value of attribute city.



19
20
21
# File 'lib/atpay/payment.rb', line 19

def city
  @city
end

#credit_card_typeObject

Returns the value of attribute credit_card_type.



27
28
29
# File 'lib/atpay/payment.rb', line 27

def credit_card_type
  @credit_card_type
end

#cvvObject

Returns the value of attribute cvv.



28
29
30
# File 'lib/atpay/payment.rb', line 28

def cvv
  @cvv
end

#dynamic_tagsObject

internal



53
54
55
# File 'lib/atpay/payment.rb', line 53

def dynamic_tags
  @dynamic_tags
end

#emailObject

Returns the value of attribute email.



16
17
18
# File 'lib/atpay/payment.rb', line 16

def email
  @email
end

#errorObject

internal



53
54
55
# File 'lib/atpay/payment.rb', line 53

def error
  @error
end

#error_idObject

internal



53
54
55
# File 'lib/atpay/payment.rb', line 53

def error_id
  @error_id
end

#errorsObject

internal



53
54
55
# File 'lib/atpay/payment.rb', line 53

def errors
  @errors
end

#expiration_dateObject

Returns the value of attribute expiration_date.



29
30
31
# File 'lib/atpay/payment.rb', line 29

def expiration_date
  @expiration_date
end

#first_installment_amountObject

Returns the value of attribute first_installment_amount.



37
38
39
# File 'lib/atpay/payment.rb', line 37

def first_installment_amount
  @first_installment_amount
end

#first_installment_intervalObject

Returns the value of attribute first_installment_interval.



39
40
41
# File 'lib/atpay/payment.rb', line 39

def first_installment_interval
  @first_installment_interval
end

#first_nameObject

user info



14
15
16
# File 'lib/atpay/payment.rb', line 14

def first_name
  @first_name
end

#free_textObject

Returns the value of attribute free_text.



33
34
35
# File 'lib/atpay/payment.rb', line 33

def free_text
  @free_text
end

#hostObject

Returns the value of attribute host.



11
12
13
# File 'lib/atpay/payment.rb', line 11

def host
  @host
end

#initial_pre_auth_amountObject

Returns the value of attribute initial_pre_auth_amount.



38
39
40
# File 'lib/atpay/payment.rb', line 38

def initial_pre_auth_amount
  @initial_pre_auth_amount
end

#ip_addressObject

Returns the value of attribute ip_address.



23
24
25
# File 'lib/atpay/payment.rb', line 23

def ip_address
  @ip_address
end

#last_nameObject

Returns the value of attribute last_name.



15
16
17
# File 'lib/atpay/payment.rb', line 15

def last_name
  @last_name
end

#name_on_cardObject

card info



25
26
27
# File 'lib/atpay/payment.rb', line 25

def name_on_card
  @name_on_card
end

#numberObject

Returns the value of attribute number.



26
27
28
# File 'lib/atpay/payment.rb', line 26

def number
  @number
end

#order_idObject

Returns the value of attribute order_id.



32
33
34
# File 'lib/atpay/payment.rb', line 32

def order_id
  @order_id
end

#org_optionsObject

internal



53
54
55
# File 'lib/atpay/payment.rb', line 53

def org_options
  @org_options
end

#passwordObject

Returns the value of attribute password.



11
12
13
# File 'lib/atpay/payment.rb', line 11

def password
  @password
end

#phoneObject

Returns the value of attribute phone.



22
23
24
# File 'lib/atpay/payment.rb', line 22

def phone
  @phone
end

#recurring_amountObject

Returns the value of attribute recurring_amount.



40
41
42
# File 'lib/atpay/payment.rb', line 40

def recurring_amount
  @recurring_amount
end

#recurring_installment_interval_methodObject

Returns the value of attribute recurring_installment_interval_method.



41
42
43
# File 'lib/atpay/payment.rb', line 41

def recurring_installment_interval_method
  @recurring_installment_interval_method
end

#recurring_installment_interval_method_additional_infoObject

Returns the value of attribute recurring_installment_interval_method_additional_info.



42
43
44
# File 'lib/atpay/payment.rb', line 42

def recurring_installment_interval_method_additional_info
  @recurring_installment_interval_method_additional_info
end

#recurring_paymentObject

recurrring info



35
36
37
# File 'lib/atpay/payment.rb', line 35

def recurring_payment
  @recurring_payment
end

#requestObject

results



48
49
50
# File 'lib/atpay/payment.rb', line 48

def request
  @request
end

#responseObject

Returns the value of attribute response.



49
50
51
# File 'lib/atpay/payment.rb', line 49

def response
  @response
end

#response_codeObject

internal



53
54
55
# File 'lib/atpay/payment.rb', line 53

def response_code
  @response_code
end

#service_expiry_methodObject

Returns the value of attribute service_expiry_method.



45
46
47
# File 'lib/atpay/payment.rb', line 45

def service_expiry_method
  @service_expiry_method
end

#service_expiry_method_additional_infoObject

Returns the value of attribute service_expiry_method_additional_info.



46
47
48
# File 'lib/atpay/payment.rb', line 46

def service_expiry_method_additional_info
  @service_expiry_method_additional_info
end

#stateObject

Returns the value of attribute state.



20
21
22
# File 'lib/atpay/payment.rb', line 20

def state
  @state
end

#sub_account_idObject

Returns the value of attribute sub_account_id.



44
45
46
# File 'lib/atpay/payment.rb', line 44

def 
  @sub_account_id
end

#successObject

Returns the value of attribute success.



50
51
52
# File 'lib/atpay/payment.rb', line 50

def success
  @success
end

#transaction_idObject

Returns the value of attribute transaction_id.



51
52
53
# File 'lib/atpay/payment.rb', line 51

def transaction_id
  @transaction_id
end

#usernameObject

Returns the value of attribute username.



11
12
13
# File 'lib/atpay/payment.rb', line 11

def username
  @username
end

#xmlObject

internal



53
54
55
# File 'lib/atpay/payment.rb', line 53

def xml
  @xml
end

#zipObject

Returns the value of attribute zip.



21
22
23
# File 'lib/atpay/payment.rb', line 21

def zip
  @zip
end

Instance Method Details

#charge(options = {}) ⇒ Object



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
101
102
103
104
105
106
107
108
109
110
# File 'lib/atpay/payment.rb', line 67

def charge(options = {})
  self.class.basic_auth(ATPAY.username, ATPAY.password)
  @org_options = options

  #user info
  @first_name = options[:first_name] if options[:first_name]
  @last_name = options[:last_name] if options[:last_name]
  @email = options[:email] if options[:email]
  @address = options[:address] if options[:address]
  @address2 = options[:address2] if options[:address2]
  @city = options[:city] if options[:city]
  @state = options[:state] if options[:state]
  @zip = options[:zip] if options[:zip]
  @phone = options[:phone] if options[:phone]
  @ip_address = options[:ip_address] if options[:ip_address]

  #card info
  @name_on_card = options[:name_on_card]
  @number = options[:number]
  @cvv = options[:cvv]
  @expiration_date = options[:expiration_date]
  @credit_card_type = options[:credit_card_type] if options[:credit_card_type]

  #charge info
  @amount = options[:amount] if options[:amount]
  @order_id = options[:order_id] if options[:order_id]
  @free_text = options[:free_text] if options[:free_text]

  #recurring info
  @recurring_payment = options[:recurring_payment] if options[:recurring_payment]
  @base_amount = options[:base_amount] if options[:base_amount]
  @first_installment_amount = options[:first_installment_amount] if options[:first_installment_amount]
  @initial_pre_auth_amount = options[:initial_pre_auth_amount] if options[:initial_pre_auth_amount]
  @first_installment_interval = options[:first_installment_interval] if options[:first_installment_interval]
  @recurring_amount = options[:recurring_amount] if options[:recurring_amount]
  @recurring_installment_interval_method = options[:recurring_installment_interval_method] if options[:recurring_installment_interval_method]
  @recurring_installment_interval_additional_info = options[:recurring_installment_interval_additional_info] if options[:recurring_installment_interval_additional_info]
  @account_id = options[:account_id] if options[:account_id]
  @sub_account_id = options[:sub_account_id] if options[:sub_account_id]
  @service_expiry_method = options[:service_expiry_method] if options[:service_expiry_method]
  @service_expiry_method_additional_info = options[:service_expiry_method_additional_info] if options[:service_expiry_method_additional_info]
  @dynamic_tags = dynamic_tags
  send_payment_request
end

#fire_payment_requestObject



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/atpay/payment.rb', line 158

def fire_payment_request
  begin
    @request = @xml = render_xml
    puts @xml
    self.class.headers  'Content-Type' => 'text/xml; charset=utf-8',
                        'Content-Length' => @xml.length.to_s,
                        'SOAPAction' => soap_action
    @response = self.class.post(url, :body => @xml)
    puts @response.body
  rescue => e
    puts e
  end
end

#handle_payment_responseObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/atpay/payment.rb', line 130

def handle_payment_response
  @success = false
  result = {}
  begin
    result = Crack::XML.parse(@response.body)['soap:Envelope']['soap:Body']['RegularTransactionResponse']['RegularTransactionResult']
    @success = (result['SuccessFlag'] == 'true')
  rescue NoMethodError
  end
  if @success
    @transaction_id = result['TransactionID']
    return true
  else
    if @response.body =~ /Unauthorized: Access is denied due to invalid credentials/
      @error = 'Access is denied due to invalid credentials'
    else
      @error_id = @response.parsed_response["Envelope"]["Body"]["RegularTransactionResponse"]["RegularTransactionResult"]["ErrorID"]
      @error = "Sorry we were unable to process your card"
      @atpay_error = @errors[@error_id.to_i] if @errors[@error_id.to_i].present?
    end
    return false
  end
end

#render_xmlObject



172
173
174
175
176
# File 'lib/atpay/payment.rb', line 172

def render_xml
  @dynamic_tags = dynamic_tags
  template = File.open(File.dirname(__FILE__)+'/request.xml.erb', 'r').read
  ERB.new(template).result(binding)
end

#send_payment_requestObject



124
125
126
127
128
# File 'lib/atpay/payment.rb', line 124

def send_payment_request
  fire_payment_request
  handle_payment_response
  success
end

#soap_actionObject



152
153
154
# File 'lib/atpay/payment.rb', line 152

def soap_action
  ['http://transactions.atpay.net','/webservices/ATPayTxWS/RegularTransaction'].join('')
end

#urlObject



155
156
157
# File 'lib/atpay/payment.rb', line 155

def url
  [@host,'/TxWS/ATPayTxWS.asmx'].join('')
end