Skip to content

Commit

Permalink
Update Instapay gateway to support capture and add address, order, an…
Browse files Browse the repository at this point in the history
…d invoice fields. Add support for CVV and AVS response
  • Loading branch information
Cody Fauser committed May 4, 2009
1 parent eeeda22 commit 39a7ce0
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 102 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
= ActiveMerchant CHANGELOG

* Add support for InstaPay gateway [brahma]
* Update Instapay gateway to support capture and add address, order, and invoice fields. Add support for CVV and AVS response [cody]
* Add support for Instapay gateway [brahma]
* Cleanup PaymentExpress reference purchases and turn on AVS [cody]
* Add reference purchases and authorizations to PaymentExpress [mocra]
* Add support for Merchant e-Solutions Gateway [Zac Williams, Robby Russell]
Expand Down
111 changes: 88 additions & 23 deletions lib/active_merchant/billing/gateways/instapay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,79 +15,144 @@ class InstapayGateway < Gateway

# The name of the gateway
self.display_name = 'InstaPay'

SUCCESS = "Accepted"
SUCCESS_MESSAGE = "The transaction has been approved"

def initialize(options = {})
requires!(options, :acctid)
requires!(options, :login)
@options = options
super
end

def authorize(money, creditcard, options = {})
post = {}
post[:authonly] = 1
add_amount(post, money)
add_invoice(post, options)
add_creditcard(post, creditcard)
add_address(post, creditcard, options)
add_address(post, options)
add_customer_data(post, options)

commit('ns_quicksale_cc', money, post)
commit('ns_quicksale_cc', post)
end

def purchase(money, creditcard, options = {})
post = {}
add_amount(post, money)
add_invoice(post, options)
add_creditcard(post, creditcard)
add_address(post, creditcard, options)
add_address(post, options)
add_customer_data(post, options)

commit('ns_quicksale_cc', money, post)
commit('ns_quicksale_cc', post)
end

def capture(money, authorization, options = {})
post = {}
add_amount(post, money)
add_reference(post, authorization)
commit('ns_quicksale_cc', post)
end

private


def add_amount(post, money)
post[:amount] = amount(money)
end

def add_reference(post, reference)
post[:postonly] = reference
end

def add_customer_data(post, options)
post[:ci_email] = options[:email]
post["ci_IP Address"] = options[:ip]
end

def add_address(post, creditcard, options)
def add_address(post, options)
if address = options[:billing_address] || options[:address]
post[:ci_billaddr1] = address[:address1]
post[:ci_billaddr2] = address[:address2]
post[:ci_billcity] = address[:city]
post[:ci_billstate] = address[:state]
post[:ci_billzip] = address[:zip]
post[:ci_billcountry] = address[:country]
post[:ci_phone] = address[:phone]
end

if address = options[:shipping_address]
post[:ci_shipaddr1] = address[:address1]
post[:ci_shipaddr2] = address[:address2]
post[:ci_shipcity] = address[:city]
post[:ci_shipstate] = address[:state]
post[:ci_shipzip] = address[:zip]
post[:ci_shipcountry] = address[:country]
end
end

def add_invoice(post, options)
post[:merchantordernumber] = options[:order_id]
post[:ci_memo] = options[:description]
post[:pocustomerrefid] = options[:invoice]
end

def add_creditcard(post, creditcard)
post[:ccnum] = creditcard.number
post[:expmon] = format(creditcard.month, :two_digits)
post[:expmon] = format(creditcard.month, :two_digits)
post[:cvv2] = creditcard.verification_value if creditcard.verification_value?
post[:expyear] = creditcard.year
post[:ccname] = creditcard.name
post[:ccname] = creditcard.name
end

def parse(body)
results = {}
fields = body.split("\n")
response = fields[1].split('=')
results[:response_code]= response[0]
responsedata = response[1].split(':')
results[:transaction_id] = responsedata[4]
fields = body.split("\r\n")

response = fields[1].split('=')
response_data = response[1].split(':')

if response[0] == SUCCESS
results[:success] = true
results[:message] = SUCCESS_MESSAGE
results[:transaction_type] = response_data[0]
results[:authorization_code] = response_data[1]
results[:reference_number] = response_data[2]
results[:batch_number] = response_data[3]
results[:transaction_id] = response_data[4]
results[:avs_result] = response_data[5]
results[:authorize_net] = response_data[6]
results[:cvv_result] = response_data[7]
else
results[:success] = false
results[:result] = response_data[0]
results[:response_code] = response_data[1]
results[:message] = response_data[2]
end

fields[1..-1].each do |pair|
key, value = pair.split('=')
results[key] = value
end
results
end

def commit(action, money, parameters)
parameters[:amount] = amount(money)
def commit(action, parameters)
data = ssl_post GATEWAY_URL , post_data(action, parameters)
response = parse(data)
message = response[:response_code]
success = response[:response_code] == 'Accepted'
Response.new(success , message, response,
:authorization => response[:transaction_id]

Response.new(response[:success] , response[:message], response,
:authorization => response[:transaction_id],
:avs_result => { :code => response[:avs_result] },
:cvv_result => response[:cvv_result]
)
end

def post_data(action, parameters = {})
post = {}
post[:acctid] = @options[:acctid]
if(@options[:merchantpin])
post[:merchantpin] = @options[:merchantpin]
post[:acctid] = @options[:login]
if(@options[:password])
post[:merchantpin] = @options[:password]
end
post[:action] = action
request = post.merge(parameters).collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join("&")
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ ideal_ing_postbank:
pem: |--
PASTE YOUR PEM FILE HERE
instapay:
login: TEST0
password:

linkpoint:
login: STOREID
pem: |--
Expand Down
37 changes: 27 additions & 10 deletions test/remote/gateways/remote_instapay_test.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,61 @@
require File.dirname(__FILE__) + '/../../test_helper'

class RemoteInstapayTest < Test::Unit::TestCase



def setup
@gateway = InstapayGateway.new(fixtures(:instapay))

@amount = 100
@credit_card = credit_card('5454545454545454')
@declined_card = credit_card('4000300011112220')
@authorization = '92888036'

@options = {
:order_id => '1',
:order_id => generate_unique_id,
:billing_address => address,
:shipping_address => address,
:description => 'Store Purchase'
}
end

def test_successful_purchase
assert response = @gateway.purchase(@amount, @credit_card, @options)
assert_success response
assert_equal 'Accepted', response.message
assert_equal InstapayGateway::SUCCESS_MESSAGE, response.message
end

def test_failed_purchase
assert response = @gateway.purchase(@amount, @declined_card, @options)
assert_failure response
assert_equal 'Declined', response.message
end

def test_succesful_auth
def test_succesful_authorization
assert response = @gateway.authorize(@amount, @credit_card, @options)
assert_success response
assert_equal 'Accepted', response.message
assert_equal InstapayGateway::SUCCESS_MESSAGE, response.message
end

def test_failed_auth
def test_failed_authorization
assert response = @gateway.authorize(@amount, @declined_card, @options)
assert_failure response
assert_equal 'Declined', response.message
end

def test_authorization_and_capture
assert authorization = @gateway.authorize(@amount, @credit_card, @options)
assert_success authorization

assert capture = @gateway.capture(@amount, authorization.authorization)
assert_success capture
assert_equal InstapayGateway::SUCCESS_MESSAGE, capture.message
end

def test_invalid_login
gateway = InstapayGateway.new(
:login => 'X',
:password => 'Y'
)

assert response = gateway.purchase(@amount, @credit_card)
assert_failure response
assert_equal "Invalid merchant", response.message
end
end
Loading

0 comments on commit 39a7ce0

Please sign in to comment.