Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an api call for calculating a postage rate #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions lib/endicia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,82 @@ def self.carrier_pickup_request(tracking_number, package_location, options = {})
response
end

# Pass in an options hash to get a postage rate.
#
# At minimum pass in:
# MailClass: ...
# ToPostalCode: ...
# FromPostalCode: ...
# WeightOz: ...
#
# Also pass in other stuff for more specifics, defaults listed
# DateAdvance
# MailpieceShape
# MailpieceDimensions
# Length
# Width
# Height
# Machinable: True
# LiveAnimalSurcharge: False
# ToCountryCode
# Services
# CertifiedMail: OFF
# COD: OFF
# DeliveryConfirmation: OFF
# ElectronicReturnReceipt: OFF
# InsuredMail: OFF
# RestrictedDelivery: OFF
# ReturnReceipt: OFF
# SignatureConfirmation: OFF
# AdultSignature: OFF
# AdultSignatureRestrictedDelivery: OFF
# CODAmount
# InsuredValue
#
# Returns a hash in the form:
#
# {
# :success => true or false
# :error_message => "the message" or nil
# :response_body => Raw response data from endicia
# :price => price data
# }
def self.calculate_postage_rate(opts = {})
opts[:Test] ||= "NO"
url = "#{label_service_url(opts)}/CalculatePostageRateXML"
insurance = extract_insurance(opts)
handle_extended_zip_code(opts)

dimension_keys = :Length, :Width, :Height
mailpiece_dimenions = extract(opts, dimension_keys)

xml = Builder::XmlMarkup.new
body = "postageRateRequestXML=" + xml.PostageRateRequest do |xm|
xm.RequesterID(opts.delete(:RequesterID) || defaults[:RequesterID])
xm.CertifiedIntermediary do |ci|
ci.AccountID(opts.delete(:AccountID) || defaults[:AccountID])
ci.PassPhrase(opts.delete(:PassPhrase) || defaults[:PassPhrase])
end
opts.each { |key, value| xm.tag!(key, value) }
xm.Services({ :InsuredMail => insurance }) if insurance
unless mailpiece_dimenions.empty?
xm.MailpieceDimensions do |md|
mailpiece_dimenions.each { |key, value| md.tag!(key, value) }
end
end
end

result = self.post(url, :body => body)

parsed = parse_result(result, "PostageRateResponse")

if parsed[:success]
parsed[:price] = result["PostageRateResponse"]["Postage"]["Rate"]
end

parsed
end

# Pass in a options hash to get all the rates available.
#
# At minimum pass in:
Expand Down