aktivemerchant 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +1596 -0
  3. data/CONTRIBUTORS +511 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +18 -0
  6. data/lib/active_merchant.rb +108 -0
  7. data/lib/active_merchant/billing.rb +13 -0
  8. data/lib/active_merchant/billing/apple_pay_payment_token.rb +22 -0
  9. data/lib/active_merchant/billing/avs_result.rb +98 -0
  10. data/lib/active_merchant/billing/base.rb +72 -0
  11. data/lib/active_merchant/billing/check.rb +76 -0
  12. data/lib/active_merchant/billing/compatibility.rb +120 -0
  13. data/lib/active_merchant/billing/credit_card.rb +352 -0
  14. data/lib/active_merchant/billing/credit_card_formatting.rb +24 -0
  15. data/lib/active_merchant/billing/credit_card_methods.rb +160 -0
  16. data/lib/active_merchant/billing/cvv_result.rb +38 -0
  17. data/lib/active_merchant/billing/gateway.rb +268 -0
  18. data/lib/active_merchant/billing/gateways.rb +17 -0
  19. data/lib/active_merchant/billing/gateways/adyen.rb +209 -0
  20. data/lib/active_merchant/billing/gateways/alfabank.rb +117 -0
  21. data/lib/active_merchant/billing/gateways/app55.rb +176 -0
  22. data/lib/active_merchant/billing/gateways/authorize_net.rb +419 -0
  23. data/lib/active_merchant/billing/gateways/authorize_net_arb.rb +417 -0
  24. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +976 -0
  25. data/lib/active_merchant/billing/gateways/balanced.rb +256 -0
  26. data/lib/active_merchant/billing/gateways/bank_frick.rb +225 -0
  27. data/lib/active_merchant/billing/gateways/banwire.rb +105 -0
  28. data/lib/active_merchant/billing/gateways/barclays_epdq.rb +314 -0
  29. data/lib/active_merchant/billing/gateways/barclays_epdq_extra_plus.rb +15 -0
  30. data/lib/active_merchant/billing/gateways/be2bill.rb +131 -0
  31. data/lib/active_merchant/billing/gateways/beanstream.rb +188 -0
  32. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +393 -0
  33. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +54 -0
  34. data/lib/active_merchant/billing/gateways/blue_pay.rb +506 -0
  35. data/lib/active_merchant/billing/gateways/bogus.rb +140 -0
  36. data/lib/active_merchant/billing/gateways/borgun.rb +210 -0
  37. data/lib/active_merchant/billing/gateways/braintree.rb +19 -0
  38. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +9 -0
  39. data/lib/active_merchant/billing/gateways/braintree_blue.rb +515 -0
  40. data/lib/active_merchant/billing/gateways/braintree_orange.rb +20 -0
  41. data/lib/active_merchant/billing/gateways/bridge_pay.rb +189 -0
  42. data/lib/active_merchant/billing/gateways/card_save.rb +23 -0
  43. data/lib/active_merchant/billing/gateways/card_stream.rb +220 -0
  44. data/lib/active_merchant/billing/gateways/cashnet.rb +191 -0
  45. data/lib/active_merchant/billing/gateways/cc5.rb +201 -0
  46. data/lib/active_merchant/billing/gateways/cecabank.rb +229 -0
  47. data/lib/active_merchant/billing/gateways/certo_direct.rb +278 -0
  48. data/lib/active_merchant/billing/gateways/checkout.rb +213 -0
  49. data/lib/active_merchant/billing/gateways/commercegate.rb +143 -0
  50. data/lib/active_merchant/billing/gateways/conekta.rb +209 -0
  51. data/lib/active_merchant/billing/gateways/cyber_source.rb +709 -0
  52. data/lib/active_merchant/billing/gateways/data_cash.rb +600 -0
  53. data/lib/active_merchant/billing/gateways/efsnet.rb +219 -0
  54. data/lib/active_merchant/billing/gateways/elavon.rb +348 -0
  55. data/lib/active_merchant/billing/gateways/epay.rb +275 -0
  56. data/lib/active_merchant/billing/gateways/evo_ca.rb +308 -0
  57. data/lib/active_merchant/billing/gateways/eway.rb +214 -0
  58. data/lib/active_merchant/billing/gateways/eway_managed.rb +291 -0
  59. data/lib/active_merchant/billing/gateways/eway_rapid.rb +524 -0
  60. data/lib/active_merchant/billing/gateways/exact.rb +218 -0
  61. data/lib/active_merchant/billing/gateways/fat_zebra.rb +173 -0
  62. data/lib/active_merchant/billing/gateways/federated_canada.rb +160 -0
  63. data/lib/active_merchant/billing/gateways/finansbank.rb +23 -0
  64. data/lib/active_merchant/billing/gateways/first_giving.rb +143 -0
  65. data/lib/active_merchant/billing/gateways/first_pay.rb +160 -0
  66. data/lib/active_merchant/billing/gateways/firstdata_e4.rb +355 -0
  67. data/lib/active_merchant/billing/gateways/garanti.rb +257 -0
  68. data/lib/active_merchant/billing/gateways/global_transport.rb +183 -0
  69. data/lib/active_merchant/billing/gateways/hdfc.rb +207 -0
  70. data/lib/active_merchant/billing/gateways/hps.rb +288 -0
  71. data/lib/active_merchant/billing/gateways/iats_payments.rb +251 -0
  72. data/lib/active_merchant/billing/gateways/ideal/ideal_base.rb +246 -0
  73. data/lib/active_merchant/billing/gateways/ideal/ideal_rabobank.pem +13 -0
  74. data/lib/active_merchant/billing/gateways/ideal/ideal_response.rb +29 -0
  75. data/lib/active_merchant/billing/gateways/ideal_rabobank.rb +66 -0
  76. data/lib/active_merchant/billing/gateways/inspire.rb +213 -0
  77. data/lib/active_merchant/billing/gateways/instapay.rb +163 -0
  78. data/lib/active_merchant/billing/gateways/iridium.rb +457 -0
  79. data/lib/active_merchant/billing/gateways/itransact.rb +448 -0
  80. data/lib/active_merchant/billing/gateways/jetpay.rb +275 -0
  81. data/lib/active_merchant/billing/gateways/linkpoint.rb +438 -0
  82. data/lib/active_merchant/billing/gateways/litle.rb +346 -0
  83. data/lib/active_merchant/billing/gateways/maxipago.rb +197 -0
  84. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +170 -0
  85. data/lib/active_merchant/billing/gateways/merchant_one.rb +114 -0
  86. data/lib/active_merchant/billing/gateways/merchant_ware.rb +319 -0
  87. data/lib/active_merchant/billing/gateways/merchant_ware_version_four.rb +268 -0
  88. data/lib/active_merchant/billing/gateways/merchant_warrior.rb +195 -0
  89. data/lib/active_merchant/billing/gateways/mercury.rb +333 -0
  90. data/lib/active_merchant/billing/gateways/metrics_global.rb +303 -0
  91. data/lib/active_merchant/billing/gateways/migs.rb +265 -0
  92. data/lib/active_merchant/billing/gateways/migs/migs_codes.rb +100 -0
  93. data/lib/active_merchant/billing/gateways/modern_payments.rb +37 -0
  94. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +219 -0
  95. data/lib/active_merchant/billing/gateways/moneris.rb +309 -0
  96. data/lib/active_merchant/billing/gateways/moneris_us.rb +291 -0
  97. data/lib/active_merchant/billing/gateways/money_movers.rb +152 -0
  98. data/lib/active_merchant/billing/gateways/nab_transact.rb +280 -0
  99. data/lib/active_merchant/billing/gateways/net_registry.rb +198 -0
  100. data/lib/active_merchant/billing/gateways/netaxept.rb +181 -0
  101. data/lib/active_merchant/billing/gateways/netbilling.rb +190 -0
  102. data/lib/active_merchant/billing/gateways/netpay.rb +223 -0
  103. data/lib/active_merchant/billing/gateways/network_merchants.rb +242 -0
  104. data/lib/active_merchant/billing/gateways/nmi.rb +256 -0
  105. data/lib/active_merchant/billing/gateways/ogone.rb +435 -0
  106. data/lib/active_merchant/billing/gateways/openpay.rb +194 -0
  107. data/lib/active_merchant/billing/gateways/optimal_payment.rb +313 -0
  108. data/lib/active_merchant/billing/gateways/orbital.rb +803 -0
  109. data/lib/active_merchant/billing/gateways/orbital/orbital_soft_descriptors.rb +47 -0
  110. data/lib/active_merchant/billing/gateways/pac_net_raven.rb +207 -0
  111. data/lib/active_merchant/billing/gateways/pago_facil.rb +122 -0
  112. data/lib/active_merchant/billing/gateways/pay_gate_xml.rb +261 -0
  113. data/lib/active_merchant/billing/gateways/pay_junction.rb +390 -0
  114. data/lib/active_merchant/billing/gateways/pay_secure.rb +112 -0
  115. data/lib/active_merchant/billing/gateways/pay_u_latam.rb +462 -0
  116. data/lib/active_merchant/billing/gateways/paybox_direct.rb +188 -0
  117. data/lib/active_merchant/billing/gateways/payex.rb +412 -0
  118. data/lib/active_merchant/billing/gateways/payflow.rb +304 -0
  119. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +209 -0
  120. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +39 -0
  121. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  122. data/lib/active_merchant/billing/gateways/payflow_express.rb +224 -0
  123. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +15 -0
  124. data/lib/active_merchant/billing/gateways/payflow_uk.rb +21 -0
  125. data/lib/active_merchant/billing/gateways/payment_express.rb +353 -0
  126. data/lib/active_merchant/billing/gateways/paymill.rb +281 -0
  127. data/lib/active_merchant/billing/gateways/paypal.rb +117 -0
  128. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +670 -0
  129. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +65 -0
  130. data/lib/active_merchant/billing/gateways/paypal/paypal_recurring_api.rb +262 -0
  131. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  132. data/lib/active_merchant/billing/gateways/paypal_digital_goods.rb +44 -0
  133. data/lib/active_merchant/billing/gateways/paypal_express.rb +264 -0
  134. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +30 -0
  135. data/lib/active_merchant/billing/gateways/payscout.rb +162 -0
  136. data/lib/active_merchant/billing/gateways/paystation.rb +199 -0
  137. data/lib/active_merchant/billing/gateways/payway.rb +207 -0
  138. data/lib/active_merchant/billing/gateways/pin.rb +197 -0
  139. data/lib/active_merchant/billing/gateways/plugnpay.rb +283 -0
  140. data/lib/active_merchant/billing/gateways/psigate.rb +216 -0
  141. data/lib/active_merchant/billing/gateways/psl_card.rb +303 -0
  142. data/lib/active_merchant/billing/gateways/qbms.rb +292 -0
  143. data/lib/active_merchant/billing/gateways/quantum.rb +276 -0
  144. data/lib/active_merchant/billing/gateways/quickpay.rb +367 -0
  145. data/lib/active_merchant/billing/gateways/realex.rb +298 -0
  146. data/lib/active_merchant/billing/gateways/redsys.rb +391 -0
  147. data/lib/active_merchant/billing/gateways/sage.rb +175 -0
  148. data/lib/active_merchant/billing/gateways/sage/sage_bankcard.rb +87 -0
  149. data/lib/active_merchant/billing/gateways/sage/sage_core.rb +114 -0
  150. data/lib/active_merchant/billing/gateways/sage/sage_vault.rb +149 -0
  151. data/lib/active_merchant/billing/gateways/sage/sage_virtual_check.rb +102 -0
  152. data/lib/active_merchant/billing/gateways/sage_pay.rb +398 -0
  153. data/lib/active_merchant/billing/gateways/sallie_mae.rb +143 -0
  154. data/lib/active_merchant/billing/gateways/secure_net.rb +252 -0
  155. data/lib/active_merchant/billing/gateways/secure_pay.rb +201 -0
  156. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +281 -0
  157. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +105 -0
  158. data/lib/active_merchant/billing/gateways/skip_jack.rb +452 -0
  159. data/lib/active_merchant/billing/gateways/smart_ps.rb +283 -0
  160. data/lib/active_merchant/billing/gateways/so_easy_pay.rb +194 -0
  161. data/lib/active_merchant/billing/gateways/spreedly_core.rb +247 -0
  162. data/lib/active_merchant/billing/gateways/stripe.rb +411 -0
  163. data/lib/active_merchant/billing/gateways/swipe_checkout.rb +157 -0
  164. data/lib/active_merchant/billing/gateways/tns.rb +227 -0
  165. data/lib/active_merchant/billing/gateways/trans_first.rb +126 -0
  166. data/lib/active_merchant/billing/gateways/transax.rb +23 -0
  167. data/lib/active_merchant/billing/gateways/transnational.rb +10 -0
  168. data/lib/active_merchant/billing/gateways/trust_commerce.rb +416 -0
  169. data/lib/active_merchant/billing/gateways/usa_epay.rb +25 -0
  170. data/lib/active_merchant/billing/gateways/usa_epay_advanced.rb +1516 -0
  171. data/lib/active_merchant/billing/gateways/usa_epay_transaction.rb +254 -0
  172. data/lib/active_merchant/billing/gateways/verifi.rb +225 -0
  173. data/lib/active_merchant/billing/gateways/viaklix.rb +183 -0
  174. data/lib/active_merchant/billing/gateways/vindicia.rb +385 -0
  175. data/lib/active_merchant/billing/gateways/webpay.rb +97 -0
  176. data/lib/active_merchant/billing/gateways/wepay.rb +189 -0
  177. data/lib/active_merchant/billing/gateways/wirecard.rb +421 -0
  178. data/lib/active_merchant/billing/gateways/worldpay.rb +331 -0
  179. data/lib/active_merchant/billing/gateways/worldpay_us.rb +181 -0
  180. data/lib/active_merchant/billing/model.rb +30 -0
  181. data/lib/active_merchant/billing/payment_token.rb +21 -0
  182. data/lib/active_merchant/billing/rails.rb +3 -0
  183. data/lib/active_merchant/billing/response.rb +91 -0
  184. data/lib/active_merchant/country.rb +332 -0
  185. data/lib/active_merchant/empty.rb +20 -0
  186. data/lib/active_merchant/errors.rb +29 -0
  187. data/lib/active_merchant/offsite_payments_shim.rb +19 -0
  188. data/lib/active_merchant/version.rb +3 -0
  189. data/lib/activemerchant.rb +1 -0
  190. data/lib/support/gateway_support.rb +71 -0
  191. data/lib/support/outbound_hosts.rb +25 -0
  192. data/lib/support/ssl_verify.rb +93 -0
  193. metadata +400 -0
@@ -0,0 +1,417 @@
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, cancel_recurring and status_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 AuthorizeNetArbGateway < Gateway
27
+ API_VERSION = '3.1'
28
+
29
+ self.test_url = 'https://apitest.authorize.net/xml/v1/request.api'
30
+ self.live_url = 'https://api.authorize.net/xml/v1/request.api'
31
+
32
+ self.default_currency = 'USD'
33
+
34
+ self.supported_countries = ['US', 'CA', 'GB']
35
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover, :diners_club, :jcb]
36
+ self.homepage_url = 'http://www.authorize.net/'
37
+ self.display_name = 'Authorize.Net'
38
+
39
+ AUTHORIZE_NET_ARB_NAMESPACE = 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'
40
+
41
+ RECURRING_ACTIONS = {
42
+ :create => 'ARBCreateSubscription',
43
+ :update => 'ARBUpdateSubscription',
44
+ :cancel => 'ARBCancelSubscription',
45
+ :status => 'ARBGetSubscriptionStatus'
46
+ }
47
+
48
+ # Creates a new AuthorizeNetArbGateway
49
+ #
50
+ # The gateway requires that a valid login and password be passed
51
+ # in the +options+ hash.
52
+ #
53
+ # ==== Options
54
+ #
55
+ # * <tt>:login</tt> -- The Authorize.Net API Login ID (REQUIRED)
56
+ # * <tt>:password</tt> -- The Authorize.Net Transaction Key. (REQUIRED)
57
+ # * <tt>:test</tt> -- +true+ or +false+. If true, perform transactions against the test server.
58
+ # Otherwise, perform transactions against the production server.
59
+ def initialize(options = {})
60
+ ActiveMerchant.deprecated "ARB functionality in ActiveMerchant is deprecated and will be removed in a future version. Please contact the ActiveMerchant maintainers if you have an interest in taking ownership of a separate gem that continues support for it."
61
+ requires!(options, :login, :password)
62
+ super
63
+ end
64
+
65
+ # Create a recurring payment.
66
+ #
67
+ # This transaction creates a new Automated Recurring Billing (ARB) subscription. Your account must have ARB enabled.
68
+ #
69
+ # ==== Parameters
70
+ #
71
+ # * <tt>money</tt> -- The amount to be charged to the customer at each interval as an Integer value in cents.
72
+ # * <tt>creditcard</tt> -- The CreditCard details for the transaction.
73
+ # * <tt>options</tt> -- A hash of parameters.
74
+ #
75
+ # ==== Options
76
+ #
77
+ # * <tt>:interval</tt> -- A hash containing information about the interval of time between payments. Must
78
+ # contain the keys <tt>:length</tt> and <tt>:unit</tt>. <tt>:unit</tt> can be either <tt>:months</tt> or <tt>:days</tt>.
79
+ # If <tt>:unit</tt> is <tt>:months</tt> then <tt>:length</tt> must be an integer between 1 and 12 inclusive.
80
+ # If <tt>:unit</tt> is <tt>:days</tt> then <tt>:length</tt> must be an integer between 7 and 365 inclusive.
81
+ # For example, to charge the customer once every three months the hash would be
82
+ # +:interval => { :unit => :months, :length => 3 }+ (REQUIRED)
83
+ # * <tt>:duration</tt> -- A hash containing keys for the <tt>:start_date</tt> the subscription begins (also the date the
84
+ # initial billing occurs) and the total number of billing <tt>:occurrences</tt> or payments for the subscription. (REQUIRED)
85
+ def recurring(money, creditcard, options={})
86
+ requires!(options, :interval, :duration, :billing_address)
87
+ requires!(options[:interval], :length, [:unit, :days, :months])
88
+ requires!(options[:duration], :start_date, :occurrences)
89
+ requires!(options[:billing_address], :first_name, :last_name)
90
+
91
+ options[:credit_card] = creditcard
92
+ options[:amount] = money
93
+
94
+ request = build_recurring_request(:create, options)
95
+ recurring_commit(:create, request)
96
+ end
97
+
98
+ # Update a recurring payment's details.
99
+ #
100
+ # This transaction updates an existing Automated Recurring Billing (ARB) subscription. Your account must have ARB enabled
101
+ # and the subscription must have already been created previously by calling +recurring()+. The ability to change certain
102
+ # details about a recurring payment is dependent on transaction history and cannot be determined until after calling
103
+ # +update_recurring()+. See the ARB XML Guide for such conditions.
104
+ #
105
+ # ==== Parameters
106
+ #
107
+ # * <tt>options</tt> -- A hash of parameters.
108
+ #
109
+ # ==== Options
110
+ #
111
+ # * <tt>:subscription_id</tt> -- A string containing the <tt>:subscription_id</tt> of the recurring payment already in place
112
+ # for a given credit card. (REQUIRED)
113
+ def update_recurring(options={})
114
+ requires!(options, :subscription_id)
115
+ request = build_recurring_request(:update, options)
116
+ recurring_commit(:update, request)
117
+ end
118
+
119
+ # Cancel a recurring payment.
120
+ #
121
+ # This transaction cancels an existing Automated Recurring Billing (ARB) subscription. Your account must have ARB enabled
122
+ # and the subscription must have already been created previously by calling recurring()
123
+ #
124
+ # ==== Parameters
125
+ #
126
+ # * <tt>subscription_id</tt> -- A string containing the +subscription_id+ of the recurring payment already in place
127
+ # for a given credit card. (REQUIRED)
128
+ def cancel_recurring(subscription_id)
129
+ request = build_recurring_request(:cancel, :subscription_id => subscription_id)
130
+ recurring_commit(:cancel, request)
131
+ end
132
+
133
+ # Get Subscription Status of a recurring payment.
134
+ #
135
+ # This transaction gets the status of an existing Automated Recurring Billing (ARB) subscription. Your account must have ARB enabled.
136
+ #
137
+ # ==== Parameters
138
+ #
139
+ # * <tt>subscription_id</tt> -- A string containing the +subscription_id+ of the recurring payment already in place
140
+ # for a given credit card. (REQUIRED)
141
+ def status_recurring(subscription_id)
142
+ request = build_recurring_request(:status, :subscription_id => subscription_id)
143
+ recurring_commit(:status, request)
144
+ end
145
+
146
+ private
147
+
148
+ # Builds recurring billing request
149
+ def build_recurring_request(action, options = {})
150
+ unless RECURRING_ACTIONS.include?(action)
151
+ raise StandardError, "Invalid Automated Recurring Billing Action: #{action}"
152
+ end
153
+
154
+ xml = Builder::XmlMarkup.new(:indent => 2)
155
+ xml.instruct!(:xml, :version => '1.0', :encoding => 'utf-8')
156
+ xml.tag!("#{RECURRING_ACTIONS[action]}Request", :xmlns => AUTHORIZE_NET_ARB_NAMESPACE) do
157
+ add_merchant_authentication(xml)
158
+ # Merchant-assigned reference ID for the request
159
+ xml.tag!('refId', options[:ref_id]) if options[:ref_id]
160
+ send("build_#{action}_subscription_request", xml, options)
161
+ end
162
+ end
163
+
164
+ # Contains the merchant’s payment gateway account authentication information
165
+ def add_merchant_authentication(xml)
166
+ xml.tag!('merchantAuthentication') do
167
+ xml.tag!('name', @options[:login])
168
+ xml.tag!('transactionKey', @options[:password])
169
+ end
170
+ end
171
+
172
+ # Builds body for ARBCreateSubscriptionRequest
173
+ def build_create_subscription_request(xml, options)
174
+ # Subscription
175
+ add_subscription(xml, options)
176
+
177
+ xml.target!
178
+ end
179
+
180
+ # Builds body for ARBUpdateSubscriptionRequest
181
+ def build_update_subscription_request(xml, options)
182
+ xml.tag!('subscriptionId', options[:subscription_id])
183
+ # Adds Subscription
184
+ add_subscription(xml, options)
185
+
186
+ xml.target!
187
+ end
188
+
189
+ # Builds body for ARBCancelSubscriptionRequest
190
+ def build_cancel_subscription_request(xml, options)
191
+ xml.tag!('subscriptionId', options[:subscription_id])
192
+
193
+ xml.target!
194
+ end
195
+
196
+ # Builds body for ARBGetSubscriptionStatusRequest
197
+ def build_status_subscription_request(xml, options)
198
+ xml.tag!('subscriptionId', options[:subscription_id])
199
+
200
+ xml.target!
201
+ end
202
+
203
+ # Adds subscription information
204
+ def add_subscription(xml, options)
205
+ xml.tag!('subscription') do
206
+ # Merchant-assigned name for the subscription (optional)
207
+ xml.tag!('name', options[:subscription_name]) if options[:subscription_name]
208
+ # Contains information about the payment schedule
209
+ add_payment_schedule(xml, options)
210
+ # The amount to be billed to the customer
211
+ # for each payment in the subscription
212
+ xml.tag!('amount', amount(options[:amount])) if options[:amount]
213
+ if trial = options[:trial]
214
+ # The amount to be charged for each payment during a trial period (conditional)
215
+ xml.tag!('trialAmount', amount(trial[:amount])) if trial[:amount]
216
+ end
217
+ # Contains either the customer’s credit card
218
+ # or bank account payment information
219
+ add_payment(xml, options)
220
+ # Contains order information (optional)
221
+ add_order(xml, options)
222
+ # Contains information about the customer
223
+ add_customer(xml, options)
224
+ # Contains the customer's billing address information
225
+ add_address(xml, 'billTo', options[:billing_address])
226
+ # Contains the customer's shipping address information (optional)
227
+ add_address(xml, 'shipTo', options[:shipping_address])
228
+ end
229
+ end
230
+
231
+ # Adds information about the interval of time between payments
232
+ def add_interval(xml, options)
233
+ interval = options[:interval]
234
+ return unless interval
235
+ xml.tag!('interval') do
236
+ # The measurement of time, in association with the Interval Unit,
237
+ # that is used to define the frequency of the billing occurrences
238
+ xml.tag!('length', interval[:length])
239
+ # The unit of time, in association with the Interval Length,
240
+ # between each billing occurrence
241
+ xml.tag!('unit', interval[:unit].to_s)
242
+ end
243
+ end
244
+
245
+ # Adds information about the subscription duration
246
+ def add_duration(xml, options)
247
+ duration = options[:duration]
248
+ return unless duration
249
+ # The date the subscription begins
250
+ # (also the date the initial billing occurs)
251
+ xml.tag!('startDate', duration[:start_date]) if duration[:start_date]
252
+ # Number of billing occurrences or payments for the subscription
253
+ xml.tag!('totalOccurrences', duration[:occurrences]) if duration[:occurrences]
254
+ end
255
+
256
+ def add_payment_schedule(xml, options)
257
+ return unless options[:interval] || options[:duration]
258
+ xml.tag!('paymentSchedule') do
259
+ # Contains information about the interval of time between payments
260
+ add_interval(xml, options)
261
+ add_duration(xml, options)
262
+ if trial = options[:trial]
263
+ # Number of billing occurrences or payments in the trial period (optional)
264
+ xml.tag!('trialOccurrences', trial[:occurrences]) if trial[:occurrences]
265
+ end
266
+ end
267
+ end
268
+
269
+ # Adds customer's credit card or bank account payment information
270
+ def add_payment(xml, options)
271
+ return unless options[:credit_card] || options[:bank_account]
272
+ xml.tag!('payment') do
273
+ # Contains the customer’s credit card information
274
+ add_credit_card(xml, options)
275
+ # Contains the customer’s bank account information
276
+ add_bank_account(xml, options)
277
+ end
278
+ end
279
+
280
+ # Adds customer’s credit card information
281
+ # Note: This element should only be included
282
+ # when the payment method is credit card.
283
+ def add_credit_card(xml, options)
284
+ credit_card = options[:credit_card]
285
+ return unless credit_card
286
+ xml.tag!('creditCard') do
287
+ # The credit card number used for payment of the subscription
288
+ xml.tag!('cardNumber', credit_card.number)
289
+ # The expiration date of the credit card used for the subscription
290
+ xml.tag!('expirationDate', expdate(credit_card))
291
+ end
292
+ end
293
+
294
+ # Adds customer’s bank account information
295
+ # Note: This element should only be included
296
+ # when the payment method is bank account.
297
+ def add_bank_account(xml, options)
298
+ bank_account = options[:bank_account]
299
+ return unless bank_account
300
+ xml.tag!('bankAccount') do
301
+ # The type of bank account used for payment of the subscription
302
+ xml.tag!('accountType', bank_account[:account_type])
303
+ # The routing number of the customer’s bank
304
+ xml.tag!('routingNumber', bank_account[:routing_number])
305
+ # The bank account number used for payment of the subscription
306
+ xml.tag!('accountNumber', bank_account[:account_number])
307
+ # The full name of the individual associated
308
+ # with the bank account number
309
+ xml.tag!('nameOfAccount', bank_account[:name_of_account])
310
+ # The full name of the individual associated
311
+ # with the bank account number (optional)
312
+ xml.tag!('bankName', bank_account[:bank_name]) if bank_account[:bank_name]
313
+ # The type of electronic check transaction used for the subscription
314
+ xml.tag!('echeckType', bank_account[:echeck_type])
315
+ end
316
+ end
317
+
318
+ # Adds order information (optional)
319
+ def add_order(xml, options)
320
+ order = options[:order]
321
+ return unless order
322
+ xml.tag!('order') do
323
+ # Merchant-assigned invoice number for the subscription (optional)
324
+ xml.tag!('invoiceNumber', order[:invoice_number])
325
+ # Description of the subscription (optional)
326
+ xml.tag!('description', order[:description])
327
+ end
328
+ end
329
+
330
+ # Adds information about the customer
331
+ def add_customer(xml, options)
332
+ customer = options[:customer]
333
+ return unless customer
334
+ xml.tag!('customer') do
335
+ xml.tag!('type', customer[:type]) if customer[:type]
336
+ xml.tag!('id', customer[:id]) if customer[:id]
337
+ xml.tag!('email', customer[:email]) if customer[:email]
338
+ xml.tag!('phoneNumber', customer[:phone_number]) if customer[:phone_number]
339
+ xml.tag!('faxNumber', customer[:fax_number]) if customer[:fax_number]
340
+ add_drivers_license(xml, options)
341
+ xml.tag!('taxId', customer[:tax_id]) if customer[:tax_id]
342
+ end
343
+ end
344
+
345
+ # Adds the customer's driver's license information (conditional)
346
+ def add_drivers_license(xml, options)
347
+ return unless customer = options[:customer]
348
+ return unless drivers_license = customer[:drivers_license]
349
+ xml.tag!('driversLicense') do
350
+ # The customer's driver's license number
351
+ xml.tag!('number', drivers_license[:number])
352
+ # The customer's driver's license state
353
+ xml.tag!('state', drivers_license[:state])
354
+ # The customer's driver's license date of birth
355
+ xml.tag!('dateOfBirth', drivers_license[:date_of_birth])
356
+ end
357
+ end
358
+
359
+ # Adds address information
360
+ def add_address(xml, container_name, address)
361
+ return if address.blank?
362
+ xml.tag!(container_name) do
363
+ xml.tag!('firstName', address[:first_name])
364
+ xml.tag!('lastName', address[:last_name])
365
+ xml.tag!('company', address[:company])
366
+ xml.tag!('address', address[:address1])
367
+ xml.tag!('city', address[:city])
368
+ xml.tag!('state', address[:state])
369
+ xml.tag!('zip', address[:zip])
370
+ xml.tag!('country', address[:country])
371
+ end
372
+ end
373
+
374
+ def expdate(credit_card)
375
+ sprintf('%04d-%02d', credit_card.year, credit_card.month)
376
+ end
377
+
378
+ def recurring_commit(action, request)
379
+ url = test? ? test_url : live_url
380
+ xml = ssl_post(url, request, "Content-Type" => "text/xml")
381
+
382
+ response = recurring_parse(action, xml)
383
+
384
+ message = response[:message] || response[:text]
385
+ test_mode = test? || message =~ /Test Mode/
386
+ success = response[:result_code] == 'Ok'
387
+
388
+ Response.new(success, message, response,
389
+ :test => test_mode,
390
+ :authorization => response[:subscription_id]
391
+ )
392
+ end
393
+
394
+ def recurring_parse(action, xml)
395
+ response = {}
396
+ xml = REXML::Document.new(xml)
397
+ root = REXML::XPath.first(xml, "//#{RECURRING_ACTIONS[action]}Response") ||
398
+ REXML::XPath.first(xml, "//ErrorResponse")
399
+ if root
400
+ root.elements.to_a.each do |node|
401
+ recurring_parse_element(response, node)
402
+ end
403
+ end
404
+
405
+ response
406
+ end
407
+
408
+ def recurring_parse_element(response, node)
409
+ if node.has_elements?
410
+ node.elements.each{|e| recurring_parse_element(response, e) }
411
+ else
412
+ response[node.name.underscore.to_sym] = node.text
413
+ end
414
+ end
415
+ end
416
+ end
417
+ end
@@ -0,0 +1,976 @@
1
+ # -*- coding: utf-8 -*-
2
+ module ActiveMerchant #:nodoc:
3
+ module Billing #:nodoc:
4
+ # ==== Customer Information Manager (CIM)
5
+ #
6
+ # The Authorize.Net Customer Information Manager (CIM) is an optional additional service that allows you to store sensitive payment information on
7
+ # Authorize.Net's servers, simplifying payments for returning customers and recurring transactions. It can also help with Payment Card Industry (PCI)
8
+ # Data Security Standard compliance, since customer data is no longer stored locally.
9
+ #
10
+ # To use the AuthorizeNetCimGateway CIM must be enabled for your account.
11
+ #
12
+ # Information about CIM is available on the {Authorize.Net website}[http://www.authorize.net/solutions/merchantsolutions/merchantservices/cim/].
13
+ # Information about the CIM API is available at the {Authorize.Net Integration Center}[http://developer.authorize.net/]
14
+ #
15
+ # ==== Login and Password
16
+ #
17
+ # The login and password are not the username and password you use to
18
+ # login to the Authorize.Net Merchant Interface. Instead, you will
19
+ # use the API Login ID as the login and Transaction Key as the
20
+ # password.
21
+ #
22
+ # ==== How to Get Your API Login ID and Transaction Key
23
+ #
24
+ # 1. Log into the Merchant Interface
25
+ # 2. Select Settings from the Main Menu
26
+ # 3. Click on API Login ID and Transaction Key in the Security section
27
+ # 4. Type in the answer to the secret question configured on setup
28
+ # 5. Click Submit
29
+ class AuthorizeNetCimGateway < Gateway
30
+ self.test_url = 'https://apitest.authorize.net/xml/v1/request.api'
31
+ self.live_url = 'https://api.authorize.net/xml/v1/request.api'
32
+
33
+ AUTHORIZE_NET_CIM_NAMESPACE = 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'
34
+
35
+ CIM_ACTIONS = {
36
+ :create_customer_profile => 'createCustomerProfile',
37
+ :create_customer_payment_profile => 'createCustomerPaymentProfile',
38
+ :create_customer_shipping_address => 'createCustomerShippingAddress',
39
+ :get_customer_profile => 'getCustomerProfile',
40
+ :get_customer_profile_ids => 'getCustomerProfileIds',
41
+ :get_customer_payment_profile => 'getCustomerPaymentProfile',
42
+ :get_customer_shipping_address => 'getCustomerShippingAddress',
43
+ :delete_customer_profile => 'deleteCustomerProfile',
44
+ :delete_customer_payment_profile => 'deleteCustomerPaymentProfile',
45
+ :delete_customer_shipping_address => 'deleteCustomerShippingAddress',
46
+ :update_customer_profile => 'updateCustomerProfile',
47
+ :update_customer_payment_profile => 'updateCustomerPaymentProfile',
48
+ :update_customer_shipping_address => 'updateCustomerShippingAddress',
49
+ :create_customer_profile_transaction => 'createCustomerProfileTransaction',
50
+ :validate_customer_payment_profile => 'validateCustomerPaymentProfile'
51
+ }
52
+
53
+ CIM_TRANSACTION_TYPES = {
54
+ :auth_capture => 'profileTransAuthCapture',
55
+ :auth_only => 'profileTransAuthOnly',
56
+ :capture_only => 'profileTransCaptureOnly',
57
+ :prior_auth_capture => 'profileTransPriorAuthCapture',
58
+ :refund => 'profileTransRefund',
59
+ :void => 'profileTransVoid'
60
+ }
61
+
62
+ CIM_VALIDATION_MODES = {
63
+ :none => 'none',
64
+ :test => 'testMode',
65
+ :live => 'liveMode',
66
+ :old => 'oldLiveMode'
67
+ }
68
+
69
+ BANK_ACCOUNT_TYPES = {
70
+ :checking => 'checking',
71
+ :savings => 'savings',
72
+ :business_checking => 'businessChecking'
73
+ }
74
+
75
+ ECHECK_TYPES = {
76
+ :ccd => 'CCD',
77
+ :ppd => 'PPD',
78
+ :web => 'WEB'
79
+ }
80
+
81
+ self.homepage_url = 'http://www.authorize.net/'
82
+ self.display_name = 'Authorize.Net CIM'
83
+ self.supported_countries = ['US']
84
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover]
85
+
86
+ # Creates a new AuthorizeNetCimGateway
87
+ #
88
+ # The gateway requires that a valid API Login ID and Transaction Key be passed
89
+ # in the +options+ hash.
90
+ #
91
+ # ==== Options
92
+ #
93
+ # * <tt>:login</tt> -- The Authorize.Net API Login ID (REQUIRED)
94
+ # * <tt>:password</tt> -- The Authorize.Net Transaction Key. (REQUIRED)
95
+ # * <tt>:test</tt> -- +true+ or +false+. If true, perform transactions against the test server.
96
+ # Otherwise, perform transactions against the production server.
97
+ # * <tt>:test_requests</tt> -- +true+ or +false+. If true, perform transactions without the
98
+ # test flag. This is useful when you need to generate card declines, AVS or CVV errors.
99
+ # Will hold the same value as :test by default.
100
+ # * <tt>:delimiter</tt> -- The delimiter used in the direct response. Default is ',' (comma).
101
+ def initialize(options = {})
102
+ requires!(options, :login, :password)
103
+ super
104
+ @options[:test_requests] = test? if @options[:test_requests].nil?
105
+ end
106
+
107
+ # Creates a new customer profile along with any customer payment profiles and customer shipping addresses
108
+ # for the customer profile.
109
+ #
110
+ # Returns a Response with the Customer Profile ID of the new customer profile in the authorization field.
111
+ # It is *CRITICAL* that you save this ID. There is no way to retrieve this through the API. You will not
112
+ # be able to create another Customer Profile with the same information.
113
+ #
114
+ #
115
+ #
116
+ # ==== Options
117
+ #
118
+ # * <tt>:profile</tt> -- A hash containing at least one of the CONDITIONAL profile options below (REQUIRED)
119
+ #
120
+ # ==== Profile
121
+ #
122
+ # * <tt>:email</tt> -- Email address associated with the customer profile (CONDITIONAL)
123
+ # * <tt>:description</tt> -- Description of the customer or customer profile (CONDITIONAL)
124
+ # * <tt>:merchant_customer_id</tt> -- Merchant assigned ID for the customer (CONDITIONAL)
125
+ # * <tt>:payment_profile</tt> -- A hash containing the elements of the new payment profile (optional)
126
+ #
127
+ # ==== Payment Profile
128
+ #
129
+ # * <tt>:payment</tt> -- A hash containing information on payment. Either :credit_card or :bank_account (optional)
130
+ def create_customer_profile(options)
131
+ requires!(options, :profile)
132
+ requires!(options[:profile], :email) unless options[:profile][:merchant_customer_id] || options[:profile][:description]
133
+ requires!(options[:profile], :description) unless options[:profile][:email] || options[:profile][:merchant_customer_id]
134
+ requires!(options[:profile], :merchant_customer_id) unless options[:profile][:description] || options[:profile][:email]
135
+
136
+ request = build_request(:create_customer_profile, options)
137
+ commit(:create_customer_profile, request)
138
+ end
139
+
140
+ # Creates a new customer payment profile for an existing customer profile.
141
+ #
142
+ # ==== Options
143
+ #
144
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer the payment profile will be added to. (REQUIRED)
145
+ # * <tt>:payment_profile</tt> -- A hash containing the elements of the new payment profile (REQUIRED)
146
+ #
147
+ # ==== Payment Profile
148
+ #
149
+ # * <tt>:payment</tt> -- A hash containing information on payment. Either :credit_card or :bank_account (REQUIRED)
150
+ def create_customer_payment_profile(options)
151
+ requires!(options, :customer_profile_id)
152
+ requires!(options, :payment_profile)
153
+ requires!(options[:payment_profile], :payment)
154
+
155
+ request = build_request(:create_customer_payment_profile, options)
156
+ commit(:create_customer_payment_profile, request)
157
+ end
158
+
159
+ # Creates a new customer shipping address for an existing customer profile.
160
+ #
161
+ # ==== Options
162
+ #
163
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer the payment profile will be added to. (REQUIRED)
164
+ # * <tt>:address</tt> -- A hash containing the elements of the shipping address (REQUIRED)
165
+ def create_customer_shipping_address(options)
166
+ requires!(options, :customer_profile_id)
167
+ requires!(options, :address)
168
+
169
+ request = build_request(:create_customer_shipping_address, options)
170
+ commit(:create_customer_shipping_address, request)
171
+ end
172
+
173
+ # Deletes an existing customer profile along with all associated customer payment profiles and customer shipping addresses.
174
+ #
175
+ # ==== Options
176
+ #
177
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer to be deleted. (REQUIRED)
178
+ def delete_customer_profile(options)
179
+ requires!(options, :customer_profile_id)
180
+
181
+ request = build_request(:delete_customer_profile, options)
182
+ commit(:delete_customer_profile, request)
183
+ end
184
+
185
+ # Deletes a customer payment profile from an existing customer profile.
186
+ #
187
+ # ==== Options
188
+ #
189
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be deleted. (REQUIRED)
190
+ # * <tt>:customer_payment_profile_id</tt> -- The Payment Profile ID of the payment profile to be deleted. (REQUIRED)
191
+ def delete_customer_payment_profile(options)
192
+ requires!(options, :customer_profile_id)
193
+ requires!(options, :customer_payment_profile_id)
194
+
195
+ request = build_request(:delete_customer_payment_profile, options)
196
+ commit(:delete_customer_payment_profile, request)
197
+ end
198
+
199
+ # Deletes a customer shipping address from an existing customer profile.
200
+ #
201
+ # ==== Options
202
+ #
203
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be deleted. (REQUIRED)
204
+ # * <tt>:customer_address_id</tt> -- The Shipping Address ID of the shipping address to be deleted. (REQUIRED)
205
+ def delete_customer_shipping_address(options)
206
+ requires!(options, :customer_profile_id)
207
+ requires!(options, :customer_address_id)
208
+
209
+ request = build_request(:delete_customer_shipping_address, options)
210
+ commit(:delete_customer_shipping_address, request)
211
+ end
212
+
213
+ # Retrieves an existing customer profile along with all the associated customer payment profiles and customer shipping addresses.
214
+ #
215
+ # Returns a Response whose params hash contains all the profile information.
216
+ #
217
+ # ==== Options
218
+ #
219
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer to retrieve. (REQUIRED)
220
+ def get_customer_profile(options)
221
+ requires!(options, :customer_profile_id)
222
+
223
+ request = build_request(:get_customer_profile, options)
224
+ commit(:get_customer_profile, request)
225
+ end
226
+
227
+ def get_customer_profile_ids(options = {})
228
+ request = build_request(:get_customer_profile_ids, options)
229
+ commit(:get_customer_profile_ids, request)
230
+ end
231
+
232
+ # Retrieve a customer payment profile for an existing customer profile.
233
+ #
234
+ # Returns a Response whose params hash contains all the payment profile information. Sensitive information such as credit card
235
+ # numbers will be masked.
236
+ #
237
+ # ==== Options
238
+ #
239
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be retrieved. (REQUIRED)
240
+ # * <tt>:customer_payment_profile_id</tt> -- The Payment Profile ID of the payment profile to be retrieved. (REQUIRED)
241
+ def get_customer_payment_profile(options)
242
+ requires!(options, :customer_profile_id)
243
+ requires!(options, :customer_payment_profile_id)
244
+
245
+ request = build_request(:get_customer_payment_profile, options)
246
+ commit(:get_customer_payment_profile, request)
247
+ end
248
+
249
+ # Retrieve a customer shipping address for an existing customer profile.
250
+ #
251
+ # Returns a Response whose params hash contains all the shipping address information.
252
+ #
253
+ # ==== Options
254
+ #
255
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be retrieved. (REQUIRED)
256
+ # * <tt>:customer_address_id</tt> -- The Shipping Address ID of the shipping address to be retrieved. (REQUIRED)
257
+ def get_customer_shipping_address(options)
258
+ requires!(options, :customer_profile_id)
259
+ requires!(options, :customer_address_id)
260
+
261
+ request = build_request(:get_customer_shipping_address, options)
262
+ commit(:get_customer_shipping_address, request)
263
+ end
264
+
265
+ # Updates an existing customer profile.
266
+ #
267
+ # Warning: if you do not provide a parameter in the <tt>:payment_profile</tt> hash, it is automatically set to nil at
268
+ # Authorize.Net. You will most likely want to first get the profile hash using get_customer_profile and then only change the
269
+ # elements you wish to change.
270
+ #
271
+ # ==== Options
272
+ #
273
+ # * <tt>:profile</tt> -- A hash containing the values the Customer Profile should be updated to. (REQUIRED)
274
+ #
275
+ # ==== Profile
276
+ #
277
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer profile to update. (REQUIRED)
278
+ def update_customer_profile(options)
279
+ requires!(options, :profile)
280
+ requires!(options[:profile], :customer_profile_id)
281
+
282
+ request = build_request(:update_customer_profile, options)
283
+ commit(:update_customer_profile, request)
284
+ end
285
+
286
+ # Updates a customer payment profile for an existing customer profile.
287
+ #
288
+ # Warning: if you do not provide a parameter in the <tt>:payment_profile</tt> hash, it is automatically set to nil at
289
+ # Authorize.Net. You will most likely want to first get the profile hash using get_customer_payment_profile and then only
290
+ # change the elements you wish to change.
291
+ #
292
+ # ==== Options
293
+ #
294
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be updated. (REQUIRED)
295
+ # * <tt>:payment_profile</tt> -- A hash containing the values the Customer Payment Profile should be updated to. (REQUIRED)
296
+ #
297
+ # ==== Payment Profile
298
+ #
299
+ # * <tt>:customer_payment_profile_id</tt> -- The Customer Payment Profile ID of the Customer Payment Profile to update. (REQUIRED)
300
+ def update_customer_payment_profile(options)
301
+ requires!(options, :customer_profile_id, :payment_profile)
302
+ requires!(options[:payment_profile], :customer_payment_profile_id)
303
+
304
+ request = build_request(:update_customer_payment_profile, options)
305
+ commit(:update_customer_payment_profile, request)
306
+ end
307
+
308
+ # Updates a customer shipping address for an existing customer profile.
309
+ #
310
+ # Warning: if you do not provide a parameter in the <tt>:address</tt> hash, it is automatically set to nil at
311
+ # Authorize.Net. You will most likely want to first get the profile hash using get_customer_shipping_address and then only
312
+ # change the elements you wish to change.
313
+ #
314
+ # ==== Options
315
+ #
316
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be updated. (REQUIRED)
317
+ # * <tt>:address</tt> -- A hash containing the values the Customer Shipping Address should be updated to. (REQUIRED)
318
+ #
319
+ # ==== Address
320
+ #
321
+ # * <tt>:customer_address_id</tt> -- The Customer Address ID of the Customer Payment Profile to update. (REQUIRED)
322
+ def update_customer_shipping_address(options)
323
+ requires!(options, :customer_profile_id, :address)
324
+ requires!(options[:address], :customer_address_id)
325
+
326
+ request = build_request(:update_customer_shipping_address, options)
327
+ commit(:update_customer_shipping_address, request)
328
+ end
329
+
330
+ # Creates a new payment transaction from an existing customer profile
331
+ #
332
+ # This is what is used to charge a customer whose information you have stored in a Customer Profile.
333
+ #
334
+ # Returns a Response object that contains the result of the transaction in <tt>params['direct_response']</tt>
335
+ #
336
+ # ==== Options
337
+ #
338
+ # * <tt>:transaction</tt> -- A hash containing information on the transaction that is being requested. (REQUIRED)
339
+ #
340
+ # ==== Transaction
341
+ #
342
+ # * <tt>:type</tt> -- The type of transaction. Can be either <tt>:auth_only</tt>, <tt>:capture_only</tt>, <tt>:auth_capture</tt>, <tt>:prior_auth_capture</tt>, <tt>:refund</tt> or <tt>:void</tt>. (REQUIRED)
343
+ # * <tt>:amount</tt> -- The amount for the transaction. Formatted with a decimal. For example "4.95" (CONDITIONAL)
344
+ # - :type == :void (NOT USED)
345
+ # - :type == :refund (OPTIONAL)
346
+ # - :type == (:auth_only, :capture_only, :auth_capture, :prior_auth_capture) (REQUIRED)
347
+ #
348
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer to use in this transaction. (CONDITIONAL)
349
+ # - :type == (:void, :prior_auth_capture) (OPTIONAL)
350
+ # - :type == :refund (CONDITIONAL - required if masked information is not being submitted [see below])
351
+ # - :type == (:auth_only, :capture_only, :auth_capture) (REQUIRED)
352
+ #
353
+ # * <tt>:customer_payment_profile_id</tt> -- The Customer Payment Profile ID of the Customer Payment Profile to use in this transaction. (CONDITIONAL)
354
+ # - :type == (:void, :prior_auth_capture) (OPTIONAL)
355
+ # - :type == :refund (CONDITIONAL - required if masked information is not being submitted [see below])
356
+ # - :type == (:auth_only, :capture_only, :auth_capture) (REQUIRED)
357
+ #
358
+ # * <tt>:trans_id</tt> -- The payment gateway assigned transaction ID of the original transaction (CONDITIONAL):
359
+ # - :type = (:void, :refund, :prior_auth_capture) (REQUIRED)
360
+ # - :type = (:auth_only, :capture_only, :auth_capture) (NOT USED)
361
+ #
362
+ # * <tt>:card_code</tt> -- CVV/CCV code (OPTIONAL)
363
+ # - :type = (:void, :refund, :prior_auth_capture) (NOT USED)
364
+ # - :type = (:auth_only, :capture_only, :auth_capture) (OPTIONAL)
365
+ #
366
+ # * <tt>:recurring_billing</tt> -- The recurring billing status (OPTIONAL)
367
+ # - :type = (:void, :refund, :prior_auth_capture) (NOT USED)
368
+ # - :type = (:auth_only, :capture_only, :auth_capture) (OPTIONAL)
369
+ #
370
+ # * <tt>:customer_shipping_address_id</tt> -- Payment gateway assigned ID associated with the customer shipping address (CONDITIONAL)
371
+ # - :type = (:void, :refund) (OPTIONAL)
372
+ # - :type = (:auth_only, :capture_only, :auth_capture) (NOT USED)
373
+ # - :type = (:prior_auth_capture) (OPTIONAL)
374
+ #
375
+ # ==== For :type == :refund only
376
+ # * <tt>:credit_card_number_masked</tt> -- (CONDITIONAL - required for credit card refunds if :customer_profile_id AND :customer_payment_profile_id are missing)
377
+ # * <tt>:bank_routing_number_masked && :bank_account_number_masked</tt> -- (CONDITIONAL - required for electronic check refunds if :customer_profile_id AND :customer_payment_profile_id are missing) (NOT ABLE TO TEST - I keep getting "ACH transactions are not accepted by this merchant." when trying to make a payment and, until that's possible I can't refund (wiseleyb@gmail.com))
378
+ def create_customer_profile_transaction(options)
379
+ requires!(options, :transaction)
380
+ requires!(options[:transaction], :type)
381
+ case options[:transaction][:type]
382
+ when :void
383
+ requires!(options[:transaction], :trans_id)
384
+ when :refund
385
+ requires!(options[:transaction], :trans_id) &&
386
+ (
387
+ (options[:transaction][:customer_profile_id] && options[:transaction][:customer_payment_profile_id]) ||
388
+ options[:transaction][:credit_card_number_masked] ||
389
+ (options[:transaction][:bank_routing_number_masked] && options[:transaction][:bank_account_number_masked])
390
+ )
391
+ when :prior_auth_capture
392
+ requires!(options[:transaction], :amount, :trans_id)
393
+ else
394
+ requires!(options[:transaction], :amount, :customer_profile_id, :customer_payment_profile_id)
395
+ end
396
+ request = build_request(:create_customer_profile_transaction, options)
397
+ commit(:create_customer_profile_transaction, request)
398
+ end
399
+
400
+ # Creates a new payment transaction for refund from an existing customer profile
401
+ #
402
+ # This is what is used to refund a transaction you have stored in a Customer Profile.
403
+ #
404
+ # Returns a Response object that contains the result of the transaction in <tt>params['direct_response']</tt>
405
+ #
406
+ # ==== Options
407
+ #
408
+ # * <tt>:transaction</tt> -- A hash containing information on the transaction that is being requested. (REQUIRED)
409
+ #
410
+ # ==== Transaction
411
+ #
412
+ # * <tt>:amount</tt> -- The total amount to be refunded (REQUIRED)
413
+ #
414
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer to use in this transaction. (CONDITIONAL :customer_payment_profile_id must be included if used)
415
+ # * <tt>:customer_payment_profile_id</tt> -- The Customer Payment Profile ID of the Customer Payment Profile to use in this transaction. (CONDITIONAL :customer_profile_id must be included if used)
416
+ #
417
+ # * <tt>:credit_card_number_masked</tt> -- Four Xs followed by the last four digits of the credit card (CONDITIONAL - used if customer_profile_id and customer_payment_profile_id aren't given)
418
+ #
419
+ # * <tt>:bank_routing_number_masked</tt> -- The last four digits of the routing number to be refunded (CONDITIONAL - must be used with :bank_account_number_masked)
420
+ # * <tt>:bank_account_number_masked</tt> -- The last four digits of the bank account number to be refunded, Ex. XXXX1234 (CONDITIONAL - must be used with :bank_routing_number_masked)
421
+ #
422
+ # * <tt>:tax</tt> - A hash containing tax information for the refund (OPTIONAL - <tt>:amount</tt>, <tt>:name</tt> (31 characters), <tt>:description</tt> (255 characters))
423
+ # * <tt>:duty</tt> - A hash containing duty information for the refund (OPTIONAL - <tt>:amount</tt>, <tt>:name</tt> (31 characters), <tt>:description</tt> (255 characters))
424
+ # * <tt>:shipping</tt> - A hash containing shipping information for the refund (OPTIONAL - <tt>:amount</tt>, <tt>:name</tt> (31 characters), <tt>:description</tt> (255 characters))
425
+ def create_customer_profile_transaction_for_refund(options)
426
+ requires!(options, :transaction)
427
+ options[:transaction][:type] = :refund
428
+ requires!(options[:transaction], :trans_id)
429
+ requires!(options[:transaction], :amount)
430
+ request = build_request(:create_customer_profile_transaction, options)
431
+ commit(:create_customer_profile_transaction, request)
432
+ end
433
+
434
+ # Creates a new payment transaction for void from an existing customer profile
435
+ #
436
+ # This is what is used to void a transaction you have stored in a Customer Profile.
437
+ #
438
+ # Returns a Response object that contains the result of the transaction in <tt>params['direct_response']</tt>
439
+ #
440
+ # ==== Options
441
+ #
442
+ # * <tt>:transaction</tt> -- A hash containing information on the transaction that is being requested. (REQUIRED)
443
+ #
444
+ # ==== Transaction
445
+ #
446
+ # * <tt>:trans_id</tt> -- The payment gateway assigned transaction id of the original transaction. (REQUIRED)
447
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer to use in this transaction.
448
+ # * <tt>:customer_payment_profile_id</tt> -- The Customer Payment Profile ID of the Customer Payment Profile to use in this transaction.
449
+ # * <tt>:customer_shipping_address_id</tt> -- Payment gateway assigned ID associated with the customer shipping address.
450
+ def create_customer_profile_transaction_for_void(options)
451
+ requires!(options, :transaction)
452
+ options[:transaction][:type] = :void
453
+ requires!(options[:transaction], :trans_id)
454
+ request = build_request(:create_customer_profile_transaction, options)
455
+ commit(:create_customer_profile_transaction, request)
456
+ end
457
+
458
+ # Verifies an existing customer payment profile by generating a test transaction
459
+ #
460
+ # Returns a Response object that contains the result of the transaction in <tt>params['direct_response']</tt>
461
+ #
462
+ # ==== Options
463
+ #
464
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer to use in this transaction. (REQUIRED)
465
+ # * <tt>:customer_payment_profile_id</tt> -- The Customer Payment Profile ID of the Customer Payment Profile to be verified. (REQUIRED)
466
+ # * <tt>:customer_address_id</tt> -- The Customer Address ID of the Customer Shipping Address to be verified. (OPTIONAL)
467
+ # * <tt>:card_code</tt> -- If the payment profile is a credit card, the CCV/CVV code to validate with (OPTIONAL)
468
+ # * <tt>:validation_mode</tt> -- <tt>:live</tt> or <tt>:test</tt> In Test Mode, only field validation is performed. (REQUIRED
469
+ # In Live Mode, a transaction is generated and submitted to the processor with the amount of $0.01. If successful, the transaction is immediately voided. (REQUIRED)
470
+ def validate_customer_payment_profile(options)
471
+ requires!(options, :customer_profile_id, :customer_payment_profile_id, :validation_mode)
472
+
473
+ request = build_request(:validate_customer_payment_profile, options)
474
+ commit(:validate_customer_payment_profile, request)
475
+ end
476
+
477
+ private
478
+
479
+ def expdate(credit_card)
480
+ if credit_card.year.present? && credit_card.month.present?
481
+ sprintf('%04d-%02d', credit_card.year, credit_card.month)
482
+ else
483
+ 'XXXX'
484
+ end
485
+ end
486
+
487
+ def build_request(action, options = {})
488
+ unless CIM_ACTIONS.include?(action)
489
+ raise StandardError, "Invalid Customer Information Manager Action: #{action}"
490
+ end
491
+
492
+ xml = Builder::XmlMarkup.new(:indent => 2)
493
+ xml.instruct!(:xml, :version => '1.0', :encoding => 'utf-8')
494
+ xml.tag!("#{CIM_ACTIONS[action]}Request", :xmlns => AUTHORIZE_NET_CIM_NAMESPACE) do
495
+ add_merchant_authentication(xml)
496
+ # Merchant-assigned reference ID for the request
497
+ xml.tag!('refId', options[:ref_id]) if options[:ref_id]
498
+ # Order options
499
+ add_order(xml, options[:order]) if options[:order]
500
+ send("build_#{action}_request", xml, options)
501
+ end
502
+ end
503
+
504
+ # Contains the merchant’s payment gateway account authentication information
505
+ def add_merchant_authentication(xml)
506
+ xml.tag!('merchantAuthentication') do
507
+ xml.tag!('name', @options[:login])
508
+ xml.tag!('transactionKey', @options[:password])
509
+ end
510
+ end
511
+
512
+ def build_create_customer_profile_request(xml, options)
513
+ add_profile(xml, options[:profile])
514
+
515
+ xml.tag!('validationMode', CIM_VALIDATION_MODES[options[:validation_mode]]) if options[:validation_mode]
516
+
517
+ if options.has_key?(:payment_profile)
518
+ xml.tag!('paymentProfile') do
519
+ add_payment_profile(xml, options[:payment_profile])
520
+ end
521
+ end
522
+
523
+ xml.target!
524
+ end
525
+
526
+ def build_create_customer_payment_profile_request(xml, options)
527
+ xml.tag!('customerProfileId', options[:customer_profile_id])
528
+
529
+ xml.tag!('paymentProfile') do
530
+ add_payment_profile(xml, options[:payment_profile])
531
+ end
532
+
533
+ xml.tag!('validationMode', CIM_VALIDATION_MODES[options[:validation_mode]]) if options[:validation_mode]
534
+
535
+ xml.target!
536
+ end
537
+
538
+ def build_create_customer_shipping_address_request(xml, options)
539
+ xml.tag!('customerProfileId', options[:customer_profile_id])
540
+
541
+ xml.tag!('address') do
542
+ add_address(xml, options[:address])
543
+ end
544
+
545
+ xml.target!
546
+ end
547
+
548
+ def build_delete_customer_profile_request(xml, options)
549
+ xml.tag!('customerProfileId', options[:customer_profile_id])
550
+ xml.target!
551
+ end
552
+
553
+ def build_delete_customer_payment_profile_request(xml, options)
554
+ xml.tag!('customerProfileId', options[:customer_profile_id])
555
+ xml.tag!('customerPaymentProfileId', options[:customer_payment_profile_id])
556
+ xml.target!
557
+ end
558
+
559
+ def build_delete_customer_shipping_address_request(xml, options)
560
+ xml.tag!('customerProfileId', options[:customer_profile_id])
561
+ xml.tag!('customerAddressId', options[:customer_address_id])
562
+ xml.target!
563
+ end
564
+
565
+ def build_get_customer_profile_request(xml, options)
566
+ xml.tag!('customerProfileId', options[:customer_profile_id])
567
+ xml.target!
568
+ end
569
+
570
+ def build_get_customer_profile_ids_request(xml, options)
571
+ xml.target!
572
+ end
573
+
574
+ def build_get_customer_payment_profile_request(xml, options)
575
+ xml.tag!('customerProfileId', options[:customer_profile_id])
576
+ xml.tag!('customerPaymentProfileId', options[:customer_payment_profile_id])
577
+ xml.target!
578
+ end
579
+
580
+ def build_get_customer_shipping_address_request(xml, options)
581
+ xml.tag!('customerProfileId', options[:customer_profile_id])
582
+ xml.tag!('customerAddressId', options[:customer_address_id])
583
+ xml.target!
584
+ end
585
+
586
+ def build_update_customer_profile_request(xml, options)
587
+ add_profile(xml, options[:profile], true)
588
+
589
+ xml.target!
590
+ end
591
+
592
+ def build_update_customer_payment_profile_request(xml, options)
593
+ xml.tag!('customerProfileId', options[:customer_profile_id])
594
+
595
+ xml.tag!('paymentProfile') do
596
+ add_payment_profile(xml, options[:payment_profile])
597
+ end
598
+
599
+ xml.tag!('validationMode', CIM_VALIDATION_MODES[options[:validation_mode]]) if options[:validation_mode]
600
+
601
+ xml.target!
602
+ end
603
+
604
+ def build_update_customer_shipping_address_request(xml, options)
605
+ xml.tag!('customerProfileId', options[:customer_profile_id])
606
+
607
+ xml.tag!('address') do
608
+ add_address(xml, options[:address])
609
+ end
610
+
611
+ xml.target!
612
+ end
613
+
614
+ def build_create_customer_profile_transaction_request(xml, options)
615
+ options[:extra_options] ||= {}
616
+ options[:extra_options].merge!('x_delim_char' => @options[:delimiter]) if @options[:delimiter]
617
+
618
+ add_transaction(xml, options[:transaction])
619
+ xml.tag!('extraOptions') do
620
+ xml.cdata!(format_extra_options(options[:extra_options]))
621
+ end unless options[:extra_options].blank?
622
+
623
+ xml.target!
624
+ end
625
+
626
+ def build_validate_customer_payment_profile_request(xml, options)
627
+ xml.tag!('customerProfileId', options[:customer_profile_id])
628
+ xml.tag!('customerPaymentProfileId', options[:customer_payment_profile_id])
629
+ xml.tag!('customerShippingAddressId', options[:customer_address_id]) if options[:customer_address_id]
630
+ tag_unless_blank(xml, 'cardCode', options[:card_code])
631
+ xml.tag!('validationMode', CIM_VALIDATION_MODES[options[:validation_mode]]) if options[:validation_mode]
632
+
633
+ xml.target!
634
+ end
635
+
636
+ # :merchant_customer_id (Optional)
637
+ # :description (Optional)
638
+ # :email (Optional)
639
+ # :payment_profiles (Optional)
640
+ def add_profile(xml, profile, update = false)
641
+ xml.tag!('profile') do
642
+ # Merchant assigned ID for the customer. Up to 20 characters. (optional)
643
+ xml.tag!('merchantCustomerId', profile[:merchant_customer_id]) if profile[:merchant_customer_id]
644
+ # Description of the customer. Up to 255 Characters (optional)
645
+ xml.tag!('description', profile[:description]) if profile[:description]
646
+ # Email Address for the customer. Up to 255 Characters (optional)
647
+ xml.tag!('email', profile[:email]) if profile[:email]
648
+
649
+ if update
650
+ xml.tag!('customerProfileId', profile[:customer_profile_id])
651
+ else
652
+ add_payment_profiles(xml, profile[:payment_profiles]) if profile[:payment_profiles]
653
+ add_ship_to_list(xml, profile[:ship_to_list]) if profile[:ship_to_list]
654
+ end
655
+ end
656
+ end
657
+
658
+ def add_transaction(xml, transaction)
659
+ unless CIM_TRANSACTION_TYPES.include?(transaction[:type])
660
+ raise StandardError, "Invalid Customer Information Manager Transaction Type: #{transaction[:type]}"
661
+ end
662
+
663
+ xml.tag!('transaction') do
664
+ xml.tag!(CIM_TRANSACTION_TYPES[transaction[:type]]) do
665
+ # The amount to be billed to the customer
666
+ case transaction[:type]
667
+ when :void
668
+ tag_unless_blank(xml,'customerProfileId', transaction[:customer_profile_id])
669
+ tag_unless_blank(xml,'customerPaymentProfileId', transaction[:customer_payment_profile_id])
670
+ tag_unless_blank(xml,'customerShippingAddressId', transaction[:customer_shipping_address_id])
671
+ xml.tag!('transId', transaction[:trans_id])
672
+ when :refund
673
+ #TODO - add lineItems field
674
+ xml.tag!('amount', transaction[:amount])
675
+ tag_unless_blank(xml, 'customerProfileId', transaction[:customer_profile_id])
676
+ tag_unless_blank(xml, 'customerPaymentProfileId', transaction[:customer_payment_profile_id])
677
+ tag_unless_blank(xml, 'customerShippingAddressId', transaction[:customer_shipping_address_id])
678
+ tag_unless_blank(xml, 'creditCardNumberMasked', transaction[:credit_card_number_masked])
679
+ tag_unless_blank(xml, 'bankRoutingNumberMasked', transaction[:bank_routing_number_masked])
680
+ tag_unless_blank(xml, 'bankAccountNumberMasked', transaction[:bank_account_number_masked])
681
+ add_order(xml, transaction[:order]) if transaction[:order].present?
682
+ xml.tag!('transId', transaction[:trans_id])
683
+ add_tax(xml, transaction[:tax]) if transaction[:tax]
684
+ add_duty(xml, transaction[:duty]) if transaction[:duty]
685
+ add_shipping(xml, transaction[:shipping]) if transaction[:shipping]
686
+ when :prior_auth_capture
687
+ xml.tag!('amount', transaction[:amount])
688
+ add_order(xml, transaction[:order]) if transaction[:order].present?
689
+ xml.tag!('transId', transaction[:trans_id])
690
+ else
691
+ xml.tag!('amount', transaction[:amount])
692
+ xml.tag!('customerProfileId', transaction[:customer_profile_id])
693
+ xml.tag!('customerPaymentProfileId', transaction[:customer_payment_profile_id])
694
+ xml.tag!('approvalCode', transaction[:approval_code]) if transaction[:type] == :capture_only
695
+ add_order(xml, transaction[:order]) if transaction[:order].present?
696
+
697
+ end
698
+ if [:auth_capture, :auth_only, :capture_only].include?(transaction[:type])
699
+ xml.tag!('recurringBilling', transaction[:recurring_billing]) if transaction.has_key?(:recurring_billing)
700
+ end
701
+ unless [:void,:refund,:prior_auth_capture].include?(transaction[:type])
702
+ tag_unless_blank(xml, 'cardCode', transaction[:card_code])
703
+ end
704
+ end
705
+ end
706
+ end
707
+
708
+ def add_tax(xml, tax)
709
+ xml.tag!('tax') do
710
+ xml.tag!('amount', tax[:amount]) if tax[:amount]
711
+ xml.tag!('name', tax[:name]) if tax[:name]
712
+ xml.tag!('description', tax[:description]) if tax[:description]
713
+ end
714
+ end
715
+
716
+ def add_duty(xml, duty)
717
+ xml.tag!('duty') do
718
+ xml.tag!('amount', duty[:amount]) if duty[:amount]
719
+ xml.tag!('name', duty[:name]) if duty[:name]
720
+ xml.tag!('description', duty[:description]) if duty[:description]
721
+ end
722
+ end
723
+
724
+ def add_shipping(xml, shipping)
725
+ xml.tag!('shipping') do
726
+ xml.tag!('amount', shipping[:amount]) if shipping[:amount]
727
+ xml.tag!('name', shipping[:name]) if shipping[:name]
728
+ xml.tag!('description', shipping[:description]) if shipping[:description]
729
+ end
730
+ end
731
+
732
+ def add_order(xml, order)
733
+ xml.tag!('order') do
734
+ xml.tag!('invoiceNumber', order[:invoice_number]) if order[:invoice_number]
735
+ xml.tag!('description', order[:description]) if order[:description]
736
+ xml.tag!('purchaseOrderNumber', order[:purchase_order_number]) if order[:purchase_order_number]
737
+ end
738
+ end
739
+
740
+ def add_payment_profiles(xml, payment_profiles)
741
+ xml.tag!('paymentProfiles') do
742
+ add_payment_profile(xml, payment_profiles)
743
+ end
744
+ end
745
+
746
+ # :customer_type => 'individual or business', # Optional
747
+ # :bill_to => @address,
748
+ # :payment => @payment
749
+ def add_payment_profile(xml, payment_profile)
750
+ # 'individual' or 'business' (optional)
751
+ xml.tag!('customerType', payment_profile[:customer_type]) if payment_profile[:customer_type]
752
+
753
+ if payment_profile[:bill_to]
754
+ xml.tag!('billTo') do
755
+ add_address(xml, payment_profile[:bill_to])
756
+ end
757
+ end
758
+
759
+ if payment_profile[:payment]
760
+ xml.tag!('payment') do
761
+ add_credit_card(xml, payment_profile[:payment][:credit_card]) if payment_profile[:payment].has_key?(:credit_card)
762
+ add_bank_account(xml, payment_profile[:payment][:bank_account]) if payment_profile[:payment].has_key?(:bank_account)
763
+ add_drivers_license(xml, payment_profile[:payment][:drivers_license]) if payment_profile[:payment].has_key?(:drivers_license)
764
+ # This element is only required for Wells Fargo SecureSource eCheck.Net merchants
765
+ # The customer's Social Security Number or Tax ID
766
+ xml.tag!('taxId', payment_profile[:payment]) if payment_profile[:payment].has_key?(:tax_id)
767
+ end
768
+ end
769
+
770
+ xml.tag!('customerPaymentProfileId', payment_profile[:customer_payment_profile_id]) if payment_profile[:customer_payment_profile_id]
771
+ end
772
+
773
+ def add_ship_to_list(xml, ship_to_list)
774
+ xml.tag!('shipToList') do
775
+ add_address(xml, ship_to_list)
776
+ end
777
+ end
778
+
779
+ def add_address(xml, address)
780
+ xml.tag!('firstName', address[:first_name])
781
+ xml.tag!('lastName', address[:last_name])
782
+ xml.tag!('company', address[:company])
783
+ xml.tag!('address', address[:address1]) if address[:address1]
784
+ xml.tag!('address', address[:address]) if address[:address]
785
+ xml.tag!('city', address[:city])
786
+ xml.tag!('state', address[:state])
787
+ xml.tag!('zip', address[:zip])
788
+ xml.tag!('country', address[:country])
789
+ xml.tag!('phoneNumber', address[:phone_number]) if address[:phone_number]
790
+ xml.tag!('faxNumber', address[:fax_number]) if address[:fax_number]
791
+
792
+ xml.tag!('customerAddressId', address[:customer_address_id]) if address[:customer_address_id]
793
+ end
794
+
795
+ # Adds customer’s credit card information
796
+ # Note: This element should only be included
797
+ # when the payment method is credit card.
798
+ def add_credit_card(xml, credit_card)
799
+ return unless credit_card
800
+ xml.tag!('creditCard') do
801
+ # The credit card number used for payment of the subscription
802
+ xml.tag!('cardNumber', full_or_masked_card_number(credit_card.number))
803
+ # The expiration date of the credit card used for the subscription
804
+ xml.tag!('expirationDate', expdate(credit_card))
805
+ # Note that Authorize.net does not save CVV codes as part of the
806
+ # payment profile. Any transactions/validations after the payment
807
+ # profile is created that wish to use CVV verification must pass
808
+ # the CVV code to authorize.net again.
809
+ xml.tag!('cardCode', credit_card.verification_value) if credit_card.verification_value?
810
+ end
811
+ end
812
+
813
+ # Adds customer’s bank account information
814
+ # Note: This element should only be included
815
+ # when the payment method is bank account.
816
+ def add_bank_account(xml, bank_account)
817
+ raise StandardError, "Invalid Bank Account Type: #{bank_account[:account_type]}" unless BANK_ACCOUNT_TYPES.include?(bank_account[:account_type])
818
+ raise StandardError, "Invalid eCheck Type: #{bank_account[:echeck_type]}" unless ECHECK_TYPES.include?(bank_account[:echeck_type])
819
+
820
+ xml.tag!('bankAccount') do
821
+ # The type of bank account
822
+ xml.tag!('accountType', BANK_ACCOUNT_TYPES[bank_account[:account_type]])
823
+ # The routing number of the customer’s bank
824
+ xml.tag!('routingNumber', bank_account[:routing_number])
825
+ # The bank account number
826
+ xml.tag!('accountNumber', bank_account[:account_number])
827
+ # The full name of the individual associated
828
+ # with the bank account number
829
+ xml.tag!('nameOnAccount', bank_account[:name_on_account])
830
+ # The type of electronic check transaction
831
+ xml.tag!('echeckType', ECHECK_TYPES[bank_account[:echeck_type]])
832
+ # The full name of the individual associated
833
+ # with the bank account number (optional)
834
+ xml.tag!('bankName', bank_account[:bank_name]) if bank_account[:bank_name]
835
+ end
836
+ end
837
+
838
+ # Adds customer’s driver's license information
839
+ # Note: This element is only required for
840
+ # Wells Fargo SecureSource eCheck.Net merchants
841
+ def add_drivers_license(xml, drivers_license)
842
+ xml.tag!('driversLicense') do
843
+ # The state of the customer's driver's license
844
+ # A valid two character state code
845
+ xml.tag!('state', drivers_license[:state])
846
+ # The customer’s driver's license number
847
+ xml.tag!('number', drivers_license[:number])
848
+ # The date of birth listed on the customer's driver's license
849
+ # YYYY-MM-DD
850
+ xml.tag!('dateOfBirth', drivers_license[:date_of_birth])
851
+ end
852
+ end
853
+
854
+ def commit(action, request)
855
+ url = test? ? test_url : live_url
856
+ xml = ssl_post(url, request, "Content-Type" => "text/xml")
857
+
858
+ response_params = parse(action, xml)
859
+
860
+ message = response_params['messages']['message']['text']
861
+ test_mode = @options[:test_requests] || message =~ /Test Mode/
862
+ success = response_params['messages']['result_code'] == 'Ok'
863
+ response_params['direct_response'] = parse_direct_response(response_params['direct_response']) if response_params['direct_response']
864
+ transaction_id = response_params['direct_response']['transaction_id'] if response_params['direct_response']
865
+
866
+ Response.new(success, message, response_params,
867
+ :test => test_mode,
868
+ :authorization => transaction_id || response_params['customer_profile_id'] || (response_params['profile'] ? response_params['profile']['customer_profile_id'] : nil)
869
+ )
870
+ end
871
+
872
+ def tag_unless_blank(xml, tag_name, data)
873
+ xml.tag!(tag_name, data) unless data.blank? || data.nil?
874
+ end
875
+
876
+ def format_extra_options(options)
877
+ options.map{ |k, v| "#{k}=#{v}" }.join('&') unless options.nil?
878
+ end
879
+
880
+ def parse_direct_response(params)
881
+ delimiter = @options[:delimiter] || ','
882
+ direct_response = {'raw' => params}
883
+ direct_response_fields = params.split(delimiter)
884
+ direct_response.merge(
885
+ {
886
+ 'response_code' => direct_response_fields[0],
887
+ 'response_subcode' => direct_response_fields[1],
888
+ 'response_reason_code' => direct_response_fields[2],
889
+ 'message' => direct_response_fields[3],
890
+ 'approval_code' => direct_response_fields[4],
891
+ 'avs_response' => direct_response_fields[5],
892
+ 'transaction_id' => direct_response_fields[6],
893
+ 'invoice_number' => direct_response_fields[7],
894
+ 'order_description' => direct_response_fields[8],
895
+ 'amount' => direct_response_fields[9],
896
+ 'method' => direct_response_fields[10],
897
+ 'transaction_type' => direct_response_fields[11],
898
+ 'customer_id' => direct_response_fields[12],
899
+ 'first_name' => direct_response_fields[13],
900
+ 'last_name' => direct_response_fields[14],
901
+ 'company' => direct_response_fields[15],
902
+ 'address' => direct_response_fields[16],
903
+ 'city' => direct_response_fields[17],
904
+ 'state' => direct_response_fields[18],
905
+ 'zip_code' => direct_response_fields[19],
906
+ 'country' => direct_response_fields[20],
907
+ 'phone' => direct_response_fields[21],
908
+ 'fax' => direct_response_fields[22],
909
+ 'email_address' => direct_response_fields[23],
910
+ 'ship_to_first_name' => direct_response_fields[24],
911
+ 'ship_to_last_name' => direct_response_fields[25],
912
+ 'ship_to_company' => direct_response_fields[26],
913
+ 'ship_to_address' => direct_response_fields[27],
914
+ 'ship_to_city' => direct_response_fields[28],
915
+ 'ship_to_state' => direct_response_fields[29],
916
+ 'ship_to_zip_code' => direct_response_fields[30],
917
+ 'ship_to_country' => direct_response_fields[31],
918
+ 'tax' => direct_response_fields[32],
919
+ 'duty' => direct_response_fields[33],
920
+ 'freight' => direct_response_fields[34],
921
+ 'tax_exempt' => direct_response_fields[35],
922
+ 'purchase_order_number' => direct_response_fields[36],
923
+ 'md5_hash' => direct_response_fields[37],
924
+ 'card_code' => direct_response_fields[38],
925
+ 'cardholder_authentication_verification_response' => direct_response_fields[39],
926
+ # The following direct response fields are only available in version 3.1 of the
927
+ # transaction response. Check your merchant account settings for details.
928
+ 'account_number' => direct_response_fields[50] || '',
929
+ 'card_type' => direct_response_fields[51] || '',
930
+ 'split_tender_id' => direct_response_fields[52] || '',
931
+ 'requested_amount' => direct_response_fields[53] || '',
932
+ 'balance_on_card' => direct_response_fields[54] || '',
933
+ }
934
+ )
935
+ end
936
+
937
+ def parse(action, xml)
938
+ xml = REXML::Document.new(xml)
939
+ root = REXML::XPath.first(xml, "//#{CIM_ACTIONS[action]}Response") ||
940
+ REXML::XPath.first(xml, "//ErrorResponse")
941
+ if root
942
+ response = parse_element(root)
943
+ end
944
+
945
+ response
946
+ end
947
+
948
+ def parse_element(node)
949
+ if node.has_elements?
950
+ response = {}
951
+ node.elements.each{ |e|
952
+ key = e.name.underscore
953
+ value = parse_element(e)
954
+ if response.has_key?(key)
955
+ if response[key].is_a?(Array)
956
+ response[key].push(value)
957
+ else
958
+ response[key] = [response[key], value]
959
+ end
960
+ else
961
+ response[key] = parse_element(e)
962
+ end
963
+ }
964
+ else
965
+ response = node.text
966
+ end
967
+
968
+ response
969
+ end
970
+
971
+ def full_or_masked_card_number(card_number)
972
+ !card_number.nil? && card_number.length == 4 ? "XXXX#{card_number}" : card_number
973
+ end
974
+ end
975
+ end
976
+ end