First, make sure you have everything setup correctly and all of your dependencies in place with:
require 'rubygems' require 'active_merchant'
ActiveMerchant expects the amounts to be given as an Integer in cents. In this case, $10 US becomes 1000.
tendollar = 1000
Next, create a credit card object using a TC approved test card.
creditcard = ActiveMerchant::Billing::CreditCard.new( :number => '4111111111111111', :month => 8, :year => 2006, :first_name => 'Longbob', :last_name => 'Longsen' ) options = { :order_id => '1230123', :email => 'bob@testbob.com', :address => { :address1 => '47 Bobway', :city => 'Bobville', :state => 'WA', :country => 'Australia', :zip => '2000' } :description => 'purchased items' }
To finish setting up, create the active_merchant object you will be using, with the eWay gateway. If you have a functional eWay account, replace :login with your account info.
gateway = ActiveMerchant::Billing::Base.gateway(:eway).new(:login => '87654321')
Now we are ready to process our transaction
response = gateway.purchase(tendollar, creditcard, options)
Sending a transaction to TrustCommerce with active_merchant returns a Response object, which consistently allows you to:
1) Check whether the transaction was successful
response.success?
2) Retrieve any message returned by eWay, either a “transaction was successful” note or an explanation of why the transaction was rejected.
response.message
3) Retrieve and store the unique transaction ID returned by eWway, for use in referencing the transaction in the future.
response.authorization
This should be enough to get you started with eWay and active_merchant. For further information, review the methods below and the rest of active_merchant’s documentation.
ewayCustomerEmail, ewayCustomerAddress, ewayCustomerPostcode
# File lib/active_merchant/billing/gateways/eway.rb, line 145 145: def purchase(money, creditcard, options = {}) 146: requires!(options, :order_id) 147: 148: post = {} 149: add_creditcard(post, creditcard) 150: add_address(post, options) 151: add_customer_data(post, options) 152: add_invoice_data(post, options) 153: # The request fails if all of the fields aren't present 154: add_optional_data(post) 155: 156: commit(money, post) 157: end
# File lib/active_merchant/billing/gateways/eway.rb, line 175 175: def add_address(post, options) 176: if address = options[:billing_address] || options[:address] 177: post[:CustomerAddress] = [ address[:address1], address[:address2], address[:city], address[:state], address[:country] ].compact.join(', ') 178: post[:CustomerPostcode] = address[:zip] 179: end 180: end
# File lib/active_merchant/billing/gateways/eway.rb, line 164 164: def add_creditcard(post, creditcard) 165: post[:CardNumber] = creditcard.number 166: post[:CardExpiryMonth] = sprintf("%.2i", creditcard.month) 167: post[:CardExpiryYear] = sprintf("%.4i", creditcard.year)[2..1] 168: post[:CustomerFirstName] = creditcard.first_name 169: post[:CustomerLastName] = creditcard.last_name 170: post[:CardHoldersName] = creditcard.name 171: 172: post[:CVN] = creditcard.verification_value if creditcard.verification_value? 173: end
# File lib/active_merchant/billing/gateways/eway.rb, line 182 182: def add_customer_data(post, options) 183: post[:CustomerEmail] = options[:email] 184: end
# File lib/active_merchant/billing/gateways/eway.rb, line 186 186: def add_invoice_data(post, options) 187: post[:CustomerInvoiceRef] = options[:order_id] 188: post[:CustomerInvoiceDescription] = options[:description] 189: end
# File lib/active_merchant/billing/gateways/eway.rb, line 191 191: def add_optional_data(post) 192: post[:TrxnNumber] = nil 193: post[:Option1] = nil 194: post[:Option2] = nil 195: post[:Option3] = nil 196: end
# File lib/active_merchant/billing/gateways/eway.rb, line 198 198: def commit(money, parameters) 199: parameters[:TotalAmount] = amount(money) 200: 201: response = parse( ssl_post(gateway_url(parameters[:CVN], test?), post_data(parameters)) ) 202: 203: Response.new(success?(response), message_from(response[:ewaytrxnerror]), response, 204: :authorization => response[:ewayauthcode], 205: :test => /\(Test( CVN)? Gateway\)/ === response[:ewaytrxnerror] 206: ) 207: end
# File lib/active_merchant/billing/gateways/eway.rb, line 267 267: def gateway_url(cvn, test) 268: if cvn 269: test ? TEST_CVN_URL : LIVE_CVN_URL 270: else 271: test ? TEST_URL : LIVE_URL 272: end 273: end
# File lib/active_merchant/billing/gateways/eway.rb, line 251 251: def message_from(message) 252: return '' if message.blank? 253: MESSAGES[message[0,2]] || message 254: end
Make a ruby type out of the response string
# File lib/active_merchant/billing/gateways/eway.rb, line 257 257: def normalize(field) 258: case field 259: when "true" then true 260: when "false" then false 261: when "" then nil 262: when "null" then nil 263: else field 264: end 265: end
Parse eway response xml into a convinient hash
# File lib/active_merchant/billing/gateways/eway.rb, line 214 214: def parse(xml) 215: # "<?xml version=\"1.0\"?>". 216: # <ewayResponse> 217: # <ewayTrxnError></ewayTrxnError> 218: # <ewayTrxnStatus>True</ewayTrxnStatus> 219: # <ewayTrxnNumber>10002</ewayTrxnNumber> 220: # <ewayTrxnOption1></ewayTrxnOption1> 221: # <ewayTrxnOption2></ewayTrxnOption2> 222: # <ewayTrxnOption3></ewayTrxnOption3> 223: # <ewayReturnAmount>10</ewayReturnAmount> 224: # <ewayAuthCode>123456</ewayAuthCode> 225: # <ewayTrxnReference>987654321</ewayTrxnReference> 226: # </ewayResponse> 227: 228: response = {} 229: xml = REXML::Document.new(xml) 230: xml.elements.each('//ewayResponse/*') do |node| 231: 232: response[node.name.downcase.to_sym] = normalize(node.text) 233: 234: end unless xml.root.nil? 235: 236: response 237: end
# File lib/active_merchant/billing/gateways/eway.rb, line 239 239: def post_data(parameters = {}) 240: parameters[:CustomerID] = @options[:login] 241: 242: xml = REXML::Document.new 243: root = xml.add_element("ewaygateway") 244: 245: parameters.each do |key, value| 246: root.add_element("eway#{key}").text = value 247: end 248: xml.to_s 249: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.