# File lib/active_merchant/billing/gateways/eway_managed.rb, line 24 24: def initialize(options = {}) 25: requires!(options, :login, :username, :password) 26: @options = options 27: 28: # eWay returns 500 code for faults, which AM snaffles. 29: # So, we tell it to allow them. 30: @options[:ignore_http_status]=true 31: super 32: end
Process a payment in the given amount against the stored credit card given by billing_id
money — The amount to be purchased as an Integer value in cents.
billing_id — The eWay provided card/customer token to charge (managedCustomerID)
options — A hash of optional parameters.
:order_id — The order number, passed to eWay as the “Invoice Reference“
:invoice — The invoice number, passed to eWay as the “Invoice Reference” unless :order_id is also given
:description — A description of the payment, passed to eWay as the “Invoice Description“
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 84 84: def purchase(money, billing_id, options={}) 85: post = {} 86: post[:managedCustomerID] = billing_id.to_s 87: post[:amount]=money 88: add_invoice(post, options) 89: 90: commit("ProcessPayment", post) 91: end
add a new customer CC to your eway account and return unique ManagedCustomerID supports storing details required by eway see “add_creditcard“ and “add_address“
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 36 36: def store(creditcard, options = {}) 37: post = {} 38: 39: # Handle our required fields 40: requires!(options, :billing_address) 41: 42: # Handle eWay specific required fields. 43: billing_address = options[:billing_address] 44: eway_requires!(billing_address) 45: 46: add_creditcard(post, creditcard) 47: add_address(post, billing_address) 48: add_misc_fields(post, options) 49: 50: commit("CreateCustomer", post) 51: end
TODO: eWay API also provides QueryCustomer TODO: eWay API also provides QueryPayment
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 96 96: def test? 97: @options[:test] || Base.gateway_mode == :test 98: end
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 53 53: def update(billing_id, creditcard, options={}) 54: post = {} 55: 56: # Handle our required fields 57: requires!(options, :billing_address) 58: 59: # Handle eWay specific required fields. 60: billing_address = options[:billing_address] 61: eway_requires!(billing_address) 62: 63: post[:managedCustomerID]=billing_id 64: add_creditcard(post, creditcard) 65: add_address(post, billing_address) 66: add_misc_fields(post, options) 67: 68: commit("UpdateCustomer", post) 69: end
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 106 106: def add_address(post, address) 107: post[:Address] = address[:address1].to_s 108: post[:Phone] = address[:phone].to_s 109: post[:PostCode] = address[:zip].to_s 110: post[:Suburb] = address[:city].to_s 111: post[:Country] = address[:country].to_s.downcase 112: post[:State] = address[:state].to_s 113: post[:Mobile] = address[:mobile].to_s 114: post[:Fax] = address[:fax].to_s 115: end
add credit card details to be stored by eway. NOTE eway requires “title” field
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 134 134: def add_creditcard(post, creditcard) 135: post[:CCNumber] = creditcard.number 136: post[:CCExpiryMonth] = sprintf("%.2i", creditcard.month) 137: post[:CCExpiryYear] = sprintf("%.4i", creditcard.year)[2..1] 138: post[:CCNameOnCard] = creditcard.name 139: post[:FirstName] = creditcard.first_name 140: post[:LastName] = creditcard.last_name 141: end
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 127 127: def add_invoice(post, options) 128: post[:invoiceReference] = options[:order_id] || options[:invoice] 129: post[:invoiceDescription] = options[:description] 130: end
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 117 117: def add_misc_fields(post, options) 118: post[:CustomerRef]=options[:billing_address][:customer_ref] || options[:customer] 119: post[:Title]=options[:billing_address][:title] 120: post[:Company]=options[:billing_address][:company] 121: post[:JobDesc]=options[:billing_address][:job_desc] 122: post[:Email]=options[:billing_address][:email] || options[:email] 123: post[:URL]=options[:billing_address][:url] 124: post[:Comments]=options[:description] 125: end
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 194 194: def commit(action, post) 195: raw = begin 196: ssl_post(test? ? TEST_URL : LIVE_URL, soap_request(post, action), 'Content-Type' => 'application/soap+xml; charset=utf-8') 197: rescue ResponseError => e 198: e.response.body 199: end 200: response = parse(raw) 201: 202: EwayResponse.new(response[:success], response[:message], response, 203: :test => test?, 204: :authorization => response[:auth_code] 205: ) 206: end
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 239 239: def default_customer_fields 240: hash={} 241: %( CustomerRef Title FirstName LastName Company JobDesc Email Address Suburb State PostCode Country Phone Mobile Fax URL Comments CCNumber CCNameOnCard CCExpiryMonth CCExpiryYear ).each do |field| 242: hash[field.to_sym]='' 243: end 244: return hash 245: end
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 247 247: def default_payment_fields 248: hash={} 249: %( managedCustomerID amount invoiceReference invoiceDescription ).each do |field| 250: hash[field.to_sym]='' 251: end 252: return hash 253: end
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 101 101: def eway_requires!(hash) 102: raise ArgumentError.new("Missing eWay required parameter in `billing_address`: title") unless hash.has_key?(:title) 103: raise ArgumentError.new("Missing eWay required parameter in `billing_address`: country") unless hash.has_key?(:country) 104: end
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 143 143: def parse(body) 144: reply = {} 145: xml = REXML::Document.new(body) 146: if root = REXML::XPath.first(xml, "//soap:Fault") then 147: reply=parse_fault(root) 148: else 149: if root = REXML::XPath.first(xml, '//ProcessPaymentResponse/ewayResponse') then 150: # Successful payment 151: reply=parse_purchase(root) 152: else 153: if root = REXML::XPath.first(xml, '//CreateCustomerResult') then 154: reply[:message]='OK' 155: reply[:CreateCustomerResult]=root.text 156: reply[:success]=true 157: else 158: if root = REXML::XPath.first(xml, '//UpdateCustomerResult') then 159: if root.text.downcase == 'true' then 160: reply[:message]='OK' 161: reply[:success]=true 162: else 163: # ERROR: This state should never occur. If there is a problem, 164: # a soap:Fault will be returned. The presence of this 165: # element always means a success. 166: raise StandardError, "Unexpected \"false\" in UpdateCustomerResult" 167: end 168: else 169: # ERROR: This state should never occur currently. We have handled 170: # responses for all the methods which we support. 171: raise StandardError, "Unexpected response" 172: end 173: end 174: end 175: end 176: return reply 177: end
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 179 179: def parse_fault(node) 180: reply={} 181: reply[:message]=REXML::XPath.first(node, '//soap:Reason/soap:Text').text 182: reply[:success]=false 183: reply 184: end
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 186 186: def parse_purchase(node) 187: reply={} 188: reply[:message]=REXML::XPath.first(node, '//ewayTrxnError').text 189: reply[:success]=(REXML::XPath.first(node, '//ewayTrxnStatus').text == 'True') 190: reply[:auth_code]=REXML::XPath.first(node, '//ewayAuthCode').text 191: reply 192: end
Where we build the full SOAP 1.2 request using builder
# File lib/active_merchant/billing/gateways/eway_managed.rb, line 209 209: def soap_request(arguments, action) 210: # eWay demands all fields be sent, but contain an empty string if blank 211: post = case action 212: when 'ProcessPayment' 213: default_payment_fields.merge(arguments) 214: else 215: default_customer_fields.merge(arguments) 216: end 217: 218: xml = Builder::XmlMarkup.new :indent => 2 219: xml.instruct! 220: xml.tag! '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'} do 221: xml.tag! 'soap12:Header' do 222: xml.tag! 'eWAYHeader', {'xmlns' => 'https://www.eway.com.au/gateway/managedpayment'} do 223: xml.tag! 'eWAYCustomerID', @options[:login] 224: xml.tag! 'Username', @options[:username] 225: xml.tag! 'Password', @options[:password] 226: end 227: end 228: xml.tag! 'soap12:Body' do |x| 229: x.tag! "#{action}", {'xmlns' => 'https://www.eway.com.au/gateway/managedpayment'} do |y| 230: post.each do |key, value| 231: y.tag! "#{key}", "#{value}" 232: end 233: end 234: end 235: end 236: xml.target! 237: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.