tlconnor-activemerchant 1.20.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. data/CHANGELOG +805 -0
  2. data/CONTRIBUTORS +274 -0
  3. data/MIT-LICENSE +20 -0
  4. data/gem-public_cert.pem +20 -0
  5. data/lib/active_merchant.rb +63 -0
  6. data/lib/active_merchant/billing.rb +9 -0
  7. data/lib/active_merchant/billing/avs_result.rb +98 -0
  8. data/lib/active_merchant/billing/base.rb +57 -0
  9. data/lib/active_merchant/billing/check.rb +68 -0
  10. data/lib/active_merchant/billing/credit_card.rb +264 -0
  11. data/lib/active_merchant/billing/credit_card_formatting.rb +21 -0
  12. data/lib/active_merchant/billing/credit_card_methods.rb +129 -0
  13. data/lib/active_merchant/billing/cvv_result.rb +38 -0
  14. data/lib/active_merchant/billing/expiry_date.rb +34 -0
  15. data/lib/active_merchant/billing/gateway.rb +170 -0
  16. data/lib/active_merchant/billing/gateways.rb +18 -0
  17. data/lib/active_merchant/billing/gateways/authorize_net.rb +694 -0
  18. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +944 -0
  19. data/lib/active_merchant/billing/gateways/barclays_epdq.rb +308 -0
  20. data/lib/active_merchant/billing/gateways/beanstream.rb +167 -0
  21. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +388 -0
  22. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +54 -0
  23. data/lib/active_merchant/billing/gateways/blue_pay.rb +11 -0
  24. data/lib/active_merchant/billing/gateways/bogus.rb +142 -0
  25. data/lib/active_merchant/billing/gateways/braintree.rb +17 -0
  26. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +9 -0
  27. data/lib/active_merchant/billing/gateways/braintree_blue.rb +308 -0
  28. data/lib/active_merchant/billing/gateways/braintree_orange.rb +21 -0
  29. data/lib/active_merchant/billing/gateways/card_save.rb +23 -0
  30. data/lib/active_merchant/billing/gateways/card_stream.rb +230 -0
  31. data/lib/active_merchant/billing/gateways/certo_direct.rb +279 -0
  32. data/lib/active_merchant/billing/gateways/cyber_source.rb +430 -0
  33. data/lib/active_merchant/billing/gateways/data_cash.rb +597 -0
  34. data/lib/active_merchant/billing/gateways/efsnet.rb +235 -0
  35. data/lib/active_merchant/billing/gateways/elavon.rb +135 -0
  36. data/lib/active_merchant/billing/gateways/epay.rb +274 -0
  37. data/lib/active_merchant/billing/gateways/eway.rb +277 -0
  38. data/lib/active_merchant/billing/gateways/eway_managed.rb +265 -0
  39. data/lib/active_merchant/billing/gateways/exact.rb +227 -0
  40. data/lib/active_merchant/billing/gateways/federated_canada.rb +168 -0
  41. data/lib/active_merchant/billing/gateways/first_pay.rb +177 -0
  42. data/lib/active_merchant/billing/gateways/garanti.rb +262 -0
  43. data/lib/active_merchant/billing/gateways/ideal/ideal_base.rb +250 -0
  44. data/lib/active_merchant/billing/gateways/ideal/ideal_rabobank.pem +13 -0
  45. data/lib/active_merchant/billing/gateways/ideal/ideal_response.rb +29 -0
  46. data/lib/active_merchant/billing/gateways/ideal_rabobank.rb +55 -0
  47. data/lib/active_merchant/billing/gateways/inspire.rb +221 -0
  48. data/lib/active_merchant/billing/gateways/instapay.rb +164 -0
  49. data/lib/active_merchant/billing/gateways/iridium.rb +258 -0
  50. data/lib/active_merchant/billing/gateways/jetpay.rb +276 -0
  51. data/lib/active_merchant/billing/gateways/linkpoint.rb +454 -0
  52. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +157 -0
  53. data/lib/active_merchant/billing/gateways/merchant_ware.rb +289 -0
  54. data/lib/active_merchant/billing/gateways/modern_payments.rb +36 -0
  55. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +220 -0
  56. data/lib/active_merchant/billing/gateways/moneris.rb +239 -0
  57. data/lib/active_merchant/billing/gateways/nab_transact.rb +244 -0
  58. data/lib/active_merchant/billing/gateways/net_registry.rb +189 -0
  59. data/lib/active_merchant/billing/gateways/netaxept.rb +239 -0
  60. data/lib/active_merchant/billing/gateways/netbilling.rb +168 -0
  61. data/lib/active_merchant/billing/gateways/nmi.rb +13 -0
  62. data/lib/active_merchant/billing/gateways/ogone.rb +330 -0
  63. data/lib/active_merchant/billing/gateways/optimal_payment.rb +277 -0
  64. data/lib/active_merchant/billing/gateways/orbital.rb +344 -0
  65. data/lib/active_merchant/billing/gateways/orbital/orbital_soft_descriptors.rb +46 -0
  66. data/lib/active_merchant/billing/gateways/pay_junction.rb +397 -0
  67. data/lib/active_merchant/billing/gateways/pay_secure.rb +120 -0
  68. data/lib/active_merchant/billing/gateways/paybox_direct.rb +207 -0
  69. data/lib/active_merchant/billing/gateways/payflow.rb +261 -0
  70. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +208 -0
  71. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +39 -0
  72. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  73. data/lib/active_merchant/billing/gateways/payflow_express.rb +222 -0
  74. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +15 -0
  75. data/lib/active_merchant/billing/gateways/payflow_uk.rb +21 -0
  76. data/lib/active_merchant/billing/gateways/payment_express.rb +235 -0
  77. data/lib/active_merchant/billing/gateways/paypal.rb +121 -0
  78. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +354 -0
  79. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +49 -0
  80. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  81. data/lib/active_merchant/billing/gateways/paypal_express.rb +229 -0
  82. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +25 -0
  83. data/lib/active_merchant/billing/gateways/paystation.rb +201 -0
  84. data/lib/active_merchant/billing/gateways/plugnpay.rb +298 -0
  85. data/lib/active_merchant/billing/gateways/psigate.rb +219 -0
  86. data/lib/active_merchant/billing/gateways/psl_card.rb +304 -0
  87. data/lib/active_merchant/billing/gateways/qbms.rb +297 -0
  88. data/lib/active_merchant/billing/gateways/quantum.rb +282 -0
  89. data/lib/active_merchant/billing/gateways/quickpay.rb +298 -0
  90. data/lib/active_merchant/billing/gateways/realex.rb +315 -0
  91. data/lib/active_merchant/billing/gateways/sage.rb +146 -0
  92. data/lib/active_merchant/billing/gateways/sage/sage_bankcard.rb +88 -0
  93. data/lib/active_merchant/billing/gateways/sage/sage_core.rb +116 -0
  94. data/lib/active_merchant/billing/gateways/sage/sage_virtual_check.rb +97 -0
  95. data/lib/active_merchant/billing/gateways/sage_pay.rb +320 -0
  96. data/lib/active_merchant/billing/gateways/sallie_mae.rb +144 -0
  97. data/lib/active_merchant/billing/gateways/samurai.rb +121 -0
  98. data/lib/active_merchant/billing/gateways/secure_net.rb +330 -0
  99. data/lib/active_merchant/billing/gateways/secure_pay.rb +31 -0
  100. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +280 -0
  101. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +113 -0
  102. data/lib/active_merchant/billing/gateways/skip_jack.rb +458 -0
  103. data/lib/active_merchant/billing/gateways/smart_ps.rb +271 -0
  104. data/lib/active_merchant/billing/gateways/stripe.rb +244 -0
  105. data/lib/active_merchant/billing/gateways/trans_first.rb +127 -0
  106. data/lib/active_merchant/billing/gateways/transax.rb +25 -0
  107. data/lib/active_merchant/billing/gateways/trust_commerce.rb +423 -0
  108. data/lib/active_merchant/billing/gateways/usa_epay.rb +23 -0
  109. data/lib/active_merchant/billing/gateways/usa_epay_advanced.rb +1496 -0
  110. data/lib/active_merchant/billing/gateways/usa_epay_transaction.rb +206 -0
  111. data/lib/active_merchant/billing/gateways/verifi.rb +233 -0
  112. data/lib/active_merchant/billing/gateways/viaklix.rb +189 -0
  113. data/lib/active_merchant/billing/gateways/wirecard.rb +318 -0
  114. data/lib/active_merchant/billing/gateways/worldpay.rb +280 -0
  115. data/lib/active_merchant/billing/integrations.rb +17 -0
  116. data/lib/active_merchant/billing/integrations/action_view_helper.rb +72 -0
  117. data/lib/active_merchant/billing/integrations/authorize_net_sim.rb +38 -0
  118. data/lib/active_merchant/billing/integrations/authorize_net_sim/helper.rb +228 -0
  119. data/lib/active_merchant/billing/integrations/authorize_net_sim/notification.rb +340 -0
  120. data/lib/active_merchant/billing/integrations/bogus.rb +23 -0
  121. data/lib/active_merchant/billing/integrations/bogus/helper.rb +17 -0
  122. data/lib/active_merchant/billing/integrations/bogus/notification.rb +11 -0
  123. data/lib/active_merchant/billing/integrations/bogus/return.rb +10 -0
  124. data/lib/active_merchant/billing/integrations/chronopay.rb +23 -0
  125. data/lib/active_merchant/billing/integrations/chronopay/helper.rb +120 -0
  126. data/lib/active_merchant/billing/integrations/chronopay/notification.rb +158 -0
  127. data/lib/active_merchant/billing/integrations/chronopay/return.rb +10 -0
  128. data/lib/active_merchant/billing/integrations/direc_pay.rb +41 -0
  129. data/lib/active_merchant/billing/integrations/direc_pay/helper.rb +200 -0
  130. data/lib/active_merchant/billing/integrations/direc_pay/notification.rb +76 -0
  131. data/lib/active_merchant/billing/integrations/direc_pay/return.rb +32 -0
  132. data/lib/active_merchant/billing/integrations/direc_pay/status.rb +37 -0
  133. data/lib/active_merchant/billing/integrations/directebanking.rb +47 -0
  134. data/lib/active_merchant/billing/integrations/directebanking/helper.rb +90 -0
  135. data/lib/active_merchant/billing/integrations/directebanking/notification.rb +120 -0
  136. data/lib/active_merchant/billing/integrations/directebanking/return.rb +11 -0
  137. data/lib/active_merchant/billing/integrations/dwolla.rb +30 -0
  138. data/lib/active_merchant/billing/integrations/dwolla/helper.rb +31 -0
  139. data/lib/active_merchant/billing/integrations/dwolla/notification.rb +55 -0
  140. data/lib/active_merchant/billing/integrations/dwolla/return.rb +38 -0
  141. data/lib/active_merchant/billing/integrations/e_payment_plans.rb +48 -0
  142. data/lib/active_merchant/billing/integrations/e_payment_plans/helper.rb +34 -0
  143. data/lib/active_merchant/billing/integrations/e_payment_plans/notification.rb +84 -0
  144. data/lib/active_merchant/billing/integrations/gestpay.rb +25 -0
  145. data/lib/active_merchant/billing/integrations/gestpay/common.rb +42 -0
  146. data/lib/active_merchant/billing/integrations/gestpay/helper.rb +70 -0
  147. data/lib/active_merchant/billing/integrations/gestpay/notification.rb +85 -0
  148. data/lib/active_merchant/billing/integrations/gestpay/return.rb +10 -0
  149. data/lib/active_merchant/billing/integrations/helper.rb +113 -0
  150. data/lib/active_merchant/billing/integrations/hi_trust.rb +27 -0
  151. data/lib/active_merchant/billing/integrations/hi_trust/helper.rb +58 -0
  152. data/lib/active_merchant/billing/integrations/hi_trust/notification.rb +59 -0
  153. data/lib/active_merchant/billing/integrations/hi_trust/return.rb +67 -0
  154. data/lib/active_merchant/billing/integrations/moneybookers.rb +26 -0
  155. data/lib/active_merchant/billing/integrations/moneybookers/helper.rb +59 -0
  156. data/lib/active_merchant/billing/integrations/moneybookers/notification.rb +129 -0
  157. data/lib/active_merchant/billing/integrations/nochex.rb +88 -0
  158. data/lib/active_merchant/billing/integrations/nochex/helper.rb +68 -0
  159. data/lib/active_merchant/billing/integrations/nochex/notification.rb +94 -0
  160. data/lib/active_merchant/billing/integrations/nochex/return.rb +10 -0
  161. data/lib/active_merchant/billing/integrations/notification.rb +62 -0
  162. data/lib/active_merchant/billing/integrations/payflow_link.rb +21 -0
  163. data/lib/active_merchant/billing/integrations/payflow_link/helper.rb +100 -0
  164. data/lib/active_merchant/billing/integrations/payflow_link/notification.rb +78 -0
  165. data/lib/active_merchant/billing/integrations/paypal.rb +39 -0
  166. data/lib/active_merchant/billing/integrations/paypal/helper.rb +119 -0
  167. data/lib/active_merchant/billing/integrations/paypal/notification.rb +154 -0
  168. data/lib/active_merchant/billing/integrations/paypal/return.rb +10 -0
  169. data/lib/active_merchant/billing/integrations/quickpay.rb +21 -0
  170. data/lib/active_merchant/billing/integrations/quickpay/helper.rb +72 -0
  171. data/lib/active_merchant/billing/integrations/quickpay/notification.rb +74 -0
  172. data/lib/active_merchant/billing/integrations/return.rb +42 -0
  173. data/lib/active_merchant/billing/integrations/sage_pay_form.rb +37 -0
  174. data/lib/active_merchant/billing/integrations/sage_pay_form/encryption.rb +33 -0
  175. data/lib/active_merchant/billing/integrations/sage_pay_form/helper.rb +127 -0
  176. data/lib/active_merchant/billing/integrations/sage_pay_form/notification.rb +210 -0
  177. data/lib/active_merchant/billing/integrations/sage_pay_form/return.rb +31 -0
  178. data/lib/active_merchant/billing/integrations/two_checkout.rb +22 -0
  179. data/lib/active_merchant/billing/integrations/two_checkout/helper.rb +59 -0
  180. data/lib/active_merchant/billing/integrations/two_checkout/notification.rb +114 -0
  181. data/lib/active_merchant/billing/integrations/two_checkout/return.rb +17 -0
  182. data/lib/active_merchant/billing/integrations/valitor.rb +33 -0
  183. data/lib/active_merchant/billing/integrations/valitor/helper.rb +86 -0
  184. data/lib/active_merchant/billing/integrations/valitor/notification.rb +13 -0
  185. data/lib/active_merchant/billing/integrations/valitor/response_fields.rb +97 -0
  186. data/lib/active_merchant/billing/integrations/valitor/return.rb +13 -0
  187. data/lib/active_merchant/billing/integrations/world_pay.rb +27 -0
  188. data/lib/active_merchant/billing/integrations/world_pay/helper.rb +100 -0
  189. data/lib/active_merchant/billing/integrations/world_pay/notification.rb +160 -0
  190. data/lib/active_merchant/billing/response.rb +32 -0
  191. data/lib/active_merchant/version.rb +3 -0
  192. data/lib/activemerchant.rb +1 -0
  193. data/lib/support/gateway_support.rb +58 -0
  194. data/lib/support/outbound_hosts.rb +25 -0
  195. metadata +411 -0
@@ -0,0 +1,142 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ # Bogus Gateway
4
+ class BogusGateway < Gateway
5
+ AUTHORIZATION = '53433'
6
+
7
+ SUCCESS_MESSAGE = "Bogus Gateway: Forced success"
8
+ FAILURE_MESSAGE = "Bogus Gateway: Forced failure"
9
+ ERROR_MESSAGE = "Bogus Gateway: Use CreditCard number 1 for success, 2 for exception and anything else for error"
10
+ CREDIT_ERROR_MESSAGE = "Bogus Gateway: Use CreditCard number 1 for success, 2 for exception and anything else for error"
11
+ UNSTORE_ERROR_MESSAGE = "Bogus Gateway: Use trans_id 1 for success, 2 for exception and anything else for error"
12
+ CAPTURE_ERROR_MESSAGE = "Bogus Gateway: Use authorization number 1 for exception, 2 for error and anything else for success"
13
+ VOID_ERROR_MESSAGE = "Bogus Gateway: Use authorization number 1 for exception, 2 for error and anything else for success"
14
+ REFUND_ERROR_MESSAGE = "Bogus Gateway: Use trans_id number 1 for exception, 2 for error and anything else for success"
15
+
16
+ self.supported_countries = ['US']
17
+ self.supported_cardtypes = [:bogus]
18
+ self.homepage_url = 'http://example.com'
19
+ self.display_name = 'Bogus'
20
+
21
+ def authorize(money, credit_card_or_reference, options = {})
22
+ money = amount(money)
23
+ case normalize(credit_card_or_reference)
24
+ when '1'
25
+ Response.new(true, SUCCESS_MESSAGE, {:authorized_amount => money}, :test => true, :authorization => AUTHORIZATION )
26
+ when '2'
27
+ Response.new(false, FAILURE_MESSAGE, {:authorized_amount => money, :error => FAILURE_MESSAGE }, :test => true)
28
+ else
29
+ raise Error, ERROR_MESSAGE
30
+ end
31
+ end
32
+
33
+ def purchase(money, credit_card_or_reference, options = {})
34
+ money = amount(money)
35
+ case normalize(credit_card_or_reference)
36
+ when '1', AUTHORIZATION
37
+ Response.new(true, SUCCESS_MESSAGE, {:paid_amount => money}, :test => true)
38
+ when '2'
39
+ Response.new(false, FAILURE_MESSAGE, {:paid_amount => money, :error => FAILURE_MESSAGE },:test => true)
40
+ else
41
+ raise Error, ERROR_MESSAGE
42
+ end
43
+ end
44
+
45
+ def recurring(money, credit_card_or_reference, options = {})
46
+ money = amount(money)
47
+ case normalize(credit_card_or_reference)
48
+ when '1'
49
+ Response.new(true, SUCCESS_MESSAGE, {:paid_amount => money}, :test => true)
50
+ when '2'
51
+ Response.new(false, FAILURE_MESSAGE, {:paid_amount => money, :error => FAILURE_MESSAGE },:test => true)
52
+ else
53
+ raise Error, ERROR_MESSAGE
54
+ end
55
+ end
56
+
57
+ def credit(money, credit_card_or_reference, options = {})
58
+ if credit_card_or_reference.is_a?(String)
59
+ deprecated CREDIT_DEPRECATION_MESSAGE
60
+ return refund(money, credit_card_or_reference, options)
61
+ end
62
+
63
+ money = amount(money)
64
+ case normalize(credit_card_or_reference)
65
+ when '1'
66
+ Response.new(true, SUCCESS_MESSAGE, {:paid_amount => money}, :test => true )
67
+ when '2'
68
+ Response.new(false, FAILURE_MESSAGE, {:paid_amount => money, :error => FAILURE_MESSAGE }, :test => true)
69
+ else
70
+ raise Error, CREDIT_ERROR_MESSAGE
71
+ end
72
+ end
73
+
74
+ def refund(money, reference, options = {})
75
+ money = amount(money)
76
+ case reference
77
+ when '1'
78
+ raise Error, REFUND_ERROR_MESSAGE
79
+ when '2'
80
+ Response.new(false, FAILURE_MESSAGE, {:paid_amount => money, :error => FAILURE_MESSAGE }, :test => true)
81
+ else
82
+ Response.new(true, SUCCESS_MESSAGE, {:paid_amount => money}, :test => true)
83
+ end
84
+ end
85
+
86
+ def capture(money, reference, options = {})
87
+ money = amount(money)
88
+ case reference
89
+ when '1'
90
+ raise Error, CAPTURE_ERROR_MESSAGE
91
+ when '2'
92
+ Response.new(false, FAILURE_MESSAGE, {:paid_amount => money, :error => FAILURE_MESSAGE }, :test => true)
93
+ else
94
+ Response.new(true, SUCCESS_MESSAGE, {:paid_amount => money}, :test => true)
95
+ end
96
+ end
97
+
98
+ def void(reference, options = {})
99
+ case reference
100
+ when '1'
101
+ raise Error, VOID_ERROR_MESSAGE
102
+ when '2'
103
+ Response.new(false, FAILURE_MESSAGE, {:authorization => reference, :error => FAILURE_MESSAGE }, :test => true)
104
+ else
105
+ Response.new(true, SUCCESS_MESSAGE, {:authorization => reference}, :test => true)
106
+ end
107
+ end
108
+
109
+ def store(credit_card_or_reference, options = {})
110
+ case normalize(credit_card_or_reference)
111
+ when '1'
112
+ Response.new(true, SUCCESS_MESSAGE, {:billingid => '1'}, :test => true, :authorization => AUTHORIZATION)
113
+ when '2'
114
+ Response.new(false, FAILURE_MESSAGE, {:billingid => nil, :error => FAILURE_MESSAGE }, :test => true)
115
+ else
116
+ raise Error, ERROR_MESSAGE
117
+ end
118
+ end
119
+
120
+ def unstore(reference, options = {})
121
+ case reference
122
+ when '1'
123
+ Response.new(true, SUCCESS_MESSAGE, {}, :test => true)
124
+ when '2'
125
+ Response.new(false, FAILURE_MESSAGE, {:error => FAILURE_MESSAGE },:test => true)
126
+ else
127
+ raise Error, UNSTORE_ERROR_MESSAGE
128
+ end
129
+ end
130
+
131
+ private
132
+
133
+ def normalize(credit_card_or_reference)
134
+ if credit_card_or_reference.respond_to?(:number)
135
+ credit_card_or_reference.number
136
+ else
137
+ credit_card_or_reference.to_s
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/braintree/braintree_common'
2
+
3
+ module ActiveMerchant #:nodoc:
4
+ module Billing #:nodoc:
5
+ class BraintreeGateway < Gateway
6
+ include BraintreeCommon
7
+
8
+ def self.new(options={})
9
+ if options.has_key?(:login)
10
+ BraintreeOrangeGateway.new(options)
11
+ else
12
+ BraintreeBlueGateway.new(options)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module BraintreeCommon
2
+ def self.included(base)
3
+ base.supported_countries = ['US']
4
+ base.supported_cardtypes = [:visa, :master, :american_express, :discover, :jcb, :diners_club]
5
+ base.homepage_url = 'http://www.braintreepaymentsolutions.com'
6
+ base.display_name = 'Braintree'
7
+ base.default_currency = 'USD'
8
+ end
9
+ end
@@ -0,0 +1,308 @@
1
+ require File.dirname(__FILE__) + '/braintree/braintree_common'
2
+
3
+ begin
4
+ require "braintree"
5
+ rescue LoadError
6
+ raise "Could not load the braintree gem. Use `gem install braintree` to install it."
7
+ end
8
+
9
+ raise "Need braintree gem 2.x.y. Run `gem install braintree --version '~>2.0'` to get the correct version." unless Braintree::Version::Major == 2
10
+
11
+ module ActiveMerchant #:nodoc:
12
+ module Billing #:nodoc:
13
+ class BraintreeBlueGateway < Gateway
14
+ include BraintreeCommon
15
+
16
+ self.display_name = 'Braintree (Blue Platform)'
17
+
18
+ def initialize(options = {})
19
+ requires!(options, :merchant_id, :public_key, :private_key)
20
+ @options = options
21
+ @merchant_account_id = options[:merchant_account_id]
22
+ Braintree::Configuration.merchant_id = options[:merchant_id]
23
+ Braintree::Configuration.public_key = options[:public_key]
24
+ Braintree::Configuration.private_key = options[:private_key]
25
+ Braintree::Configuration.environment = (options[:environment] || (test? ? :sandbox : :production)).to_sym
26
+ Braintree::Configuration.logger.level = Logger::ERROR if Braintree::Configuration.logger
27
+ Braintree::Configuration.custom_user_agent = "ActiveMerchant #{ActiveMerchant::VERSION}"
28
+ super
29
+ end
30
+
31
+ def authorize(money, credit_card_or_vault_id, options = {})
32
+ create_transaction(:sale, money, credit_card_or_vault_id, options)
33
+ end
34
+
35
+ def capture(money, authorization, options = {})
36
+ commit do
37
+ result = Braintree::Transaction.submit_for_settlement(authorization, amount(money).to_s)
38
+ Response.new(result.success?, message_from_result(result))
39
+ end
40
+ end
41
+
42
+ def purchase(money, credit_card_or_vault_id, options = {})
43
+ authorize(money, credit_card_or_vault_id, options.merge(:submit_for_settlement => true))
44
+ end
45
+
46
+ def credit(money, credit_card_or_vault_id, options = {})
47
+ create_transaction(:credit, money, credit_card_or_vault_id, options)
48
+ end
49
+
50
+ def refund(*args)
51
+ # legacy signature: #refund(transaction_id, options = {})
52
+ # new signature: #refund(money, transaction_id, options = {})
53
+ money, transaction_id, options = extract_refund_args(args)
54
+ money = amount(money).to_s if money
55
+
56
+ commit do
57
+ result = Braintree::Transaction.refund(transaction_id, money)
58
+ Response.new(result.success?, message_from_result(result),
59
+ {:braintree_transaction => (transaction_hash(result.transaction) if result.success?)},
60
+ {:authorization => (result.transaction.id if result.success?)}
61
+ )
62
+ end
63
+ end
64
+
65
+ def void(authorization, options = {})
66
+ commit do
67
+ result = Braintree::Transaction.void(authorization)
68
+ Response.new(result.success?, message_from_result(result),
69
+ {:braintree_transaction => (transaction_hash(result.transaction) if result.success?)},
70
+ {:authorization => (result.transaction.id if result.success?)}
71
+ )
72
+ end
73
+ end
74
+
75
+ def store(creditcard, options = {})
76
+ commit do
77
+ result = Braintree::Customer.create(
78
+ :first_name => creditcard.first_name,
79
+ :last_name => creditcard.last_name,
80
+ :email => options[:email],
81
+ :credit_card => {
82
+ :number => creditcard.number,
83
+ :cvv => creditcard.verification_value,
84
+ :expiration_month => creditcard.month.to_s.rjust(2, "0"),
85
+ :expiration_year => creditcard.year.to_s
86
+ }
87
+ )
88
+ Response.new(result.success?, message_from_result(result),
89
+ {
90
+ :braintree_customer => (customer_hash(result.customer) if result.success?),
91
+ :customer_vault_id => (result.customer.id if result.success?)
92
+ }
93
+ )
94
+ end
95
+ end
96
+
97
+ def update(vault_id, creditcard, options = {})
98
+ braintree_credit_card = nil
99
+ customer_update_result = commit do
100
+ braintree_credit_card = Braintree::Customer.find(vault_id).credit_cards.detect { |cc| cc.default? }
101
+ return Response.new(false, 'Braintree::NotFoundError') if braintree_credit_card.nil?
102
+ result = Braintree::Customer.update(vault_id,
103
+ :first_name => creditcard.first_name,
104
+ :last_name => creditcard.last_name,
105
+ :email => options[:email]
106
+ )
107
+ Response.new(result.success?, message_from_result(result),
108
+ :braintree_customer => (customer_hash(Braintree::Customer.find(vault_id)) if result.success?)
109
+ )
110
+ end
111
+ return customer_update_result unless customer_update_result.success?
112
+ credit_card_update_result = commit do
113
+ result = Braintree::CreditCard.update(braintree_credit_card.token,
114
+ :number => creditcard.number,
115
+ :expiration_month => creditcard.month.to_s.rjust(2, "0"),
116
+ :expiration_year => creditcard.year.to_s
117
+ )
118
+ Response.new(result.success?, message_from_result(result),
119
+ :braintree_customer => (customer_hash(Braintree::Customer.find(vault_id)) if result.success?)
120
+ )
121
+ end
122
+ end
123
+
124
+ def unstore(customer_vault_id)
125
+ commit do
126
+ Braintree::Customer.delete(customer_vault_id)
127
+ Response.new(true, "OK")
128
+ end
129
+ end
130
+ alias_method :delete, :unstore
131
+
132
+ private
133
+
134
+ def map_address(address)
135
+ return {} if address.nil?
136
+ {
137
+ :street_address => address[:address1],
138
+ :extended_address => address[:address2],
139
+ :company => address[:company],
140
+ :locality => address[:city],
141
+ :region => address[:state],
142
+ :postal_code => address[:zip],
143
+ :country_name => address[:country]
144
+ }
145
+ end
146
+
147
+ def commit(&block)
148
+ yield
149
+ rescue Braintree::BraintreeError => ex
150
+ Response.new(false, ex.class.to_s)
151
+ end
152
+
153
+ def message_from_result(result)
154
+ if result.success?
155
+ "OK"
156
+ else
157
+ result.errors.map { |e| "#{e.message} (#{e.code})" }.join(" ")
158
+ end
159
+ end
160
+
161
+ def create_transaction(transaction_type, money, credit_card_or_vault_id, options)
162
+ transaction_params = create_transaction_parameters(money, credit_card_or_vault_id, options)
163
+
164
+ commit do
165
+ result = Braintree::Transaction.send(transaction_type, transaction_params)
166
+ response_params, response_options, avs_result, cvv_result = {}, {}, {}, {}
167
+ if result.success?
168
+ response_params[:braintree_transaction] = transaction_hash(result.transaction)
169
+ response_params[:customer_vault_id] = result.transaction.customer_details.id
170
+ response_options[:authorization] = result.transaction.id
171
+ end
172
+ if result.transaction
173
+ response_options[:avs_result] = {
174
+ :code => nil, :message => nil,
175
+ :street_match => result.transaction.avs_street_address_response_code,
176
+ :postal_match => result.transaction.avs_postal_code_response_code
177
+ }
178
+ response_options[:cvv_result] = result.transaction.cvv_response_code
179
+ if result.transaction.status == "gateway_rejected"
180
+ message = "Transaction declined - gateway rejected"
181
+ else
182
+ message = "#{result.transaction.processor_response_code} #{result.transaction.processor_response_text}"
183
+ end
184
+ else
185
+ message = message_from_result(result)
186
+ end
187
+ response = Response.new(result.success?, message, response_params, response_options)
188
+ response.cvv_result['message'] = ''
189
+ response
190
+ end
191
+ end
192
+
193
+ def extract_refund_args(args)
194
+ options = args.extract_options!
195
+
196
+ # money, transaction_id, options
197
+ if args.length == 1 # legacy signature
198
+ return nil, args[0], options
199
+ elsif args.length == 2
200
+ return args[0], args[1], options
201
+ else
202
+ raise ArgumentError, "wrong number of arguments (#{args.length} for 2)"
203
+ end
204
+ end
205
+
206
+ def customer_hash(customer)
207
+ credit_cards = customer.credit_cards.map do |cc|
208
+ {
209
+ "bin" => cc.bin,
210
+ "expiration_date" => cc.expiration_date
211
+ }
212
+ end
213
+
214
+ {
215
+ "email" => customer.email,
216
+ "first_name" => customer.first_name,
217
+ "last_name" => customer.last_name,
218
+ "credit_cards" => credit_cards
219
+ }
220
+ end
221
+
222
+ def transaction_hash(transaction)
223
+ if transaction.vault_customer
224
+ vault_customer = {
225
+ }
226
+ vault_customer["credit_cards"] = transaction.vault_customer.credit_cards.map do |cc|
227
+ {
228
+ "bin" => cc.bin
229
+ }
230
+ end
231
+ else
232
+ vault_customer = nil
233
+ end
234
+
235
+ customer_details = {
236
+ "id" => transaction.customer_details.id,
237
+ "email" => transaction.customer_details.email
238
+ }
239
+
240
+ billing_details = {
241
+ "street_address" => transaction.billing_details.street_address,
242
+ "extended_address" => transaction.billing_details.extended_address,
243
+ "company" => transaction.billing_details.company,
244
+ "locality" => transaction.billing_details.locality,
245
+ "region" => transaction.billing_details.region,
246
+ "postal_code" => transaction.billing_details.postal_code,
247
+ "country_name" => transaction.billing_details.country_name,
248
+ }
249
+
250
+ shipping_details = {
251
+ "street_address" => transaction.shipping_details.street_address,
252
+ "extended_address" => transaction.shipping_details.extended_address,
253
+ "company" => transaction.shipping_details.company,
254
+ "locality" => transaction.shipping_details.locality,
255
+ "region" => transaction.shipping_details.region,
256
+ "postal_code" => transaction.shipping_details.postal_code,
257
+ "country_name" => transaction.shipping_details.country_name,
258
+ }
259
+
260
+ {
261
+ "order_id" => transaction.order_id,
262
+ "status" => transaction.status,
263
+ "customer_details" => customer_details,
264
+ "billing_details" => billing_details,
265
+ "shipping_details" => shipping_details,
266
+ "vault_customer" => vault_customer,
267
+ "merchant_account_id" => transaction.merchant_account_id
268
+ }
269
+ end
270
+
271
+ def create_transaction_parameters(money, credit_card_or_vault_id, options)
272
+ parameters = {
273
+ :amount => amount(money).to_s,
274
+ :order_id => options[:order_id],
275
+ :customer => {
276
+ :id => options[:store] == true ? "" : options[:store],
277
+ :email => options[:email]
278
+ },
279
+ :options => {
280
+ :store_in_vault => options[:store] ? true : false,
281
+ :submit_for_settlement => options[:submit_for_settlement]
282
+ }
283
+ }
284
+ if merchant_account_id = (options[:merchant_account_id] || @merchant_account_id)
285
+ parameters[:merchant_account_id] = merchant_account_id
286
+ end
287
+ if credit_card_or_vault_id.is_a?(String) || credit_card_or_vault_id.is_a?(Integer)
288
+ parameters[:customer_id] = credit_card_or_vault_id
289
+ else
290
+ parameters[:customer].merge!(
291
+ :first_name => credit_card_or_vault_id.first_name,
292
+ :last_name => credit_card_or_vault_id.last_name
293
+ )
294
+ parameters[:credit_card] = {
295
+ :number => credit_card_or_vault_id.number,
296
+ :cvv => credit_card_or_vault_id.verification_value,
297
+ :expiration_month => credit_card_or_vault_id.month.to_s.rjust(2, "0"),
298
+ :expiration_year => credit_card_or_vault_id.year.to_s
299
+ }
300
+ end
301
+ parameters[:billing] = map_address(options[:billing_address]) if options[:billing_address]
302
+ parameters[:shipping] = map_address(options[:shipping_address]) if options[:shipping_address]
303
+ parameters
304
+ end
305
+ end
306
+ end
307
+ end
308
+