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,143 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ class CommercegateGateway < Gateway
4
+ self.test_url = self.live_url = 'https://secure.commercegate.com/gateway/nvp'
5
+
6
+ self.supported_countries = %w(
7
+ AD AT AX BE BG CH CY CZ DE DK ES FI FR GB GG
8
+ GI GR HR HU IE IM IS IT JE LI LT LU LV MC MT
9
+ NL NO PL PT RO SE SI SK VA
10
+ )
11
+
12
+ self.money_format = :dollars
13
+ self.default_currency = 'EUR'
14
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover]
15
+ self.homepage_url = 'http://www.commercegate.com/'
16
+ self.display_name = 'CommerceGate'
17
+
18
+ def initialize(options = {})
19
+ requires!(options, :login, :password, :site_id, :offer_id)
20
+ super
21
+ end
22
+
23
+ def authorize(money, creditcard, options = {})
24
+ post = {}
25
+ add_creditcard(post, creditcard)
26
+ add_auth_purchase_options(post, money, options)
27
+ commit('AUTH', post)
28
+ end
29
+
30
+ def capture(money, authorization, options = {})
31
+ post = {}
32
+ post[:currencyCode] = (options[:currency] || currency(money))
33
+ post[:amount] = amount(money)
34
+ post[:transID] = authorization
35
+ commit('CAPTURE', post)
36
+ end
37
+
38
+ def purchase(money, creditcard, options = {})
39
+ post = {}
40
+ add_creditcard(post, creditcard)
41
+ add_auth_purchase_options(post, money, options)
42
+ commit('SALE', post)
43
+ end
44
+
45
+ def refund(money, identification, options = {})
46
+ post = {}
47
+ post[:currencyCode] = options[:currency] || currency(money)
48
+ post[:amount] = amount(money)
49
+ post[:transID] = identification
50
+ commit('REFUND', post)
51
+ end
52
+
53
+ def void(identification, options = {})
54
+ post = {}
55
+ post[:transID] = identification
56
+ commit('VOID_AUTH', post)
57
+ end
58
+
59
+ private
60
+
61
+ def add_address(post, address)
62
+ if address
63
+ post[:address] = address[:address1]
64
+ post[:city] = address[:city]
65
+ post[:state] = address[:state]
66
+ post[:postalCode] = address[:zip]
67
+ end
68
+ post[:countryCode] = ((address && address[:country]) || "US")
69
+ end
70
+
71
+ def add_auth_purchase_options(post, money, options)
72
+ add_address(post, options[:address])
73
+
74
+ post[:customerIP] = options[:ip] || "127.0.0.1"
75
+ post[:amount] = amount(money)
76
+ post[:email] = options[:email] || "unknown@example.com"
77
+ post[:currencyCode]= options[:currency] || currency(money)
78
+ post[:merchAcct] = options[:merchant]
79
+
80
+ end
81
+
82
+ def add_creditcard(params, creditcard)
83
+ params[:firstName] = creditcard.first_name
84
+ params[:lastName] = creditcard.last_name
85
+ params[:cardNumber] = creditcard.number
86
+ params[:expiryMonth] = creditcard.month
87
+ params[:expiryYear] = creditcard.year
88
+ params[:cvv] = creditcard.verification_value if creditcard.verification_value?
89
+ end
90
+
91
+ def commit(action, parameters)
92
+ parameters[:apiUsername] = @options[:login]
93
+ parameters[:apiPassword] = @options[:password]
94
+ parameters[:siteID] = @options[:site_id]
95
+ parameters[:offerID] = @options[:offer_id]
96
+ parameters[:action] = action
97
+
98
+ response = parse(ssl_post(self.live_url, post_data(parameters)))
99
+
100
+ Response.new(
101
+ successful?(response),
102
+ message_from(response),
103
+ response,
104
+ authorization: response['transID'],
105
+ test: test?,
106
+ avs_result: {code: response['avsCode']},
107
+ cvv_result: response['cvvCode']
108
+ )
109
+ end
110
+
111
+ def parse(body)
112
+ results = {}
113
+
114
+ body.split(/\&/).each do |pair|
115
+ key,val = pair.split(%r{=})
116
+ results[key] = CGI.unescape(val)
117
+ end
118
+
119
+ results
120
+ end
121
+
122
+ def successful?(response)
123
+ response['returnCode'] == '0'
124
+ end
125
+
126
+ def message_from(response)
127
+ if response['returnText'].present?
128
+ response['returnText']
129
+ else
130
+ "Invalid response received from the CommerceGate API. " +
131
+ "Please contact CommerceGate support if you continue to receive this message. " +
132
+ "(The raw response returned by the API was #{response.inspect})"
133
+ end
134
+ end
135
+
136
+ def post_data(parameters)
137
+ parameters.collect do |key, value|
138
+ "#{key}=#{CGI.escape(value.to_s)}"
139
+ end.join("&")
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,209 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ class ConektaGateway < Gateway
4
+ self.live_url = 'https://api.conekta.io/'
5
+
6
+ self.supported_countries = ['MX']
7
+ self.supported_cardtypes = [:visa, :master, :american_express]
8
+ self.homepage_url = 'https://conekta.io/'
9
+ self.display_name = 'Conekta Gateway'
10
+ self.money_format = :cents
11
+ self.default_currency = 'MXN'
12
+
13
+ def initialize(options = {})
14
+ requires!(options, :key)
15
+ options[:version] ||= '0.3.0'
16
+ super
17
+ end
18
+
19
+ def purchase(money, payment_source, options = {})
20
+ post = {}
21
+
22
+ add_order(post, money, options)
23
+ add_payment_source(post, payment_source, options)
24
+ add_details_data(post, options)
25
+
26
+ commit(:post, 'charges', post)
27
+ end
28
+
29
+ def authorize(money, payment_source, options = {})
30
+ post = {}
31
+
32
+ add_order(post, money, options)
33
+ add_payment_source(post, payment_source, options)
34
+ add_details_data(post, options)
35
+
36
+ post[:capture] = false
37
+ commit(:post, "charges", post)
38
+ end
39
+
40
+ def capture(money, identifier, options = {})
41
+ post = {}
42
+
43
+ post[:order_id] = identifier
44
+ add_order(post, money, options)
45
+
46
+ commit(:post, "charges/#{identifier}/capture", post)
47
+ end
48
+
49
+ def refund(money, identifier, options)
50
+ post = {}
51
+
52
+ post[:order_id] = identifier
53
+ add_order(post, money, options)
54
+
55
+ commit(:post, "charges/#{identifier}/refund", post)
56
+ end
57
+
58
+ private
59
+
60
+ def add_order(post, money, options)
61
+ post[:description] = options[:description] || "Active Merchant Purchase"
62
+ post[:reference_id] = options[:order_id] if options[:order_id]
63
+ post[:currency] = (options[:currency] || currency(money)).downcase
64
+ post[:amount] = amount(money)
65
+ end
66
+
67
+ def add_details_data(post, options)
68
+ details = {}
69
+ details[:name] = options[:customer] if options[:customer]
70
+ details[:email] = options[:email] if options[:email]
71
+ details[:phone] = options[:phone] if options[:phone]
72
+ details[:device_fingerprint] = options[:device_fingerprint] if options[:device_fingerprint]
73
+ details[:ip] = options[:ip] if options[:ip]
74
+ add_billing_address(details, options)
75
+ add_line_items(details, options)
76
+ add_shipment(details, options)
77
+
78
+ post[:details] = details
79
+ end
80
+
81
+ def add_shipment(post, options)
82
+ shipment = {}
83
+ shipment[:carrier] = options[:carrier] if options[:carrier]
84
+ shipment[:service] = options[:service] if options[:service]
85
+ shipment[:tracking_number] = options[:tracking_number] if options[:tracking_number]
86
+ shipment[:price] = options[:price] if options[:price]
87
+ add_shipment_address(shipment, options)
88
+ post[:shipment] = shipment
89
+ end
90
+
91
+ def add_shipment_address(post, options)
92
+ if(address = options[:shipping_address])
93
+ post[:address] = {}
94
+ post[:address][:street1] = address[:address1] if address[:address1]
95
+ post[:address][:street2] = address[:address2] if address[:address2]
96
+ post[:address][:street3] = address[:address3] if address[:address3]
97
+ post[:address][:city] = address[:city] if address[:city]
98
+ post[:address][:state] = address[:state] if address[:state]
99
+ post[:address][:country] = address[:country] if address[:country]
100
+ post[:address][:zip] = address[:zip] if address[:zip]
101
+ end
102
+ end
103
+
104
+ def add_line_items(post, options)
105
+ post[:line_items] = (options[:line_items] || []).collect do |line_item|
106
+ line_item
107
+ end
108
+ end
109
+
110
+ def add_billing_address(post, options)
111
+ if(address = (options[:billing_address] || options[:address]))
112
+ post[:billing_address] = {}
113
+ post[:billing_address][:street1] = address[:address1] if address[:address1]
114
+ post[:billing_address][:street2] = address[:address2] if address[:address2]
115
+ post[:billing_address][:street3] = address[:address3] if address[:address3]
116
+ post[:billing_address][:city] = address[:city] if address[:city]
117
+ post[:billing_address][:state] = address[:state] if address[:state]
118
+ post[:billing_address][:country] = address[:country] if address[:country]
119
+ post[:billing_address][:zip] = address[:zip] if address[:zip]
120
+ post[:billing_address][:company_name] = address[:company_name] if address[:company_name]
121
+ post[:billing_address][:tax_id] = address[:tax_id] if address[:tax_id]
122
+ post[:billing_address][:name] = address[:name] if address[:name]
123
+ post[:billing_address][:phone] = address[:phone] if address[:phone]
124
+ post[:billing_address][:email] = address[:email] if address[:email]
125
+ end
126
+ end
127
+
128
+ def add_address(post, options)
129
+ if(address = (options[:billing_address] || options[:address]))
130
+ post[:address] = {}
131
+ post[:address][:street1] = address[:address1] if address[:address1]
132
+ post[:address][:street2] = address[:address2] if address[:address2]
133
+ post[:address][:street3] = address[:address3] if address[:address3]
134
+ post[:address][:city] = address[:city] if address[:city]
135
+ post[:address][:state] = address[:state] if address[:state]
136
+ post[:address][:country] = address[:country] if address[:country]
137
+ post[:address][:zip] = address[:zip] if address[:zip]
138
+ end
139
+ end
140
+
141
+ def add_payment_source(post, payment_source, options)
142
+ if payment_source.kind_of?(String)
143
+ post[:card] = payment_source
144
+ elsif payment_source.respond_to?(:number)
145
+ post[:card] = {}
146
+ post[:card][:name] = payment_source.name
147
+ post[:card][:cvc] = payment_source.verification_value
148
+ post[:card][:number] = payment_source.number
149
+ post[:card][:exp_month] = "#{sprintf("%02d", payment_source.month)}"
150
+ post[:card][:exp_year] = "#{"#{payment_source.year}"[-2, 2]}"
151
+ add_address(post[:card], options)
152
+ end
153
+ end
154
+
155
+ def parse(body)
156
+ return {} unless body
157
+ JSON.parse(body)
158
+ end
159
+
160
+ def headers(meta)
161
+ {
162
+ "Accept" => "application/vnd.conekta-v#{options[:version]}+json",
163
+ "Authorization" => "Basic " + Base64.encode64("#{options[:key]}:"),
164
+ "RaiseHtmlError" => "false",
165
+ "Conekta-Client-User-Agent" => {"agent"=>"Conekta ActiveMerchantBindings/#{ActiveMerchant::VERSION}"}.to_json,
166
+ "X-Conekta-Client-User-Agent" => user_agent,
167
+ "X-Conekta-Client-User-Metadata" => meta.to_json
168
+ }
169
+ end
170
+
171
+ def commit(method, url, parameters, options = {})
172
+ success = false
173
+ begin
174
+ raw_response = parse(ssl_request(method, live_url + url, (parameters ? parameters.to_query : nil), headers(options[:meta])))
175
+ success = (raw_response.key?("object") && (raw_response["object"] != "error"))
176
+ rescue ResponseError => e
177
+ raw_response = response_error(e.response.body)
178
+ rescue JSON::ParserError
179
+ raw_response = json_error(raw_response)
180
+ end
181
+
182
+ Response.new(
183
+ success,
184
+ raw_response["message"],
185
+ raw_response,
186
+ test: test?,
187
+ authorization: raw_response["id"]
188
+ )
189
+ end
190
+
191
+ def response_error(raw_response)
192
+ begin
193
+ parse(raw_response)
194
+ rescue JSON::ParserError
195
+ json_error(raw_response)
196
+ end
197
+ end
198
+
199
+ def json_error(raw_response)
200
+ msg = 'Invalid response received from the Conekta API.'
201
+ msg += " (The raw response returned by the API was #{raw_response.inspect})"
202
+ {
203
+ "message" => msg
204
+ }
205
+ end
206
+ end
207
+ end
208
+ end
209
+
@@ -0,0 +1,709 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ # See the remote and mocked unit test files for example usage. Pay special
4
+ # attention to the contents of the options hash.
5
+ #
6
+ # Initial setup instructions can be found in
7
+ # http://cybersource.com/support_center/implementation/downloads/soap_api/SOAP_toolkits.pdf
8
+ #
9
+ # Debugging
10
+ # If you experience an issue with this gateway be sure to examine the
11
+ # transaction information from a general transaction search inside the
12
+ # CyberSource Business Center for the full error messages including field
13
+ # names.
14
+ #
15
+ # Important Notes
16
+ # * For checks you can purchase and store.
17
+ # * AVS and CVV only work against the production server. You will always
18
+ # get back X for AVS and no response for CVV against the test server.
19
+ # * Nexus is the list of states or provinces where you have a physical
20
+ # presence. Nexus is used to calculate tax. Leave blank to tax everyone.
21
+ # * If you want to calculate VAT for overseas customers you must supply a
22
+ # registration number in the options hash as vat_reg_number.
23
+ # * productCode is a value in the line_items hash that is used to tell
24
+ # CyberSource what kind of item you are selling. It is used when
25
+ # calculating tax/VAT.
26
+ # * All transactions use dollar values.
27
+ # * To process pinless debit cards through the pinless debit card
28
+ # network, your Cybersource merchant account must accept pinless
29
+ # debit card payments.
30
+ class CyberSourceGateway < Gateway
31
+ self.test_url = 'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor'
32
+ self.live_url = 'https://ics2ws.ic3.com/commerce/1.x/transactionProcessor'
33
+
34
+ XSD_VERSION = "1.104"
35
+
36
+ # visa, master, american_express, discover
37
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover]
38
+ self.supported_countries = %w(US BR CA CN DK FI FR DE JP MX NO SE GB SG)
39
+ self.default_currency = 'USD'
40
+ self.homepage_url = 'http://www.cybersource.com'
41
+ self.display_name = 'CyberSource'
42
+
43
+ # map credit card to the CyberSource expected representation
44
+ @@credit_card_codes = {
45
+ :visa => '001',
46
+ :master => '002',
47
+ :american_express => '003',
48
+ :discover => '004'
49
+ }
50
+
51
+ # map response codes to something humans can read
52
+ @@response_codes = {
53
+ :r100 => "Successful transaction",
54
+ :r101 => "Request is missing one or more required fields" ,
55
+ :r102 => "One or more fields contains invalid data",
56
+ :r150 => "General failure",
57
+ :r151 => "The request was received but a server time-out occurred",
58
+ :r152 => "The request was received, but a service timed out",
59
+ :r200 => "The authorization request was approved by the issuing bank but declined by CyberSource because it did not pass the AVS check",
60
+ :r201 => "The issuing bank has questions about the request",
61
+ :r202 => "Expired card",
62
+ :r203 => "General decline of the card",
63
+ :r204 => "Insufficient funds in the account",
64
+ :r205 => "Stolen or lost card",
65
+ :r207 => "Issuing bank unavailable",
66
+ :r208 => "Inactive card or card not authorized for card-not-present transactions",
67
+ :r209 => "American Express Card Identifiction Digits (CID) did not match",
68
+ :r210 => "The card has reached the credit limit",
69
+ :r211 => "Invalid card verification number",
70
+ :r221 => "The customer matched an entry on the processor's negative file",
71
+ :r230 => "The authorization request was approved by the issuing bank but declined by CyberSource because it did not pass the card verification check",
72
+ :r231 => "Invalid account number",
73
+ :r232 => "The card type is not accepted by the payment processor",
74
+ :r233 => "General decline by the processor",
75
+ :r234 => "A problem exists with your CyberSource merchant configuration",
76
+ :r235 => "The requested amount exceeds the originally authorized amount",
77
+ :r236 => "Processor failure",
78
+ :r237 => "The authorization has already been reversed",
79
+ :r238 => "The authorization has already been captured",
80
+ :r239 => "The requested transaction amount must match the previous transaction amount",
81
+ :r240 => "The card type sent is invalid or does not correlate with the credit card number",
82
+ :r241 => "The request ID is invalid",
83
+ :r242 => "You requested a capture, but there is no corresponding, unused authorization record.",
84
+ :r243 => "The transaction has already been settled or reversed",
85
+ :r244 => "The bank account number failed the validation check",
86
+ :r246 => "The capture or credit is not voidable because the capture or credit information has already been submitted to your processor",
87
+ :r247 => "You requested a credit for a capture that was previously voided",
88
+ :r250 => "The request was received, but a time-out occurred with the payment processor",
89
+ :r254 => "Your CyberSource account is prohibited from processing stand-alone refunds",
90
+ :r255 => "Your CyberSource account is not configured to process the service in the country you specified"
91
+ }
92
+
93
+ # These are the options that can be used when creating a new CyberSource
94
+ # Gateway object.
95
+ #
96
+ # :login => your username
97
+ #
98
+ # :password => the transaction key you generated in the Business Center
99
+ #
100
+ # :test => true sets the gateway to test mode
101
+ #
102
+ # :vat_reg_number => your VAT registration number
103
+ #
104
+ # :nexus => "WI CA QC" sets the states/provinces where you have a physical
105
+ # presence for tax purposes
106
+ #
107
+ # :ignore_avs => true don't want to use AVS so continue processing even
108
+ # if AVS would have failed
109
+ #
110
+ # :ignore_cvv => true don't want to use CVV so continue processing even
111
+ # if CVV would have failed
112
+ def initialize(options = {})
113
+ requires!(options, :login, :password)
114
+ super
115
+ end
116
+
117
+ # Request an authorization for an amount from CyberSource
118
+ #
119
+ # You must supply an :order_id in the options hash
120
+ def authorize(money, creditcard_or_reference, options = {})
121
+ requires!(options, :order_id)
122
+ setup_address_hash(options)
123
+ commit(build_auth_request(money, creditcard_or_reference, options), options )
124
+ end
125
+
126
+ def auth_reversal(money, identification, options = {})
127
+ commit(build_auth_reversal_request(money, identification, options), options)
128
+ end
129
+
130
+ # Capture an authorization that has previously been requested
131
+ def capture(money, authorization, options = {})
132
+ setup_address_hash(options)
133
+ commit(build_capture_request(money, authorization, options), options)
134
+ end
135
+
136
+ # Purchase is an auth followed by a capture
137
+ # You must supply an order_id in the options hash
138
+ # options[:pinless_debit_card] => true # attempts to process as pinless debit card
139
+ def purchase(money, payment_method_or_reference, options = {})
140
+ requires!(options, :order_id)
141
+ setup_address_hash(options)
142
+ commit(build_purchase_request(money, payment_method_or_reference, options), options)
143
+ end
144
+
145
+ def void(identification, options = {})
146
+ commit(build_void_request(identification, options), options)
147
+ end
148
+
149
+ def refund(money, identification, options = {})
150
+ commit(build_refund_request(money, identification, options), options)
151
+ end
152
+
153
+ # Adds credit to a subscription (stand alone credit).
154
+ def credit(money, reference, options = {})
155
+ requires!(options, :order_id)
156
+ commit(build_credit_request(money, reference, options), options)
157
+ end
158
+
159
+ # Stores a customer subscription/profile with type "on-demand".
160
+ # To charge the card while creating a profile, pass
161
+ # options[:setup_fee] => money
162
+ def store(payment_method, options = {})
163
+ requires!(options, :order_id)
164
+ setup_address_hash(options)
165
+ commit(build_create_subscription_request(payment_method, options), options)
166
+ end
167
+
168
+ # Updates a customer subscription/profile
169
+ def update(reference, creditcard, options = {})
170
+ requires!(options, :order_id)
171
+ setup_address_hash(options)
172
+ commit(build_update_subscription_request(reference, creditcard, options), options)
173
+ end
174
+
175
+ # Removes a customer subscription/profile
176
+ def unstore(reference, options = {})
177
+ requires!(options, :order_id)
178
+ commit(build_delete_subscription_request(reference, options), options)
179
+ end
180
+
181
+ # Retrieves a customer subscription/profile
182
+ def retrieve(reference, options = {})
183
+ requires!(options, :order_id)
184
+ commit(build_retrieve_subscription_request(reference, options), options)
185
+ end
186
+
187
+ # CyberSource requires that you provide line item information for tax
188
+ # calculations. If you do not have prices for each item or want to
189
+ # simplify the situation then pass in one fake line item that costs the
190
+ # subtotal of the order
191
+ #
192
+ # The line_item hash goes in the options hash and should look like
193
+ #
194
+ # :line_items => [
195
+ # {
196
+ # :declared_value => '1',
197
+ # :quantity => '2',
198
+ # :code => 'default',
199
+ # :description => 'Giant Walrus',
200
+ # :sku => 'WA323232323232323'
201
+ # },
202
+ # {
203
+ # :declared_value => '6',
204
+ # :quantity => '1',
205
+ # :code => 'default',
206
+ # :description => 'Marble Snowcone',
207
+ # :sku => 'FAKE1232132113123'
208
+ # }
209
+ # ]
210
+ #
211
+ # This functionality is only supported by this particular gateway may
212
+ # be changed at any time
213
+ def calculate_tax(creditcard, options)
214
+ requires!(options, :line_items)
215
+ setup_address_hash(options)
216
+ commit(build_tax_calculation_request(creditcard, options), options)
217
+ end
218
+
219
+ # Determines if a card can be used for Pinless Debit Card transactions
220
+ def validate_pinless_debit_card(creditcard, options = {})
221
+ requires!(options, :order_id)
222
+ commit(build_validate_pinless_debit_request(creditcard,options), options)
223
+ end
224
+
225
+ private
226
+
227
+ # Create all address hash key value pairs so that we still function if we
228
+ # were only provided with one or two of them
229
+ def setup_address_hash(options)
230
+ options[:billing_address] = options[:billing_address] || options[:address] || {}
231
+ options[:shipping_address] = options[:shipping_address] || {}
232
+ end
233
+
234
+ def build_auth_request(money, creditcard_or_reference, options)
235
+ xml = Builder::XmlMarkup.new :indent => 2
236
+ add_payment_method_or_subscription(xml, money, creditcard_or_reference, options)
237
+ add_auth_service(xml, creditcard_or_reference, options)
238
+ add_business_rules_data(xml)
239
+ add_network_token(xml, creditcard_or_reference, options) if token_based_card?(creditcard_or_reference, options)
240
+ xml.target!
241
+ end
242
+
243
+ def build_tax_calculation_request(creditcard, options)
244
+ xml = Builder::XmlMarkup.new :indent => 2
245
+ add_address(xml, creditcard, options[:billing_address], options, false)
246
+ add_address(xml, creditcard, options[:shipping_address], options, true)
247
+ add_line_item_data(xml, options)
248
+ add_purchase_data(xml, 0, false, options)
249
+ add_tax_service(xml)
250
+ add_business_rules_data(xml)
251
+ xml.target!
252
+ end
253
+
254
+ def build_capture_request(money, authorization, options)
255
+ order_id, request_id, request_token = authorization.split(";")
256
+ options[:order_id] = order_id
257
+
258
+ xml = Builder::XmlMarkup.new :indent => 2
259
+ add_purchase_data(xml, money, true, options)
260
+ add_capture_service(xml, request_id, request_token)
261
+ add_business_rules_data(xml)
262
+ xml.target!
263
+ end
264
+
265
+ def build_purchase_request(money, payment_method_or_reference, options)
266
+ xml = Builder::XmlMarkup.new :indent => 2
267
+ add_payment_method_or_subscription(xml, money, payment_method_or_reference, options)
268
+ if !payment_method_or_reference.is_a?(String) && card_brand(payment_method_or_reference) == 'check'
269
+ add_check_service(xml)
270
+ else
271
+ add_purchase_service(xml, payment_method_or_reference, options)
272
+ add_business_rules_data(xml) unless options[:pinless_debit_card]
273
+ add_network_token(xml, payment_method_or_reference, options) if token_based_card?(payment_method_or_reference, options)
274
+ end
275
+ xml.target!
276
+ end
277
+
278
+ def build_void_request(identification, options)
279
+ order_id, request_id, request_token = identification.split(";")
280
+ options[:order_id] = order_id
281
+
282
+ xml = Builder::XmlMarkup.new :indent => 2
283
+ add_void_service(xml, request_id, request_token)
284
+ xml.target!
285
+ end
286
+
287
+ def build_auth_reversal_request(money, identification, options)
288
+ order_id, request_id, request_token = identification.split(";")
289
+ options[:order_id] = order_id
290
+ xml = Builder::XmlMarkup.new :indent => 2
291
+ add_purchase_data(xml, money, true, options)
292
+ add_auth_reversal_service(xml, request_id, request_token)
293
+ xml.target!
294
+ end
295
+
296
+ def build_refund_request(money, identification, options)
297
+ order_id, request_id, request_token = identification.split(";")
298
+ options[:order_id] = order_id
299
+
300
+ xml = Builder::XmlMarkup.new :indent => 2
301
+ add_purchase_data(xml, money, true, options)
302
+ add_credit_service(xml, request_id, request_token)
303
+
304
+ xml.target!
305
+ end
306
+
307
+ def build_credit_request(money, reference, options)
308
+ xml = Builder::XmlMarkup.new :indent => 2
309
+
310
+ add_purchase_data(xml, money, true, options)
311
+ add_subscription(xml, options, reference)
312
+ add_credit_service(xml)
313
+
314
+ xml.target!
315
+ end
316
+
317
+ def build_create_subscription_request(payment_method, options)
318
+ default_subscription_params = {:frequency => "on-demand", :amount => 0, :automatic_renew => false}
319
+ options[:subscription] = default_subscription_params.update(
320
+ options[:subscription] || {}
321
+ )
322
+
323
+ xml = Builder::XmlMarkup.new :indent => 2
324
+ add_address(xml, payment_method, options[:billing_address], options)
325
+ add_purchase_data(xml, options[:setup_fee] || 0, true, options)
326
+ if card_brand(payment_method) == 'check'
327
+ add_check(xml, payment_method)
328
+ add_check_payment_method(xml)
329
+ else
330
+ add_creditcard(xml, payment_method)
331
+ add_creditcard_payment_method(xml)
332
+ end
333
+ add_subscription(xml, options)
334
+ if options[:setup_fee]
335
+ if card_brand(payment_method) == 'check'
336
+ add_check_service(xml, options)
337
+ else
338
+ add_purchase_service(xml, payment_method, options)
339
+ end
340
+ end
341
+ add_subscription_create_service(xml, options)
342
+ add_business_rules_data(xml)
343
+ add_network_token(xml, payment_method, options) if options[:setup_fee] and token_based_card?(payment_method, options)
344
+ xml.target!
345
+ end
346
+
347
+ def build_update_subscription_request(reference, creditcard, options)
348
+ xml = Builder::XmlMarkup.new :indent => 2
349
+ add_address(xml, creditcard, options[:billing_address], options) unless options[:billing_address].blank?
350
+ add_purchase_data(xml, options[:setup_fee], true, options) unless options[:setup_fee].blank?
351
+ add_creditcard(xml, creditcard) if creditcard
352
+ add_creditcard_payment_method(xml) if creditcard
353
+ add_subscription(xml, options, reference)
354
+ add_subscription_update_service(xml, options)
355
+ add_business_rules_data(xml)
356
+ xml.target!
357
+ end
358
+
359
+ def build_delete_subscription_request(reference, options)
360
+ xml = Builder::XmlMarkup.new :indent => 2
361
+ add_subscription(xml, options, reference)
362
+ add_subscription_delete_service(xml, options)
363
+ xml.target!
364
+ end
365
+
366
+ def build_retrieve_subscription_request(reference, options)
367
+ xml = Builder::XmlMarkup.new :indent => 2
368
+ add_subscription(xml, options, reference)
369
+ add_subscription_retrieve_service(xml, options)
370
+ xml.target!
371
+ end
372
+
373
+ def build_validate_pinless_debit_request(creditcard,options)
374
+ xml = Builder::XmlMarkup.new :indent => 2
375
+ add_creditcard(xml, creditcard)
376
+ add_validate_pinless_debit_service(xml)
377
+ xml.target!
378
+ end
379
+
380
+ def add_business_rules_data(xml)
381
+ xml.tag! 'businessRules' do
382
+ xml.tag!('ignoreAVSResult', 'true') if @options[:ignore_avs]
383
+ xml.tag!('ignoreCVResult', 'true') if @options[:ignore_cvv]
384
+ end
385
+ end
386
+
387
+ def add_line_item_data(xml, options)
388
+ options[:line_items].each_with_index do |value, index|
389
+ xml.tag! 'item', {'id' => index} do
390
+ xml.tag! 'unitPrice', amount(value[:declared_value])
391
+ xml.tag! 'quantity', value[:quantity]
392
+ xml.tag! 'productCode', value[:code] || 'shipping_only'
393
+ xml.tag! 'productName', value[:description]
394
+ xml.tag! 'productSKU', value[:sku]
395
+ end
396
+ end
397
+ end
398
+
399
+ def add_merchant_data(xml, options)
400
+ xml.tag! 'merchantID', @options[:login]
401
+ xml.tag! 'merchantReferenceCode', options[:order_id]
402
+ xml.tag! 'clientLibrary' ,'Ruby Active Merchant'
403
+ xml.tag! 'clientLibraryVersion', VERSION
404
+ xml.tag! 'clientEnvironment' , RUBY_PLATFORM
405
+ end
406
+
407
+ def add_purchase_data(xml, money = 0, include_grand_total = false, options={})
408
+ xml.tag! 'purchaseTotals' do
409
+ xml.tag! 'currency', options[:currency] || currency(money)
410
+ xml.tag!('grandTotalAmount', amount(money)) if include_grand_total
411
+ end
412
+ end
413
+
414
+ def add_address(xml, payment_method, address, options, shipTo = false)
415
+ xml.tag! shipTo ? 'shipTo' : 'billTo' do
416
+ xml.tag! 'firstName', payment_method.first_name if payment_method
417
+ xml.tag! 'lastName', payment_method.last_name if payment_method
418
+ xml.tag! 'street1', address[:address1]
419
+ xml.tag! 'street2', address[:address2] unless address[:address2].blank?
420
+ xml.tag! 'city', address[:city]
421
+ xml.tag! 'state', address[:state]
422
+ xml.tag! 'postalCode', address[:zip]
423
+ xml.tag! 'country', address[:country]
424
+ xml.tag! 'company', address[:company] unless address[:company].blank?
425
+ xml.tag! 'companyTaxID', address[:companyTaxID] unless address[:company_tax_id].blank?
426
+ xml.tag! 'phoneNumber', address[:phone] unless address[:phone].blank?
427
+ xml.tag! 'email', options[:email]
428
+ xml.tag! 'driversLicenseNumber', options[:drivers_license_number] unless options[:drivers_license_number].blank?
429
+ xml.tag! 'driversLicenseState', options[:drivers_license_state] unless options[:drivers_license_state].blank?
430
+ end
431
+ end
432
+
433
+ def add_creditcard(xml, creditcard)
434
+ xml.tag! 'card' do
435
+ xml.tag! 'accountNumber', creditcard.number
436
+ xml.tag! 'expirationMonth', format(creditcard.month, :two_digits)
437
+ xml.tag! 'expirationYear', format(creditcard.year, :four_digits)
438
+ xml.tag!('cvNumber', creditcard.verification_value) unless (@options[:ignore_cvv] || creditcard.verification_value.blank? )
439
+ xml.tag! 'cardType', @@credit_card_codes[card_brand(creditcard).to_sym]
440
+ end
441
+ end
442
+
443
+ def add_check(xml, check)
444
+ xml.tag! 'check' do
445
+ xml.tag! 'accountNumber', check.account_number
446
+ xml.tag! 'accountType', check.account_type[0]
447
+ xml.tag! 'bankTransitNumber', check.routing_number
448
+ end
449
+ end
450
+
451
+ def add_tax_service(xml)
452
+ xml.tag! 'taxService', {'run' => 'true'} do
453
+ xml.tag!('nexus', @options[:nexus]) unless @options[:nexus].blank?
454
+ xml.tag!('sellerRegistration', @options[:vat_reg_number]) unless @options[:vat_reg_number].blank?
455
+ end
456
+ end
457
+
458
+ def add_auth_service(xml, payment_method_or_reference, options)
459
+ if token_based_card?(payment_method_or_reference, options)
460
+ # Sadly, for each type of card there is different rules on how to send this
461
+ # See http://apps.cybersource.com/library/documentation/dev_guides/tokenization/Tokenization.pdf
462
+ if card_brand(payment_method_or_reference) == 'visa'
463
+ xml.tag! 'ccAuthService', {'run' => 'true'} do
464
+ # Cryptogram for payment network tokenization transactions. The value for this field must be 28-character base64 or 40-character hex binary
465
+ xml.tag! 'cavv', options[:cryptogram]
466
+ # Type of payer authentication fields that are being used for the payment network tokenization transaction
467
+ xml.tag! 'commerceIndicator', 'vbv' # Verified by Visa
468
+ # Cryptogram for payment network tokenization transactions. The value for this field must be 28-character base64 or 40-character hex binary
469
+ xml.tag! 'xid', options[:cryptogram]
470
+ end
471
+ elsif card_brand(payment_method_or_reference) == 'master'
472
+ xml.tag! 'ucaf' do
473
+ # Cryptogram for payment network tokenization transactions with MasterCard
474
+ xml.tag! 'authenticationData', options[:cryptogram]
475
+ # Required field for payment network tokenization transactions with MasterCard
476
+ xml.tag! 'collectionIndicator', '2'
477
+ end
478
+ xml.tag! 'ccAuthService', {'run' => 'true'} do
479
+ # Type of payer authentication fields that are being used for the payment network tokenization transaction
480
+ xml.tag! 'commerceIndicator', 'spa' # MasterCard SecureCode
481
+ end
482
+ elsif card_brand(payment_method_or_reference) == 'american_express'
483
+ # For the American Express card type, the cryptogram is a 40-byte binary value that
484
+ # you must split into two 20-byte binary values (block A and block B). Send the first
485
+ # 20-byte value (block A) in the cardholder authentication verification value (CAVV)
486
+ # field. Send the second 20-byte value (block B) in the transaction ID (XID) field.
487
+ # The incoming cryptogram is base64 encoded, so we first decode it, then split it
488
+ # then re-encode it for transmission (chomping off a trailing newline ruby adds).
489
+ base64_decoded_cryptogram = Base64.decode64(options[:cryptogram])
490
+ block_a = base64_decoded_cryptogram[0, 20]
491
+ block_b = base64_decoded_cryptogram[20, 20]
492
+ xml.tag! 'ccAuthService', {'run' => 'true'} do
493
+ xml.tag! 'cavv', Base64.encode64(block_a).chomp
494
+ # Type of payer authentication fields that are being used for the payment network tokenization transaction
495
+ xml.tag! 'commerceIndicator', 'aesk' # American Express SafeKey
496
+ xml.tag! 'xid', Base64.encode64(block_b).chomp
497
+ end
498
+ end
499
+ else
500
+ xml.tag! 'ccAuthService', {'run' => 'true'}
501
+ end
502
+ end
503
+
504
+ def add_capture_service(xml, request_id, request_token)
505
+ xml.tag! 'ccCaptureService', {'run' => 'true'} do
506
+ xml.tag! 'authRequestID', request_id
507
+ xml.tag! 'authRequestToken', request_token
508
+ end
509
+ end
510
+
511
+ def add_purchase_service(xml, payment_method_or_reference, options)
512
+ if options[:pinless_debit_card]
513
+ xml.tag! 'pinlessDebitService', {'run' => 'true'}
514
+ else
515
+ add_auth_service(xml, payment_method_or_reference, options)
516
+ xml.tag! 'ccCaptureService', {'run' => 'true'}
517
+ end
518
+ end
519
+
520
+ def add_void_service(xml, request_id, request_token)
521
+ xml.tag! 'voidService', {'run' => 'true'} do
522
+ xml.tag! 'voidRequestID', request_id
523
+ xml.tag! 'voidRequestToken', request_token
524
+ end
525
+ end
526
+
527
+ def add_auth_reversal_service(xml, request_id, request_token)
528
+ xml.tag! 'ccAuthReversalService', {'run' => 'true'} do
529
+ xml.tag! 'authRequestID', request_id
530
+ xml.tag! 'authRequestToken', request_token
531
+ end
532
+ end
533
+
534
+ def add_credit_service(xml, request_id = nil, request_token = nil)
535
+ xml.tag! 'ccCreditService', {'run' => 'true'} do
536
+ xml.tag! 'captureRequestID', request_id if request_id
537
+ xml.tag! 'captureRequestToken', request_token if request_token
538
+ end
539
+ end
540
+
541
+ def add_check_service(xml)
542
+ xml.tag! 'ecDebitService', {'run' => 'true'}
543
+ end
544
+
545
+ def add_subscription_create_service(xml, options)
546
+ xml.tag! 'paySubscriptionCreateService', {'run' => 'true'}
547
+ end
548
+
549
+ def add_subscription_update_service(xml, options)
550
+ xml.tag! 'paySubscriptionUpdateService', {'run' => 'true'}
551
+ end
552
+
553
+ def add_subscription_delete_service(xml, options)
554
+ xml.tag! 'paySubscriptionDeleteService', {'run' => 'true'}
555
+ end
556
+
557
+ def add_subscription_retrieve_service(xml, options)
558
+ xml.tag! 'paySubscriptionRetrieveService', {'run' => 'true'}
559
+ end
560
+
561
+ def add_subscription(xml, options, reference = nil)
562
+ options[:subscription] ||= {}
563
+
564
+ xml.tag! 'recurringSubscriptionInfo' do
565
+ if reference
566
+ _, subscription_id, _ = reference.split(";")
567
+ xml.tag! 'subscriptionID', subscription_id
568
+ end
569
+
570
+ xml.tag! 'status', options[:subscription][:status] if options[:subscription][:status]
571
+ xml.tag! 'amount', amount(options[:subscription][:amount]) if options[:subscription][:amount]
572
+ xml.tag! 'numberOfPayments', options[:subscription][:occurrences] if options[:subscription][:occurrences]
573
+ xml.tag! 'automaticRenew', options[:subscription][:automatic_renew] if options[:subscription][:automatic_renew]
574
+ xml.tag! 'frequency', options[:subscription][:frequency] if options[:subscription][:frequency]
575
+ xml.tag! 'startDate', options[:subscription][:start_date].strftime("%Y%m%d") if options[:subscription][:start_date]
576
+ xml.tag! 'endDate', options[:subscription][:end_date].strftime("%Y%m%d") if options[:subscription][:end_date]
577
+ xml.tag! 'approvalRequired', options[:subscription][:approval_required] || false
578
+ xml.tag! 'event', options[:subscription][:event] if options[:subscription][:event]
579
+ xml.tag! 'billPayment', options[:subscription][:bill_payment] if options[:subscription][:bill_payment]
580
+ end
581
+ end
582
+
583
+ def add_creditcard_payment_method(xml)
584
+ xml.tag! 'subscription' do
585
+ xml.tag! 'paymentMethod', "credit card"
586
+ end
587
+ end
588
+
589
+ def add_check_payment_method(xml)
590
+ xml.tag! 'subscription' do
591
+ xml.tag! 'paymentMethod', "check"
592
+ end
593
+ end
594
+
595
+ def add_payment_method_or_subscription(xml, money, payment_method_or_reference, options)
596
+ if payment_method_or_reference.is_a?(String)
597
+ add_purchase_data(xml, money, true, options)
598
+ add_subscription(xml, options, payment_method_or_reference)
599
+ elsif card_brand(payment_method_or_reference) == 'check'
600
+ add_address(xml, payment_method_or_reference, options[:billing_address], options)
601
+ add_purchase_data(xml, money, true, options)
602
+ add_check(xml, payment_method_or_reference)
603
+ else
604
+ add_address(xml, payment_method_or_reference, options[:billing_address], options)
605
+ add_address(xml, payment_method_or_reference, options[:shipping_address], options, true)
606
+ add_purchase_data(xml, money, true, options)
607
+ add_creditcard(xml, payment_method_or_reference)
608
+ end
609
+ end
610
+
611
+ def add_validate_pinless_debit_service(xml)
612
+ xml.tag!'pinlessDebitValidateService', {'run' => 'true'}
613
+ end
614
+
615
+ def add_network_token(xml, payment_method_or_reference, options)
616
+ xml.tag! 'paymentNetworkToken' do
617
+ # Type of transaction that provided the token data. This value does not specify the token
618
+ # service provider; it specifies the entity that provided you with information about the token.
619
+ # 1: In-app transaction.
620
+ # An application on the customer’s mobile device provided the
621
+ # token data for an e-commerce transaction.
622
+ # For recurring transactions, use this value if the original transaction was an in-app
623
+ # e-commerce transaction.
624
+ # See http://apps.cybersource.com/library/documentation/dev_guides/tokenization/Tokenization.pdf
625
+ xml.tag! 'transactionType', '1'
626
+ end
627
+ end
628
+
629
+ def token_based_card?(payment_method_or_reference, options)
630
+ !payment_method_or_reference.is_a?(String) && card_brand(payment_method_or_reference) && options[:cryptogram]
631
+ end
632
+
633
+ # Where we actually build the full SOAP request using builder
634
+ def build_request(body, options)
635
+ xml = Builder::XmlMarkup.new :indent => 2
636
+ xml.instruct!
637
+ xml.tag! 's:Envelope', {'xmlns:s' => 'http://schemas.xmlsoap.org/soap/envelope/'} do
638
+ xml.tag! 's:Header' do
639
+ xml.tag! 'wsse:Security', {'s:mustUnderstand' => '1', 'xmlns:wsse' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'} do
640
+ xml.tag! 'wsse:UsernameToken' do
641
+ xml.tag! 'wsse:Username', @options[:login]
642
+ xml.tag! 'wsse:Password', @options[:password], 'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'
643
+ end
644
+ end
645
+ end
646
+ xml.tag! 's:Body', {'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema'} do
647
+ xml.tag! 'requestMessage', {'xmlns' => "urn:schemas-cybersource-com:transaction-data-#{XSD_VERSION}"} do
648
+ add_merchant_data(xml, options)
649
+ xml << body
650
+ end
651
+ end
652
+ end
653
+ xml.target!
654
+ end
655
+
656
+ # Contact CyberSource, make the SOAP request, and parse the reply into a
657
+ # Response object
658
+ def commit(request, options)
659
+ response = parse(ssl_post(test? ? self.test_url : self.live_url, build_request(request, options)))
660
+
661
+ success = response[:decision] == "ACCEPT"
662
+ message = @@response_codes[('r' + response[:reasonCode]).to_sym] rescue response[:message]
663
+ authorization = success ? [ options[:order_id], response[:requestID], response[:requestToken] ].compact.join(";") : nil
664
+
665
+ Response.new(success, message, response,
666
+ :test => test?,
667
+ :authorization => authorization,
668
+ :avs_result => { :code => response[:avsCode] },
669
+ :cvv_result => response[:cvCode]
670
+ )
671
+ end
672
+
673
+ # Parse the SOAP response
674
+ # Technique inspired by the Paypal Gateway
675
+ def parse(xml)
676
+ reply = {}
677
+ xml = REXML::Document.new(xml)
678
+ if root = REXML::XPath.first(xml, "//c:replyMessage")
679
+ root.elements.to_a.each do |node|
680
+ case node.name
681
+ when 'c:reasonCode'
682
+ reply[:message] = reply(node.text)
683
+ else
684
+ parse_element(reply, node)
685
+ end
686
+ end
687
+ elsif root = REXML::XPath.first(xml, "//soap:Fault")
688
+ parse_element(reply, root)
689
+ reply[:message] = "#{reply[:faultcode]}: #{reply[:faultstring]}"
690
+ end
691
+ return reply
692
+ end
693
+
694
+ def parse_element(reply, node)
695
+ if node.has_elements?
696
+ node.elements.each{|e| parse_element(reply, e) }
697
+ else
698
+ if node.parent.name =~ /item/
699
+ parent = node.parent.name + (node.parent.attributes["id"] ? "_" + node.parent.attributes["id"] : '')
700
+ reply[(parent + '_' + node.name).to_sym] = node.text
701
+ else
702
+ reply[node.name.to_sym] = node.text
703
+ end
704
+ end
705
+ return reply
706
+ end
707
+ end
708
+ end
709
+ end