/vendor/gems/ruby/1.9.1/gems/activemerchant-1.15.0/lib/active_merchant/billing/gateways/sallie_mae.rb

https://github.com/pgsql/spree · Ruby · 144 lines · 115 code · 25 blank · 4 comment · 12 complexity · 0a277938ff06402caf80e37468720dc6 MD5 · raw file

  1. module ActiveMerchant #:nodoc:
  2. module Billing #:nodoc:
  3. class SallieMaeGateway < Gateway
  4. URL = 'https://trans.salliemae.com/cgi-bin/process.cgi'
  5. # The countries the gateway supports merchants from as 2 digit ISO country codes
  6. self.supported_countries = ['US']
  7. # The card types supported by the payment gateway
  8. self.supported_cardtypes = [:visa, :master, :american_express, :discover]
  9. # The homepage URL of the gateway
  10. self.homepage_url = 'http://www.salliemae.com/'
  11. # The name of the gateway
  12. self.display_name = 'Sallie Mae'
  13. def initialize(options = {})
  14. requires!(options, :login)
  15. @options = options
  16. super
  17. end
  18. def test?
  19. @options[:login] == "TEST0"
  20. end
  21. def authorize(money, creditcard, options = {})
  22. post = PostData.new
  23. add_invoice(post, options)
  24. add_creditcard(post, creditcard)
  25. add_address(post, creditcard, options)
  26. add_customer_data(post, options)
  27. commit(:authonly, money, post)
  28. end
  29. def purchase(money, creditcard, options = {})
  30. post = PostData.new
  31. add_invoice(post, options)
  32. add_creditcard(post, creditcard)
  33. add_address(post, creditcard, options)
  34. add_customer_data(post, options)
  35. commit(:sale, money, post)
  36. end
  37. def capture(money, authorization, options = {})
  38. post = PostData.new
  39. post[:postonly] = authorization
  40. commit(:capture, money, post)
  41. end
  42. private
  43. def add_customer_data(post, options)
  44. if address = options[:billing_address] || options[:shipping_address] || options[:address]
  45. post[:ci_phone] = address[:phone].to_s
  46. end
  47. post[:ci_email] = options[:email].to_s unless options[:email].blank?
  48. post[:ci_IP] = options[:ip].to_s unless options[:ip].blank?
  49. end
  50. def add_address(post, creditcard, options)
  51. if address = options[:billing_address] || options[:address]
  52. post[:ci_billaddr1] = address[:address1].to_s
  53. post[:ci_billaddr2] = address[:address2].to_s unless address[:address2].blank?
  54. post[:ci_billcity] = address[:city].to_s
  55. post[:ci_billstate] = address[:state].to_s
  56. post[:ci_billzip] = address[:zip].to_s
  57. end
  58. if shipping_address = options[:shipping_address] || options[:address]
  59. post[:ci_shipaddr1] = shipping_address[:address1].to_s
  60. post[:ci_shipaddr2] = shipping_address[:address2].to_s unless shipping_address[:address2].blank?
  61. post[:ci_shipcity] = shipping_address[:city].to_s
  62. post[:ci_shipstate] = shipping_address[:state].to_s
  63. post[:ci_shipzip] = shipping_address[:zip].to_s
  64. end
  65. end
  66. def add_invoice(post, options)
  67. memo = "OrderID: #{options[:order_id]}\nDescription: #{options[:description]}"
  68. post[:ci_memo] = memo
  69. end
  70. def add_creditcard(post, creditcard)
  71. post[:ccnum] = creditcard.number.to_s
  72. post[:ccname] = creditcard.name.to_s
  73. post[:cvv2] = creditcard.verification_value.to_s if creditcard.verification_value?
  74. post[:expmon] = creditcard.month.to_s
  75. post[:expyear] = creditcard.year.to_s
  76. end
  77. def parse(body)
  78. h = {}
  79. body.gsub!("<html><body><plaintext>", "")
  80. body.
  81. split("\r\n").
  82. map do |i|
  83. a = i.split("=")
  84. h[a.first] = a.last unless a.first.nil?
  85. end
  86. h
  87. end
  88. def commit(action, money, parameters)
  89. parameters[:acctid] = @options[:login].to_s
  90. parameters[:subid] = @options[:sub_id].to_s unless @options[:sub_id].blank?
  91. parameters[:amount] = amount(money)
  92. case action
  93. when :sale
  94. parameters[:action] = "ns_quicksale_cc"
  95. when :authonly
  96. parameters[:action] = "ns_quicksale_cc"
  97. parameters[:authonly] = 1
  98. when :capture
  99. parameters[:action] = "ns_quicksale_cc"
  100. end
  101. response = parse(ssl_post(URL, parameters.to_post_data) || "")
  102. Response.new(successful?(response), message_from(response), response,
  103. :test => test?,
  104. :authorization => response["refcode"]
  105. )
  106. end
  107. def successful?(response)
  108. response["Status"] == "Accepted"
  109. end
  110. def message_from(response)
  111. if successful?(response)
  112. "Accepted"
  113. else
  114. response["Reason"].split(":")[2].capitalize unless response["Reason"].nil?
  115. end
  116. end
  117. end
  118. end
  119. end