glebm-activemerchant 1.7.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. data/CHANGELOG +535 -0
  2. data/CONTRIBUTORS +142 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +136 -0
  5. data/gem-public_cert.pem +20 -0
  6. data/lib/active_merchant.rb +46 -0
  7. data/lib/active_merchant/billing.rb +9 -0
  8. data/lib/active_merchant/billing/avs_result.rb +98 -0
  9. data/lib/active_merchant/billing/base.rb +57 -0
  10. data/lib/active_merchant/billing/check.rb +68 -0
  11. data/lib/active_merchant/billing/credit_card.rb +159 -0
  12. data/lib/active_merchant/billing/credit_card_formatting.rb +21 -0
  13. data/lib/active_merchant/billing/credit_card_methods.rb +125 -0
  14. data/lib/active_merchant/billing/cvv_result.rb +38 -0
  15. data/lib/active_merchant/billing/expiry_date.rb +34 -0
  16. data/lib/active_merchant/billing/gateway.rb +163 -0
  17. data/lib/active_merchant/billing/gateways.rb +18 -0
  18. data/lib/active_merchant/billing/gateways/authorize_net.rb +654 -0
  19. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +736 -0
  20. data/lib/active_merchant/billing/gateways/beanstream.rb +102 -0
  21. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +244 -0
  22. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +54 -0
  23. data/lib/active_merchant/billing/gateways/bogus.rb +102 -0
  24. data/lib/active_merchant/billing/gateways/braintree.rb +17 -0
  25. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +9 -0
  26. data/lib/active_merchant/billing/gateways/braintree_blue.rb +210 -0
  27. data/lib/active_merchant/billing/gateways/braintree_orange.rb +17 -0
  28. data/lib/active_merchant/billing/gateways/card_stream.rb +230 -0
  29. data/lib/active_merchant/billing/gateways/cyber_source.rb +406 -0
  30. data/lib/active_merchant/billing/gateways/data_cash.rb +593 -0
  31. data/lib/active_merchant/billing/gateways/efsnet.rb +229 -0
  32. data/lib/active_merchant/billing/gateways/elavon.rb +134 -0
  33. data/lib/active_merchant/billing/gateways/eway.rb +277 -0
  34. data/lib/active_merchant/billing/gateways/exact.rb +222 -0
  35. data/lib/active_merchant/billing/gateways/first_pay.rb +172 -0
  36. data/lib/active_merchant/billing/gateways/garanti.rb +222 -0
  37. data/lib/active_merchant/billing/gateways/instapay.rb +164 -0
  38. data/lib/active_merchant/billing/gateways/jetpay.rb +270 -0
  39. data/lib/active_merchant/billing/gateways/linkpoint.rb +449 -0
  40. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +154 -0
  41. data/lib/active_merchant/billing/gateways/merchant_ware.rb +283 -0
  42. data/lib/active_merchant/billing/gateways/modern_payments.rb +36 -0
  43. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +220 -0
  44. data/lib/active_merchant/billing/gateways/moneris.rb +205 -0
  45. data/lib/active_merchant/billing/gateways/net_registry.rb +189 -0
  46. data/lib/active_merchant/billing/gateways/netbilling.rb +168 -0
  47. data/lib/active_merchant/billing/gateways/ogone.rb +279 -0
  48. data/lib/active_merchant/billing/gateways/pay_junction.rb +392 -0
  49. data/lib/active_merchant/billing/gateways/pay_secure.rb +120 -0
  50. data/lib/active_merchant/billing/gateways/payflow.rb +236 -0
  51. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +207 -0
  52. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +39 -0
  53. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  54. data/lib/active_merchant/billing/gateways/payflow_express.rb +138 -0
  55. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +15 -0
  56. data/lib/active_merchant/billing/gateways/payflow_uk.rb +21 -0
  57. data/lib/active_merchant/billing/gateways/payment_express.rb +230 -0
  58. data/lib/active_merchant/billing/gateways/paypal.rb +121 -0
  59. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +326 -0
  60. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +38 -0
  61. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  62. data/lib/active_merchant/billing/gateways/paypal_express.rb +135 -0
  63. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +20 -0
  64. data/lib/active_merchant/billing/gateways/plugnpay.rb +292 -0
  65. data/lib/active_merchant/billing/gateways/psigate.rb +214 -0
  66. data/lib/active_merchant/billing/gateways/psl_card.rb +304 -0
  67. data/lib/active_merchant/billing/gateways/quickpay.rb +213 -0
  68. data/lib/active_merchant/billing/gateways/realex.rb +200 -0
  69. data/lib/active_merchant/billing/gateways/sage.rb +146 -0
  70. data/lib/active_merchant/billing/gateways/sage/sage_bankcard.rb +88 -0
  71. data/lib/active_merchant/billing/gateways/sage/sage_core.rb +116 -0
  72. data/lib/active_merchant/billing/gateways/sage/sage_virtual_check.rb +97 -0
  73. data/lib/active_merchant/billing/gateways/sage_pay.rb +317 -0
  74. data/lib/active_merchant/billing/gateways/sallie_mae.rb +144 -0
  75. data/lib/active_merchant/billing/gateways/secure_pay.rb +31 -0
  76. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +157 -0
  77. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +113 -0
  78. data/lib/active_merchant/billing/gateways/skip_jack.rb +453 -0
  79. data/lib/active_merchant/billing/gateways/smart_ps.rb +265 -0
  80. data/lib/active_merchant/billing/gateways/trans_first.rb +127 -0
  81. data/lib/active_merchant/billing/gateways/transax.rb +25 -0
  82. data/lib/active_merchant/billing/gateways/trust_commerce.rb +418 -0
  83. data/lib/active_merchant/billing/gateways/usa_epay.rb +194 -0
  84. data/lib/active_merchant/billing/gateways/verifi.rb +228 -0
  85. data/lib/active_merchant/billing/gateways/viaklix.rb +189 -0
  86. data/lib/active_merchant/billing/gateways/wirecard.rb +318 -0
  87. data/lib/active_merchant/billing/integrations.rb +17 -0
  88. data/lib/active_merchant/billing/integrations/action_view_helper.rb +76 -0
  89. data/lib/active_merchant/billing/integrations/bogus.rb +23 -0
  90. data/lib/active_merchant/billing/integrations/bogus/helper.rb +17 -0
  91. data/lib/active_merchant/billing/integrations/bogus/notification.rb +11 -0
  92. data/lib/active_merchant/billing/integrations/bogus/return.rb +10 -0
  93. data/lib/active_merchant/billing/integrations/chronopay.rb +23 -0
  94. data/lib/active_merchant/billing/integrations/chronopay/helper.rb +120 -0
  95. data/lib/active_merchant/billing/integrations/chronopay/notification.rb +158 -0
  96. data/lib/active_merchant/billing/integrations/chronopay/return.rb +10 -0
  97. data/lib/active_merchant/billing/integrations/gestpay.rb +25 -0
  98. data/lib/active_merchant/billing/integrations/gestpay/common.rb +42 -0
  99. data/lib/active_merchant/billing/integrations/gestpay/helper.rb +70 -0
  100. data/lib/active_merchant/billing/integrations/gestpay/notification.rb +85 -0
  101. data/lib/active_merchant/billing/integrations/gestpay/return.rb +10 -0
  102. data/lib/active_merchant/billing/integrations/helper.rb +93 -0
  103. data/lib/active_merchant/billing/integrations/hi_trust.rb +27 -0
  104. data/lib/active_merchant/billing/integrations/hi_trust/helper.rb +58 -0
  105. data/lib/active_merchant/billing/integrations/hi_trust/notification.rb +59 -0
  106. data/lib/active_merchant/billing/integrations/hi_trust/return.rb +67 -0
  107. data/lib/active_merchant/billing/integrations/nochex.rb +88 -0
  108. data/lib/active_merchant/billing/integrations/nochex/helper.rb +68 -0
  109. data/lib/active_merchant/billing/integrations/nochex/notification.rb +94 -0
  110. data/lib/active_merchant/billing/integrations/nochex/return.rb +10 -0
  111. data/lib/active_merchant/billing/integrations/notification.rb +62 -0
  112. data/lib/active_merchant/billing/integrations/paypal.rb +39 -0
  113. data/lib/active_merchant/billing/integrations/paypal/helper.rb +119 -0
  114. data/lib/active_merchant/billing/integrations/paypal/notification.rb +154 -0
  115. data/lib/active_merchant/billing/integrations/paypal/return.rb +10 -0
  116. data/lib/active_merchant/billing/integrations/quickpay.rb +17 -0
  117. data/lib/active_merchant/billing/integrations/quickpay/helper.rb +72 -0
  118. data/lib/active_merchant/billing/integrations/quickpay/notification.rb +74 -0
  119. data/lib/active_merchant/billing/integrations/return.rb +35 -0
  120. data/lib/active_merchant/billing/integrations/two_checkout.rb +23 -0
  121. data/lib/active_merchant/billing/integrations/two_checkout/helper.rb +59 -0
  122. data/lib/active_merchant/billing/integrations/two_checkout/notification.rb +114 -0
  123. data/lib/active_merchant/billing/integrations/two_checkout/return.rb +17 -0
  124. data/lib/active_merchant/billing/response.rb +32 -0
  125. data/lib/active_merchant/common.rb +14 -0
  126. data/lib/active_merchant/common/connection.rb +172 -0
  127. data/lib/active_merchant/common/country.rb +319 -0
  128. data/lib/active_merchant/common/error.rb +26 -0
  129. data/lib/active_merchant/common/post_data.rb +24 -0
  130. data/lib/active_merchant/common/posts_data.rb +47 -0
  131. data/lib/active_merchant/common/requires_parameters.rb +16 -0
  132. data/lib/active_merchant/common/utils.rb +18 -0
  133. data/lib/active_merchant/common/validateable.rb +76 -0
  134. data/lib/active_merchant/version.rb +3 -0
  135. data/lib/activemerchant.rb +1 -0
  136. data/lib/certs/cacert.pem +7815 -0
  137. data/lib/support/gateway_support.rb +58 -0
  138. data/lib/support/outbound_hosts.rb +25 -0
  139. metadata +252 -0
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + '/sage_core'
2
+
3
+ module ActiveMerchant #:nodoc:
4
+ module Billing #:nodoc:
5
+ class SageBankcardGateway < Gateway #:nodoc:
6
+ include SageCore
7
+ self.url = 'https://www.sagepayments.net/cgi-bin/eftBankcard.dll?transaction'
8
+ self.source = 'bankcard'
9
+
10
+ # Credit cards supported by Sage
11
+ # * VISA
12
+ # * MasterCard
13
+ # * AMEX
14
+ # * Diners
15
+ # * Carte Blanche
16
+ # * Discover
17
+ # * JCB
18
+ # * Sears
19
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover, :jcb, :diners_club]
20
+
21
+ def authorize(money, credit_card, options = {})
22
+ post = {}
23
+ add_credit_card(post, credit_card)
24
+ add_transaction_data(post, money, options)
25
+ commit(:authorization, post)
26
+ end
27
+
28
+ def purchase(money, credit_card, options = {})
29
+ post = {}
30
+ add_credit_card(post, credit_card)
31
+ add_transaction_data(post, money, options)
32
+ commit(:purchase, post)
33
+ end
34
+
35
+ # The +money+ amount is not used. The entire amount of the
36
+ # initial authorization will be captured.
37
+ def capture(money, reference, options = {})
38
+ post = {}
39
+ add_reference(post, reference)
40
+ commit(:capture, post)
41
+ end
42
+
43
+ def void(reference, options = {})
44
+ post = {}
45
+ add_reference(post, reference)
46
+ commit(:void, post)
47
+ end
48
+
49
+ def credit(money, credit_card, options = {})
50
+ post = {}
51
+ add_credit_card(post, credit_card)
52
+ add_transaction_data(post, money, options)
53
+ commit(:credit, post)
54
+ end
55
+
56
+ private
57
+ def exp_date(credit_card)
58
+ year = sprintf("%.4i", credit_card.year)
59
+ month = sprintf("%.2i", credit_card.month)
60
+
61
+ "#{month}#{year[-2..-1]}"
62
+ end
63
+
64
+ def add_credit_card(post, credit_card)
65
+ post[:C_name] = credit_card.name
66
+ post[:C_cardnumber] = credit_card.number
67
+ post[:C_exp] = exp_date(credit_card)
68
+ post[:C_cvv] = credit_card.verification_value if credit_card.verification_value?
69
+ end
70
+
71
+ def parse(data)
72
+ response = {}
73
+ response[:success] = data[1,1]
74
+ response[:code] = data[2,6]
75
+ response[:message] = data[8,32].strip
76
+ response[:front_end] = data[40, 2]
77
+ response[:cvv_result] = data[42, 1]
78
+ response[:avs_result] = data[43, 1].strip
79
+ response[:risk] = data[44, 2]
80
+ response[:reference] = data[46, 10]
81
+
82
+ response[:order_number], response[:recurring] = data[57...-1].split("\034")
83
+ response
84
+ end
85
+ end
86
+ end
87
+ end
88
+
@@ -0,0 +1,116 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ module SageCore #:nodoc:
4
+ def self.included(base)
5
+ base.cattr_accessor :url
6
+ base.cattr_accessor :source
7
+ base.supported_countries = ['US', 'CA']
8
+ base.homepage_url = 'http://www.sagepayments.com'
9
+ base.display_name = 'Sage Payment Solutions'
10
+ end
11
+
12
+ # Transactions types:
13
+ # <tt>01</tt> - Sale
14
+ # <tt>02</tt> - AuthOnly
15
+ # <tt>03</tt> - Force/PriorAuthSale
16
+ # <tt>04</tt> - Void
17
+ # <tt>06</tt> - Credit
18
+ # <tt>11</tt> - PriorAuthSale by Reference*
19
+ TRANSACTIONS = {
20
+ :purchase => '01',
21
+ :authorization => '02',
22
+ :capture => '11',
23
+ :void => '04',
24
+ :credit => '06'
25
+ }
26
+
27
+ def initialize(options = {})
28
+ requires!(options, :login, :password)
29
+ @options = options
30
+ super
31
+ end
32
+
33
+ private
34
+ def add_invoice(post, options)
35
+ post[:T_ordernum] = options[:order_id].slice(0, 20)
36
+ post[:T_tax] = amount(options[:tax]) unless options[:tax].blank?
37
+ post[:T_shipping] = amount(options[:shipping]) unless options[:shipping].blank?
38
+ end
39
+
40
+ def add_reference(post, reference)
41
+ ref, source = reference.to_s.split(";")
42
+ post[:T_reference] = ref
43
+ end
44
+
45
+ def add_amount(post, money)
46
+ post[:T_amt] = amount(money)
47
+ end
48
+
49
+ def add_customer_data(post, options)
50
+ post[:T_customer_number] = options[:customer]
51
+ end
52
+
53
+ def add_addresses(post, options)
54
+ billing_address = options[:billing_address] || options[:address] || {}
55
+
56
+ post[:C_address] = billing_address[:address1]
57
+ post[:C_city] = billing_address[:city]
58
+
59
+ if ['US', 'CA'].include?(billing_address[:country])
60
+ post[:C_state] = billing_address[:state]
61
+ else
62
+ post[:C_state] = "Outside of United States"
63
+ end
64
+
65
+ post[:C_zip] = billing_address[:zip]
66
+ post[:C_country] = billing_address[:country]
67
+ post[:C_telephone] = billing_address[:phone]
68
+ post[:C_fax] = billing_address[:fax]
69
+ post[:C_email] = options[:email]
70
+
71
+ if shipping_address = options[:shipping_address]
72
+ post[:C_ship_name] = shipping_address[:name]
73
+ post[:C_ship_address] = shipping_address[:address1]
74
+ post[:C_ship_city] = shipping_address[:city]
75
+ post[:C_ship_state] = shipping_address[:state]
76
+ post[:C_ship_zip] = shipping_address[:zip]
77
+ post[:C_ship_country] = shipping_address[:country]
78
+ end
79
+ end
80
+
81
+ def add_transaction_data(post, money, options)
82
+ add_amount(post, money)
83
+ add_invoice(post, options)
84
+ add_addresses(post, options)
85
+ add_customer_data(post, options)
86
+ end
87
+
88
+ def commit(action, params)
89
+ response = parse(ssl_post(url, post_data(action, params)))
90
+
91
+ Response.new(success?(response), response[:message], response,
92
+ :test => test?,
93
+ :authorization => authorization_from(response),
94
+ :avs_result => { :code => response[:avs_result] },
95
+ :cvv_result => response[:cvv_result]
96
+ )
97
+ end
98
+
99
+ def authorization_from(response)
100
+ "#{response[:reference]};#{source}"
101
+ end
102
+
103
+ def success?(response)
104
+ response[:success] == 'A'
105
+ end
106
+
107
+ def post_data(action, params = {})
108
+ params[:M_id] = @options[:login]
109
+ params[:M_key] = @options[:password]
110
+ params[:T_code] = TRANSACTIONS[action]
111
+
112
+ params.collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join("&")
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,97 @@
1
+ require File.dirname(__FILE__) + '/sage_core'
2
+
3
+ module ActiveMerchant #:nodoc:
4
+ module Billing #:nodoc:
5
+ class SageVirtualCheckGateway < Gateway #:nodoc:
6
+ include SageCore
7
+ self.url = 'https://www.sagepayments.net/cgi-bin/eftVirtualCheck.dll?transaction'
8
+ self.source = 'virtual_check'
9
+
10
+ def purchase(money, credit_card, options = {})
11
+ post = {}
12
+ add_check(post, credit_card)
13
+ add_check_customer_data(post, options)
14
+ add_transaction_data(post, money, options)
15
+ commit(:purchase, post)
16
+ end
17
+
18
+ def void(reference, options = {})
19
+ post = {}
20
+ add_reference(post, reference)
21
+ commit(:void, post)
22
+ end
23
+
24
+ def credit(money, credit_card, options = {})
25
+ post = {}
26
+ add_check(post, credit_card)
27
+ add_check_customer_data(post, options)
28
+ add_transaction_data(post, money, options)
29
+ commit(:credit, post)
30
+ end
31
+
32
+ private
33
+ def add_check(post, check)
34
+ post[:C_first_name] = check.first_name
35
+ post[:C_last_name] = check.last_name
36
+ post[:C_rte] = check.routing_number
37
+ post[:C_acct] = check.account_number
38
+ post[:C_check_number] = check.number
39
+ post[:C_acct_type] = account_type(check)
40
+ end
41
+
42
+ def add_check_customer_data(post, options)
43
+ # Required  Customer Type – (NACHA Transaction Class)
44
+ # CCD for Commercial, Merchant Initiated
45
+ # PPD for Personal, Merchant Initiated
46
+ # WEB for Internet, Consumer Initiated
47
+ # RCK for Returned Checks
48
+ # ARC for Account Receivable Entry
49
+ # TEL for TelephoneInitiated
50
+ post[:C_customer_type] = "WEB"
51
+
52
+ # Optional  10  Digit Originator  ID – Assigned  By for  each transaction  class  or  business  purpose. If  not provided, the default Originator ID for the specific  Customer Type will be applied. 
53
+ post[:C_originator_id] = options[:originator_id]
54
+
55
+ # Optional  Transaction Addenda
56
+ post[:T_addenda] = options[:addenda]
57
+
58
+ # Required  Check  Writer  Social  Security  Number  (  Numbers Only, No Dashes ) 
59
+ post[:C_ssn] = options[:ssn].to_s.gsub(/[^\d]/, '')
60
+
61
+ post[:C_dl_state_code] = options[:drivers_license_state]
62
+ post[:C_dl_number] = options[:drivers_license_number]
63
+ post[:C_dob] = format_birth_date(options[:date_of_birth])
64
+ end
65
+
66
+ def format_birth_date(date)
67
+ date.respond_to?(:strftime) ? date.strftime("%m/%d/%Y") : date
68
+ end
69
+
70
+ # DDA for Checking
71
+ # SAV for Savings 
72
+ def account_type(check)
73
+ case check.account_type
74
+ when 'checking' then 'DDA'
75
+ when 'savings' then 'SAV'
76
+ else raise ArgumentError, "Unknown account type #{check.account_type}"
77
+ end
78
+ end
79
+
80
+ def parse(data)
81
+ response = {}
82
+ response[:success] = data[1,1]
83
+ response[:code] = data[2,6].strip
84
+ response[:message] = data[8,32].strip
85
+ response[:risk] = data[40, 2]
86
+ response[:reference] = data[42, 10]
87
+
88
+ extra_data = data[53...-1].split("\034")
89
+ response[:order_number] = extra_data[0]
90
+ response[:authentication_indicator] = extra_data[1]
91
+ response[:authentication_disclosure] = extra_data[2]
92
+ response
93
+ end
94
+ end
95
+ end
96
+ end
97
+
@@ -0,0 +1,317 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ class SagePayGateway < Gateway
4
+ cattr_accessor :simulate
5
+ self.simulate = false
6
+
7
+ TEST_URL = 'https://test.sagepay.com/gateway/service'
8
+ LIVE_URL = 'https://live.sagepay.com/gateway/service'
9
+ SIMULATOR_URL = 'https://test.sagepay.com/Simulator'
10
+
11
+ APPROVED = 'OK'
12
+
13
+ TRANSACTIONS = {
14
+ :purchase => 'PAYMENT',
15
+ :credit => 'REFUND',
16
+ :authorization => 'DEFERRED',
17
+ :capture => 'RELEASE',
18
+ :void => 'VOID',
19
+ :abort => 'ABORT'
20
+ }
21
+
22
+ CREDIT_CARDS = {
23
+ :visa => "VISA",
24
+ :master => "MC",
25
+ :delta => "DELTA",
26
+ :solo => "SOLO",
27
+ :switch => "MAESTRO",
28
+ :maestro => "MAESTRO",
29
+ :american_express => "AMEX",
30
+ :electron => "UKE",
31
+ :diners_club => "DC",
32
+ :jcb => "JCB"
33
+ }
34
+
35
+ CURRENCIES_WITHOUT_FRACTIONS = [ 'YEN' ]
36
+
37
+ ELECTRON = /^(424519|42496[23]|450875|48440[6-8]|4844[1-5][1-5]|4917[3-5][0-9]|491880)\d{10}(\d{3})?$/
38
+
39
+ AVS_CVV_CODE = {
40
+ "NOTPROVIDED" => nil,
41
+ "NOTCHECKED" => 'X',
42
+ "MATCHED" => 'Y',
43
+ "NOTMATCHED" => 'N'
44
+ }
45
+
46
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover, :jcb, :switch, :solo, :maestro, :diners_club]
47
+ self.supported_countries = ['GB']
48
+ self.default_currency = 'GBP'
49
+
50
+ self.homepage_url = 'http://www.sagepay.com'
51
+ self.display_name = 'SagePay'
52
+
53
+ def initialize(options = {})
54
+ requires!(options, :login)
55
+ @options = options
56
+ super
57
+ end
58
+
59
+ def test?
60
+ @options[:test] || super
61
+ end
62
+
63
+ def purchase(money, credit_card, options = {})
64
+ requires!(options, :order_id)
65
+
66
+ post = {}
67
+
68
+ add_amount(post, money, options)
69
+ add_invoice(post, options)
70
+ add_credit_card(post, credit_card)
71
+ add_address(post, options)
72
+ add_customer_data(post, options)
73
+
74
+ commit(:purchase, post)
75
+ end
76
+
77
+ def authorize(money, credit_card, options = {})
78
+ requires!(options, :order_id)
79
+
80
+ post = {}
81
+
82
+ add_amount(post, money, options)
83
+ add_invoice(post, options)
84
+ add_credit_card(post, credit_card)
85
+ add_address(post, options)
86
+ add_customer_data(post, options)
87
+
88
+ commit(:authorization, post)
89
+ end
90
+
91
+ # You can only capture a transaction once, even if you didn't capture the full amount the first time.
92
+ def capture(money, identification, options = {})
93
+ post = {}
94
+
95
+ add_reference(post, identification)
96
+ add_release_amount(post, money, options)
97
+
98
+ commit(:capture, post)
99
+ end
100
+
101
+ def void(identification, options = {})
102
+ post = {}
103
+
104
+ add_reference(post, identification)
105
+ action = abort_or_void_from(identification)
106
+
107
+ commit(action, post)
108
+ end
109
+
110
+ # Crediting requires a new order_id to passed in, as well as a description
111
+ def credit(money, identification, options = {})
112
+ requires!(options, :order_id, :description)
113
+
114
+ post = {}
115
+
116
+ add_credit_reference(post, identification)
117
+ add_amount(post, money, options)
118
+ add_invoice(post, options)
119
+
120
+ commit(:credit, post)
121
+ end
122
+
123
+ private
124
+ def add_reference(post, identification)
125
+ order_id, transaction_id, authorization, security_key = identification.split(';')
126
+
127
+ add_pair(post, :VendorTxCode, order_id)
128
+ add_pair(post, :VPSTxId, transaction_id)
129
+ add_pair(post, :TxAuthNo, authorization)
130
+ add_pair(post, :SecurityKey, security_key)
131
+ end
132
+
133
+ def add_credit_reference(post, identification)
134
+ order_id, transaction_id, authorization, security_key = identification.split(';')
135
+
136
+ add_pair(post, :RelatedVendorTxCode, order_id)
137
+ add_pair(post, :RelatedVPSTxId, transaction_id)
138
+ add_pair(post, :RelatedTxAuthNo, authorization)
139
+ add_pair(post, :RelatedSecurityKey, security_key)
140
+ end
141
+
142
+ def add_amount(post, money, options)
143
+ currency = options[:currency] || currency(money)
144
+ add_pair(post, :Amount, localized_amount(money, currency), :required => true)
145
+ add_pair(post, :Currency, currency, :required => true)
146
+ end
147
+
148
+ # doesn't actually use the currency -- dodgy!
149
+ def add_release_amount(post, money, options)
150
+ add_pair(post, :ReleaseAmount, amount(money), :required => true)
151
+ end
152
+
153
+ def add_customer_data(post, options)
154
+ add_pair(post, :CustomerEMail, options[:email][0,255]) unless options[:email].blank?
155
+ add_pair(post, :BillingPhone, options[:phone].gsub(/[^0-9+]/, '')[0,20]) unless options[:phone].blank?
156
+ add_pair(post, :ClientIPAddress, options[:ip])
157
+ end
158
+
159
+ def add_address(post, options)
160
+ if billing_address = options[:billing_address] || options[:address]
161
+ first_name, last_name = parse_first_and_last_name(billing_address[:name])
162
+ add_pair(post, :BillingSurname, last_name)
163
+ add_pair(post, :BillingFirstnames, first_name)
164
+ add_pair(post, :BillingAddress1, billing_address[:address1])
165
+ add_pair(post, :BillingAddress2, billing_address[:address2])
166
+ add_pair(post, :BillingCity, billing_address[:city])
167
+ add_pair(post, :BillingState, billing_address[:state]) if billing_address[:country] == 'US'
168
+ add_pair(post, :BillingCountry, billing_address[:country])
169
+ add_pair(post, :BillingPostCode, billing_address[:zip])
170
+ end
171
+
172
+ if shipping_address = options[:shipping_address] || billing_address
173
+ first_name, last_name = parse_first_and_last_name(shipping_address[:name])
174
+ add_pair(post, :DeliverySurname, last_name)
175
+ add_pair(post, :DeliveryFirstnames, first_name)
176
+ add_pair(post, :DeliveryAddress1, shipping_address[:address1])
177
+ add_pair(post, :DeliveryAddress2, shipping_address[:address2])
178
+ add_pair(post, :DeliveryCity, shipping_address[:city])
179
+ add_pair(post, :DeliveryState, shipping_address[:state]) if shipping_address[:country] == 'US'
180
+ add_pair(post, :DeliveryCountry, shipping_address[:country])
181
+ add_pair(post, :DeliveryPostCode, shipping_address[:zip])
182
+ end
183
+ end
184
+
185
+ def add_invoice(post, options)
186
+ add_pair(post, :VendorTxCode, sanitize_order_id(options[:order_id]), :required => true)
187
+ add_pair(post, :Description, options[:description] || options[:order_id])
188
+ end
189
+
190
+ def add_credit_card(post, credit_card)
191
+ add_pair(post, :CardHolder, credit_card.name, :required => true)
192
+ add_pair(post, :CardNumber, credit_card.number, :required => true)
193
+
194
+ add_pair(post, :ExpiryDate, format_date(credit_card.month, credit_card.year), :required => true)
195
+
196
+ if requires_start_date_or_issue_number?(credit_card)
197
+ add_pair(post, :StartDate, format_date(credit_card.start_month, credit_card.start_year))
198
+ add_pair(post, :IssueNumber, credit_card.issue_number)
199
+ end
200
+ add_pair(post, :CardType, map_card_type(credit_card))
201
+
202
+ add_pair(post, :CV2, credit_card.verification_value)
203
+ end
204
+
205
+ def sanitize_order_id(order_id)
206
+ order_id.to_s.gsub(/[^-a-zA-Z0-9._]/, '')
207
+ end
208
+
209
+ def map_card_type(credit_card)
210
+ raise ArgumentError, "The credit card type must be provided" if card_brand(credit_card).blank?
211
+
212
+ card_type = card_brand(credit_card).to_sym
213
+
214
+ # Check if it is an electron card
215
+ if card_type == :visa && credit_card.number =~ ELECTRON
216
+ CREDIT_CARDS[:electron]
217
+ else
218
+ CREDIT_CARDS[card_type]
219
+ end
220
+ end
221
+
222
+ # MMYY format
223
+ def format_date(month, year)
224
+ return nil if year.blank? || month.blank?
225
+
226
+ year = sprintf("%.4i", year)
227
+ month = sprintf("%.2i", month)
228
+
229
+ "#{month}#{year[-2..-1]}"
230
+ end
231
+
232
+ def commit(action, parameters)
233
+ response = parse( ssl_post(url_for(action), post_data(action, parameters)) )
234
+
235
+ Response.new(response["Status"] == APPROVED, message_from(response), response,
236
+ :test => test?,
237
+ :authorization => authorization_from(response, parameters, action),
238
+ :avs_result => {
239
+ :street_match => AVS_CVV_CODE[ response["AddressResult"] ],
240
+ :postal_match => AVS_CVV_CODE[ response["PostCodeResult"] ],
241
+ },
242
+ :cvv_result => AVS_CVV_CODE[ response["CV2Result"] ]
243
+ )
244
+ end
245
+
246
+ def authorization_from(response, params, action)
247
+ [ params[:VendorTxCode],
248
+ response["VPSTxId"],
249
+ response["TxAuthNo"],
250
+ response["SecurityKey"],
251
+ action ].join(";")
252
+ end
253
+
254
+ def abort_or_void_from(identification)
255
+ original_transaction = identification.split(';').last
256
+ original_transaction == 'authorization' ? :abort : :void
257
+ end
258
+
259
+ def url_for(action)
260
+ simulate ? build_simulator_url(action) : build_url(action)
261
+ end
262
+
263
+ def build_url(action)
264
+ endpoint = [ :purchase, :authorization ].include?(action) ? "vspdirect-register" : TRANSACTIONS[action].downcase
265
+ "#{test? ? TEST_URL : LIVE_URL}/#{endpoint}.vsp"
266
+ end
267
+
268
+ def build_simulator_url(action)
269
+ endpoint = [ :purchase, :authorization ].include?(action) ? "VSPDirectGateway.asp" : "VSPServerGateway.asp?Service=Vendor#{TRANSACTIONS[action].capitalize}Tx"
270
+ "#{SIMULATOR_URL}/#{endpoint}"
271
+ end
272
+
273
+ def message_from(response)
274
+ response['Status'] == APPROVED ? 'Success' : (response['StatusDetail'] || 'Unspecified error') # simonr 20080207 can't actually get non-nil blanks, so this is shorter
275
+ end
276
+
277
+ def post_data(action, parameters = {})
278
+ parameters.update(
279
+ :Vendor => @options[:login],
280
+ :TxType => TRANSACTIONS[action],
281
+ :VPSProtocol => "2.23"
282
+ )
283
+
284
+ parameters.collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join("&")
285
+ end
286
+
287
+ # SagePay returns data in the following format
288
+ # Key1=value1
289
+ # Key2=value2
290
+ def parse(body)
291
+ result = {}
292
+ body.to_s.each_line do |pair|
293
+ result[$1] = $2 if pair.strip =~ /\A([^=]+)=(.+)\Z/im
294
+ end
295
+ result
296
+ end
297
+
298
+ def add_pair(post, key, value, options = {})
299
+ post[key] = value if !value.blank? || options[:required]
300
+ end
301
+
302
+ def parse_first_and_last_name(value)
303
+ name = value.to_s.split(' ')
304
+
305
+ last_name = name.pop || ''
306
+ first_name = name.join(' ')
307
+ [ first_name[0,20], last_name[0,20] ]
308
+ end
309
+
310
+ def localized_amount(money, currency)
311
+ amount = amount(money)
312
+ CURRENCIES_WITHOUT_FRACTIONS.include?(currency.to_s) ? amount.split('.').first : amount
313
+ end
314
+ end
315
+ end
316
+ end
317
+