Skip to content

Instantly share code, notes, and snippets.

@marklocklear
Created January 12, 2018 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marklocklear/9ec25b2721f4356a2ab35e02207dfe28 to your computer and use it in GitHub Desktop.
Save marklocklear/9ec25b2721f4356a2ab35e02207dfe28 to your computer and use it in GitHub Desktop.
def authorize_response
request_xml = "<?xml version='1.0' encoding='utf-8'?>
<createTransactionRequest
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns='AnetApi/xml/v1/schema/AnetApiSchema.xsd'>
<merchantAuthentication>
<name>#{ENV['MERCH_NAME']}</name>
<transactionKey>#{ENV['MERCH_KEY']}</transactionKey>
</merchantAuthentication>
<refId>#{self.id}</refId>
<transactionRequest>
<transactionType>authCaptureTransaction</transactionType>
<amount>#{self.grand_total}</amount>
<payment>
<creditCard>
<cardNumber>#{self.card_num}</cardNumber>
<expirationDate>#{self.month}#{self.year}</expirationDate>
<cardCode>#{self.security_code}</cardCode>
</creditCard>
</payment>
<order>
<invoiceNumber>#{self.id}</invoiceNumber>
</order>
<lineItems>"
self.line_items.each do |line_item|
request_xml << "<lineItem>
<itemId>#{line_item.line_itemable.id rescue 'N/A'}</itemId>
<name>#{line_item.line_itemable.title.truncate(31) rescue 'N/A'}</name>
<description>#{line_item.line_itemable_type.titleize rescue 'N/A'}</description>
<quantity>#{line_item.qty rescue 'N/A'}</quantity>
<unitPrice>#{line_item.line_itemable.price rescue 'N/A'}</unitPrice>
</lineItem>"
end
request_xml << "</lineItems>"
if self.shippable?
request_xml << "<shipping>
<amount>#{self.shipping_total}</amount>
</shipping>"
end
request_xml << "<poNumber>#{self.id}</poNumber>
<customer>
<id>#{self.user.id}</id>
<email>#{self.user.email}</email>
</customer>
<billTo>
<firstName>#{self.user.first_name}</firstName>
<lastName>#{self.user.last_name}</lastName>
<address>#{self.billing_address.address_1}</address>
<city>#{self.billing_address.city}</city>
<state>#{self.billing_address.state}</state>
<zip>#{self.billing_address.postal_code}</zip>
<country>#{self.billing_address.country}</country>
</billTo>"
if self.shippable?
request_xml << "<shipTo>
<firstName>#{self.user.first_name}</firstName>
<lastName>#{self.user.last_name}</lastName>
<address>#{self.shipping_address.address_1}</address>
<city>#{self.shipping_address.city}</city>
<state>#{self.shipping_address.state}</state>
<zip>#{self.shipping_address.postal_code}</zip>
<country>#{self.shipping_address.country}</country>
</shipTo>"
end
request_xml << "<customerIP>#{self.ip_address}</customerIP>
<transactionSettings>
<setting>
<settingName>duplicateWindow</settingName>
<settingValue>900</settingValue>
</setting>
</transactionSettings>
</transactionRequest>
</createTransactionRequest>"
response = Typhoeus.post(
"#{ENV['MERCH_LOCATION']}",
:body => request_xml.gsub("\n",''),
:headers => {'Content-Type' => "text/xml; charset=utf-8"}
)
doc = Nokogiri::XML(response.body)
doc.remove_namespaces!
return doc
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment