activemerchant 1.3.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. data.tar.gz.sig +0 -0
  2. data/CHANGELOG +58 -0
  3. data/CONTRIBUTERS +25 -0
  4. data/MIT-LICENSE +3 -3
  5. data/README +16 -10
  6. data/Rakefile +4 -3
  7. data/lib/active_merchant.rb +7 -1
  8. data/lib/active_merchant/billing/check.rb +16 -9
  9. data/lib/active_merchant/billing/gateway.rb +1 -1
  10. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +702 -0
  11. data/lib/active_merchant/billing/gateways/beanstream.rb +102 -0
  12. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +233 -0
  13. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +54 -0
  14. data/lib/active_merchant/billing/gateways/braintree.rb +10 -1
  15. data/lib/active_merchant/billing/gateways/cyber_source.rb +26 -2
  16. data/lib/active_merchant/billing/gateways/data_cash.rb +255 -59
  17. data/lib/active_merchant/billing/gateways/modern_payments.rb +36 -0
  18. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +214 -0
  19. data/lib/active_merchant/billing/gateways/net_registry.rb +1 -0
  20. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +2 -2
  21. data/lib/active_merchant/billing/gateways/payflow_express.rb +3 -11
  22. data/lib/active_merchant/billing/gateways/payment_express.rb +2 -2
  23. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +39 -21
  24. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  25. data/lib/active_merchant/billing/gateways/paypal_express.rb +3 -12
  26. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +20 -0
  27. data/lib/active_merchant/billing/gateways/protx.rb +25 -25
  28. data/lib/active_merchant/billing/gateways/sage.rb +145 -0
  29. data/lib/active_merchant/billing/gateways/sage/sage_bankcard.rb +88 -0
  30. data/lib/active_merchant/billing/gateways/sage/sage_core.rb +110 -0
  31. data/lib/active_merchant/billing/gateways/sage/sage_virtual_check.rb +97 -0
  32. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +3 -1
  33. data/lib/active_merchant/billing/gateways/skip_jack.rb +2 -0
  34. data/lib/active_merchant/billing/gateways/trust_commerce.rb +1 -1
  35. data/lib/active_merchant/billing/gateways/wirecard.rb +304 -0
  36. data/lib/active_merchant/billing/integrations.rb +8 -2
  37. data/lib/active_merchant/billing/integrations/action_view_helper.rb +18 -4
  38. data/lib/active_merchant/billing/integrations/hi_trust/notification.rb +4 -2
  39. data/lib/active_merchant/billing/integrations/notification.rb +10 -1
  40. data/lib/active_merchant/lib/posts_data.rb +12 -3
  41. data/script/destroy +0 -0
  42. data/script/generate +0 -0
  43. data/test/extra/binding_of_caller.rb +0 -0
  44. data/test/extra/breakpoint.rb +0 -0
  45. data/test/fixtures.yml +24 -0
  46. data/test/remote/gateways/remote_authorize_net_cim_test.rb +459 -0
  47. data/test/remote/gateways/remote_beanstream_interac_test.rb +53 -0
  48. data/test/remote/gateways/remote_beanstream_test.rb +150 -0
  49. data/test/remote/gateways/remote_braintree_test.rb +22 -0
  50. data/test/remote/gateways/remote_cyber_source_test.rb +28 -3
  51. data/test/remote/gateways/remote_data_cash_test.rb +250 -48
  52. data/test/remote/gateways/remote_modern_payments_cim_test.rb +58 -0
  53. data/test/remote/gateways/remote_modern_payments_test.rb +43 -0
  54. data/test/remote/gateways/remote_sage_bankcard_test.rb +109 -0
  55. data/test/remote/gateways/remote_sage_test.rb +87 -0
  56. data/test/remote/gateways/remote_sage_virtual_check_test.rb +62 -0
  57. data/test/remote/gateways/remote_wirecard_test.rb +76 -0
  58. data/test/remote/integrations/remote_paypal_integration_test.rb +15 -3
  59. data/test/test_helper.rb +31 -13
  60. data/test/unit/check_test.rb +14 -2
  61. data/test/unit/credit_card_methods_test.rb +18 -0
  62. data/test/unit/gateways/authorize_net_cim_test.rb +641 -0
  63. data/test/unit/gateways/beanstream_interac_test.rb +51 -0
  64. data/test/unit/gateways/beanstream_test.rb +108 -0
  65. data/test/unit/gateways/braintree_test.rb +2 -5
  66. data/test/unit/gateways/cyber_source_test.rb +18 -0
  67. data/test/unit/gateways/data_cash_test.rb +32 -4
  68. data/test/unit/gateways/gateway_test.rb +8 -1
  69. data/test/unit/gateways/modern_payments_cim_test.rb +171 -0
  70. data/test/unit/gateways/net_registry_test.rb +6 -0
  71. data/test/unit/gateways/payflow_express_test.rb +18 -2
  72. data/test/unit/gateways/paypal_express_test.rb +154 -0
  73. data/test/unit/gateways/paypal_test.rb +140 -0
  74. data/test/unit/gateways/sage_bankcard_test.rb +162 -0
  75. data/test/unit/gateways/sage_virtual_check_test.rb +71 -0
  76. data/test/unit/gateways/secure_pay_au_test.rb +58 -1
  77. data/test/unit/gateways/skip_jack_test.rb +8 -0
  78. data/test/unit/gateways/verifi_test.rb +0 -1
  79. data/test/unit/gateways/wirecard_test.rb +232 -0
  80. data/test/unit/integrations/action_view_helper_test.rb +3 -0
  81. data/test/unit/integrations/notifications/hi_trust_notification_test.rb +23 -2
  82. data/test/unit/integrations/notifications/notification_test.rb +13 -0
  83. data/test/unit/posts_data_test.rb +20 -6
  84. metadata +40 -5
  85. metadata.gz.sig +0 -0
data.tar.gz.sig CHANGED
Binary file
data/CHANGELOG CHANGED
@@ -1,5 +1,63 @@
1
1
  = ActiveMerchant CHANGELOG
2
2
 
3
+ == Version 1.4.0 (November 27, 2008)
4
+
5
+ * Return failed authorization when SkipJack purchase fails [Tron, cody]
6
+ * Update README [cody]
7
+ * Add metadata to Authorize.net CIM gateway [cody]
8
+ * Make ActionViewHelper compatible with changes to concat method in ActionPack [cody]
9
+ * Remove PayPal and Payflow Name-Value gateways. PayPal is no longer terminating the Payflow XML API. [cody]
10
+ * Don't directly use the inflector in the action view helper [cody]
11
+ * Work around Rails Inflector change [cody]
12
+ * Add configurable timeouts to PostsData [Michael Koziarski]
13
+ * Add valid_sender? method to gateway integrations [Soleone]
14
+ * Fix PayPal error parsing [cody]
15
+ * Fix MIT-LICENSE [cody]
16
+ * Add a payment gateway for Website Payments Pro Canada [cody]
17
+ * Fix shipping amount option in Sage gateway [Darrick Wiebe]
18
+ * Improved message and error message handling [Soleone]
19
+ * Get Wirecard working in the Live environment [Soleone]
20
+ * Remove dead code in PayPal Common API files [cody]
21
+ * Use the PayPal short error message if the long message is empty [cody]
22
+ * Fix unit tests when being run by Cruise Control [cody]
23
+ * Add support for PayPal Fraud Review Response [cody]
24
+ * Add testing support for German Wirecard Gateway [Soleone]
25
+ * Specify required version of ActiveSupport [cody]
26
+ * Make ssl_strict a superclass_delegating_accessor so the entire application's validation of SSL certs can be disabled in the event of certificate problem. [cody]
27
+ * Make Gateway.application_id a superclass_delegating_accessor so it can be set from outside the subclass definition [cody]
28
+ * Add Discover to the list of supported card types for Braintree [cody]
29
+ * Add support for Modern Payments gateway [Jeremy Nicoll, cody]
30
+ * Add support for EFT/ACH and Interac Online to the BeanstreamGateway [cody]
31
+ * Document the SageGateway [cody]
32
+ * Add support for echecks with SageGateway. [cody]
33
+ * Handle all successful SecurePay AU response codes [cody]
34
+ * Get SageGateway working with real test account. Improve test suite. [cody]
35
+ * Unify TrustCommerce, Payment Express, and Braintree CC storage [benjamin.curtis]
36
+ * Update to use new Payflow Pro URLs [cody]
37
+ * Fix missing Content-Type header for Ruby 1.8.4 [cody]
38
+ * Fix Authorize.Net CIM response.message [patrick.t.joyce]
39
+ * Add JCB and Diners Club as supported cards to SageGateway [cody]
40
+ * Add CA country code to Sage gateway's supported countries [cody]
41
+ * Add support for Sage Payment Solutions gateway [cody]
42
+ * Add test mode detection to Beanstream [cody]
43
+ * Add support for Beanstream payment gateway [xiaobozz]
44
+ * Add support for PayPal NV Pair API. Will be used to replace the usage of the PayPal SOAP API in ActiveMerchant in the future [Greg Furmanek, cody]
45
+ * Protx does support UK Maestro [cody]
46
+ * Add tests for length of UK Maestro cards [cody]
47
+ * Return all the error messages and codes from paypal responses [cody]
48
+ * Fail hard when attempting to capture without a credit card with NetRegistry [cody]
49
+ * Add support for the order fields to the create_customer_profile_transaction in Authorize.net CIM. [Patrick T. Joyce]
50
+ * Strip invalid characters and limit lengths of Protx customer data [Simon Russell]
51
+ * Fix empty start or end dates in Protx [Simon Russell]
52
+ * Add support for Authorize.net CIM [Patrick T. Joyce, Ian Lotinsky]
53
+ * Add option to skip order review to all PayPal Express gateways [garret.alfert, cody]
54
+ * Add capturing partial amounts, fix issue number formatting, fix authorization string when nil values returned, fix parsing of multiple '=' characters, simplify message_from [Simon Russell]
55
+ * Fix StartDate in ProtxGatewy [cody]
56
+ * Add support for refunds and continuous authority references to DataCashGateway [joel.chippindale]
57
+ * Fix gross in HiTrust notification. Don't use Money object in Verifi gateway [cody]
58
+ * Initial implementation of Payflow Name-Value API [Greg Furmanek]
59
+ * Add support for CyberSource credits [mjuneja]
60
+
3
61
  == Version 1.3.2 (February 24, 2008)
4
62
 
5
63
  * Actually fix the bug by adding extdata element to Payflow Requests [cody]
@@ -63,3 +63,28 @@ SkipJack Gateway (Nov 29, 2007)
63
63
  HiTRUST Gateway (Dec 10, 2007)
64
64
 
65
65
  * Jaded Pixel
66
+
67
+ Payflow NV Gateway (Mar 03, 2008)
68
+
69
+ * Greg Furmanek
70
+
71
+ PaypalNVGateway (Apr 12, 2008)
72
+
73
+ * Greg Furmanek
74
+
75
+ Beanstream (May 13, 2008)
76
+
77
+ * Created by xiaobozz ( xiaobozzz at gmail dot com )
78
+
79
+ Sage (June, 2008)
80
+
81
+ * Cody
82
+
83
+ Modern Payments (June 13, 2008)
84
+
85
+ * Initial implementation by Jeremy Nicoll
86
+ * Additional portions by Cody Fauser
87
+
88
+ Wirecard Gateway (June 30, 2008)
89
+
90
+ * Initial implementation by Soleone
@@ -13,8 +13,8 @@ included in all copies or substantial portions of the Software.
13
13
 
14
14
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
15
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
17
- NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
18
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
19
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README CHANGED
@@ -9,9 +9,11 @@ Please visit the {ActiveMerchant homepage}[http://activemerchant.org] for more r
9
9
 
10
10
  == Supported Direct Payment Gateways
11
11
 
12
- The {ActiveMerchant Wiki}[http://code.google.com/p/activemerchant/wiki] contains a {table of features supported by each gateway}[http://code.google.com/p/activemerchant/wiki/SupportedGatewayPaymentOperations].
12
+ The {ActiveMerchant Wiki}[http://github.com/Shopify/active_merchant/wikis] contains a {table of features supported by each gateway}[http://github.com/Shopify/active_merchant/wikis/gatewayfeaturematrix].
13
13
 
14
- * {Authorize.net}[http://www.authorize.net/] - US
14
+ * {Authorize.Net CIM}[http://www.authorize.net/] - US
15
+ * {Authorize.Net}[http://www.authorize.net/] - US
16
+ * {Beanstream.com}[http://www.beanstream.com/] - CA
15
17
  * {Braintree}[http://www.braintreepaymentsolutions.com] - US
16
18
  * {CardStream}[http://www.cardstream.com/] - GB
17
19
  * {CyberSource}[http://www.cybersource.com] - US
@@ -20,16 +22,17 @@ The {ActiveMerchant Wiki}[http://code.google.com/p/activemerchant/wiki] contains
20
22
  * {eWAY}[http://www.eway.com.au/] - AU
21
23
  * {E-xact}[http://www.e-xact.com] - CA, US
22
24
  * {LinkPoint}[http://www.linkpoint.com/] - US
25
+ * {Modern Payments}[http://www.modpay.com] - US
23
26
  * {Moneris}[http://www.moneris.com/] - CA
24
27
  * {NetRegistry}[http://www.netregistry.com.au] - AU
25
28
  * {NETbilling}[http://www.netbilling.com] - US
26
29
  * {PayJunction}[http://www.payjunction.com/] - US
27
30
  * {PaySecure}[http://www.commsecure.com.au/paysecure.shtml] - AU
28
31
  * {PayPal Express Checkout}[https://www.paypal.com/cgi-bin/webscr?cmd=xpt/merchant/ExpressCheckoutIntro-outside] - US, CA, SG, AU
29
- * {PayPal Express Checkout (UK)}[https://www.paypal.com/uk/cgi-bin/webscr?cmd=_additional-payment-overview-outside] - GB
30
32
  * {PayPal Payflow Pro}[https://www.paypal.com/cgi-bin/webscr?cmd=_payflow-pro-overview-outside] - US, CA, SG, AU
31
33
  * {PayPal Website Payments Pro (UK)}[https://www.paypal.com/uk/cgi-bin/webscr?cmd=_wp-pro-overview-outside] - GB
32
34
  * {PaymentExpress}[http://www.paymentexpress.com/] - AU, MY, NZ, SG, ZA, GB, US
35
+ * {PayPal Website Payments Pro (CA)}[https://www.paypal.com/cgi-bin/webscr?cmd=_wp-pro-overview-outside] - CA
33
36
  * {PayPal Express Checkout}[https://www.paypal.com/cgi-bin/webscr?cmd=xpt/merchant/ExpressCheckoutIntro-outside] - US
34
37
  * {PayPal Website Payments Pro (US)}[https://www.paypal.com/cgi-bin/webscr?cmd=_wp-pro-overview-outside] - US
35
38
  * {Plug'n Pay}[http://www.plugnpay.com/] - US
@@ -38,6 +41,8 @@ The {ActiveMerchant Wiki}[http://code.google.com/p/activemerchant/wiki] contains
38
41
  * {PSL Payment Solutions}[http://www.paymentsolutionsltd.com/] - GB
39
42
  * {Quickpay}[http://quickpay.dk/] - DK
40
43
  * {Realex}[http://www.realexpayments.com/] - IE, GB
44
+ * {Sage Payment Solutions}[http://www.sagepayments.com] - US, CA
45
+ * {SecurePay}[http://securepay.com.au] - AU
41
46
  * {SecurePay}[http://www.securepay.com/] - US
42
47
  * {SecurePayTech}[http://www.securepaytech.com/] - NZ
43
48
  * {SkipJack}[http://www.skipjack.com/] - US, CA
@@ -46,6 +51,7 @@ The {ActiveMerchant Wiki}[http://code.google.com/p/activemerchant/wiki] contains
46
51
  * {USA ePay}[http://www.usaepay.com/] - US
47
52
  * {Verifi}[http://www.verifi.com/] - US
48
53
  * {ViaKLIX}[http://viaklix.com] - US
54
+ * {Wirecard}[http://www.wirecard.com] - DE
49
55
 
50
56
  == Supported Offsite Payment Gateways
51
57
 
@@ -58,23 +64,23 @@ The {ActiveMerchant Wiki}[http://code.google.com/p/activemerchant/wiki] contains
58
64
 
59
65
  == Download
60
66
 
61
- Currently this library is available with svn from:
67
+ Currently this library is available with git from:
62
68
 
63
- http://activemerchant.googlecode.com/svn/trunk/active_merchant
69
+ git://github.com/Shopify/active_merchant.git
64
70
 
65
71
  == Installation
66
72
 
67
- === From Subversion
73
+ === From Git
68
74
 
69
- You can check out the latest source from svn:
75
+ You can check out the latest source from git:
70
76
 
71
- > svn co http://activemerchant.googlecode.com/svn/trunk/active_merchant
77
+ > git pull git://github.com/Shopify/active_merchant.git
72
78
 
73
79
  === As a Rails plugin
74
80
 
75
81
  ActiveMerchant includes an init.rb file. This means that Rails will automatically load ActiveMerchant on startup. Run the following command from the root directory of your Rails project to install ActiveMerchant as a Rails plugin:
76
82
 
77
- > ./script/plugin install http://activemerchant.googlecode.com/svn/trunk/active_merchant
83
+ > ./script/plugin install git://github.com/Shopify/active_merchant.git
78
84
 
79
85
  === From Ruby Gems
80
86
 
@@ -124,4 +130,4 @@ Installation from RubyGems
124
130
 
125
131
  == Contributing
126
132
 
127
- Please see the {ActiveMerchant Guide to Contributing}[http://code.google.com/p/activemerchant/wiki/Contributing] for information on adding a new gateway to ActiveMerchant.
133
+ Please see the {ActiveMerchant Guide to Contributing}[http://github.com/Shopify/active_merchant/wikis/contributing] for information on adding a new gateway to ActiveMerchant.
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ require File.dirname(__FILE__) + '/lib/tasks/cia'
8
8
  require File.dirname(__FILE__) + '/lib/support/gateway_support'
9
9
 
10
10
 
11
- PKG_VERSION = "1.3.2"
11
+ PKG_VERSION = "1.4.0"
12
12
  PKG_NAME = "activemerchant"
13
13
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
14
14
 
@@ -80,8 +80,8 @@ spec = Gem::Specification.new do |s|
80
80
 
81
81
  s.files = PKG_FILES
82
82
 
83
+ s.rubyforge_project = "activemerchant"
83
84
  s.require_path = 'lib'
84
- s.autorequire = 'active_merchant'
85
85
  s.author = "Tobias Luetke"
86
86
  s.email = "tobi@leetsoft.com"
87
87
  s.homepage = "http://activemerchant.org/"
@@ -115,6 +115,7 @@ task :publish => [ :package ] do
115
115
  packages = %w( gem tgz zip ).collect{ |ext| "pkg/#{PKG_NAME}-#{PKG_VERSION}.#{ext}" }
116
116
 
117
117
  rubyforge = RubyForge.new
118
+ rubyforge.configure
118
119
  rubyforge.login
119
120
  rubyforge.add_release(PKG_NAME, PKG_NAME, "REL #{PKG_VERSION}", *packages)
120
121
  end
@@ -157,4 +158,4 @@ namespace :gateways do
157
158
  end
158
159
 
159
160
 
160
-
161
+
@@ -28,7 +28,13 @@ begin
28
28
  require 'active_support'
29
29
  rescue LoadError
30
30
  require 'rubygems'
31
- require 'activesupport'
31
+
32
+ if respond_to? :gem
33
+ gem 'activesupport', ">=2.0.0"
34
+ require 'active_support'
35
+ else
36
+ require_gem 'activesupport'
37
+ end
32
38
  end
33
39
 
34
40
  begin
@@ -9,19 +9,26 @@ module ActiveMerchant #:nodoc:
9
9
  class Check
10
10
  include Validateable
11
11
 
12
- attr_accessor :name, :routing_number, :account_number, :account_holder_type, :account_type
12
+ attr_accessor :first_name, :last_name, :routing_number, :account_number, :account_holder_type, :account_type, :number
13
13
 
14
- def initialize(args={})
15
- args = args.with_indifferent_access
16
- self.name = args[:name]
17
- self.routing_number = args[:routing_number]
18
- self.account_number = args[:account_number]
19
- self.account_holder_type = args[:account_holder_type]
20
- self.account_type = args[:account_type]
14
+ # Used for Canadian bank accounts
15
+ attr_accessor :institution_number, :transit_number
16
+
17
+ def name
18
+ @name ||= "#{@first_name} #{@last_name}".strip
19
+ end
20
+
21
+ def name=(value)
22
+ return if value.blank?
23
+
24
+ @name = value
25
+ segments = value.split(' ')
26
+ @last_name = segments.pop
27
+ @first_name = segments.join(' ')
21
28
  end
22
29
 
23
30
  def validate
24
- for attr in [:name, :routing_number, :account_number]
31
+ [:name, :routing_number, :account_number].each do |attr|
25
32
  errors.add(attr, "cannot be empty") if self.send(attr).blank?
26
33
  end
27
34
 
@@ -93,7 +93,7 @@ module ActiveMerchant #:nodoc:
93
93
 
94
94
  # The application making the calls to the gateway
95
95
  # Useful for things like the PayPal build notation (BN) id fields
96
- class_inheritable_accessor :application_id
96
+ superclass_delegating_accessor :application_id
97
97
  self.application_id = 'ActiveMerchant'
98
98
 
99
99
  attr_reader :options
@@ -0,0 +1,702 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ # ==== Custotmer Information Manager (CIM)
4
+ #
5
+ # The Authorize.Net Customer Information Manager (CIM) is an optional additional service that allows you to store sensitive payment information on
6
+ # Authorize.Net's servers, simplifying payments for returning customers and recurring transactions. It can also help with Payment Card Industry (PCI)
7
+ # Data Security Standard compliance, since customer data is no longer stored locally.
8
+ #
9
+ # To use the AuthorizeNetCimGateway CIM must be enabled for your account.
10
+ #
11
+ # Information about CIM is available on the {Authorize.Net website}[http://www.authorize.net/solutions/merchantsolutions/merchantservices/cim/].
12
+ # Information about the CIM API is available at the {Authorize.Net Integration Center}[http://developer.authorize.net/]
13
+ #
14
+ # ==== Login and Password
15
+ #
16
+ # The login and password are not the username and password you use to
17
+ # login to the Authorize.Net Merchant Interface. Instead, you will
18
+ # use the API Login ID as the login and Transaction Key as the
19
+ # password.
20
+ #
21
+ # ==== How to Get Your API Login ID and Transaction Key
22
+ #
23
+ # 1. Log into the Merchant Interface
24
+ # 2. Select Settings from the Main Menu
25
+ # 3. Click on API Login ID and Transaction Key in the Security section
26
+ # 4. Type in the answer to the secret question configured on setup
27
+ # 5. Click Submit
28
+ class AuthorizeNetCimGateway < Gateway
29
+
30
+ class_inheritable_accessor :test_url, :live_url
31
+
32
+ self.test_url = 'https://apitest.authorize.net/xml/v1/request.api'
33
+ self.live_url = 'https://api.authorize.net/xml/v1/request.api'
34
+
35
+ AUTHORIZE_NET_CIM_NAMESPACE = 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'
36
+
37
+ CIM_ACTIONS = {
38
+ :create_customer_profile => 'createCustomerProfile',
39
+ :create_customer_payment_profile => 'createCustomerPaymentProfile',
40
+ :create_customer_shipping_address => 'createCustomerShippingAddress',
41
+ :get_customer_profile => 'getCustomerProfile',
42
+ :get_customer_payment_profile => 'getCustomerPaymentProfile',
43
+ :get_customer_shipping_address => 'getCustomerShippingAddress',
44
+ :delete_customer_profile => 'deleteCustomerProfile',
45
+ :delete_customer_payment_profile => 'deleteCustomerPaymentProfile',
46
+ :delete_customer_shipping_address => 'deleteCustomerShippingAddress',
47
+ :update_customer_profile => 'updateCustomerProfile',
48
+ :update_customer_payment_profile => 'updateCustomerPaymentProfile',
49
+ :update_customer_shipping_address => 'updateCustomerShippingAddress',
50
+ :create_customer_profile_transaction => 'createCustomerProfileTransaction',
51
+ :validate_customer_payment_profile => 'validateCustomerPaymentProfile'
52
+ }
53
+
54
+ CIM_TRANSACTION_TYPES = {
55
+ :auth_capture => 'profileTransAuthCapture',
56
+ :auth_only => 'profileTransAuthOnly',
57
+ :capture_only => 'profileTransCaptureOnly'
58
+ }
59
+
60
+ CIM_VALIDATION_MODES = {
61
+ :none => 'none',
62
+ :test => 'testMode',
63
+ :live => 'liveMode'
64
+ }
65
+
66
+ BANK_ACCOUNT_TYPES = {
67
+ :checking => 'checking',
68
+ :savings => 'savings',
69
+ :business_checking => 'businessChecking'
70
+ }
71
+
72
+ ECHECK_TYPES = {
73
+ :ccd => 'CCD',
74
+ :ppd => 'PPD'
75
+ }
76
+
77
+ self.homepage_url = 'http://www.authorize.net/'
78
+ self.display_name = 'Authorize.Net CIM'
79
+ self.supported_countries = ['US']
80
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover]
81
+
82
+ # Creates a new AuthorizeNetCimGateway
83
+ #
84
+ # The gateway requires that a valid API Login ID and Transaction Key be passed
85
+ # in the +options+ hash.
86
+ #
87
+ # ==== Options
88
+ #
89
+ # * <tt>:login</tt> -- The Authorize.Net API Login ID (REQUIRED)
90
+ # * <tt>:password</tt> -- The Authorize.Net Transaction Key. (REQUIRED)
91
+ # * <tt>:test</tt> -- +true+ or +false+. If true, perform transactions against the test server.
92
+ # Otherwise, perform transactions against the production server.
93
+ def initialize(options = {})
94
+ requires!(options, :login, :password)
95
+ @options = options
96
+ super
97
+ end
98
+
99
+ # Creates a new customer profile along with any customer payment profiles and customer shipping addresses
100
+ # for the customer profile.
101
+ #
102
+ # Returns a Response with the Customer Profile ID of the new customer profile in the authorization field.
103
+ # It is *CRITICAL* that you save this ID. There is no way to retrieve this through the API. You will not
104
+ # be able to create another Customer Profile with the same information.
105
+ #
106
+ # ==== Options
107
+ #
108
+ # TODO
109
+ def create_customer_profile(options)
110
+ # TODO Add requires
111
+ request = build_request(:create_customer_profile, options)
112
+ commit(:create_customer_profile, request)
113
+ end
114
+
115
+ # Creates a new customer payment profile for an existing customer profile.
116
+ #
117
+ # ==== Options
118
+ #
119
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer the payment profile will be added to. (REQUIRED)
120
+ # * <tt>:payment_profile</tt> -- A hash containing the elements of the new payment profile (REQUIRED)
121
+ #
122
+ # ==== Payment Profile
123
+ #
124
+ # * <tt>:payment</tt> -- A hash containing information on payment. Either :credit_card or :bank_account (REQUIRED)
125
+ def create_customer_payment_profile(options)
126
+ requires!(options, :customer_profile_id)
127
+ requires!(options, :payment_profile)
128
+ requires!(options[:payment_profile], :payment)
129
+
130
+ request = build_request(:create_customer_payment_profile, options)
131
+ commit(:create_customer_payment_profile, request)
132
+ end
133
+
134
+ # Creates a new customer shipping address for an existing customer profile.
135
+ #
136
+ # ==== Options
137
+ #
138
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer the payment profile will be added to. (REQUIRED)
139
+ # * <tt>:address</tt> -- A hash containing the elements of the shipping address (REQUIRED)
140
+ def create_customer_shipping_address(options)
141
+ requires!(options, :customer_profile_id)
142
+ requires!(options, :address)
143
+
144
+ request = build_request(:create_customer_shipping_address, options)
145
+ commit(:create_customer_shipping_address, request)
146
+ end
147
+
148
+ # Deletes an existing customer profile along with all associated customer payment profiles and customer shipping addresses.
149
+ #
150
+ # ==== Options
151
+ #
152
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer to be deleted. (REQUIRED)
153
+ def delete_customer_profile(options)
154
+ requires!(options, :customer_profile_id)
155
+
156
+ request = build_request(:delete_customer_profile, options)
157
+ commit(:delete_customer_profile, request)
158
+ end
159
+
160
+ # Deletes a customer payment profile from an existing customer profile.
161
+ #
162
+ # ==== Options
163
+ #
164
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be deleted. (REQUIRED)
165
+ # * <tt>:customer_payment_profile_id</tt> -- The Payment Profile ID of the payment profile to be deleted. (REQUIRED)
166
+ def delete_customer_payment_profile(options)
167
+ requires!(options, :customer_profile_id)
168
+ requires!(options, :customer_payment_profile_id)
169
+
170
+ request = build_request(:delete_customer_payment_profile, options)
171
+ commit(:delete_customer_payment_profile, request)
172
+ end
173
+
174
+ # Deletes a customer shipping address from an existing customer profile.
175
+ #
176
+ # ==== Options
177
+ #
178
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be deleted. (REQUIRED)
179
+ # * <tt>:customer_address_id</tt> -- The Shipping Address ID of the shipping address to be deleted. (REQUIRED)
180
+ def delete_customer_shipping_address(options)
181
+ requires!(options, :customer_profile_id)
182
+ requires!(options, :customer_address_id)
183
+
184
+ request = build_request(:delete_customer_shipping_address, options)
185
+ commit(:delete_customer_shipping_address, request)
186
+ end
187
+
188
+ # Retrieves an existing customer profile along with all the associated customer payment profiles and customer shipping addresses.
189
+ #
190
+ # Returns a Response whose params hash contains all the profile information.
191
+ #
192
+ # ==== Options
193
+ #
194
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer to retrieve. (REQUIRED)
195
+ def get_customer_profile(options)
196
+ requires!(options, :customer_profile_id)
197
+
198
+ request = build_request(:get_customer_profile, options)
199
+ commit(:get_customer_profile, request)
200
+ end
201
+
202
+ # Retrieve a customer payment profile for an existing customer profile.
203
+ #
204
+ # Returns a Response whose params hash contains all the payment profile information. Sensitive information such as credit card
205
+ # numbers will be masked.
206
+ #
207
+ # ==== Options
208
+ #
209
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be retrieved. (REQUIRED)
210
+ # * <tt>:customer_payment_profile_id</tt> -- The Payment Profile ID of the payment profile to be retrieved. (REQUIRED)
211
+ def get_customer_payment_profile(options)
212
+ requires!(options, :customer_profile_id)
213
+ requires!(options, :customer_payment_profile_id)
214
+
215
+ request = build_request(:get_customer_payment_profile, options)
216
+ commit(:get_customer_payment_profile, request)
217
+ end
218
+
219
+ # Retrieve a customer shipping address for an existing customer profile.
220
+ #
221
+ # Returns a Response whose params hash contains all the shipping address information.
222
+ #
223
+ # ==== Options
224
+ #
225
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be retrieved. (REQUIRED)
226
+ # * <tt>:customer_address_id</tt> -- The Shipping Address ID of the shipping address to be retrieved. (REQUIRED)
227
+ def get_customer_shipping_address(options)
228
+ requires!(options, :customer_profile_id)
229
+ requires!(options, :customer_address_id)
230
+
231
+ request = build_request(:get_customer_shipping_address, options)
232
+ commit(:get_customer_shipping_address, request)
233
+ end
234
+
235
+ # Updates an existing customer profile.
236
+ #
237
+ # Warning: if you do not provide a parameter in the <tt>:payment_profile</tt> hash, it is automatically set to nil at
238
+ # Authorize.Net. You will most likely want to first get the profile hash using get_customer_profile and then only change the
239
+ # elements you wish to change.
240
+ #
241
+ # ==== Options
242
+ #
243
+ # * <tt>:profile</tt> -- A hash containing the values the Customer Profile should be updated to. (REQUIRED)
244
+ #
245
+ # ==== Profile
246
+ #
247
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer profile to update. (REQUIRED)
248
+ def update_customer_profile(options)
249
+ requires!(options, :profile)
250
+ requires!(options[:profile], :customer_profile_id)
251
+
252
+ request = build_request(:update_customer_profile, options)
253
+ commit(:update_customer_profile, request)
254
+ end
255
+
256
+ # Updates a customer payment profile for an existing customer profile.
257
+ #
258
+ # Warning: if you do not provide a parameter in the <tt>:payment_profile</tt> hash, it is automatically set to nil at
259
+ # Authorize.Net. You will most likely want to first get the profile hash using get_customer_payment_profile and then only
260
+ # change the elements you wish to change.
261
+ #
262
+ # ==== Options
263
+ #
264
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be updated. (REQUIRED)
265
+ # * <tt>:payment_profile</tt> -- A hash containing the values the Customer Payment Profile should be updated to. (REQUIRED)
266
+ #
267
+ # ==== Payment Profile
268
+ #
269
+ # * <tt>:customer_payment_profile_id</tt> -- The Customer Payment Profile ID of the Customer Payment Profile to update. (REQUIRED)
270
+ def update_customer_payment_profile(options)
271
+ requires!(options, :customer_profile_id, :payment_profile)
272
+ requires!(options[:payment_profile], :customer_payment_profile_id)
273
+
274
+ request = build_request(:update_customer_payment_profile, options)
275
+ commit(:update_customer_payment_profile, request)
276
+ end
277
+
278
+ # Updates a customer shipping address for an existing customer profile.
279
+ #
280
+ # Warning: if you do not provide a parameter in the <tt>:address</tt> hash, it is automatically set to nil at
281
+ # Authorize.Net. You will most likely want to first get the profile hash using get_customer_shipping_address and then only
282
+ # change the elements you wish to change.
283
+ #
284
+ # ==== Options
285
+ #
286
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer with the payment profile to be updated. (REQUIRED)
287
+ # * <tt>:address</tt> -- A hash containing the values the Customer Shipping Address should be updated to. (REQUIRED)
288
+ #
289
+ # ==== Address
290
+ #
291
+ # * <tt>:customer_address_id</tt> -- The Customer Address ID of the Customer Payment Profile to update. (REQUIRED)
292
+ def update_customer_shipping_address(options)
293
+ requires!(options, :customer_profile_id, :address)
294
+ requires!(options[:address], :customer_address_id)
295
+
296
+ request = build_request(:update_customer_shipping_address, options)
297
+ commit(:update_customer_shipping_address, request)
298
+ end
299
+
300
+ # Creates a new payment transaction from an existing customer profile
301
+ #
302
+ # This is what is used to charge a customer whose information you have stored in a Customer Profile.
303
+ #
304
+ # Returns a Response object that contains the result of the transaction in <tt>params['direct_response']</tt>
305
+ #
306
+ # ==== Options
307
+ #
308
+ # * <tt>:transaction</tt> -- A hash containing information on the transaction that is being requested. (REQUIRED)
309
+ #
310
+ # ==== Transaction
311
+ #
312
+ # * <tt>:type</tt> -- The type of transaction. Can be either <tt>:auth_only</tt>, <tt>:capture_only</tt>, or <tt>:auth_capture</tt>. (REQUIRED)
313
+ # * <tt>:amount</tt> -- The amount for the tranaction. Formatted with a decimal. For example "4.95" (REQUIRED)
314
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer to use in this transaction. (REQUIRED)
315
+ # * <tt>:customer_payment_profile_id</tt> -- The Customer Payment Profile ID of the Customer Payment Profile to use in this transaction. (REQUIRED)
316
+ def create_customer_profile_transaction(options)
317
+ requires!(options, :transaction)
318
+ requires!(options[:transaction], :type, :amount, :customer_profile_id, :customer_payment_profile_id)
319
+
320
+ request = build_request(:create_customer_profile_transaction, options)
321
+ commit(:create_customer_profile_transaction, request)
322
+ end
323
+
324
+ # Verifies an existing customer payment profile by generating a test transaction
325
+ #
326
+ # Returns a Response object that contains the result of the transaction in <tt>params['direct_response']</tt>
327
+ #
328
+ # ==== Options
329
+ #
330
+ # * <tt>:customer_profile_id</tt> -- The Customer Profile ID of the customer to use in this transaction. (REQUIRED)
331
+ # * <tt>:customer_payment_profile_id</tt> -- The Customer Payment Profile ID of the Customer Payment Profile to be verified. (REQUIRED)
332
+ # * <tt>:customer_address_id</tt> -- The Customer Address ID of the Customer Shipping Address to be verified.
333
+ # * <tt>:validation_mode</tt> -- <tt>:live</tt> or <tt>:test</tt> In Test Mode, only field validation is performed.
334
+ # 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)
335
+ def validate_customer_payment_profile(options)
336
+ requires!(options, :customer_profile_id, :customer_payment_profile_id, :validation_mode)
337
+
338
+ request = build_request(:validate_customer_payment_profile, options)
339
+ commit(:validate_customer_payment_profile, request)
340
+ end
341
+
342
+ private
343
+
344
+ def expdate(credit_card)
345
+ sprintf('%04d-%02d', credit_card.year, credit_card.month)
346
+ end
347
+
348
+ def build_request(action, options = {})
349
+ unless CIM_ACTIONS.include?(action)
350
+ raise StandardError, "Invalid Customer Information Manager Action: #{action}"
351
+ end
352
+
353
+ xml = Builder::XmlMarkup.new(:indent => 2)
354
+ xml.instruct!(:xml, :version => '1.0', :encoding => 'utf-8')
355
+ xml.tag!("#{CIM_ACTIONS[action]}Request", :xmlns => AUTHORIZE_NET_CIM_NAMESPACE) do
356
+ add_merchant_authentication(xml)
357
+ # Merchant-assigned reference ID for the request
358
+ xml.tag!('refId', options[:ref_id]) if options[:ref_id]
359
+ send("build_#{action}_request", xml, options)
360
+ end
361
+ end
362
+
363
+ # Contains the merchant’s payment gateway account authentication information
364
+ def add_merchant_authentication(xml)
365
+ xml.tag!('merchantAuthentication') do
366
+ xml.tag!('name', @options[:login])
367
+ xml.tag!('transactionKey', @options[:password])
368
+ end
369
+ end
370
+
371
+ def build_create_customer_profile_request(xml, options)
372
+ add_profile(xml, options[:profile])
373
+
374
+ xml.target!
375
+ end
376
+
377
+ def build_create_customer_payment_profile_request(xml, options)
378
+ xml.tag!('customerProfileId', options[:customer_profile_id])
379
+
380
+ xml.tag!('paymentProfile') do
381
+ add_payment_profile(xml, options[:payment_profile])
382
+ end
383
+
384
+ xml.tag!('validationMode', CIM_VALIDATION_MODES[options[:validation_mode]]) if options[:validation_mode]
385
+
386
+ xml.target!
387
+ end
388
+
389
+ def build_create_customer_shipping_address_request(xml, options)
390
+ xml.tag!('customerProfileId', options[:customer_profile_id])
391
+
392
+ xml.tag!('address') do
393
+ add_address(xml, options[:address])
394
+ end
395
+
396
+ xml.target!
397
+ end
398
+
399
+ def build_delete_customer_profile_request(xml, options)
400
+ xml.tag!('customerProfileId', options[:customer_profile_id])
401
+ xml.target!
402
+ end
403
+
404
+ def build_delete_customer_payment_profile_request(xml, options)
405
+ xml.tag!('customerProfileId', options[:customer_profile_id])
406
+ xml.tag!('customerPaymentProfileId', options[:customer_payment_profile_id])
407
+ xml.target!
408
+ end
409
+
410
+ def build_delete_customer_shipping_address_request(xml, options)
411
+ xml.tag!('customerProfileId', options[:customer_profile_id])
412
+ xml.tag!('customerAddressId', options[:customer_address_id])
413
+ xml.target!
414
+ end
415
+
416
+ def build_get_customer_profile_request(xml, options)
417
+ xml.tag!('customerProfileId', options[:customer_profile_id])
418
+ xml.target!
419
+ end
420
+
421
+ def build_get_customer_payment_profile_request(xml, options)
422
+ xml.tag!('customerProfileId', options[:customer_profile_id])
423
+ xml.tag!('customerPaymentProfileId', options[:customer_payment_profile_id])
424
+ xml.target!
425
+ end
426
+
427
+ def build_get_customer_shipping_address_request(xml, options)
428
+ xml.tag!('customerProfileId', options[:customer_profile_id])
429
+ xml.tag!('customerAddressId', options[:customer_address_id])
430
+ xml.target!
431
+ end
432
+
433
+ def build_update_customer_profile_request(xml, options)
434
+ add_profile(xml, options[:profile], true)
435
+
436
+ xml.target!
437
+ end
438
+
439
+ def build_update_customer_payment_profile_request(xml, options)
440
+ xml.tag!('customerProfileId', options[:customer_profile_id])
441
+
442
+ xml.tag!('paymentProfile') do
443
+ add_payment_profile(xml, options[:payment_profile])
444
+ end
445
+
446
+ xml.target!
447
+ end
448
+
449
+ def build_update_customer_shipping_address_request(xml, options)
450
+ xml.tag!('customerProfileId', options[:customer_profile_id])
451
+
452
+ xml.tag!('address') do
453
+ add_address(xml, options[:address])
454
+ end
455
+
456
+ xml.target!
457
+ end
458
+
459
+ def build_create_customer_profile_transaction_request(xml, options)
460
+ add_transaction(xml, options[:transaction])
461
+
462
+ xml.target!
463
+ end
464
+
465
+ def build_validate_customer_payment_profile_request(xml, options)
466
+ xml.tag!('customerProfileId', options[:customer_profile_id])
467
+ xml.tag!('customerPaymentProfileId', options[:customer_payment_profile_id])
468
+ xml.tag!('customerShippingAddressId', options[:customer_address_id]) if options[:customer_address_id]
469
+ xml.tag!('validationMode', CIM_VALIDATION_MODES[options[:validation_mode]]) if options[:validation_mode]
470
+
471
+ xml.target!
472
+ end
473
+
474
+ # :merchant_customer_id (Optional)
475
+ # :description (Optional)
476
+ # :email (Optional)
477
+ # :payment_profiles (Optional)
478
+ def add_profile(xml, profile, update = false)
479
+ xml.tag!('profile') do
480
+ # Merchant assigned ID for the customer. Up to 20 characters. (optional)
481
+ xml.tag!('merchantCustomerId', profile[:merchant_customer_id]) if profile[:merchant_customer_id]
482
+ # Description of the customer. Up to 255 Characters (optional)
483
+ xml.tag!('description', profile[:description]) if profile[:description]
484
+ # Email Address for the customer. Up to 255 Characters (optional)
485
+ xml.tag!('email', profile[:email]) if profile[:email]
486
+
487
+ if update
488
+ xml.tag!('customerProfileId', profile[:customer_profile_id])
489
+ else
490
+ add_payment_profiles(xml, profile[:payment_profiles]) if profile[:payment_profiles]
491
+ add_ship_to_list(xml, profile[:ship_to_list]) if profile[:ship_to_list]
492
+ end
493
+ end
494
+ end
495
+
496
+ def add_transaction(xml, transaction)
497
+ unless CIM_TRANSACTION_TYPES.include?(transaction[:type])
498
+ raise StandardError, "Invalid Customer Information Manager Transaction Type: #{transaction[:type]}"
499
+ end
500
+
501
+ xml.tag!('transaction') do
502
+ xml.tag!(CIM_TRANSACTION_TYPES[transaction[:type]]) do
503
+ # The amount to be billed to the customer
504
+ xml.tag!('amount', transaction[:amount])
505
+ xml.tag!('customerProfileId', transaction[:customer_profile_id])
506
+ xml.tag!('customerPaymentProfileId', transaction[:customer_payment_profile_id])
507
+ xml.tag!('approvalCode', transaction[:approval_code]) if transaction[:type] == :capture_only
508
+ add_order(xml, transaction[:order]) if transaction[:order]
509
+ end
510
+ end
511
+ end
512
+
513
+ def add_order(xml, order)
514
+ xml.tag!('order') do
515
+ xml.tag!('invoiceNumber', order[:invoice_number]) if order[:invoice_number]
516
+ xml.tag!('description', order[:description]) if order[:description]
517
+ xml.tag!('purchaseOrderNumber', order[:purchase_order_number]) if order[:purchase_order_number]
518
+ end
519
+ end
520
+
521
+ def add_payment_profiles(xml, payment_profiles)
522
+ xml.tag!('paymentProfiles') do
523
+ add_payment_profile(xml, payment_profiles)
524
+ end
525
+ end
526
+
527
+ # :customer_type => 'individual or business', # Optional
528
+ # :bill_to => @address,
529
+ # :payment => @payment
530
+ def add_payment_profile(xml, payment_profile)
531
+ # 'individual' or 'business' (optional)
532
+ xml.tag!('customerType', payment_profile[:customer_type]) if payment_profile[:customer_type]
533
+
534
+ if payment_profile[:bill_to]
535
+ xml.tag!('billTo') do
536
+ add_address(xml, payment_profile[:bill_to])
537
+ end
538
+ end
539
+
540
+ if payment_profile[:payment]
541
+ xml.tag!('payment') do
542
+ add_credit_card(xml, payment_profile[:payment][:credit_card]) if payment_profile[:payment].has_key?(:credit_card)
543
+ add_bank_account(xml, payment_profile[:payment][:bank_account]) if payment_profile[:payment].has_key?(:bank_account)
544
+ add_drivers_license(xml, payment_profile[:payment][:drivers_license]) if payment_profile[:payment].has_key?(:drivers_license)
545
+ # This element is only required for Wells Fargo SecureSource eCheck.Net merchants
546
+ # The customer's Social Security Number or Tax ID
547
+ xml.tag!('taxId', payment_profile[:payment]) if payment_profile[:payment].has_key?(:tax_id)
548
+ end
549
+ end
550
+
551
+ xml.tag!('customerPaymentProfileId', payment_profile[:customer_payment_profile_id]) if payment_profile[:customer_payment_profile_id]
552
+ end
553
+
554
+ def add_ship_to_list(xml, ship_to_list)
555
+ xml.tag!('shipToList') do
556
+ add_address(xml, ship_to_list)
557
+ end
558
+ end
559
+
560
+ def add_address(xml, address)
561
+ xml.tag!('firstName', address[:first_name])
562
+ xml.tag!('lastName', address[:last_name])
563
+ xml.tag!('company', address[:company])
564
+ xml.tag!('address', address[:address1]) if address[:address1]
565
+ xml.tag!('address', address[:address]) if address[:address]
566
+ xml.tag!('city', address[:city])
567
+ xml.tag!('state', address[:state])
568
+ xml.tag!('zip', address[:zip])
569
+ xml.tag!('country', address[:country])
570
+ xml.tag!('phoneNumber', address[:phone_number]) if address[:phone_number]
571
+ xml.tag!('faxNumber', address[:fax_number]) if address[:fax_number]
572
+
573
+ xml.tag!('customerAddressId', address[:customer_address_id]) if address[:customer_address_id]
574
+ end
575
+
576
+ # Adds customer’s credit card information
577
+ # Note: This element should only be included
578
+ # when the payment method is credit card.
579
+ def add_credit_card(xml, credit_card)
580
+ return unless credit_card
581
+ xml.tag!('creditCard') do
582
+ # The credit card number used for payment of the subscription
583
+ xml.tag!('cardNumber', credit_card.number)
584
+ # The expiration date of the credit card used for the subscription
585
+ xml.tag!('expirationDate', expdate(credit_card))
586
+ end
587
+ end
588
+
589
+ # Adds customer’s bank account information
590
+ # Note: This element should only be included
591
+ # when the payment method is bank account.
592
+ def add_bank_account(xml, bank_account)
593
+ raise StandardError, "Invalid Bank Account Type: #{bank_account[:account_type]}" unless BANK_ACCOUNT_TYPES.include?(bank_account[:account_type])
594
+ raise StandardError, "Invalid eCheck Type: #{bank_account[:echeck_type]}" unless ECHECK_TYPES.include?(bank_account[:echeck_type])
595
+
596
+ xml.tag!('bankAccount') do
597
+ # The type of bank account
598
+ xml.tag!('accountType', BANK_ACCOUNT_TYPES[bank_account[:account_type]])
599
+ # The routing number of the customer’s bank
600
+ xml.tag!('routingNumber', bank_account[:routing_number])
601
+ # The bank account number
602
+ xml.tag!('accountNumber', bank_account[:account_number])
603
+ # The full name of the individual associated
604
+ # with the bank account number
605
+ xml.tag!('nameOnAccount', bank_account[:name_on_account])
606
+ # The type of electronic check transaction
607
+ xml.tag!('echeckType', ECHECK_TYPES[bank_account[:echeck_type]])
608
+ # The full name of the individual associated
609
+ # with the bank account number (optional)
610
+ xml.tag!('bankName', bank_account[:bank_name]) if bank_account[:bank_name]
611
+ end
612
+ end
613
+
614
+ # Adds customer’s driver's license information
615
+ # Note: This element is only required for
616
+ # Wells Fargo SecureSource eCheck.Net merchants
617
+ def add_drivers_license(xml, drivers_license)
618
+ xml.tag!('driversLicense') do
619
+ # The state of the customer's driver's license
620
+ # A valid two character state code
621
+ xml.tag!('state', drivers_license[:state])
622
+ # The customer’s driver's license number
623
+ xml.tag!('number', drivers_license[:number])
624
+ # The date of birth listed on the customer's driver's license
625
+ # YYYY-MM-DD
626
+ xml.tag!('dateOfBirth', drivers_license[:date_of_birth])
627
+ end
628
+ end
629
+
630
+ def commit(action, request)
631
+ url = test? ? test_url : live_url
632
+ xml = ssl_post(url, request, "Content-Type" => "text/xml")
633
+
634
+ response_params = parse(action, xml)
635
+
636
+ message = response_params['messages']['message']['text']
637
+ test_mode = test? || message =~ /Test Mode/
638
+ success = response_params['messages']['result_code'] == 'Ok'
639
+
640
+ response = Response.new(success, message, response_params,
641
+ :test => test_mode,
642
+ :authorization => response_params['customer_profile_id'] || (response_params['profile'] ? response_params['profile']['customer_profile_id'] : nil)
643
+ )
644
+
645
+ response.params['direct_response'] = parse_direct_response(response) if response.params['direct_response']
646
+ response
647
+ end
648
+
649
+ def parse_direct_response(response)
650
+ direct_response = {'raw' => response.params['direct_response']}
651
+ direct_response_fields = response.params['direct_response'].split(',')
652
+
653
+ direct_response.merge(
654
+ {
655
+ 'approval_code' => direct_response_fields[4],
656
+ 'message' => direct_response_fields[3],
657
+ 'transaction_type' => direct_response_fields[11],
658
+ 'amount' => direct_response_fields[9],
659
+ 'invoice_number' => direct_response_fields[7],
660
+ 'order_description' => direct_response_fields[8],
661
+ 'purchase_order_number' => direct_response_fields[36]
662
+ # TODO fill in other fields
663
+ }
664
+ )
665
+ end
666
+
667
+ def parse(action, xml)
668
+ xml = REXML::Document.new(xml)
669
+ root = REXML::XPath.first(xml, "//#{CIM_ACTIONS[action]}Response") ||
670
+ REXML::XPath.first(xml, "//ErrorResponse")
671
+ if root
672
+ response = parse_element(root)
673
+ end
674
+
675
+ response
676
+ end
677
+
678
+ def parse_element(node)
679
+ if node.has_elements?
680
+ response = {}
681
+ node.elements.each{ |e|
682
+ key = e.name.underscore
683
+ value = parse_element(e)
684
+ if response.has_key?(key)
685
+ if response[key].is_a?(Array)
686
+ response[key].push(value)
687
+ else
688
+ response[key] = [response[key], value]
689
+ end
690
+ else
691
+ response[key] = parse_element(e)
692
+ end
693
+ }
694
+ else
695
+ response = node.text
696
+ end
697
+
698
+ response
699
+ end
700
+ end
701
+ end
702
+ end