Skip to content

Instantly share code, notes, and snippets.

@geermc4
Created September 18, 2012 21:27
Show Gist options
  • Save geermc4/3746034 to your computer and use it in GitHub Desktop.
Save geermc4/3746034 to your computer and use it in GitHub Desktop.
USPS Label for spree
def usps
test = "Test='YES'" if Spree::ActiveShipping::Config[:test_mode]
xml = []
xml << "<LabelRequest #{test || ''} LabelType='Default' LabelSize='4X6' ImageFormat='PDF'>"
xml << "<RequesterID>#{self.usps_req_id}</RequesterID>"
xml << "<AccountID>#{self.usps_account}</AccountID>"
xml << "<PassPhrase>#{self.usps_password}</PassPhrase>"
xml << "<MailClass>#{self.shipping_method}</MailClass>"
xml << "<DateAdvance>0</DateAdvance>"
xml << "<WeightOz>#{@weight}</WeightOz>"
xml << "<Stealth>FALSE</Stealth>"
xml << "<Services InsuredMail='OFF' SignatureConfirmation='OFF' />"
xml << "<Value>0</Value>"
xml << "<Description>Label for order ##{@order.id}</Description>"
xml << "<PartnerCustomerID>#{self.user_id}</PartnerCustomerID>"
xml << "<PartnerTransactionID>#{self.shipment_id}</PartnerTransactionID>"
xml << "<ToName>#{self.to_name}</ToName>"
xml << "<ToAddress1>#{self.to_address1}</ToAddress1>"
xml << "<ToCity>#{self.to_city}</ToCity>"
xml << "<ToState>#{self.to_state}</ToState>"
xml << "<ToPostalCode>#{self.to_zip}</ToPostalCode>"
xml << "<ToDeliveryPoint>00</ToDeliveryPoint>"
xml << "<ToPhone>#{self.to_telephone}</ToPhone>"
xml << "<FromName>#{self.origin_name}</FromName>"
xml << "<FromCompany>#{self.origin_company}</FromCompany>"
xml << "<ReturnAddress1>#{self.origin_address}</ReturnAddress1>"
xml << "<FromCity>#{self.origin_city}</FromCity>"
xml << "<FromState>#{self.origin_state}</FromState>"
xml << "<FromPostalCode>#{self.origin_zip}</FromPostalCode>"
xml << "<FromPhone>#{self.origin_telephone}</FromPhone>"
xml << "</LabelRequest>"
url = "https://www.envmgr.com/LabelService/EwsLabelService.asmx/GetPostageLabelXML"
c = Curl::Easy.http_post(url, Curl::PostField.content('labelRequestXML', xml.join), :verbose => true)
c.follow_location = true
c.ssl_verify_host = false
xml = Nokogiri::XML::Document.parse(c.body_str)
img = xml.search('Base64LabelImage')
File.open("#{@path}#{@file}",'w') { |f| f.write Base64.decode64(img.inner_text) }
update_tracking_number xml.search("TrackingNumber").inner_text
pdf_crop
@file
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment