yetanothernguyen-activemerchant 1.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. data/CHANGELOG +711 -0
  2. data/CONTRIBUTORS +245 -0
  3. data/MIT-LICENSE +20 -0
  4. data/gem-public_cert.pem +20 -0
  5. data/lib/active_merchant.rb +52 -0
  6. data/lib/active_merchant/billing.rb +9 -0
  7. data/lib/active_merchant/billing/avs_result.rb +98 -0
  8. data/lib/active_merchant/billing/base.rb +57 -0
  9. data/lib/active_merchant/billing/check.rb +68 -0
  10. data/lib/active_merchant/billing/credit_card.rb +178 -0
  11. data/lib/active_merchant/billing/credit_card_formatting.rb +21 -0
  12. data/lib/active_merchant/billing/credit_card_methods.rb +125 -0
  13. data/lib/active_merchant/billing/cvv_result.rb +38 -0
  14. data/lib/active_merchant/billing/expiry_date.rb +34 -0
  15. data/lib/active_merchant/billing/gateway.rb +170 -0
  16. data/lib/active_merchant/billing/gateways.rb +18 -0
  17. data/lib/active_merchant/billing/gateways/authorize_net.rb +672 -0
  18. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +858 -0
  19. data/lib/active_merchant/billing/gateways/barclays_epdq.rb +308 -0
  20. data/lib/active_merchant/billing/gateways/beanstream.rb +139 -0
  21. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +282 -0
  22. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +54 -0
  23. data/lib/active_merchant/billing/gateways/blue_pay.rb +11 -0
  24. data/lib/active_merchant/billing/gateways/bogus.rb +142 -0
  25. data/lib/active_merchant/billing/gateways/braintree.rb +17 -0
  26. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +9 -0
  27. data/lib/active_merchant/billing/gateways/braintree_blue.rb +303 -0
  28. data/lib/active_merchant/billing/gateways/braintree_orange.rb +17 -0
  29. data/lib/active_merchant/billing/gateways/card_stream.rb +230 -0
  30. data/lib/active_merchant/billing/gateways/cyber_source.rb +430 -0
  31. data/lib/active_merchant/billing/gateways/data_cash.rb +597 -0
  32. data/lib/active_merchant/billing/gateways/efsnet.rb +235 -0
  33. data/lib/active_merchant/billing/gateways/elavon.rb +134 -0
  34. data/lib/active_merchant/billing/gateways/epay.rb +268 -0
  35. data/lib/active_merchant/billing/gateways/eway.rb +277 -0
  36. data/lib/active_merchant/billing/gateways/eway_managed.rb +231 -0
  37. data/lib/active_merchant/billing/gateways/exact.rb +222 -0
  38. data/lib/active_merchant/billing/gateways/federated_canada.rb +168 -0
  39. data/lib/active_merchant/billing/gateways/first_pay.rb +177 -0
  40. data/lib/active_merchant/billing/gateways/garanti.rb +262 -0
  41. data/lib/active_merchant/billing/gateways/ideal/ideal_base.rb +250 -0
  42. data/lib/active_merchant/billing/gateways/ideal/ideal_rabobank.pem +13 -0
  43. data/lib/active_merchant/billing/gateways/ideal/ideal_response.rb +29 -0
  44. data/lib/active_merchant/billing/gateways/ideal_rabobank.rb +55 -0
  45. data/lib/active_merchant/billing/gateways/inspire.rb +221 -0
  46. data/lib/active_merchant/billing/gateways/instapay.rb +164 -0
  47. data/lib/active_merchant/billing/gateways/iridium.rb +258 -0
  48. data/lib/active_merchant/billing/gateways/jetpay.rb +276 -0
  49. data/lib/active_merchant/billing/gateways/linkpoint.rb +454 -0
  50. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +156 -0
  51. data/lib/active_merchant/billing/gateways/merchant_ware.rb +289 -0
  52. data/lib/active_merchant/billing/gateways/modern_payments.rb +36 -0
  53. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +220 -0
  54. data/lib/active_merchant/billing/gateways/moneris.rb +209 -0
  55. data/lib/active_merchant/billing/gateways/net_registry.rb +189 -0
  56. data/lib/active_merchant/billing/gateways/netaxept.rb +239 -0
  57. data/lib/active_merchant/billing/gateways/netbilling.rb +168 -0
  58. data/lib/active_merchant/billing/gateways/nmi.rb +13 -0
  59. data/lib/active_merchant/billing/gateways/ogone.rb +292 -0
  60. data/lib/active_merchant/billing/gateways/orbital.rb +321 -0
  61. data/lib/active_merchant/billing/gateways/orbital/orbital_soft_descriptors.rb +46 -0
  62. data/lib/active_merchant/billing/gateways/pay_junction.rb +392 -0
  63. data/lib/active_merchant/billing/gateways/pay_secure.rb +120 -0
  64. data/lib/active_merchant/billing/gateways/paybox_direct.rb +207 -0
  65. data/lib/active_merchant/billing/gateways/payflow.rb +253 -0
  66. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +207 -0
  67. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +39 -0
  68. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  69. data/lib/active_merchant/billing/gateways/payflow_express.rb +222 -0
  70. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +15 -0
  71. data/lib/active_merchant/billing/gateways/payflow_uk.rb +21 -0
  72. data/lib/active_merchant/billing/gateways/payment_express.rb +235 -0
  73. data/lib/active_merchant/billing/gateways/paypal.rb +121 -0
  74. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +351 -0
  75. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +49 -0
  76. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  77. data/lib/active_merchant/billing/gateways/paypal_express.rb +177 -0
  78. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +25 -0
  79. data/lib/active_merchant/billing/gateways/paystation.rb +201 -0
  80. data/lib/active_merchant/billing/gateways/plugnpay.rb +298 -0
  81. data/lib/active_merchant/billing/gateways/psigate.rb +219 -0
  82. data/lib/active_merchant/billing/gateways/psl_card.rb +304 -0
  83. data/lib/active_merchant/billing/gateways/qbms.rb +293 -0
  84. data/lib/active_merchant/billing/gateways/quantum.rb +282 -0
  85. data/lib/active_merchant/billing/gateways/quickpay.rb +223 -0
  86. data/lib/active_merchant/billing/gateways/realex.rb +311 -0
  87. data/lib/active_merchant/billing/gateways/sage.rb +146 -0
  88. data/lib/active_merchant/billing/gateways/sage/sage_bankcard.rb +88 -0
  89. data/lib/active_merchant/billing/gateways/sage/sage_core.rb +116 -0
  90. data/lib/active_merchant/billing/gateways/sage/sage_virtual_check.rb +97 -0
  91. data/lib/active_merchant/billing/gateways/sage_pay.rb +320 -0
  92. data/lib/active_merchant/billing/gateways/sallie_mae.rb +144 -0
  93. data/lib/active_merchant/billing/gateways/secure_net.rb +330 -0
  94. data/lib/active_merchant/billing/gateways/secure_pay.rb +31 -0
  95. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +193 -0
  96. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +113 -0
  97. data/lib/active_merchant/billing/gateways/skip_jack.rb +453 -0
  98. data/lib/active_merchant/billing/gateways/smart_ps.rb +271 -0
  99. data/lib/active_merchant/billing/gateways/stripe.rb +212 -0
  100. data/lib/active_merchant/billing/gateways/trans_first.rb +127 -0
  101. data/lib/active_merchant/billing/gateways/transax.rb +25 -0
  102. data/lib/active_merchant/billing/gateways/trust_commerce.rb +423 -0
  103. data/lib/active_merchant/billing/gateways/usa_epay.rb +194 -0
  104. data/lib/active_merchant/billing/gateways/verifi.rb +233 -0
  105. data/lib/active_merchant/billing/gateways/viaklix.rb +189 -0
  106. data/lib/active_merchant/billing/gateways/wirecard.rb +318 -0
  107. data/lib/active_merchant/billing/gateways/worldpay.rb +280 -0
  108. data/lib/active_merchant/billing/integrations.rb +17 -0
  109. data/lib/active_merchant/billing/integrations/action_view_helper.rb +70 -0
  110. data/lib/active_merchant/billing/integrations/bogus.rb +23 -0
  111. data/lib/active_merchant/billing/integrations/bogus/helper.rb +17 -0
  112. data/lib/active_merchant/billing/integrations/bogus/notification.rb +11 -0
  113. data/lib/active_merchant/billing/integrations/bogus/return.rb +10 -0
  114. data/lib/active_merchant/billing/integrations/chronopay.rb +23 -0
  115. data/lib/active_merchant/billing/integrations/chronopay/helper.rb +120 -0
  116. data/lib/active_merchant/billing/integrations/chronopay/notification.rb +158 -0
  117. data/lib/active_merchant/billing/integrations/chronopay/return.rb +10 -0
  118. data/lib/active_merchant/billing/integrations/direc_pay.rb +41 -0
  119. data/lib/active_merchant/billing/integrations/direc_pay/helper.rb +200 -0
  120. data/lib/active_merchant/billing/integrations/direc_pay/notification.rb +76 -0
  121. data/lib/active_merchant/billing/integrations/direc_pay/return.rb +32 -0
  122. data/lib/active_merchant/billing/integrations/direc_pay/status.rb +37 -0
  123. data/lib/active_merchant/billing/integrations/directebanking.rb +47 -0
  124. data/lib/active_merchant/billing/integrations/directebanking/helper.rb +90 -0
  125. data/lib/active_merchant/billing/integrations/directebanking/notification.rb +120 -0
  126. data/lib/active_merchant/billing/integrations/directebanking/return.rb +11 -0
  127. data/lib/active_merchant/billing/integrations/e_payment_plans.rb +48 -0
  128. data/lib/active_merchant/billing/integrations/e_payment_plans/helper.rb +34 -0
  129. data/lib/active_merchant/billing/integrations/e_payment_plans/notification.rb +84 -0
  130. data/lib/active_merchant/billing/integrations/gestpay.rb +25 -0
  131. data/lib/active_merchant/billing/integrations/gestpay/common.rb +42 -0
  132. data/lib/active_merchant/billing/integrations/gestpay/helper.rb +70 -0
  133. data/lib/active_merchant/billing/integrations/gestpay/notification.rb +85 -0
  134. data/lib/active_merchant/billing/integrations/gestpay/return.rb +10 -0
  135. data/lib/active_merchant/billing/integrations/helper.rb +96 -0
  136. data/lib/active_merchant/billing/integrations/hi_trust.rb +27 -0
  137. data/lib/active_merchant/billing/integrations/hi_trust/helper.rb +58 -0
  138. data/lib/active_merchant/billing/integrations/hi_trust/notification.rb +59 -0
  139. data/lib/active_merchant/billing/integrations/hi_trust/return.rb +67 -0
  140. data/lib/active_merchant/billing/integrations/migs.rb +22 -0
  141. data/lib/active_merchant/billing/integrations/migs/helper.rb +64 -0
  142. data/lib/active_merchant/billing/integrations/migs/return.rb +54 -0
  143. data/lib/active_merchant/billing/integrations/moneybookers.rb +26 -0
  144. data/lib/active_merchant/billing/integrations/moneybookers/helper.rb +59 -0
  145. data/lib/active_merchant/billing/integrations/moneybookers/notification.rb +129 -0
  146. data/lib/active_merchant/billing/integrations/nochex.rb +88 -0
  147. data/lib/active_merchant/billing/integrations/nochex/helper.rb +68 -0
  148. data/lib/active_merchant/billing/integrations/nochex/notification.rb +94 -0
  149. data/lib/active_merchant/billing/integrations/nochex/return.rb +10 -0
  150. data/lib/active_merchant/billing/integrations/notification.rb +62 -0
  151. data/lib/active_merchant/billing/integrations/paypal.rb +39 -0
  152. data/lib/active_merchant/billing/integrations/paypal/helper.rb +119 -0
  153. data/lib/active_merchant/billing/integrations/paypal/notification.rb +154 -0
  154. data/lib/active_merchant/billing/integrations/paypal/return.rb +10 -0
  155. data/lib/active_merchant/billing/integrations/quickpay.rb +21 -0
  156. data/lib/active_merchant/billing/integrations/quickpay/helper.rb +72 -0
  157. data/lib/active_merchant/billing/integrations/quickpay/notification.rb +74 -0
  158. data/lib/active_merchant/billing/integrations/return.rb +42 -0
  159. data/lib/active_merchant/billing/integrations/sage_pay_form.rb +37 -0
  160. data/lib/active_merchant/billing/integrations/sage_pay_form/encryption.rb +33 -0
  161. data/lib/active_merchant/billing/integrations/sage_pay_form/helper.rb +111 -0
  162. data/lib/active_merchant/billing/integrations/sage_pay_form/notification.rb +210 -0
  163. data/lib/active_merchant/billing/integrations/sage_pay_form/return.rb +31 -0
  164. data/lib/active_merchant/billing/integrations/two_checkout.rb +23 -0
  165. data/lib/active_merchant/billing/integrations/two_checkout/helper.rb +59 -0
  166. data/lib/active_merchant/billing/integrations/two_checkout/notification.rb +114 -0
  167. data/lib/active_merchant/billing/integrations/two_checkout/return.rb +17 -0
  168. data/lib/active_merchant/billing/integrations/valitor.rb +33 -0
  169. data/lib/active_merchant/billing/integrations/valitor/helper.rb +86 -0
  170. data/lib/active_merchant/billing/integrations/valitor/notification.rb +13 -0
  171. data/lib/active_merchant/billing/integrations/valitor/response_fields.rb +97 -0
  172. data/lib/active_merchant/billing/integrations/valitor/return.rb +13 -0
  173. data/lib/active_merchant/billing/integrations/world_pay.rb +27 -0
  174. data/lib/active_merchant/billing/integrations/world_pay/helper.rb +100 -0
  175. data/lib/active_merchant/billing/integrations/world_pay/notification.rb +160 -0
  176. data/lib/active_merchant/billing/response.rb +32 -0
  177. data/lib/active_merchant/common.rb +14 -0
  178. data/lib/active_merchant/common/connection.rb +177 -0
  179. data/lib/active_merchant/common/country.rb +328 -0
  180. data/lib/active_merchant/common/error.rb +26 -0
  181. data/lib/active_merchant/common/post_data.rb +24 -0
  182. data/lib/active_merchant/common/posts_data.rb +63 -0
  183. data/lib/active_merchant/common/requires_parameters.rb +16 -0
  184. data/lib/active_merchant/common/utils.rb +22 -0
  185. data/lib/active_merchant/common/validateable.rb +81 -0
  186. data/lib/active_merchant/railtie.rb +9 -0
  187. data/lib/active_merchant/version.rb +3 -0
  188. data/lib/activemerchant.rb +1 -0
  189. data/lib/certs/cacert.pem +7815 -0
  190. data/lib/support/gateway_support.rb +58 -0
  191. data/lib/support/outbound_hosts.rb +25 -0
  192. metadata +281 -0
@@ -0,0 +1,38 @@
1
+ module ActiveMerchant
2
+ module Billing
3
+ # Result of the Card Verification Value check
4
+ # http://www.bbbonline.org/eExport/doc/MerchantGuide_cvv2.pdf
5
+ # Check additional codes from cybersource website
6
+ class CVVResult
7
+
8
+ MESSAGES = {
9
+ 'D' => 'Suspicious transaction',
10
+ 'I' => 'Failed data validation check',
11
+ 'M' => 'Match',
12
+ 'N' => 'No Match',
13
+ 'P' => 'Not Processed',
14
+ 'S' => 'Should have been present',
15
+ 'U' => 'Issuer unable to process request',
16
+ 'X' => 'Card does not support verification'
17
+ }
18
+
19
+ def self.messages
20
+ MESSAGES
21
+ end
22
+
23
+ attr_reader :code, :message
24
+
25
+ def initialize(code)
26
+ @code = code.upcase unless code.blank?
27
+ @message = MESSAGES[@code]
28
+ end
29
+
30
+ def to_hash
31
+ {
32
+ 'code' => code,
33
+ 'message' => message
34
+ }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,34 @@
1
+ require 'date'
2
+
3
+ module ActiveMerchant
4
+ module Billing
5
+ class CreditCard
6
+ class ExpiryDate #:nodoc:
7
+ attr_reader :month, :year
8
+ def initialize(month, year)
9
+ @month = month.to_i
10
+ @year = year.to_i
11
+ end
12
+
13
+ def expired? #:nodoc:
14
+ Time.now.utc > expiration
15
+ end
16
+
17
+ def expiration #:nodoc:
18
+ begin
19
+ Time.utc(year, month, month_days, 23, 59, 59)
20
+ rescue ArgumentError
21
+ Time.at(0).utc
22
+ end
23
+ end
24
+
25
+ private
26
+ def month_days
27
+ mdays = [nil,31,28,31,30,31,30,31,31,30,31,30,31]
28
+ mdays[2] = 29 if Date.leap?(year)
29
+ mdays[month]
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,170 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'active_merchant/billing/response'
4
+
5
+ module ActiveMerchant #:nodoc:
6
+ module Billing #:nodoc:
7
+ #
8
+ # == Description
9
+ # The Gateway class is the base class for all ActiveMerchant gateway implementations.
10
+ #
11
+ # The standard list of gateway functions that most concrete gateway subclasses implement is:
12
+ #
13
+ # * <tt>purchase(money, creditcard, options = {})</tt>
14
+ # * <tt>authorize(money, creditcard, options = {})</tt>
15
+ # * <tt>capture(money, authorization, options = {})</tt>
16
+ # * <tt>void(identification, options = {})</tt>
17
+ # * <tt>credit(money, identification, options = {})</tt>
18
+ #
19
+ # Some gateways include features for recurring billing
20
+ #
21
+ # * <tt>recurring(money, creditcard, options = {})</tt>
22
+ #
23
+ # Some gateways also support features for storing credit cards:
24
+ #
25
+ # * <tt>store(creditcard, options = {})</tt>
26
+ # * <tt>unstore(identification, options = {})</tt>
27
+ #
28
+ # === Gateway Options
29
+ # The options hash consists of the following options:
30
+ #
31
+ # * <tt>:order_id</tt> - The order number
32
+ # * <tt>:ip</tt> - The IP address of the customer making the purchase
33
+ # * <tt>:customer</tt> - The name, customer number, or other information that identifies the customer
34
+ # * <tt>:invoice</tt> - The invoice number
35
+ # * <tt>:merchant</tt> - The name or description of the merchant offering the product
36
+ # * <tt>:description</tt> - A description of the transaction
37
+ # * <tt>:email</tt> - The email address of the customer
38
+ # * <tt>:currency</tt> - The currency of the transaction. Only important when you are using a currency that is not the default with a gateway that supports multiple currencies.
39
+ # * <tt>:billing_address</tt> - A hash containing the billing address of the customer.
40
+ # * <tt>:shipping_address</tt> - A hash containing the shipping address of the customer.
41
+ #
42
+ # The <tt>:billing_address</tt>, and <tt>:shipping_address</tt> hashes can have the following keys:
43
+ #
44
+ # * <tt>:name</tt> - The full name of the customer.
45
+ # * <tt>:company</tt> - The company name of the customer.
46
+ # * <tt>:address1</tt> - The primary street address of the customer.
47
+ # * <tt>:address2</tt> - Additional line of address information.
48
+ # * <tt>:city</tt> - The city of the customer.
49
+ # * <tt>:state</tt> - The state of the customer. The 2 digit code for US and Canadian addresses. The full name of the state or province for foreign addresses.
50
+ # * <tt>:country</tt> - The [ISO 3166-1-alpha-2 code](http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm) for the customer.
51
+ # * <tt>:zip</tt> - The zip or postal code of the customer.
52
+ # * <tt>:phone</tt> - The phone number of the customer.
53
+ #
54
+ # == Implmenting new gateways
55
+ #
56
+ # See the {ActiveMerchant Guide to Contributing}[http://code.google.com/p/activemerchant/wiki/Contributing]
57
+ #
58
+ class Gateway
59
+ include PostsData
60
+ include RequiresParameters
61
+ include CreditCardFormatting
62
+ include Utils
63
+
64
+ DEBIT_CARDS = [ :switch, :solo ]
65
+ CURRENCIES_WITHOUT_FRACTIONS = [ 'JPY' ]
66
+ CREDIT_DEPRECATION_MESSAGE = "Support for using credit to refund existing transactions is deprecated and will be removed from a future release of ActiveMerchant. Please use the refund method instead."
67
+
68
+ cattr_reader :implementations
69
+ @@implementations = []
70
+
71
+ def self.inherited(subclass)
72
+ super
73
+ @@implementations << subclass
74
+ end
75
+
76
+ # The format of the amounts used by the gateway
77
+ # :dollars => '12.50'
78
+ # :cents => '1250'
79
+ class_attribute :money_format
80
+ self.money_format = :dollars
81
+
82
+ # The default currency for the transactions if no currency is provided
83
+ class_attribute :default_currency
84
+
85
+ # The countries of merchants the gateway supports
86
+ class_attribute :supported_countries
87
+ self.supported_countries = []
88
+
89
+ # The supported card types for the gateway
90
+ class_attribute :supported_cardtypes
91
+ self.supported_cardtypes = []
92
+
93
+ class_attribute :homepage_url
94
+ class_attribute :display_name
95
+
96
+ # The application making the calls to the gateway
97
+ # Useful for things like the PayPal build notation (BN) id fields
98
+ superclass_delegating_accessor :application_id
99
+ self.application_id = 'ActiveMerchant'
100
+
101
+ attr_reader :options
102
+
103
+ # Use this method to check if your gateway of interest supports a credit card of some type
104
+ def self.supports?(card_type)
105
+ supported_cardtypes.include?(card_type.to_sym)
106
+ end
107
+
108
+ def self.card_brand(source)
109
+ result = source.respond_to?(:brand) ? source.brand : source.type
110
+ result.to_s.downcase
111
+ end
112
+
113
+ def card_brand(source)
114
+ self.class.card_brand(source)
115
+ end
116
+
117
+ # Initialize a new gateway.
118
+ #
119
+ # See the documentation for the gateway you will be using to make sure there are no other
120
+ # required options.
121
+ def initialize(options = {})
122
+ end
123
+
124
+ # Are we running in test mode?
125
+ def test?
126
+ Base.gateway_mode == :test
127
+ end
128
+
129
+ private # :nodoc: all
130
+
131
+ def name
132
+ self.class.name.scan(/\:\:(\w+)Gateway/).flatten.first
133
+ end
134
+
135
+ def amount(money)
136
+ return nil if money.nil?
137
+ cents = if money.respond_to?(:cents)
138
+ deprecated "Support for Money objects is deprecated and will be removed from a future release of ActiveMerchant. Please use an Integer value in cents"
139
+ money.cents
140
+ else
141
+ money
142
+ end
143
+
144
+ if money.is_a?(String)
145
+ raise ArgumentError, 'money amount must be a positive Integer in cents.'
146
+ end
147
+
148
+ if self.money_format == :cents
149
+ cents.to_s
150
+ else
151
+ sprintf("%.2f", cents.to_f / 100)
152
+ end
153
+ end
154
+
155
+ def localized_amount(money, currency)
156
+ amount = amount(money)
157
+ CURRENCIES_WITHOUT_FRACTIONS.include?(currency.to_s) ? amount.split('.').first : amount
158
+ end
159
+
160
+ def currency(money)
161
+ money.respond_to?(:currency) ? money.currency : self.default_currency
162
+ end
163
+
164
+ def requires_start_date_or_issue_number?(credit_card)
165
+ return false if card_brand(credit_card).blank?
166
+ DEBIT_CARDS.include?(card_brand(credit_card).to_sym)
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,18 @@
1
+ module ActiveMerchant
2
+ module Billing
3
+ autoload :Gateway, 'active_merchant/billing/gateway'
4
+
5
+ Dir[File.dirname(__FILE__) + '/gateways/**/*.rb'].each do |f|
6
+
7
+ # Get camelized class name
8
+ filename = File.basename(f, '.rb')
9
+ # Add _gateway suffix
10
+ gateway_name = filename + '_gateway'
11
+ # Camelize the string to get the class name
12
+ gateway_class = gateway_name.camelize
13
+
14
+ # Register for autoloading
15
+ autoload gateway_class, f
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,672 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ # For more information on the Authorize.Net Gateway please visit their {Integration Center}[http://developer.authorize.net/]
4
+ #
5
+ # The login and password are not the username and password you use to
6
+ # login to the Authorize.Net Merchant Interface. Instead, you will
7
+ # use the API Login ID as the login and Transaction Key as the
8
+ # password.
9
+ #
10
+ # ==== How to Get Your API Login ID and Transaction Key
11
+ #
12
+ # 1. Log into the Merchant Interface
13
+ # 2. Select Settings from the Main Menu
14
+ # 3. Click on API Login ID and Transaction Key in the Security section
15
+ # 4. Type in the answer to the secret question configured on setup
16
+ # 5. Click Submit
17
+ #
18
+ # ==== Automated Recurring Billing (ARB)
19
+ #
20
+ # Automated Recurring Billing (ARB) is an optional service for submitting and managing recurring, or subscription-based, transactions.
21
+ #
22
+ # To use recurring, update_recurring, and cancel_recurring ARB must be enabled for your account.
23
+ #
24
+ # Information about ARB is available on the {Authorize.Net website}[http://www.authorize.net/solutions/merchantsolutions/merchantservices/automatedrecurringbilling/].
25
+ # Information about the ARB API is available at the {Authorize.Net Integration Center}[http://developer.authorize.net/]
26
+ class AuthorizeNetGateway < Gateway
27
+ API_VERSION = '3.1'
28
+
29
+ class_attribute :test_url, :live_url, :arb_test_url, :arb_live_url
30
+
31
+ self.test_url = "https://test.authorize.net/gateway/transact.dll"
32
+ self.live_url = "https://secure.authorize.net/gateway/transact.dll"
33
+
34
+ self.arb_test_url = 'https://apitest.authorize.net/xml/v1/request.api'
35
+ self.arb_live_url = 'https://api.authorize.net/xml/v1/request.api'
36
+
37
+ class_attribute :duplicate_window
38
+
39
+ APPROVED, DECLINED, ERROR, FRAUD_REVIEW = 1, 2, 3, 4
40
+
41
+ RESPONSE_CODE, RESPONSE_REASON_CODE, RESPONSE_REASON_TEXT = 0, 2, 3
42
+ AVS_RESULT_CODE, TRANSACTION_ID, CARD_CODE_RESPONSE_CODE = 5, 6, 38
43
+
44
+ self.supported_countries = ['US']
45
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover, :diners_club, :jcb]
46
+ self.homepage_url = 'http://www.authorize.net/'
47
+ self.display_name = 'Authorize.Net'
48
+
49
+ CARD_CODE_ERRORS = %w( N S )
50
+ AVS_ERRORS = %w( A E N R W Z )
51
+ AVS_REASON_CODES = %w(27 45)
52
+
53
+ AUTHORIZE_NET_ARB_NAMESPACE = 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'
54
+
55
+ RECURRING_ACTIONS = {
56
+ :create => 'ARBCreateSubscription',
57
+ :update => 'ARBUpdateSubscription',
58
+ :cancel => 'ARBCancelSubscription'
59
+ }
60
+
61
+ # Creates a new AuthorizeNetGateway
62
+ #
63
+ # The gateway requires that a valid login and password be passed
64
+ # in the +options+ hash.
65
+ #
66
+ # ==== Options
67
+ #
68
+ # * <tt>:login</tt> -- The Authorize.Net API Login ID (REQUIRED)
69
+ # * <tt>:password</tt> -- The Authorize.Net Transaction Key. (REQUIRED)
70
+ # * <tt>:test</tt> -- +true+ or +false+. If true, perform transactions against the test server.
71
+ # Otherwise, perform transactions against the production server.
72
+ def initialize(options = {})
73
+ requires!(options, :login, :password)
74
+ @options = options
75
+ super
76
+ end
77
+
78
+ # Performs an authorization, which reserves the funds on the customer's credit card, but does not
79
+ # charge the card.
80
+ #
81
+ # ==== Parameters
82
+ #
83
+ # * <tt>money</tt> -- The amount to be authorized as an Integer value in cents.
84
+ # * <tt>creditcard</tt> -- The CreditCard details for the transaction.
85
+ # * <tt>options</tt> -- A hash of optional parameters.
86
+ def authorize(money, creditcard, options = {})
87
+ post = {}
88
+ add_invoice(post, options)
89
+ add_creditcard(post, creditcard)
90
+ add_address(post, options)
91
+ add_customer_data(post, options)
92
+ add_duplicate_window(post)
93
+
94
+ commit('AUTH_ONLY', money, post)
95
+ end
96
+
97
+ # Perform a purchase, which is essentially an authorization and capture in a single operation.
98
+ #
99
+ # ==== Parameters
100
+ #
101
+ # * <tt>money</tt> -- The amount to be purchased as an Integer value in cents.
102
+ # * <tt>creditcard</tt> -- The CreditCard details for the transaction.
103
+ # * <tt>options</tt> -- A hash of optional parameters.
104
+ def purchase(money, creditcard, options = {})
105
+ post = {}
106
+ add_invoice(post, options)
107
+ add_creditcard(post, creditcard)
108
+ add_address(post, options)
109
+ add_customer_data(post, options)
110
+ add_duplicate_window(post)
111
+
112
+ commit('AUTH_CAPTURE', money, post)
113
+ end
114
+
115
+ # Captures the funds from an authorized transaction.
116
+ #
117
+ # ==== Parameters
118
+ #
119
+ # * <tt>money</tt> -- The amount to be captured as an Integer value in cents.
120
+ # * <tt>authorization</tt> -- The authorization returned from the previous authorize request.
121
+ def capture(money, authorization, options = {})
122
+ post = {:trans_id => authorization}
123
+ add_customer_data(post, options)
124
+ commit('PRIOR_AUTH_CAPTURE', money, post)
125
+ end
126
+
127
+ # Void a previous transaction
128
+ #
129
+ # ==== Parameters
130
+ #
131
+ # * <tt>authorization</tt> - The authorization returned from the previous authorize request.
132
+ def void(authorization, options = {})
133
+ post = {:trans_id => authorization}
134
+ add_duplicate_window(post)
135
+ commit('VOID', nil, post)
136
+ end
137
+
138
+ # Refund a transaction.
139
+ #
140
+ # This transaction indicates to the gateway that
141
+ # money should flow from the merchant to the customer.
142
+ #
143
+ # ==== Parameters
144
+ #
145
+ # * <tt>money</tt> -- The amount to be credited to the customer as an Integer value in cents.
146
+ # * <tt>identification</tt> -- The ID of the original transaction against which the refund is being issued.
147
+ # * <tt>options</tt> -- A hash of parameters.
148
+ #
149
+ # ==== Options
150
+ #
151
+ # * <tt>:card_number</tt> -- The credit card number the refund is being issued to. (REQUIRED)
152
+ # * <tt>:first_name</tt> -- The first name of the account being refunded.
153
+ # * <tt>:last_name</tt> -- The last name of the account being refunded.
154
+ # * <tt>:zip</tt> -- The postal code of the account being refunded.
155
+ def refund(money, identification, options = {})
156
+ requires!(options, :card_number)
157
+
158
+ post = { :trans_id => identification,
159
+ :card_num => options[:card_number]
160
+ }
161
+
162
+ post[:first_name] = options[:first_name] if options[:first_name]
163
+ post[:last_name] = options[:last_name] if options[:last_name]
164
+ post[:zip] = options[:zip] if options[:zip]
165
+
166
+ add_invoice(post, options)
167
+ add_duplicate_window(post)
168
+
169
+ commit('CREDIT', money, post)
170
+ end
171
+
172
+ def credit(money, identification, options = {})
173
+ deprecated CREDIT_DEPRECATION_MESSAGE
174
+ refund(money, identification, options)
175
+ end
176
+
177
+ # Create a recurring payment.
178
+ #
179
+ # This transaction creates a new Automated Recurring Billing (ARB) subscription. Your account must have ARB enabled.
180
+ #
181
+ # ==== Parameters
182
+ #
183
+ # * <tt>money</tt> -- The amount to be charged to the customer at each interval as an Integer value in cents.
184
+ # * <tt>creditcard</tt> -- The CreditCard details for the transaction.
185
+ # * <tt>options</tt> -- A hash of parameters.
186
+ #
187
+ # ==== Options
188
+ #
189
+ # * <tt>:interval</tt> -- A hash containing information about the interval of time between payments. Must
190
+ # contain the keys <tt>:length</tt> and <tt>:unit</tt>. <tt>:unit</tt> can be either <tt>:months</tt> or <tt>:days</tt>.
191
+ # If <tt>:unit</tt> is <tt>:months</tt> then <tt>:length</tt> must be an integer between 1 and 12 inclusive.
192
+ # If <tt>:unit</tt> is <tt>:days</tt> then <tt>:length</tt> must be an integer between 7 and 365 inclusive.
193
+ # For example, to charge the customer once every three months the hash would be
194
+ # +:interval => { :unit => :months, :length => 3 }+ (REQUIRED)
195
+ # * <tt>:duration</tt> -- A hash containing keys for the <tt>:start_date</tt> the subscription begins (also the date the
196
+ # initial billing occurs) and the total number of billing <tt>:occurences</tt> or payments for the subscription. (REQUIRED)
197
+ def recurring(money, creditcard, options={})
198
+ requires!(options, :interval, :duration, :billing_address)
199
+ requires!(options[:interval], :length, [:unit, :days, :months])
200
+ requires!(options[:duration], :start_date, :occurrences)
201
+ requires!(options[:billing_address], :first_name, :last_name)
202
+
203
+ options[:credit_card] = creditcard
204
+ options[:amount] = money
205
+
206
+ request = build_recurring_request(:create, options)
207
+ recurring_commit(:create, request)
208
+ end
209
+
210
+ # Update a recurring payment's details.
211
+ #
212
+ # This transaction updates an existing Automated Recurring Billing (ARB) subscription. Your account must have ARB enabled
213
+ # and the subscription must have already been created previously by calling +recurring()+. The ability to change certain
214
+ # details about a recurring payment is dependent on transaction history and cannot be determined until after calling
215
+ # +update_recurring()+. See the ARB XML Guide for such conditions.
216
+ #
217
+ # ==== Parameters
218
+ #
219
+ # * <tt>options</tt> -- A hash of parameters.
220
+ #
221
+ # ==== Options
222
+ #
223
+ # * <tt>:subscription_id</tt> -- A string containing the <tt>:subscription_id</tt> of the recurring payment already in place
224
+ # for a given credit card. (REQUIRED)
225
+ def update_recurring(options={})
226
+ requires!(options, :subscription_id)
227
+ request = build_recurring_request(:update, options)
228
+ recurring_commit(:update, request)
229
+ end
230
+
231
+ # Cancel a recurring payment.
232
+ #
233
+ # This transaction cancels an existing Automated Recurring Billing (ARB) subscription. Your account must have ARB enabled
234
+ # and the subscription must have already been created previously by calling recurring()
235
+ #
236
+ # ==== Parameters
237
+ #
238
+ # * <tt>subscription_id</tt> -- A string containing the +subscription_id+ of the recurring payment already in place
239
+ # for a given credit card. (REQUIRED)
240
+ def cancel_recurring(subscription_id)
241
+ request = build_recurring_request(:cancel, :subscription_id => subscription_id)
242
+ recurring_commit(:cancel, request)
243
+ end
244
+
245
+ private
246
+
247
+ def commit(action, money, parameters)
248
+ parameters[:amount] = amount(money) unless action == 'VOID'
249
+
250
+ # Only activate the test_request when the :test option is passed in
251
+ parameters[:test_request] = @options[:test] ? 'TRUE' : 'FALSE'
252
+
253
+ url = test? ? self.test_url : self.live_url
254
+ data = ssl_post url, post_data(action, parameters)
255
+
256
+ response = parse(data)
257
+
258
+ message = message_from(response)
259
+
260
+ # Return the response. The authorization can be taken out of the transaction_id
261
+ # Test Mode on/off is something we have to parse from the response text.
262
+ # It usually looks something like this
263
+ #
264
+ # (TESTMODE) Successful Sale
265
+ test_mode = test? || message =~ /TESTMODE/
266
+
267
+ Response.new(success?(response), message, response,
268
+ :test => test_mode,
269
+ :authorization => response[:transaction_id],
270
+ :fraud_review => fraud_review?(response),
271
+ :avs_result => { :code => response[:avs_result_code] },
272
+ :cvv_result => response[:card_code]
273
+ )
274
+ end
275
+
276
+ def success?(response)
277
+ response[:response_code] == APPROVED
278
+ end
279
+
280
+ def fraud_review?(response)
281
+ response[:response_code] == FRAUD_REVIEW
282
+ end
283
+
284
+ def parse(body)
285
+ fields = split(body)
286
+
287
+ results = {
288
+ :response_code => fields[RESPONSE_CODE].to_i,
289
+ :response_reason_code => fields[RESPONSE_REASON_CODE],
290
+ :response_reason_text => fields[RESPONSE_REASON_TEXT],
291
+ :avs_result_code => fields[AVS_RESULT_CODE],
292
+ :transaction_id => fields[TRANSACTION_ID],
293
+ :card_code => fields[CARD_CODE_RESPONSE_CODE]
294
+ }
295
+ results
296
+ end
297
+
298
+ def post_data(action, parameters = {})
299
+ post = {}
300
+
301
+ post[:version] = API_VERSION
302
+ post[:login] = @options[:login]
303
+ post[:tran_key] = @options[:password]
304
+ post[:relay_response] = "FALSE"
305
+ post[:type] = action
306
+ post[:delim_data] = "TRUE"
307
+ post[:delim_char] = ","
308
+ post[:encap_char] = "$"
309
+
310
+ request = post.merge(parameters).collect { |key, value| "x_#{key}=#{CGI.escape(value.to_s)}" }.join("&")
311
+ request
312
+ end
313
+
314
+ def add_invoice(post, options)
315
+ post[:invoice_num] = options[:order_id]
316
+ post[:description] = options[:description]
317
+ end
318
+
319
+ def add_creditcard(post, creditcard)
320
+ post[:card_num] = creditcard.number
321
+ post[:card_code] = creditcard.verification_value if creditcard.verification_value?
322
+ post[:exp_date] = expdate(creditcard)
323
+ post[:first_name] = creditcard.first_name
324
+ post[:last_name] = creditcard.last_name
325
+ end
326
+
327
+ def add_customer_data(post, options)
328
+ if options.has_key? :email
329
+ post[:email] = options[:email]
330
+ post[:email_customer] = false
331
+ end
332
+
333
+ if options.has_key? :customer
334
+ post[:cust_id] = options[:customer]
335
+ end
336
+
337
+ if options.has_key? :ip
338
+ post[:customer_ip] = options[:ip]
339
+ end
340
+ end
341
+
342
+ # x_duplicate_window won't be sent by default, because sending it changes the response.
343
+ # "If this field is present in the request with or without a value, an enhanced duplicate transaction response will be sent."
344
+ # (as of 2008-12-30) http://www.authorize.net/support/AIM_guide_SCC.pdf
345
+ def add_duplicate_window(post)
346
+ unless duplicate_window.nil?
347
+ post[:duplicate_window] = duplicate_window
348
+ end
349
+ end
350
+
351
+ def add_address(post, options)
352
+ if address = options[:billing_address] || options[:address]
353
+ post[:address] = address[:address1].to_s
354
+ post[:company] = address[:company].to_s
355
+ post[:phone] = address[:phone].to_s
356
+ post[:zip] = address[:zip].to_s
357
+ post[:city] = address[:city].to_s
358
+ post[:country] = address[:country].to_s
359
+ post[:state] = address[:state].blank? ? 'n/a' : address[:state]
360
+ end
361
+
362
+ if address = options[:shipping_address]
363
+ post[:ship_to_first_name] = address[:first_name].to_s
364
+ post[:ship_to_last_name] = address[:last_name].to_s
365
+ post[:ship_to_address] = address[:address1].to_s
366
+ post[:ship_to_company] = address[:company].to_s
367
+ post[:ship_to_phone] = address[:phone].to_s
368
+ post[:ship_to_zip] = address[:zip].to_s
369
+ post[:ship_to_city] = address[:city].to_s
370
+ post[:ship_to_country] = address[:country].to_s
371
+ post[:ship_to_state] = address[:state].blank? ? 'n/a' : address[:state]
372
+ end
373
+ end
374
+
375
+ # Make a ruby type out of the response string
376
+ def normalize(field)
377
+ case field
378
+ when "true" then true
379
+ when "false" then false
380
+ when "" then nil
381
+ when "null" then nil
382
+ else field
383
+ end
384
+ end
385
+
386
+ def message_from(results)
387
+ if results[:response_code] == DECLINED
388
+ return CVVResult.messages[ results[:card_code] ] if CARD_CODE_ERRORS.include?(results[:card_code])
389
+ if AVS_REASON_CODES.include?(results[:response_reason_code]) && AVS_ERRORS.include?(results[:avs_result_code])
390
+ return AVSResult.messages[ results[:avs_result_code] ]
391
+ end
392
+ end
393
+
394
+ (results[:response_reason_text] ? results[:response_reason_text].chomp('.') : '')
395
+ end
396
+
397
+ def expdate(creditcard)
398
+ year = sprintf("%.4i", creditcard.year)
399
+ month = sprintf("%.2i", creditcard.month)
400
+
401
+ "#{month}#{year[-2..-1]}"
402
+ end
403
+
404
+ def split(response)
405
+ response[1..-2].split(/\$,\$/)
406
+ end
407
+
408
+ # ARB
409
+
410
+ # Builds recurring billing request
411
+ def build_recurring_request(action, options = {})
412
+ unless RECURRING_ACTIONS.include?(action)
413
+ raise StandardError, "Invalid Automated Recurring Billing Action: #{action}"
414
+ end
415
+
416
+ xml = Builder::XmlMarkup.new(:indent => 2)
417
+ xml.instruct!(:xml, :version => '1.0', :encoding => 'utf-8')
418
+ xml.tag!("#{RECURRING_ACTIONS[action]}Request", :xmlns => AUTHORIZE_NET_ARB_NAMESPACE) do
419
+ add_arb_merchant_authentication(xml)
420
+ # Merchant-assigned reference ID for the request
421
+ xml.tag!('refId', options[:ref_id]) if options[:ref_id]
422
+ send("build_arb_#{action}_subscription_request", xml, options)
423
+ end
424
+ end
425
+
426
+ # Contains the merchant’s payment gateway account authentication information
427
+ def add_arb_merchant_authentication(xml)
428
+ xml.tag!('merchantAuthentication') do
429
+ xml.tag!('name', @options[:login])
430
+ xml.tag!('transactionKey', @options[:password])
431
+ end
432
+ end
433
+
434
+ # Builds body for ARBCreateSubscriptionRequest
435
+ def build_arb_create_subscription_request(xml, options)
436
+ # Subscription
437
+ add_arb_subscription(xml, options)
438
+
439
+ xml.target!
440
+ end
441
+
442
+ # Builds body for ARBUpdateSubscriptionRequest
443
+ def build_arb_update_subscription_request(xml, options)
444
+ xml.tag!('subscriptionId', options[:subscription_id])
445
+ # Adds Subscription
446
+ add_arb_subscription(xml, options)
447
+
448
+ xml.target!
449
+ end
450
+
451
+ # Builds body for ARBCancelSubscriptionRequest
452
+ def build_arb_cancel_subscription_request(xml, options)
453
+ xml.tag!('subscriptionId', options[:subscription_id])
454
+
455
+ xml.target!
456
+ end
457
+
458
+ # Adds subscription information
459
+ def add_arb_subscription(xml, options)
460
+ xml.tag!('subscription') do
461
+ # Merchant-assigned name for the subscription (optional)
462
+ xml.tag!('name', options[:subscription_name]) if options[:subscription_name]
463
+ # Contains information about the payment schedule
464
+ add_arb_payment_schedule(xml, options)
465
+ # The amount to be billed to the customer
466
+ # for each payment in the subscription
467
+ xml.tag!('amount', amount(options[:amount])) if options[:amount]
468
+ if trial = options[:trial]
469
+ # The amount to be charged for each payment during a trial period (conditional)
470
+ xml.tag!('trialAmount', amount(trial[:amount])) if trial[:amount]
471
+ end
472
+ # Contains either the customer’s credit card
473
+ # or bank account payment information
474
+ add_arb_payment(xml, options)
475
+ # Contains order information (optional)
476
+ add_arb_order(xml, options)
477
+ # Contains information about the customer
478
+ add_arb_customer(xml, options)
479
+ # Contains the customer's billing address information
480
+ add_arb_address(xml, 'billTo', options[:billing_address])
481
+ # Contains the customer's shipping address information (optional)
482
+ add_arb_address(xml, 'shipTo', options[:shipping_address])
483
+ end
484
+ end
485
+
486
+ # Adds information about the interval of time between payments
487
+ def add_arb_interval(xml, options)
488
+ interval = options[:interval]
489
+ return unless interval
490
+ xml.tag!('interval') do
491
+ # The measurement of time, in association with the Interval Unit,
492
+ # that is used to define the frequency of the billing occurrences
493
+ xml.tag!('length', interval[:length])
494
+ # The unit of time, in association with the Interval Length,
495
+ # between each billing occurrence
496
+ xml.tag!('unit', interval[:unit].to_s)
497
+ end
498
+ end
499
+
500
+ # Adds information about the subscription duration
501
+ def add_arb_duration(xml, options)
502
+ duration = options[:duration]
503
+ return unless duration
504
+ # The date the subscription begins
505
+ # (also the date the initial billing occurs)
506
+ xml.tag!('startDate', duration[:start_date]) if duration[:start_date]
507
+ # Number of billing occurrences or payments for the subscription
508
+ xml.tag!('totalOccurrences', duration[:occurrences]) if duration[:occurrences]
509
+ end
510
+
511
+ def add_arb_payment_schedule(xml, options)
512
+ return unless options[:interval] || options[:duration]
513
+ xml.tag!('paymentSchedule') do
514
+ # Contains information about the interval of time between payments
515
+ add_arb_interval(xml, options)
516
+ add_arb_duration(xml, options)
517
+ if trial = options[:trial]
518
+ # Number of billing occurrences or payments in the trial period (optional)
519
+ xml.tag!('trialOccurrences', trial[:occurrences]) if trial[:occurrences]
520
+ end
521
+ end
522
+ end
523
+
524
+ # Adds customer's credit card or bank account payment information
525
+ def add_arb_payment(xml, options)
526
+ return unless options[:credit_card] || options[:bank_account]
527
+ xml.tag!('payment') do
528
+ # Contains the customer’s credit card information
529
+ add_arb_credit_card(xml, options)
530
+ # Contains the customer’s bank account information
531
+ add_arb_bank_account(xml, options)
532
+ end
533
+ end
534
+
535
+ # Adds customer’s credit card information
536
+ # Note: This element should only be included
537
+ # when the payment method is credit card.
538
+ def add_arb_credit_card(xml, options)
539
+ credit_card = options[:credit_card]
540
+ return unless credit_card
541
+ xml.tag!('creditCard') do
542
+ # The credit card number used for payment of the subscription
543
+ xml.tag!('cardNumber', credit_card.number)
544
+ # The expiration date of the credit card used for the subscription
545
+ xml.tag!('expirationDate', arb_expdate(credit_card))
546
+ end
547
+ end
548
+
549
+ # Adds customer’s bank account information
550
+ # Note: This element should only be included
551
+ # when the payment method is bank account.
552
+ def add_arb_bank_account(xml, options)
553
+ bank_account = options[:bank_account]
554
+ return unless bank_account
555
+ xml.tag!('bankAccount') do
556
+ # The type of bank account used for payment of the subscription
557
+ xml.tag!('accountType', bank_account[:account_type])
558
+ # The routing number of the customer’s bank
559
+ xml.tag!('routingNumber', bank_account[:routing_number])
560
+ # The bank account number used for payment of the subscription
561
+ xml.tag!('accountNumber', bank_account[:account_number])
562
+ # The full name of the individual associated
563
+ # with the bank account number
564
+ xml.tag!('nameOfAccount', bank_account[:name_of_account])
565
+ # The full name of the individual associated
566
+ # with the bank account number (optional)
567
+ xml.tag!('bankName', bank_account[:bank_name]) if bank_account[:bank_name]
568
+ # The type of electronic check transaction used for the subscription
569
+ xml.tag!('echeckType', bank_account[:echeck_type])
570
+ end
571
+ end
572
+
573
+ # Adds order information (optional)
574
+ def add_arb_order(xml, options)
575
+ order = options[:order]
576
+ return unless order
577
+ xml.tag!('order') do
578
+ # Merchant-assigned invoice number for the subscription (optional)
579
+ xml.tag!('invoiceNumber', order[:invoice_number])
580
+ # Description of the subscription (optional)
581
+ xml.tag!('description', order[:description])
582
+ end
583
+ end
584
+
585
+ # Adds information about the customer
586
+ def add_arb_customer(xml, options)
587
+ customer = options[:customer]
588
+ return unless customer
589
+ xml.tag!('customer') do
590
+ xml.tag!('type', customer[:type]) if customer[:type]
591
+ xml.tag!('id', customer[:id]) if customer[:id]
592
+ xml.tag!('email', customer[:email]) if customer[:email]
593
+ xml.tag!('phoneNumber', customer[:phone_number]) if customer[:phone_number]
594
+ xml.tag!('faxNumber', customer[:fax_number]) if customer[:fax_number]
595
+ add_arb_drivers_license(xml, options)
596
+ xml.tag!('taxId', customer[:tax_id]) if customer[:tax_id]
597
+ end
598
+ end
599
+
600
+ # Adds the customer's driver's license information (conditional)
601
+ def add_arb_drivers_license(xml, options)
602
+ return unless customer = options[:customer]
603
+ return unless drivers_license = customer[:drivers_license]
604
+ xml.tag!('driversLicense') do
605
+ # The customer's driver's license number
606
+ xml.tag!('number', drivers_license[:number])
607
+ # The customer's driver's license state
608
+ xml.tag!('state', drivers_license[:state])
609
+ # The customer's driver's license date of birth
610
+ xml.tag!('dateOfBirth', drivers_license[:date_of_birth])
611
+ end
612
+ end
613
+
614
+ # Adds address information
615
+ def add_arb_address(xml, container_name, address)
616
+ return if address.blank?
617
+ xml.tag!(container_name) do
618
+ xml.tag!('firstName', address[:first_name])
619
+ xml.tag!('lastName', address[:last_name])
620
+ xml.tag!('company', address[:company])
621
+ xml.tag!('address', address[:address1])
622
+ xml.tag!('city', address[:city])
623
+ xml.tag!('state', address[:state])
624
+ xml.tag!('zip', address[:zip])
625
+ xml.tag!('country', address[:country])
626
+ end
627
+ end
628
+
629
+ def arb_expdate(credit_card)
630
+ sprintf('%04d-%02d', credit_card.year, credit_card.month)
631
+ end
632
+
633
+ def recurring_commit(action, request)
634
+ url = test? ? arb_test_url : arb_live_url
635
+ xml = ssl_post(url, request, "Content-Type" => "text/xml")
636
+
637
+ response = recurring_parse(action, xml)
638
+
639
+ message = response[:message] || response[:text]
640
+ test_mode = test? || message =~ /Test Mode/
641
+ success = response[:result_code] == 'Ok'
642
+
643
+ Response.new(success, message, response,
644
+ :test => test_mode,
645
+ :authorization => response[:subscription_id]
646
+ )
647
+ end
648
+
649
+ def recurring_parse(action, xml)
650
+ response = {}
651
+ xml = REXML::Document.new(xml)
652
+ root = REXML::XPath.first(xml, "//#{RECURRING_ACTIONS[action]}Response") ||
653
+ REXML::XPath.first(xml, "//ErrorResponse")
654
+ if root
655
+ root.elements.to_a.each do |node|
656
+ recurring_parse_element(response, node)
657
+ end
658
+ end
659
+
660
+ response
661
+ end
662
+
663
+ def recurring_parse_element(response, node)
664
+ if node.has_elements?
665
+ node.elements.each{|e| recurring_parse_element(response, e) }
666
+ else
667
+ response[node.name.underscore.to_sym] = node.text
668
+ end
669
+ end
670
+ end
671
+ end
672
+ end