Skip to content
Browse files

Small refactoring of UsaEpayGateway

git-svn-id: https://activemerchant.googlecode.com/svn/trunk/active_merchant@583 6513ea26-6c20-0410-8a68-89cd7235086d
  • Loading branch information...
1 parent 3cc6bb1 commit 8c95aaf23e959affe4f39cdb0c3f1806ae599c6d codyfauser committed
Showing with 51 additions and 41 deletions.
  1. +1 −0 CHANGELOG
  2. +50 −41 lib/active_merchant/billing/gateways/usa_epay.rb
View
1 CHANGELOG
@@ -1,5 +1,6 @@
= ActiveMerchant CHANGELOG
+* Small refactoring of UsaEpayGateway [cody]
* Update UsaEpayGateway to support avs and cvv data. Remove test_result_from_cc_number. [cody]
* Replace all usage of :address with :billing_address in test cases [cody]
* Update TrustCommerce docs now that the gateway falls back to SSL post when tclink isn't available [cody]
View
91 lib/active_merchant/billing/gateways/usa_epay.rb
@@ -9,42 +9,60 @@ class UsaEpayGateway < Gateway
self.homepage_url = 'http://www.usaepay.com/'
self.display_name = 'USA ePay'
+ TRANSACTIONS = {
+ :authorization => 'authonly',
+ :purchase => 'sale',
+ :capture => 'capture'
+ }
+
def initialize(options = {})
requires!(options, :login)
@options = options
super
end
- def authorize(money, creditcard, options = {})
+ def authorize(money, credit_card, options = {})
post = {}
+
+ add_amount(post, money)
add_invoice(post, options)
- add_creditcard(post, creditcard)
- add_address(post, creditcard, options)
+ add_credit_card(post, credit_card)
+ add_address(post, credit_card, options)
add_customer_data(post, options)
- commit('authonly', money, post)
+ commit(:authorization, post)
end
- def purchase(money, creditcard, options = {})
+ def purchase(money, credit_card, options = {})
post = {}
+
+ add_amount(post, money)
add_invoice(post, options)
- add_creditcard(post, creditcard)
- add_address(post, creditcard, options)
+ add_credit_card(post, credit_card)
+ add_address(post, credit_card, options)
add_customer_data(post, options)
- commit('sale', money, post)
+ commit(:purchase, post)
end
def capture(money, authorization, options = {})
- post = {:refNum => authorization}
- commit('capture', money, post)
+ post = {
+ :refNum => authorization
+ }
+
+ add_amount(post, money)
+ commit(:capture, post)
end
private
- def expdate(creditcard)
- year = format(creditcard.year, :two_digits)
- month = format(creditcard.month, :two_digits)
+ def add_amount(post, money)
+ post[:amount] = amount(money)
+ end
+
+ def expdate(credit_card)
+ year = format(credit_card.year, :two_digits)
+ month = format(credit_card.month, :two_digits)
"#{month}#{year}"
end
@@ -68,18 +86,18 @@ def add_customer_data(post, options)
end
end
- def add_address(post, creditcard, options)
+ def add_address(post, credit_card, options)
billing_address = options[:billing_address] || options[:address]
- add_address_for_type(:billing, post, creditcard, billing_address) if billing_address
- add_address_for_type(:shipping, post, creditcard, options[:shipping_address]) if options[:shipping_address]
+ add_address_for_type(:billing, post, credit_card, billing_address) if billing_address
+ add_address_for_type(:shipping, post, credit_card, options[:shipping_address]) if options[:shipping_address]
end
- def add_address_for_type(type, post, creditcard, address)
+ def add_address_for_type(type, post, credit_card, address)
prefix = address_key_prefix(type)
- post[address_key(prefix, 'fname')] = creditcard.first_name
- post[address_key(prefix, 'lname')] = creditcard.last_name
+ post[address_key(prefix, 'fname')] = credit_card.first_name
+ post[address_key(prefix, 'lname')] = credit_card.last_name
post[address_key(prefix, 'company')] = address[:company] unless address[:company].blank?
post[address_key(prefix, 'street')] = address[:address1] unless address[:address1].blank?
post[address_key(prefix, 'street2')] = address[:address2] unless address[:address2].blank?
@@ -105,11 +123,11 @@ def add_invoice(post, options)
post[:invoice] = options[:order_id]
end
- def add_creditcard(post, creditcard)
- post[:card] = creditcard.number
- post[:cvv2] = creditcard.verification_value if creditcard.verification_value?
- post[:expir] = expdate(creditcard)
- post[:name] = creditcard.name
+ def add_credit_card(post, credit_card)
+ post[:card] = credit_card.number
+ post[:cvv2] = credit_card.verification_value if credit_card.verification_value?
+ post[:expir] = expdate(credit_card)
+ post[:name] = credit_card.name
end
def parse(body)
@@ -138,18 +156,10 @@ def parse(body)
end
- def commit(action, money, parameters)
- parameters[:software] = 'Active Merchant'
- parameters[:amount] = amount(money)
- parameters[:testmode] = @options[:test] ? 1 : 0
-
- data = ssl_post(URL, post_data(action, parameters))
+ def commit(action, parameters)
+ response = parse( ssl_post(URL, post_data(action, parameters)) )
- response = parse(data)
- success = response[:status] == 'Approved'
- message = message_from(response)
-
- Response.new(success, message, response,
+ Response.new(response[:status] == 'Approved', message_from(response), response,
:test => @options[:test] || test?,
:authorization => response[:ref_num],
:cvv_result => response[:cvv2_result_code],
@@ -171,13 +181,12 @@ def message_from(response)
end
def post_data(action, parameters = {})
- post = {}
-
- post[:command] = action
- post[:key] = @options[:login]
+ parameters[:command] = TRANSACTIONS[action]
+ parameters[:key] = @options[:login]
+ parameters[:software] = 'Active Merchant'
+ parameters[:testmode] = @options[:test] ? 1 : 0
- request = post.merge(parameters).collect { |key, value| "UM#{key}=#{CGI.escape(value.to_s)}" }.join("&")
- request
+ parameters.collect { |key, value| "UM#{key}=#{CGI.escape(value.to_s)}" }.join("&")
end
end
end

0 comments on commit 8c95aaf

Please sign in to comment.
Something went wrong with that request. Please try again.