Skip to content

Commit

Permalink
EwayManagedGateway#purchase now sends only expected fields and can se…
Browse files Browse the repository at this point in the history
…t invoiceReference (from options[:order_id] or options[:invoice]) and invoiceDescription (from options[:description])
  • Loading branch information
moklett committed Aug 26, 2011
1 parent ecceaa1 commit 2b85332
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
40 changes: 36 additions & 4 deletions lib/active_merchant/billing/gateways/eway_managed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,25 @@ def update(billing_id, creditcard, options={})
commit("UpdateCustomer", post)
end

#process payment for given amount from stored CC "ManagedCustomerID = billing_id"
# Process a payment in the given amount against the stored credit card given by billing_id
#
# ==== Parameters
#
# * <tt>money</tt> -- The amount to be purchased as an Integer value in cents.
# * <tt>billing_id</tt> -- The eWay provided card/customer token to charge (managedCustomerID)
# * <tt>options</tt> -- A hash of optional parameters.
#
# ==== Options
#
# * <tt>:order_id</tt> -- The order number, passed to eWay as the "Invoice Reference"
# * <tt>:invoice</tt> -- The invoice number, passed to eWay as the "Invoice Reference" unless :order_id is also given
# * <tt>:description</tt> -- A description of the payment, passed to eWay as the "Invoice Description"
def purchase(money, billing_id, options={})
post = {}
post[:managedCustomerID] = billing_id.to_s
post[:amount]=money

add_invoice(post, options)

commit("ProcessPayment", post)
end

Expand Down Expand Up @@ -111,6 +124,12 @@ def add_misc_fields(post, options)
post[:Comments]=options[:description]
end

def add_invoice(post, options)
post[:invoiceReference] = options[:order_id] || options[:invoice]
post[:invoiceDescription] = options[:description]
end


# add credit card details to be stored by eway. NOTE eway requires "title" field
def add_creditcard(post, creditcard)
post[:CCNumber] = creditcard.number
Expand Down Expand Up @@ -189,7 +208,12 @@ def commit(action, post)
# Where we build the full SOAP 1.2 request using builder
def soap_request(arguments, action)
# eWay demands all fields be sent, but contain an empty string if blank
post=default_fields.merge(arguments)
post = case action
when 'ProcessPayment'
default_payment_fields.merge(arguments)
else
default_customer_fields.merge(arguments)
end

xml = Builder::XmlMarkup.new :indent => 2
xml.instruct!
Expand All @@ -212,14 +236,22 @@ def soap_request(arguments, action)
xml.target!
end

def default_fields
def default_customer_fields
hash={}
%w( CustomerRef Title FirstName LastName Company JobDesc Email Address Suburb State PostCode Country Phone Mobile Fax URL Comments CCNumber CCNameOnCard CCExpiryMonth CCExpiryYear ).each do |field|
hash[field.to_sym]=''
end
return hash
end

def default_payment_fields
hash={}
%w( managedCustomerID amount invoiceReference invoiceDescription ).each do |field|
hash[field.to_sym]=''
end
return hash
end

class EwayResponse < Response
# add a method to response so we can easily get the eway token "ManagedCustomerID"
def token
Expand Down
72 changes: 71 additions & 1 deletion test/unit/gateways/eway_managed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def setup
:email => '[email protected]',
:order_id => '1000',
:customer => 'mycustomerref',
:description => 'My Description'
:description => 'My Description',
:invoice => 'invoice-4567'
}
end

Expand Down Expand Up @@ -80,6 +81,51 @@ def test_successful_purchase
assert_equal "123456", response.authorization
assert response.test?
end

def test_expected_request_on_purchase
@gateway.expects(:ssl_post).with { |endpoint, data, headers|
# Compare the actual and expected XML documents, by converting them to Hashes first
expected = Hash.from_xml(expected_purchase_request)
actual = Hash.from_xml(data)
expected == actual
}.returns(successful_purchase_response)
@gateway.purchase(@amount, @valid_customer_id, @options)
end

def test_purchase_invoice_reference_comes_from_order_id_or_invoice
options = @options.dup

# invoiceReference == options[:order_id]
options[:order_id] = 'order_id'
options.delete(:invoice)

@gateway.expects(:ssl_post).with { |endpoint, data, headers|
request_hash = Hash.from_xml(data)
request_hash['Envelope']['Body']['ProcessPayment']['invoiceReference'] == 'order_id'
}.returns(successful_purchase_response)
@gateway.purchase(@amount, @valid_customer_id, options)

# invoiceReference == options[:invoice]
options[:invoice] = 'invoice'
options.delete(:order_id)

@gateway.expects(:ssl_post).with { |endpoint, data, headers|
request_hash = Hash.from_xml(data)
request_hash['Envelope']['Body']['ProcessPayment']['invoiceReference'] == 'invoice'
}.returns(successful_purchase_response)
@gateway.purchase(@amount, @valid_customer_id, options)

# invoiceReference == options[:order_id] || options[:invoice]
options[:order_id] = 'order_id'
options[:invoice] = 'invoice'

@gateway.expects(:ssl_post).with { |endpoint, data, headers|
request_hash = Hash.from_xml(data)
request_hash['Envelope']['Body']['ProcessPayment']['invoiceReference'] == 'order_id'
}.returns(successful_purchase_response)
@gateway.purchase(@amount, @valid_customer_id, options)

end

def test_invalid_customer_id
@gateway.expects(:ssl_post).returns(unsuccessful_authorization_response)
Expand Down Expand Up @@ -286,4 +332,28 @@ def expected_store_request
XML
end

# Documented here: https://www.eway.com.au/gateway/ManagedPaymentService/managedCreditCardPayment.asmx?op=CreateCustomer
def expected_purchase_request
<<-XML
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Header>
<eWAYHeader xmlns="https://www.eway.com.au/gateway/managedpayment">
<eWAYCustomerID>login</eWAYCustomerID>
<Username>username</Username>
<Password>password</Password>
</eWAYHeader>
</soap12:Header>
<soap12:Body>
<ProcessPayment xmlns="https://www.eway.com.au/gateway/managedpayment">
<managedCustomerID>#{@valid_customer_id}</managedCustomerID>
<amount>#{@amount}</amount>
<invoiceReference>#{@options[:order_id] || @options[:invoice]}</invoiceReference>
<invoiceDescription>#{@options[:description]}</invoiceDescription>
</ProcessPayment>
</soap12:Body>
</soap12:Envelope>
XML
end

end

0 comments on commit 2b85332

Please sign in to comment.