start_activemerchant 1.50.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +1769 -0
  3. data/CONTRIBUTORS +540 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +226 -0
  6. data/lib/active_merchant.rb +67 -0
  7. data/lib/active_merchant/billing.rb +15 -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 +404 -0
  14. data/lib/active_merchant/billing/credit_card_formatting.rb +24 -0
  15. data/lib/active_merchant/billing/credit_card_methods.rb +195 -0
  16. data/lib/active_merchant/billing/cvv_result.rb +38 -0
  17. data/lib/active_merchant/billing/gateway.rb +291 -0
  18. data/lib/active_merchant/billing/gateways.rb +14 -0
  19. data/lib/active_merchant/billing/gateways/allied_wallet.rb +203 -0
  20. data/lib/active_merchant/billing/gateways/app55.rb +176 -0
  21. data/lib/active_merchant/billing/gateways/authorize_net.rb +510 -0
  22. data/lib/active_merchant/billing/gateways/authorize_net_arb.rb +417 -0
  23. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +976 -0
  24. data/lib/active_merchant/billing/gateways/axcessms.rb +181 -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 +192 -0
  32. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +389 -0
  33. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +58 -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 +211 -0
  37. data/lib/active_merchant/billing/gateways/bpoint.rb +277 -0
  38. data/lib/active_merchant/billing/gateways/braintree.rb +19 -0
  39. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +9 -0
  40. data/lib/active_merchant/billing/gateways/braintree_blue.rb +574 -0
  41. data/lib/active_merchant/billing/gateways/braintree_orange.rb +20 -0
  42. data/lib/active_merchant/billing/gateways/bridge_pay.rb +189 -0
  43. data/lib/active_merchant/billing/gateways/card_save.rb +23 -0
  44. data/lib/active_merchant/billing/gateways/card_stream.rb +238 -0
  45. data/lib/active_merchant/billing/gateways/cashnet.rb +202 -0
  46. data/lib/active_merchant/billing/gateways/cc5.rb +201 -0
  47. data/lib/active_merchant/billing/gateways/cecabank.rb +229 -0
  48. data/lib/active_merchant/billing/gateways/cenpos.rb +262 -0
  49. data/lib/active_merchant/billing/gateways/certo_direct.rb +278 -0
  50. data/lib/active_merchant/billing/gateways/checkout.rb +216 -0
  51. data/lib/active_merchant/billing/gateways/checkout_v2.rb +200 -0
  52. data/lib/active_merchant/billing/gateways/commercegate.rb +143 -0
  53. data/lib/active_merchant/billing/gateways/conekta.rb +210 -0
  54. data/lib/active_merchant/billing/gateways/cyber_source.rb +720 -0
  55. data/lib/active_merchant/billing/gateways/data_cash.rb +600 -0
  56. data/lib/active_merchant/billing/gateways/dibs.rb +206 -0
  57. data/lib/active_merchant/billing/gateways/efsnet.rb +219 -0
  58. data/lib/active_merchant/billing/gateways/elavon.rb +348 -0
  59. data/lib/active_merchant/billing/gateways/epay.rb +274 -0
  60. data/lib/active_merchant/billing/gateways/evo_ca.rb +308 -0
  61. data/lib/active_merchant/billing/gateways/eway.rb +214 -0
  62. data/lib/active_merchant/billing/gateways/eway_managed.rb +291 -0
  63. data/lib/active_merchant/billing/gateways/eway_rapid.rb +522 -0
  64. data/lib/active_merchant/billing/gateways/exact.rb +227 -0
  65. data/lib/active_merchant/billing/gateways/ezic.rb +206 -0
  66. data/lib/active_merchant/billing/gateways/fat_zebra.rb +213 -0
  67. data/lib/active_merchant/billing/gateways/federated_canada.rb +160 -0
  68. data/lib/active_merchant/billing/gateways/finansbank.rb +23 -0
  69. data/lib/active_merchant/billing/gateways/first_giving.rb +143 -0
  70. data/lib/active_merchant/billing/gateways/first_pay.rb +160 -0
  71. data/lib/active_merchant/billing/gateways/firstdata_e4.rb +413 -0
  72. data/lib/active_merchant/billing/gateways/flo2cash.rb +215 -0
  73. data/lib/active_merchant/billing/gateways/flo2cash_simple.rb +20 -0
  74. data/lib/active_merchant/billing/gateways/garanti.rb +261 -0
  75. data/lib/active_merchant/billing/gateways/global_transport.rb +179 -0
  76. data/lib/active_merchant/billing/gateways/hdfc.rb +207 -0
  77. data/lib/active_merchant/billing/gateways/hps.rb +287 -0
  78. data/lib/active_merchant/billing/gateways/iats_payments.rb +277 -0
  79. data/lib/active_merchant/billing/gateways/ideal/ideal_base.rb +246 -0
  80. data/lib/active_merchant/billing/gateways/ideal/ideal_rabobank.pem +13 -0
  81. data/lib/active_merchant/billing/gateways/ideal/ideal_response.rb +29 -0
  82. data/lib/active_merchant/billing/gateways/ideal_rabobank.rb +66 -0
  83. data/lib/active_merchant/billing/gateways/inspire.rb +219 -0
  84. data/lib/active_merchant/billing/gateways/instapay.rb +163 -0
  85. data/lib/active_merchant/billing/gateways/ipp.rb +175 -0
  86. data/lib/active_merchant/billing/gateways/iridium.rb +457 -0
  87. data/lib/active_merchant/billing/gateways/itransact.rb +448 -0
  88. data/lib/active_merchant/billing/gateways/jetpay.rb +275 -0
  89. data/lib/active_merchant/billing/gateways/linkpoint.rb +438 -0
  90. data/lib/active_merchant/billing/gateways/litle.rb +345 -0
  91. data/lib/active_merchant/billing/gateways/maxipago.rb +197 -0
  92. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +170 -0
  93. data/lib/active_merchant/billing/gateways/merchant_one.rb +114 -0
  94. data/lib/active_merchant/billing/gateways/merchant_partners.rb +245 -0
  95. data/lib/active_merchant/billing/gateways/merchant_ware.rb +319 -0
  96. data/lib/active_merchant/billing/gateways/merchant_ware_version_four.rb +268 -0
  97. data/lib/active_merchant/billing/gateways/merchant_warrior.rb +195 -0
  98. data/lib/active_merchant/billing/gateways/mercury.rb +326 -0
  99. data/lib/active_merchant/billing/gateways/metrics_global.rb +303 -0
  100. data/lib/active_merchant/billing/gateways/migs.rb +280 -0
  101. data/lib/active_merchant/billing/gateways/migs/migs_codes.rb +100 -0
  102. data/lib/active_merchant/billing/gateways/modern_payments.rb +37 -0
  103. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +219 -0
  104. data/lib/active_merchant/billing/gateways/monei.rb +307 -0
  105. data/lib/active_merchant/billing/gateways/moneris.rb +309 -0
  106. data/lib/active_merchant/billing/gateways/moneris_us.rb +298 -0
  107. data/lib/active_merchant/billing/gateways/money_movers.rb +152 -0
  108. data/lib/active_merchant/billing/gateways/nab_transact.rb +290 -0
  109. data/lib/active_merchant/billing/gateways/net_registry.rb +198 -0
  110. data/lib/active_merchant/billing/gateways/netaxept.rb +181 -0
  111. data/lib/active_merchant/billing/gateways/netbilling.rb +224 -0
  112. data/lib/active_merchant/billing/gateways/netpay.rb +223 -0
  113. data/lib/active_merchant/billing/gateways/network_merchants.rb +242 -0
  114. data/lib/active_merchant/billing/gateways/nmi.rb +256 -0
  115. data/lib/active_merchant/billing/gateways/ogone.rb +435 -0
  116. data/lib/active_merchant/billing/gateways/omise.rb +319 -0
  117. data/lib/active_merchant/billing/gateways/openpay.rb +194 -0
  118. data/lib/active_merchant/billing/gateways/optimal_payment.rb +314 -0
  119. data/lib/active_merchant/billing/gateways/orbital.rb +834 -0
  120. data/lib/active_merchant/billing/gateways/orbital/orbital_soft_descriptors.rb +47 -0
  121. data/lib/active_merchant/billing/gateways/pac_net_raven.rb +207 -0
  122. data/lib/active_merchant/billing/gateways/pago_facil.rb +122 -0
  123. data/lib/active_merchant/billing/gateways/pay_conex.rb +246 -0
  124. data/lib/active_merchant/billing/gateways/pay_gate_xml.rb +277 -0
  125. data/lib/active_merchant/billing/gateways/pay_hub.rb +213 -0
  126. data/lib/active_merchant/billing/gateways/pay_junction.rb +390 -0
  127. data/lib/active_merchant/billing/gateways/pay_secure.rb +112 -0
  128. data/lib/active_merchant/billing/gateways/paybox_direct.rb +188 -0
  129. data/lib/active_merchant/billing/gateways/payex.rb +412 -0
  130. data/lib/active_merchant/billing/gateways/payflow.rb +308 -0
  131. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +220 -0
  132. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +39 -0
  133. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  134. data/lib/active_merchant/billing/gateways/payflow_express.rb +224 -0
  135. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +15 -0
  136. data/lib/active_merchant/billing/gateways/payflow_uk.rb +21 -0
  137. data/lib/active_merchant/billing/gateways/payment_express.rb +353 -0
  138. data/lib/active_merchant/billing/gateways/paymill.rb +282 -0
  139. data/lib/active_merchant/billing/gateways/paypal.rb +129 -0
  140. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +679 -0
  141. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +65 -0
  142. data/lib/active_merchant/billing/gateways/paypal/paypal_recurring_api.rb +262 -0
  143. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  144. data/lib/active_merchant/billing/gateways/paypal_digital_goods.rb +44 -0
  145. data/lib/active_merchant/billing/gateways/paypal_express.rb +264 -0
  146. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +30 -0
  147. data/lib/active_merchant/billing/gateways/payscout.rb +162 -0
  148. data/lib/active_merchant/billing/gateways/paystation.rb +199 -0
  149. data/lib/active_merchant/billing/gateways/payu_in.rb +247 -0
  150. data/lib/active_merchant/billing/gateways/payway.rb +207 -0
  151. data/lib/active_merchant/billing/gateways/pin.rb +207 -0
  152. data/lib/active_merchant/billing/gateways/plugnpay.rb +283 -0
  153. data/lib/active_merchant/billing/gateways/psigate.rb +216 -0
  154. data/lib/active_merchant/billing/gateways/psl_card.rb +303 -0
  155. data/lib/active_merchant/billing/gateways/qbms.rb +292 -0
  156. data/lib/active_merchant/billing/gateways/quantum.rb +276 -0
  157. data/lib/active_merchant/billing/gateways/quickbooks.rb +280 -0
  158. data/lib/active_merchant/billing/gateways/quickpay.rb +26 -0
  159. data/lib/active_merchant/billing/gateways/quickpay/quickpay_common.rb +188 -0
  160. data/lib/active_merchant/billing/gateways/quickpay/quickpay_v10.rb +240 -0
  161. data/lib/active_merchant/billing/gateways/quickpay/quickpay_v4to7.rb +227 -0
  162. data/lib/active_merchant/billing/gateways/qvalent.rb +179 -0
  163. data/lib/active_merchant/billing/gateways/realex.rb +298 -0
  164. data/lib/active_merchant/billing/gateways/redsys.rb +406 -0
  165. data/lib/active_merchant/billing/gateways/s5.rb +226 -0
  166. data/lib/active_merchant/billing/gateways/sage.rb +173 -0
  167. data/lib/active_merchant/billing/gateways/sage/sage_bankcard.rb +89 -0
  168. data/lib/active_merchant/billing/gateways/sage/sage_core.rb +115 -0
  169. data/lib/active_merchant/billing/gateways/sage/sage_vault.rb +149 -0
  170. data/lib/active_merchant/billing/gateways/sage/sage_virtual_check.rb +97 -0
  171. data/lib/active_merchant/billing/gateways/sage_pay.rb +399 -0
  172. data/lib/active_merchant/billing/gateways/sallie_mae.rb +143 -0
  173. data/lib/active_merchant/billing/gateways/secure_net.rb +263 -0
  174. data/lib/active_merchant/billing/gateways/secure_pay.rb +201 -0
  175. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +281 -0
  176. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +105 -0
  177. data/lib/active_merchant/billing/gateways/skip_jack.rb +451 -0
  178. data/lib/active_merchant/billing/gateways/smart_ps.rb +283 -0
  179. data/lib/active_merchant/billing/gateways/so_easy_pay.rb +194 -0
  180. data/lib/active_merchant/billing/gateways/spreedly_core.rb +247 -0
  181. data/lib/active_merchant/billing/gateways/stripe.rb +489 -0
  182. data/lib/active_merchant/billing/gateways/swipe_checkout.rb +157 -0
  183. data/lib/active_merchant/billing/gateways/tns.rb +227 -0
  184. data/lib/active_merchant/billing/gateways/trans_first.rb +126 -0
  185. data/lib/active_merchant/billing/gateways/transax.rb +23 -0
  186. data/lib/active_merchant/billing/gateways/transnational.rb +10 -0
  187. data/lib/active_merchant/billing/gateways/trust_commerce.rb +416 -0
  188. data/lib/active_merchant/billing/gateways/usa_epay.rb +25 -0
  189. data/lib/active_merchant/billing/gateways/usa_epay_advanced.rb +1516 -0
  190. data/lib/active_merchant/billing/gateways/usa_epay_transaction.rb +259 -0
  191. data/lib/active_merchant/billing/gateways/vanco.rb +280 -0
  192. data/lib/active_merchant/billing/gateways/verifi.rb +225 -0
  193. data/lib/active_merchant/billing/gateways/viaklix.rb +183 -0
  194. data/lib/active_merchant/billing/gateways/webpay.rb +97 -0
  195. data/lib/active_merchant/billing/gateways/wepay.rb +205 -0
  196. data/lib/active_merchant/billing/gateways/wirecard.rb +420 -0
  197. data/lib/active_merchant/billing/gateways/worldpay.rb +331 -0
  198. data/lib/active_merchant/billing/gateways/worldpay_online_payments.rb +204 -0
  199. data/lib/active_merchant/billing/gateways/worldpay_us.rb +181 -0
  200. data/lib/active_merchant/billing/model.rb +30 -0
  201. data/lib/active_merchant/billing/network_tokenization_credit_card.rb +24 -0
  202. data/lib/active_merchant/billing/payment_token.rb +21 -0
  203. data/lib/active_merchant/billing/rails.rb +3 -0
  204. data/lib/active_merchant/billing/response.rb +92 -0
  205. data/lib/active_merchant/connection.rb +172 -0
  206. data/lib/active_merchant/country.rb +332 -0
  207. data/lib/active_merchant/empty.rb +20 -0
  208. data/lib/active_merchant/errors.rb +35 -0
  209. data/lib/active_merchant/network_connection_retries.rb +79 -0
  210. data/lib/active_merchant/post_data.rb +24 -0
  211. data/lib/active_merchant/posts_data.rb +84 -0
  212. data/lib/active_merchant/version.rb +3 -0
  213. data/lib/activemerchant.rb +1 -0
  214. data/lib/certs/cacert.pem +3866 -0
  215. data/lib/support/gateway_support.rb +71 -0
  216. data/lib/support/outbound_hosts.rb +28 -0
  217. data/lib/support/ssl_verify.rb +93 -0
  218. metadata +387 -0
@@ -0,0 +1,210 @@
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
+ post[: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
+ "Accept-Language" => "es",
164
+ "Authorization" => "Basic " + Base64.encode64("#{options[:key]}:"),
165
+ "RaiseHtmlError" => "false",
166
+ "Conekta-Client-User-Agent" => {"agent"=>"Conekta ActiveMerchantBindings/#{ActiveMerchant::VERSION}"}.to_json,
167
+ "X-Conekta-Client-User-Agent" => user_agent,
168
+ "X-Conekta-Client-User-Metadata" => meta.to_json
169
+ }
170
+ end
171
+
172
+ def commit(method, url, parameters, options = {})
173
+ success = false
174
+ begin
175
+ raw_response = parse(ssl_request(method, live_url + url, (parameters ? parameters.to_query : nil), headers(options[:meta])))
176
+ success = (raw_response.key?("object") && (raw_response["object"] != "error"))
177
+ rescue ResponseError => e
178
+ raw_response = response_error(e.response.body)
179
+ rescue JSON::ParserError
180
+ raw_response = json_error(raw_response)
181
+ end
182
+
183
+ Response.new(
184
+ success,
185
+ raw_response["message_to_purchaser"],
186
+ raw_response,
187
+ test: test?,
188
+ authorization: raw_response["id"]
189
+ )
190
+ end
191
+
192
+ def response_error(raw_response)
193
+ begin
194
+ parse(raw_response)
195
+ rescue JSON::ParserError
196
+ json_error(raw_response)
197
+ end
198
+ end
199
+
200
+ def json_error(raw_response)
201
+ msg = 'Invalid response received from the Conekta API.'
202
+ msg += " (The raw response returned by the API was #{raw_response.inspect})"
203
+ {
204
+ "message" => msg
205
+ }
206
+ end
207
+ end
208
+ end
209
+ end
210
+
@@ -0,0 +1,720 @@
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
+ # * The order of the XML elements does matter, make sure to follow the order in
31
+ # the documentation exactly.
32
+ class CyberSourceGateway < Gateway
33
+ self.test_url = 'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor'
34
+ self.live_url = 'https://ics2ws.ic3.com/commerce/1.x/transactionProcessor'
35
+
36
+ XSD_VERSION = "1.109"
37
+
38
+ # visa, master, american_express, discover
39
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover]
40
+ self.supported_countries = %w(US BR CA CN DK FI FR DE JP MX NO SE GB SG)
41
+ self.default_currency = 'USD'
42
+ self.homepage_url = 'http://www.cybersource.com'
43
+ self.display_name = 'CyberSource'
44
+
45
+ # map credit card to the CyberSource expected representation
46
+ @@credit_card_codes = {
47
+ :visa => '001',
48
+ :master => '002',
49
+ :american_express => '003',
50
+ :discover => '004'
51
+ }
52
+
53
+ # map response codes to something humans can read
54
+ @@response_codes = {
55
+ :r100 => "Successful transaction",
56
+ :r101 => "Request is missing one or more required fields" ,
57
+ :r102 => "One or more fields contains invalid data",
58
+ :r150 => "General failure",
59
+ :r151 => "The request was received but a server time-out occurred",
60
+ :r152 => "The request was received, but a service timed out",
61
+ :r200 => "The authorization request was approved by the issuing bank but declined by CyberSource because it did not pass the AVS check",
62
+ :r201 => "The issuing bank has questions about the request",
63
+ :r202 => "Expired card",
64
+ :r203 => "General decline of the card",
65
+ :r204 => "Insufficient funds in the account",
66
+ :r205 => "Stolen or lost card",
67
+ :r207 => "Issuing bank unavailable",
68
+ :r208 => "Inactive card or card not authorized for card-not-present transactions",
69
+ :r209 => "American Express Card Identifiction Digits (CID) did not match",
70
+ :r210 => "The card has reached the credit limit",
71
+ :r211 => "Invalid card verification number",
72
+ :r221 => "The customer matched an entry on the processor's negative file",
73
+ :r230 => "The authorization request was approved by the issuing bank but declined by CyberSource because it did not pass the card verification check",
74
+ :r231 => "Invalid account number",
75
+ :r232 => "The card type is not accepted by the payment processor",
76
+ :r233 => "General decline by the processor",
77
+ :r234 => "A problem exists with your CyberSource merchant configuration",
78
+ :r235 => "The requested amount exceeds the originally authorized amount",
79
+ :r236 => "Processor failure",
80
+ :r237 => "The authorization has already been reversed",
81
+ :r238 => "The authorization has already been captured",
82
+ :r239 => "The requested transaction amount must match the previous transaction amount",
83
+ :r240 => "The card type sent is invalid or does not correlate with the credit card number",
84
+ :r241 => "The request ID is invalid",
85
+ :r242 => "You requested a capture, but there is no corresponding, unused authorization record.",
86
+ :r243 => "The transaction has already been settled or reversed",
87
+ :r244 => "The bank account number failed the validation check",
88
+ :r246 => "The capture or credit is not voidable because the capture or credit information has already been submitted to your processor",
89
+ :r247 => "You requested a credit for a capture that was previously voided",
90
+ :r250 => "The request was received, but a time-out occurred with the payment processor",
91
+ :r254 => "Your CyberSource account is prohibited from processing stand-alone refunds",
92
+ :r255 => "Your CyberSource account is not configured to process the service in the country you specified"
93
+ }
94
+
95
+ # These are the options that can be used when creating a new CyberSource
96
+ # Gateway object.
97
+ #
98
+ # :login => your username
99
+ #
100
+ # :password => the transaction key you generated in the Business Center
101
+ #
102
+ # :test => true sets the gateway to test mode
103
+ #
104
+ # :vat_reg_number => your VAT registration number
105
+ #
106
+ # :nexus => "WI CA QC" sets the states/provinces where you have a physical
107
+ # presence for tax purposes
108
+ #
109
+ # :ignore_avs => true don't want to use AVS so continue processing even
110
+ # if AVS would have failed
111
+ #
112
+ # :ignore_cvv => true don't want to use CVV so continue processing even
113
+ # if CVV would have failed
114
+ def initialize(options = {})
115
+ requires!(options, :login, :password)
116
+ super
117
+ end
118
+
119
+ # Request an authorization for an amount from CyberSource
120
+ #
121
+ # You must supply an :order_id in the options hash
122
+ def authorize(money, creditcard_or_reference, options = {})
123
+ requires!(options, :order_id)
124
+ setup_address_hash(options)
125
+ commit(build_auth_request(money, creditcard_or_reference, options), options )
126
+ end
127
+
128
+ def auth_reversal(money, identification, options = {})
129
+ commit(build_auth_reversal_request(money, identification, options), options)
130
+ end
131
+
132
+ # Capture an authorization that has previously been requested
133
+ def capture(money, authorization, options = {})
134
+ setup_address_hash(options)
135
+ commit(build_capture_request(money, authorization, options), options)
136
+ end
137
+
138
+ # Purchase is an auth followed by a capture
139
+ # You must supply an order_id in the options hash
140
+ # options[:pinless_debit_card] => true # attempts to process as pinless debit card
141
+ def purchase(money, payment_method_or_reference, options = {})
142
+ requires!(options, :order_id)
143
+ setup_address_hash(options)
144
+ commit(build_purchase_request(money, payment_method_or_reference, options), options)
145
+ end
146
+
147
+ def void(identification, options = {})
148
+ commit(build_void_request(identification, options), options)
149
+ end
150
+
151
+ def refund(money, identification, options = {})
152
+ commit(build_refund_request(money, identification, options), options)
153
+ end
154
+
155
+ def verify(payment, options = {})
156
+ MultiResponse.run(:use_first_response) do |r|
157
+ r.process { authorize(100, payment, options) }
158
+ r.process(:ignore_result) { void(r.authorization, options) }
159
+ end
160
+ end
161
+
162
+ # Adds credit to a subscription (stand alone credit).
163
+ def credit(money, reference, options = {})
164
+ requires!(options, :order_id)
165
+ commit(build_credit_request(money, reference, options), options)
166
+ end
167
+
168
+ # Stores a customer subscription/profile with type "on-demand".
169
+ # To charge the card while creating a profile, pass
170
+ # options[:setup_fee] => money
171
+ def store(payment_method, options = {})
172
+ requires!(options, :order_id)
173
+ setup_address_hash(options)
174
+ commit(build_create_subscription_request(payment_method, options), options)
175
+ end
176
+
177
+ # Updates a customer subscription/profile
178
+ def update(reference, creditcard, options = {})
179
+ requires!(options, :order_id)
180
+ setup_address_hash(options)
181
+ commit(build_update_subscription_request(reference, creditcard, options), options)
182
+ end
183
+
184
+ # Removes a customer subscription/profile
185
+ def unstore(reference, options = {})
186
+ requires!(options, :order_id)
187
+ commit(build_delete_subscription_request(reference, options), options)
188
+ end
189
+
190
+ # Retrieves a customer subscription/profile
191
+ def retrieve(reference, options = {})
192
+ requires!(options, :order_id)
193
+ commit(build_retrieve_subscription_request(reference, options), options)
194
+ end
195
+
196
+ # CyberSource requires that you provide line item information for tax
197
+ # calculations. If you do not have prices for each item or want to
198
+ # simplify the situation then pass in one fake line item that costs the
199
+ # subtotal of the order
200
+ #
201
+ # The line_item hash goes in the options hash and should look like
202
+ #
203
+ # :line_items => [
204
+ # {
205
+ # :declared_value => '1',
206
+ # :quantity => '2',
207
+ # :code => 'default',
208
+ # :description => 'Giant Walrus',
209
+ # :sku => 'WA323232323232323'
210
+ # },
211
+ # {
212
+ # :declared_value => '6',
213
+ # :quantity => '1',
214
+ # :code => 'default',
215
+ # :description => 'Marble Snowcone',
216
+ # :sku => 'FAKE1232132113123'
217
+ # }
218
+ # ]
219
+ #
220
+ # This functionality is only supported by this particular gateway may
221
+ # be changed at any time
222
+ def calculate_tax(creditcard, options)
223
+ requires!(options, :line_items)
224
+ setup_address_hash(options)
225
+ commit(build_tax_calculation_request(creditcard, options), options)
226
+ end
227
+
228
+ # Determines if a card can be used for Pinless Debit Card transactions
229
+ def validate_pinless_debit_card(creditcard, options = {})
230
+ requires!(options, :order_id)
231
+ commit(build_validate_pinless_debit_request(creditcard,options), options)
232
+ end
233
+
234
+ def supports_scrubbing?
235
+ true
236
+ end
237
+
238
+ def scrub(transcript)
239
+ transcript.
240
+ gsub(%r((<wsse:Password [^>]*>)[^<]*(</wsse:Password>))i, '\1[FILTERED]\2').
241
+ gsub(%r((<accountNumber>)[^<]*(</accountNumber>))i, '\1[FILTERED]\2').
242
+ gsub(%r((<cvNumber>)[^<]*(</cvNumber>))i, '\1[FILTERED]\2').
243
+ gsub(%r((<cavv>)[^<]*(</cavv>))i, '\1[FILTERED]\2').
244
+ gsub(%r((<xid>)[^<]*(</xid>))i, '\1[FILTERED]\2').
245
+ gsub(%r((<authenticationData>)[^<]*(</authenticationData>))i, '\1[FILTERED]\2')
246
+ end
247
+
248
+ private
249
+
250
+ # Create all address hash key value pairs so that we still function if we
251
+ # were only provided with one or two of them
252
+ def setup_address_hash(options)
253
+ options[:billing_address] = options[:billing_address] || options[:address] || {}
254
+ options[:shipping_address] = options[:shipping_address] || {}
255
+ end
256
+
257
+ def build_auth_request(money, creditcard_or_reference, options)
258
+ xml = Builder::XmlMarkup.new :indent => 2
259
+ add_payment_method_or_subscription(xml, money, creditcard_or_reference, options)
260
+ add_auth_service(xml, creditcard_or_reference, options)
261
+ add_business_rules_data(xml, creditcard_or_reference, options)
262
+ xml.target!
263
+ end
264
+
265
+ def build_tax_calculation_request(creditcard, options)
266
+ xml = Builder::XmlMarkup.new :indent => 2
267
+ add_address(xml, creditcard, options[:billing_address], options, false)
268
+ add_address(xml, creditcard, options[:shipping_address], options, true)
269
+ add_line_item_data(xml, options)
270
+ add_purchase_data(xml, 0, false, options)
271
+ add_tax_service(xml)
272
+ add_business_rules_data(xml, creditcard, options)
273
+ xml.target!
274
+ end
275
+
276
+ def build_capture_request(money, authorization, options)
277
+ order_id, request_id, request_token = authorization.split(";")
278
+ options[:order_id] = order_id
279
+
280
+ xml = Builder::XmlMarkup.new :indent => 2
281
+ add_purchase_data(xml, money, true, options)
282
+ add_capture_service(xml, request_id, request_token)
283
+ add_business_rules_data(xml, authorization, options)
284
+ xml.target!
285
+ end
286
+
287
+ def build_purchase_request(money, payment_method_or_reference, options)
288
+ xml = Builder::XmlMarkup.new :indent => 2
289
+ add_payment_method_or_subscription(xml, money, payment_method_or_reference, options)
290
+ if !payment_method_or_reference.is_a?(String) && card_brand(payment_method_or_reference) == 'check'
291
+ add_check_service(xml)
292
+ else
293
+ add_purchase_service(xml, payment_method_or_reference, options)
294
+ add_business_rules_data(xml, payment_method_or_reference, options) unless options[:pinless_debit_card]
295
+ end
296
+ xml.target!
297
+ end
298
+
299
+ def build_void_request(identification, options)
300
+ order_id, request_id, request_token = identification.split(";")
301
+ options[:order_id] = order_id
302
+
303
+ xml = Builder::XmlMarkup.new :indent => 2
304
+ add_void_service(xml, request_id, request_token)
305
+ xml.target!
306
+ end
307
+
308
+ def build_auth_reversal_request(money, identification, options)
309
+ order_id, request_id, request_token = identification.split(";")
310
+ options[:order_id] = order_id
311
+ xml = Builder::XmlMarkup.new :indent => 2
312
+ add_purchase_data(xml, money, true, options)
313
+ add_auth_reversal_service(xml, request_id, request_token)
314
+ xml.target!
315
+ end
316
+
317
+ def build_refund_request(money, identification, options)
318
+ order_id, request_id, request_token = identification.split(";")
319
+ options[:order_id] = order_id
320
+
321
+ xml = Builder::XmlMarkup.new :indent => 2
322
+ add_purchase_data(xml, money, true, options)
323
+ add_credit_service(xml, request_id, request_token)
324
+
325
+ xml.target!
326
+ end
327
+
328
+ def build_credit_request(money, reference, options)
329
+ xml = Builder::XmlMarkup.new :indent => 2
330
+
331
+ add_purchase_data(xml, money, true, options)
332
+ add_subscription(xml, options, reference)
333
+ add_credit_service(xml)
334
+
335
+ xml.target!
336
+ end
337
+
338
+ def build_create_subscription_request(payment_method, options)
339
+ default_subscription_params = {:frequency => "on-demand", :amount => 0, :automatic_renew => false}
340
+ options[:subscription] = default_subscription_params.update(
341
+ options[:subscription] || {}
342
+ )
343
+
344
+ xml = Builder::XmlMarkup.new :indent => 2
345
+ add_address(xml, payment_method, options[:billing_address], options)
346
+ add_purchase_data(xml, options[:setup_fee] || 0, true, options)
347
+ if card_brand(payment_method) == 'check'
348
+ add_check(xml, payment_method)
349
+ add_check_payment_method(xml)
350
+ else
351
+ add_creditcard(xml, payment_method)
352
+ add_creditcard_payment_method(xml)
353
+ end
354
+ add_subscription(xml, options)
355
+ if options[:setup_fee]
356
+ if card_brand(payment_method) == 'check'
357
+ add_check_service(xml, options)
358
+ else
359
+ add_purchase_service(xml, payment_method, options)
360
+ end
361
+ end
362
+ add_subscription_create_service(xml, options)
363
+ add_business_rules_data(xml, payment_method, options)
364
+ xml.target!
365
+ end
366
+
367
+ def build_update_subscription_request(reference, creditcard, options)
368
+ xml = Builder::XmlMarkup.new :indent => 2
369
+ add_address(xml, creditcard, options[:billing_address], options) unless options[:billing_address].blank?
370
+ add_purchase_data(xml, options[:setup_fee], true, options) unless options[:setup_fee].blank?
371
+ add_creditcard(xml, creditcard) if creditcard
372
+ add_creditcard_payment_method(xml) if creditcard
373
+ add_subscription(xml, options, reference)
374
+ add_subscription_update_service(xml, options)
375
+ add_business_rules_data(xml, creditcard, options)
376
+ xml.target!
377
+ end
378
+
379
+ def build_delete_subscription_request(reference, options)
380
+ xml = Builder::XmlMarkup.new :indent => 2
381
+ add_subscription(xml, options, reference)
382
+ add_subscription_delete_service(xml, options)
383
+ xml.target!
384
+ end
385
+
386
+ def build_retrieve_subscription_request(reference, options)
387
+ xml = Builder::XmlMarkup.new :indent => 2
388
+ add_subscription(xml, options, reference)
389
+ add_subscription_retrieve_service(xml, options)
390
+ xml.target!
391
+ end
392
+
393
+ def build_validate_pinless_debit_request(creditcard,options)
394
+ xml = Builder::XmlMarkup.new :indent => 2
395
+ add_creditcard(xml, creditcard)
396
+ add_validate_pinless_debit_service(xml)
397
+ xml.target!
398
+ end
399
+
400
+ def add_business_rules_data(xml, payment_method, options)
401
+ prioritized_options = [options, @options]
402
+
403
+ unless network_tokenization?(payment_method)
404
+ xml.tag! 'businessRules' do
405
+ xml.tag!('ignoreAVSResult', 'true') if extract_option(prioritized_options, :ignore_avs)
406
+ xml.tag!('ignoreCVResult', 'true') if extract_option(prioritized_options, :ignore_cvv)
407
+ end
408
+ end
409
+ end
410
+
411
+ def extract_option prioritized_options, option_name
412
+ options_matching_key = prioritized_options.detect do |options|
413
+ options.has_key? option_name
414
+ end
415
+ options_matching_key[option_name] if options_matching_key
416
+ end
417
+
418
+ def add_line_item_data(xml, options)
419
+ options[:line_items].each_with_index do |value, index|
420
+ xml.tag! 'item', {'id' => index} do
421
+ xml.tag! 'unitPrice', amount(value[:declared_value])
422
+ xml.tag! 'quantity', value[:quantity]
423
+ xml.tag! 'productCode', value[:code] || 'shipping_only'
424
+ xml.tag! 'productName', value[:description]
425
+ xml.tag! 'productSKU', value[:sku]
426
+ end
427
+ end
428
+ end
429
+
430
+ def add_merchant_data(xml, options)
431
+ xml.tag! 'merchantID', @options[:login]
432
+ xml.tag! 'merchantReferenceCode', options[:order_id]
433
+ xml.tag! 'clientLibrary' ,'Ruby Active Merchant'
434
+ xml.tag! 'clientLibraryVersion', VERSION
435
+ xml.tag! 'clientEnvironment' , RUBY_PLATFORM
436
+ end
437
+
438
+ def add_purchase_data(xml, money = 0, include_grand_total = false, options={})
439
+ xml.tag! 'purchaseTotals' do
440
+ xml.tag! 'currency', options[:currency] || currency(money)
441
+ xml.tag!('grandTotalAmount', amount(money)) if include_grand_total
442
+ end
443
+ end
444
+
445
+ def add_address(xml, payment_method, address, options, shipTo = false)
446
+ xml.tag! shipTo ? 'shipTo' : 'billTo' do
447
+ xml.tag! 'firstName', payment_method.first_name if payment_method
448
+ xml.tag! 'lastName', payment_method.last_name if payment_method
449
+ xml.tag! 'street1', address[:address1]
450
+ xml.tag! 'street2', address[:address2] unless address[:address2].blank?
451
+ xml.tag! 'city', address[:city]
452
+ xml.tag! 'state', address[:state]
453
+ xml.tag! 'postalCode', address[:zip]
454
+ xml.tag! 'country', address[:country]
455
+ xml.tag! 'company', address[:company] unless address[:company].blank?
456
+ xml.tag! 'companyTaxID', address[:companyTaxID] unless address[:company_tax_id].blank?
457
+ xml.tag! 'phoneNumber', address[:phone] unless address[:phone].blank?
458
+ xml.tag! 'email', options[:email]
459
+ xml.tag! 'driversLicenseNumber', options[:drivers_license_number] unless options[:drivers_license_number].blank?
460
+ xml.tag! 'driversLicenseState', options[:drivers_license_state] unless options[:drivers_license_state].blank?
461
+ end
462
+ end
463
+
464
+ def add_creditcard(xml, creditcard)
465
+ xml.tag! 'card' do
466
+ xml.tag! 'accountNumber', creditcard.number
467
+ xml.tag! 'expirationMonth', format(creditcard.month, :two_digits)
468
+ xml.tag! 'expirationYear', format(creditcard.year, :four_digits)
469
+ xml.tag!('cvNumber', creditcard.verification_value) unless (@options[:ignore_cvv] || creditcard.verification_value.blank? )
470
+ xml.tag! 'cardType', @@credit_card_codes[card_brand(creditcard).to_sym]
471
+ end
472
+ end
473
+
474
+ def add_check(xml, check)
475
+ xml.tag! 'check' do
476
+ xml.tag! 'accountNumber', check.account_number
477
+ xml.tag! 'accountType', check.account_type[0]
478
+ xml.tag! 'bankTransitNumber', check.routing_number
479
+ end
480
+ end
481
+
482
+ def add_tax_service(xml)
483
+ xml.tag! 'taxService', {'run' => 'true'} do
484
+ xml.tag!('nexus', @options[:nexus]) unless @options[:nexus].blank?
485
+ xml.tag!('sellerRegistration', @options[:vat_reg_number]) unless @options[:vat_reg_number].blank?
486
+ end
487
+ end
488
+
489
+ def add_auth_service(xml, payment_method, options)
490
+ if network_tokenization?(payment_method)
491
+ add_network_tokenization(xml, payment_method, options)
492
+ else
493
+ xml.tag! 'ccAuthService', {'run' => 'true'}
494
+ end
495
+ end
496
+
497
+ def network_tokenization?(payment_method)
498
+ payment_method.is_a?(NetworkTokenizationCreditCard)
499
+ end
500
+
501
+ def add_network_tokenization(xml, payment_method, options)
502
+ return unless network_tokenization?(payment_method)
503
+
504
+ case card_brand(payment_method).to_sym
505
+ when :visa
506
+ xml.tag! 'ccAuthService', {'run' => 'true'} do
507
+ xml.tag!("cavv", payment_method.payment_cryptogram)
508
+ xml.tag!("commerceIndicator", "vbv")
509
+ xml.tag!("xid", payment_method.payment_cryptogram)
510
+ end
511
+ when :mastercard
512
+ xml.tag! 'ucaf' do
513
+ xml.tag!("authenticationData", payment_method.payment_cryptogram)
514
+ xml.tag!("collectionIndicator", "2")
515
+ end
516
+ xml.tag! 'ccAuthService', {'run' => 'true'} do
517
+ xml.tag!("commerceIndicator", "spa")
518
+ end
519
+ when :american_express
520
+ cryptogram = Base64.decode64(payment_method.payment_cryptogram)
521
+ xml.tag! 'ccAuthService', {'run' => 'true'} do
522
+ xml.tag!("cavv", Base64.encode64(cryptogram[0...20]))
523
+ xml.tag!("commerceIndicator", "aesk")
524
+ xml.tag!("xid", Base64.encode64(cryptogram[20...40]))
525
+ end
526
+ end
527
+
528
+ xml.tag! 'paymentNetworkToken' do
529
+ xml.tag!('transactionType', "1")
530
+ end
531
+ end
532
+
533
+ def add_capture_service(xml, request_id, request_token)
534
+ xml.tag! 'ccCaptureService', {'run' => 'true'} do
535
+ xml.tag! 'authRequestID', request_id
536
+ xml.tag! 'authRequestToken', request_token
537
+ end
538
+ end
539
+
540
+ def add_purchase_service(xml, payment_method, options)
541
+ if options[:pinless_debit_card]
542
+ xml.tag! 'pinlessDebitService', {'run' => 'true'}
543
+ else
544
+ add_auth_service(xml, payment_method, options)
545
+ xml.tag! 'ccCaptureService', {'run' => 'true'}
546
+ end
547
+ end
548
+
549
+ def add_void_service(xml, request_id, request_token)
550
+ xml.tag! 'voidService', {'run' => 'true'} do
551
+ xml.tag! 'voidRequestID', request_id
552
+ xml.tag! 'voidRequestToken', request_token
553
+ end
554
+ end
555
+
556
+ def add_auth_reversal_service(xml, request_id, request_token)
557
+ xml.tag! 'ccAuthReversalService', {'run' => 'true'} do
558
+ xml.tag! 'authRequestID', request_id
559
+ xml.tag! 'authRequestToken', request_token
560
+ end
561
+ end
562
+
563
+ def add_credit_service(xml, request_id = nil, request_token = nil)
564
+ xml.tag! 'ccCreditService', {'run' => 'true'} do
565
+ xml.tag! 'captureRequestID', request_id if request_id
566
+ xml.tag! 'captureRequestToken', request_token if request_token
567
+ end
568
+ end
569
+
570
+ def add_check_service(xml)
571
+ xml.tag! 'ecDebitService', {'run' => 'true'}
572
+ end
573
+
574
+ def add_subscription_create_service(xml, options)
575
+ xml.tag! 'paySubscriptionCreateService', {'run' => 'true'}
576
+ end
577
+
578
+ def add_subscription_update_service(xml, options)
579
+ xml.tag! 'paySubscriptionUpdateService', {'run' => 'true'}
580
+ end
581
+
582
+ def add_subscription_delete_service(xml, options)
583
+ xml.tag! 'paySubscriptionDeleteService', {'run' => 'true'}
584
+ end
585
+
586
+ def add_subscription_retrieve_service(xml, options)
587
+ xml.tag! 'paySubscriptionRetrieveService', {'run' => 'true'}
588
+ end
589
+
590
+ def add_subscription(xml, options, reference = nil)
591
+ options[:subscription] ||= {}
592
+
593
+ xml.tag! 'recurringSubscriptionInfo' do
594
+ if reference
595
+ _, subscription_id, _ = reference.split(";")
596
+ xml.tag! 'subscriptionID', subscription_id
597
+ end
598
+
599
+ xml.tag! 'status', options[:subscription][:status] if options[:subscription][:status]
600
+ xml.tag! 'amount', amount(options[:subscription][:amount]) if options[:subscription][:amount]
601
+ xml.tag! 'numberOfPayments', options[:subscription][:occurrences] if options[:subscription][:occurrences]
602
+ xml.tag! 'automaticRenew', options[:subscription][:automatic_renew] if options[:subscription][:automatic_renew]
603
+ xml.tag! 'frequency', options[:subscription][:frequency] if options[:subscription][:frequency]
604
+ xml.tag! 'startDate', options[:subscription][:start_date].strftime("%Y%m%d") if options[:subscription][:start_date]
605
+ xml.tag! 'endDate', options[:subscription][:end_date].strftime("%Y%m%d") if options[:subscription][:end_date]
606
+ xml.tag! 'approvalRequired', options[:subscription][:approval_required] || false
607
+ xml.tag! 'event', options[:subscription][:event] if options[:subscription][:event]
608
+ xml.tag! 'billPayment', options[:subscription][:bill_payment] if options[:subscription][:bill_payment]
609
+ end
610
+ end
611
+
612
+ def add_creditcard_payment_method(xml)
613
+ xml.tag! 'subscription' do
614
+ xml.tag! 'paymentMethod', "credit card"
615
+ end
616
+ end
617
+
618
+ def add_check_payment_method(xml)
619
+ xml.tag! 'subscription' do
620
+ xml.tag! 'paymentMethod', "check"
621
+ end
622
+ end
623
+
624
+ def add_payment_method_or_subscription(xml, money, payment_method_or_reference, options)
625
+ if payment_method_or_reference.is_a?(String)
626
+ add_purchase_data(xml, money, true, options)
627
+ add_subscription(xml, options, payment_method_or_reference)
628
+ elsif card_brand(payment_method_or_reference) == 'check'
629
+ add_address(xml, payment_method_or_reference, options[:billing_address], options)
630
+ add_purchase_data(xml, money, true, options)
631
+ add_check(xml, payment_method_or_reference)
632
+ else
633
+ add_address(xml, payment_method_or_reference, options[:billing_address], options)
634
+ add_address(xml, payment_method_or_reference, options[:shipping_address], options, true)
635
+ add_purchase_data(xml, money, true, options)
636
+ add_creditcard(xml, payment_method_or_reference)
637
+ end
638
+ end
639
+
640
+ def add_validate_pinless_debit_service(xml)
641
+ xml.tag!'pinlessDebitValidateService', {'run' => 'true'}
642
+ end
643
+
644
+ # Where we actually build the full SOAP request using builder
645
+ def build_request(body, options)
646
+ xml = Builder::XmlMarkup.new :indent => 2
647
+ xml.instruct!
648
+ xml.tag! 's:Envelope', {'xmlns:s' => 'http://schemas.xmlsoap.org/soap/envelope/'} do
649
+ xml.tag! 's:Header' do
650
+ 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
651
+ xml.tag! 'wsse:UsernameToken' do
652
+ xml.tag! 'wsse:Username', @options[:login]
653
+ xml.tag! 'wsse:Password', @options[:password], 'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'
654
+ end
655
+ end
656
+ end
657
+ xml.tag! 's:Body', {'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema'} do
658
+ xml.tag! 'requestMessage', {'xmlns' => "urn:schemas-cybersource-com:transaction-data-#{XSD_VERSION}"} do
659
+ add_merchant_data(xml, options)
660
+ xml << body
661
+ end
662
+ end
663
+ end
664
+ xml.target!
665
+ end
666
+
667
+ # Contact CyberSource, make the SOAP request, and parse the reply into a
668
+ # Response object
669
+ def commit(request, options)
670
+ response = parse(ssl_post(test? ? self.test_url : self.live_url, build_request(request, options)))
671
+
672
+ success = response[:decision] == "ACCEPT"
673
+ message = @@response_codes[('r' + response[:reasonCode]).to_sym] rescue response[:message]
674
+ authorization = success ? [ options[:order_id], response[:requestID], response[:requestToken] ].compact.join(";") : nil
675
+
676
+ Response.new(success, message, response,
677
+ :test => test?,
678
+ :authorization => authorization,
679
+ :avs_result => { :code => response[:avsCode] },
680
+ :cvv_result => response[:cvCode]
681
+ )
682
+ end
683
+
684
+ # Parse the SOAP response
685
+ # Technique inspired by the Paypal Gateway
686
+ def parse(xml)
687
+ reply = {}
688
+ xml = REXML::Document.new(xml)
689
+ if root = REXML::XPath.first(xml, "//c:replyMessage")
690
+ root.elements.to_a.each do |node|
691
+ case node.name
692
+ when 'c:reasonCode'
693
+ reply[:message] = reply(node.text)
694
+ else
695
+ parse_element(reply, node)
696
+ end
697
+ end
698
+ elsif root = REXML::XPath.first(xml, "//soap:Fault")
699
+ parse_element(reply, root)
700
+ reply[:message] = "#{reply[:faultcode]}: #{reply[:faultstring]}"
701
+ end
702
+ return reply
703
+ end
704
+
705
+ def parse_element(reply, node)
706
+ if node.has_elements?
707
+ node.elements.each{|e| parse_element(reply, e) }
708
+ else
709
+ if node.parent.name =~ /item/
710
+ parent = node.parent.name + (node.parent.attributes["id"] ? "_" + node.parent.attributes["id"] : '')
711
+ reply[(parent + '_' + node.name).to_sym] = node.text
712
+ else
713
+ reply[node.name.to_sym] = node.text
714
+ end
715
+ end
716
+ return reply
717
+ end
718
+ end
719
+ end
720
+ end