99"""
1010import logging
1111import binascii
12+ import datetime
13+
1214from example_config import CONFIG_OBJ
1315from fedex .services .ship_service import FedexProcessShipmentRequest
1416
17+ # What kind of file do you want this example to generate?
18+ # Valid choices for this example are PDF, PNG
19+ GENERATE_IMAGE_TYPE = 'PDF'
20+
21+
1522# Set this to the INFO level to see the response from Fedex printed in stdout.
1623logging .basicConfig (level = logging .INFO )
1724
1825# This is the object that will be handling our tracking request.
1926# We're using the FedexConfig object from example_config.py in this dir.
20- shipment = FedexProcessShipmentRequest (CONFIG_OBJ )
27+ customer_transaction_id = "*** ShipService Request v17 using Python ***" # Optional transaction_id
28+ shipment = FedexProcessShipmentRequest (CONFIG_OBJ , customer_transaction_id = customer_transaction_id )
2129
2230# This is very generalized, top-level information.
2331# REGULAR_PICKUP, REQUEST_COURIER, DROP_BOX, BUSINESS_SERVICE_CENTER or STATION
24- shipment .RequestedShipment .DropoffType = 'REGULAR_PICKUP '
32+ shipment .RequestedShipment .DropoffType = 'BUSINESS_SERVICE_CENTER '
2533
2634# See page 355 in WS_ShipService.pdf for a full list. Here are the common ones:
2735# STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, FEDEX_EXPRESS_SAVER
2836shipment .RequestedShipment .ServiceType = 'PRIORITY_OVERNIGHT'
2937
3038# What kind of package this will be shipped in.
31- # FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING
39+ # FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, FEDEX_ENVELOPE
3240shipment .RequestedShipment .PackagingType = 'FEDEX_PAK'
3341
3442# Shipper contact info.
5563shipment .RequestedShipment .Recipient .Address .StateOrProvinceCode = 'VA'
5664shipment .RequestedShipment .Recipient .Address .PostalCode = '20171'
5765shipment .RequestedShipment .Recipient .Address .CountryCode = 'US'
58- # This is needed to ensure an accurate rate quote with the response.
66+ # This is needed to ensure an accurate rate quote with the response. Use AddressValidation to get ResidentialStatus
5967shipment .RequestedShipment .Recipient .Address .Residential = True
6068shipment .RequestedShipment .EdtRequestType = 'NONE'
6169
70+ # Senders account information
6271shipment .RequestedShipment .ShippingChargesPayment .Payor .ResponsibleParty .AccountNumber = CONFIG_OBJ .account_number
72+
6373# Who pays for the shipment?
6474# RECIPIENT, SENDER or THIRD_PARTY
6575shipment .RequestedShipment .ShippingChargesPayment .PaymentType = 'SENDER'
7080
7181# Specifies which format the label file will be sent to you in.
7282# DPL, EPL2, PDF, PNG, ZPLII
73- shipment .RequestedShipment .LabelSpecification .ImageType = 'PNG'
83+ shipment .RequestedShipment .LabelSpecification .ImageType = GENERATE_IMAGE_TYPE
7484
7585# To use doctab stocks, you must change ImageType above to one of the
7686# label printer formats (ZPLII, EPL2, DPL).
7787# See documentation for paper types, there quite a few.
78- shipment .RequestedShipment .LabelSpecification .LabelStockType = 'PAPER_4X6 '
88+ shipment .RequestedShipment .LabelSpecification .LabelStockType = 'PAPER_7X4.75 '
7989
8090# This indicates if the top or bottom of the label comes out of the
8191# printer first.
8292# BOTTOM_EDGE_OF_TEXT_FIRST or TOP_EDGE_OF_TEXT_FIRST
83- shipment .RequestedShipment .LabelSpecification .LabelPrintingOrientation = 'BOTTOM_EDGE_OF_TEXT_FIRST'
93+ # Timestamp in YYYY-MM-DDThh:mm:ss format, e.g. 2002-05-30T09:00:00
94+ shipment .RequestedShipment .ShipTimestamp = datetime .datetime .now ().replace (microsecond = 0 ).isoformat ()
8495
96+ # BOTTOM_EDGE_OF_TEXT_FIRST, TOP_EDGE_OF_TEXT_FIRST
97+ shipment .RequestedShipment .LabelSpecification .LabelPrintingOrientation = 'TOP_EDGE_OF_TEXT_FIRST'
98+
99+ # Delete the flags we don't want.
100+ # Can be SHIPPING_LABEL_FIRST, SHIPPING_LABEL_LAST or delete
101+ if hasattr (shipment .RequestedShipment .LabelSpecification , 'LabelOrder' ):
102+ del shipment .RequestedShipment .LabelSpecification .LabelOrder # Delete, not using.
103+
104+ # Create Weight, in pounds.
85105package1_weight = shipment .create_wsdl_object_of_type ('Weight' )
86- # Weight, in pounds.
87106package1_weight .Value = 1.0
88107package1_weight .Units = "LB"
89108
109+ # Create PackageLineItem
90110package1 = shipment .create_wsdl_object_of_type ('RequestedPackageLineItem' )
91- package1 .PhysicalPackaging = 'BOX'
111+ # BAG, BARREL, BASKET, BOX, BUCKET, BUNDLE, CARTON, CASE, CONTAINER, ENVELOPE etc..
112+ package1 .PhysicalPackaging = 'ENVELOPE'
92113package1 .Weight = package1_weight
93114# Un-comment this to see the other variables you may set on a package.
94115#print package1
99120
100121# If you'd like to see some documentation on the ship service WSDL, un-comment
101122# this line. (Spammy).
102- #print shipment.client
123+ # print shipment.client
103124
104125# Un-comment this to see your complete, ready-to-send request as it stands
105126# before it is actually sent. This is useful for seeing what values you can
106127# change.
107128#print shipment.RequestedShipment
129+ #print shipment.ClientDetail
130+ #print shipment.TransactionDetail
108131
109132# If you want to make sure that all of your entered details are valid, you
110133# can call this and parse it just like you would via send_request(). If
111134# shipment.response.HighestSeverity == "SUCCESS", your shipment is valid.
112- #shipment.send_validation_request()
135+ #print shipment.send_validation_request()
113136
114137# Fires off the request, sets the 'response' attribute on the object.
115138shipment .send_request ()
118141# attributes through the response attribute on the request object. This is
119142# good to un-comment to see the variables returned by the Fedex reply.
120143print shipment .response
144+ #print shipment.client.last_received()
145+
146+ # See the request printed out.
147+ #print shipment.client.last_sent()
121148
122149# Here is the overall end result of the query.
123150print "HighestSeverity:" , shipment .response .HighestSeverity
151+
124152# Getting the tracking number from the new shipment.
125153print "Tracking #:" , shipment .response .CompletedShipmentDetail .CompletedPackageDetails [0 ].TrackingIds [0 ].TrackingNumber
126- # Net shipping costs.
127- print "Net Shipping Cost (US$):" , shipment .response .CompletedShipmentDetail .CompletedPackageDetails [0 ].PackageRating .PackageRateDetails [0 ].NetCharge .Amount
154+
155+ # Net shipping costs. Only show if available. Sometimes sandbox will not include this in the response.
156+ CompletedPackageDetails = shipment .response .CompletedShipmentDetail .CompletedPackageDetails [0 ]
157+ if hasattr (CompletedPackageDetails , 'PackageRating' ):
158+ print "Net Shipping Cost (US$):" , CompletedPackageDetails .PackageRating .PackageRateDetails [0 ].NetCharge .Amount
159+ else :
160+ print 'WARNING: Unable to get rate.'
128161
129162# Get the label image in ASCII format from the reply. Note the list indices
130163# we're using. You'll need to adjust or iterate through these if your shipment
131164# has multiple packages.
165+
132166ascii_label_data = shipment .response .CompletedShipmentDetail .CompletedPackageDetails [0 ].Label .Parts [0 ].Image
167+
133168# Convert the ASCII data to binary.
134169label_binary_data = binascii .a2b_base64 (ascii_label_data )
135170
136171"""
137- This is an example of how to dump a label to a PNG file.
172+ This is an example of how to dump a label to a local file.
138173"""
139174# This will be the file we write the label out to.
140- png_file = open ('example_shipment_label.png' , 'wb' )
141- png_file .write (label_binary_data )
142- png_file .close ()
175+ out_path = 'example_shipment_label.%s' % GENERATE_IMAGE_TYPE .lower ()
176+ print "Writing to file" , out_path
177+ out_file = open (out_path , 'wb' )
178+ out_file .write (label_binary_data )
179+ out_file .close ()
143180
144181"""
145182This is an example of how to print the label to a serial printer. This will not
161198#label_printer = serial.Serial(0)
162199#print "SELECTED SERIAL PORT: "+ label_printer.portstr
163200#label_printer.write(label_binary_data)
164- #label_printer.close()
201+ #label_printer.close()
202+
0 commit comments