1+ #!/usr/bin/env python
2+ """
3+ This example shows how to create shipments. The variables populated below
4+ represents the minimum required values. You will need to fill all of these, or
5+ risk seeing a SchemaValidationError exception thrown.
6+
7+ Near the bottom of the module, you'll see some different ways to handle the
8+ label data that is returned with the reply.
9+ """
10+ import logging
11+ import binascii
12+ from example_config import CONFIG_OBJ
13+ from fedex .services .ship_service import FedexProcessShipmentRequest
14+
15+ # Set this to the INFO level to see the response from Fedex printed in stdout.
16+ #logging.basicConfig(filename="suds.log", level=logging.DEBUG)
17+ logging .basicConfig (level = logging .INFO )
18+ # This is the object that will be handling our tracking request.
19+ # We're using the FedexConfig object from example_config.py in this dir.
20+ shipment = FedexProcessShipmentRequest (CONFIG_OBJ )
21+ shipment .RequestedShipment .DropoffType = 'REGULAR_PICKUP'
22+ shipment .RequestedShipment .ServiceType = 'FEDEX_FREIGHT_ECONOMY'
23+ shipment .RequestedShipment .PackagingType = 'YOUR_PACKAGING'
24+
25+ shipment .RequestedShipment .FreightShipmentDetail .FedExFreightAccountNumber = CONFIG_OBJ .freight_account_number
26+
27+ # Shipper contact info.
28+ shipment .RequestedShipment .Shipper .Contact .PersonName = 'Sender Name'
29+ shipment .RequestedShipment .Shipper .Contact .CompanyName = 'Some Company'
30+ shipment .RequestedShipment .Shipper .Contact .PhoneNumber = '9012638716'
31+
32+ # Shipper address.
33+ shipment .RequestedShipment .Shipper .Address .StreetLines = ['1202 Chalet Ln' ]
34+ shipment .RequestedShipment .Shipper .Address .City = 'Harrison'
35+ shipment .RequestedShipment .Shipper .Address .StateOrProvinceCode = 'AR'
36+ shipment .RequestedShipment .Shipper .Address .PostalCode = '72601'
37+ shipment .RequestedShipment .Shipper .Address .CountryCode = 'US'
38+ shipment .RequestedShipment .Shipper .Address .Residential = True
39+
40+ # Recipient contact info.
41+ shipment .RequestedShipment .Recipient .Contact .PersonName = 'Recipient Name'
42+ shipment .RequestedShipment .Recipient .Contact .CompanyName = 'Recipient Company'
43+ shipment .RequestedShipment .Recipient .Contact .PhoneNumber = '9012637906'
44+
45+ # Recipient address
46+ shipment .RequestedShipment .Recipient .Address .StreetLines = ['2000 Freight LTL Testing' ]
47+ shipment .RequestedShipment .Recipient .Address .City = 'Harrison'
48+ shipment .RequestedShipment .Recipient .Address .StateOrProvinceCode = 'AR'
49+ shipment .RequestedShipment .Recipient .Address .PostalCode = '72601'
50+ shipment .RequestedShipment .Recipient .Address .CountryCode = 'US'
51+
52+ # This is needed to ensure an accurate rate quote with the response.
53+ shipment .RequestedShipment .Recipient .Address .Residential = False
54+ shipment .RequestedShipment .FreightShipmentDetail .TotalHandlingUnits = 1
55+ shipment .RequestedShipment .ShippingChargesPayment .Payor .ResponsibleParty .AccountNumber = CONFIG_OBJ .freight_account_number
56+
57+ shipment .RequestedShipment .FreightShipmentDetail .FedExFreightBillingContactAndAddress .Contact .PersonName = 'Sender Name'
58+ shipment .RequestedShipment .FreightShipmentDetail .FedExFreightBillingContactAndAddress .Contact .CompanyName = 'Some Company'
59+ shipment .RequestedShipment .FreightShipmentDetail .FedExFreightBillingContactAndAddress .Contact .PhoneNumber = '9012638716'
60+
61+ shipment .RequestedShipment .FreightShipmentDetail .FedExFreightBillingContactAndAddress .Address .StreetLines = ['2000 Freight LTL Testing' ]
62+ shipment .RequestedShipment .FreightShipmentDetail .FedExFreightBillingContactAndAddress .Address .City = 'Harrison'
63+ shipment .RequestedShipment .FreightShipmentDetail .FedExFreightBillingContactAndAddress .Address .StateOrProvinceCode = 'AR'
64+ shipment .RequestedShipment .FreightShipmentDetail .FedExFreightBillingContactAndAddress .Address .PostalCode = '72601'
65+ shipment .RequestedShipment .FreightShipmentDetail .FedExFreightBillingContactAndAddress .Address .CountryCode = 'US'
66+ shipment .RequestedShipment .FreightShipmentDetail .FedExFreightBillingContactAndAddress .Address .Residential = False
67+ spec = shipment .create_wsdl_object_of_type ('ShippingDocumentSpecification' )
68+
69+ spec .ShippingDocumentTypes = [spec .CertificateOfOrigin ]
70+ # shipment.RequestedShipment.ShippingDocumentSpecification = spec
71+
72+ role = shipment .create_wsdl_object_of_type ('FreightShipmentRoleType' )
73+
74+ shipment .RequestedShipment .FreightShipmentDetail .Role = role .SHIPPER
75+ shipment .RequestedShipment .FreightShipmentDetail .CollectTermsType = 'STANDARD'
76+
77+
78+ # Specifies the label type to be returned.
79+ shipment .RequestedShipment .LabelSpecification .LabelFormatType = 'FEDEX_FREIGHT_STRAIGHT_BILL_OF_LADING'
80+
81+ # Specifies which format the label file will be sent to you in.
82+ # DPL, EPL2, PDF, PNG, ZPLII
83+ shipment .RequestedShipment .LabelSpecification .ImageType = 'PDF'
84+
85+ # To use doctab stocks, you must change ImageType above to one of the
86+ # label printer formats (ZPLII, EPL2, DPL).
87+ # See documentation for paper types, there quite a few.
88+ shipment .RequestedShipment .LabelSpecification .LabelStockType = 'PAPER_LETTER'
89+
90+ # This indicates if the top or bottom of the label comes out of the
91+ # printer first.
92+ # BOTTOM_EDGE_OF_TEXT_FIRST or TOP_EDGE_OF_TEXT_FIRST
93+ shipment .RequestedShipment .LabelSpecification .LabelPrintingOrientation = 'BOTTOM_EDGE_OF_TEXT_FIRST'
94+ shipment .RequestedShipment .EdtRequestType = 'NONE'
95+
96+ package1_weight = shipment .create_wsdl_object_of_type ('Weight' )
97+ package1_weight .Value = 500.0
98+ package1_weight .Units = "LB"
99+
100+ shipment .RequestedShipment .FreightShipmentDetail .PalletWeight = package1_weight
101+
102+ package1 = shipment .create_wsdl_object_of_type ('FreightShipmentLineItem' )
103+ package1 .Weight = package1_weight
104+ package1 .Packaging = 'PALLET'
105+ package1 .Description = 'Products'
106+ package1 .FreightClass = 'CLASS_500'
107+ package1 .HazardousMaterials = None
108+ package1 .Pieces = 12
109+
110+
111+ shipment .RequestedShipment .FreightShipmentDetail .LineItems = package1
112+
113+ # If you'd like to see some documentation on the ship service WSDL, un-comment
114+ # this line. (Spammy).
115+ #print shipment.client
116+
117+ # Un-comment this to see your complete, ready-to-send request as it stands
118+ # before it is actually sent. This is useful for seeing what values you can
119+ # change.
120+ #print shipment.RequestedShipment
121+
122+ # If you want to make sure that all of your entered details are valid, you
123+ # can call this and parse it just like you would via send_request(). If
124+ # shipment.response.HighestSeverity == "SUCCESS", your shipment is valid.
125+ #shipment.send_validation_request()
126+
127+ # Fires off the request, sets the 'response' attribute on the object.
128+ shipment .send_request ()
129+
130+ # This will show the reply to your shipment being sent. You can access the
131+ # attributes through the response attribute on the request object. This is
132+ # good to un-comment to see the variables returned by the Fedex reply.
133+ print shipment .response
134+ # Here is the overall end result of the query.
135+ # print "HighestSeverity:", shipment.response.HighestSeverity
136+ # # Getting the tracking number from the new shipment.
137+ # print "Tracking #:", shipment.response.CompletedShipmentDetail.CompletedPackageDetails[0].TrackingIds[0].TrackingNumber
138+ # # Net shipping costs.
139+ # print "Net Shipping Cost (US$):", shipment.response.CompletedShipmentDetail.CompletedPackageDetails[0].PackageRating.PackageRateDetails[0].NetCharge.Amount
140+
141+ # # Get the label image in ASCII format from the reply. Note the list indices
142+ # we're using. You'll need to adjust or iterate through these if your shipment
143+ # has multiple packages.
144+
145+ ascii_label_data = shipment .response .CompletedShipmentDetail .ShipmentDocuments [0 ].Parts [0 ].Image
146+
147+ # Convert the ASCII data to binary.
148+ label_binary_data = binascii .a2b_base64 (ascii_label_data )
149+
150+ """
151+ This is an example of how to dump a label to a PNG file.
152+ """
153+ # This will be the file we write the label out to.
154+ pdf_file = open ('example_shipment_label.pdf' , 'wb' )
155+ pdf_file .write (label_binary_data )
156+ pdf_file .close ()
157+
158+ """
159+ This is an example of how to print the label to a serial printer. This will not
160+ work for all label printers, consult your printer's documentation for more
161+ details on what formats it can accept.
162+ """
163+ # Pipe the binary directly to the label printer. Works under Linux
164+ # without requiring PySerial. This WILL NOT work on other platforms.
165+ #label_printer = open("/dev/ttyS0", "w")
166+ #label_printer.write(label_binary_data)
167+ #label_printer.close()
168+
169+ """
170+ This is a potential cross-platform solution using pySerial. This has not been
171+ tested in a long time and may or may not work. For Windows, Mac, and other
172+ platforms, you may want to go this route.
173+ """
174+ #import serial
175+ #label_printer = serial.Serial(0)
176+ #print "SELECTED SERIAL PORT: "+ label_printer.portstr
177+ #label_printer.write(label_binary_data)
178+ #label_printer.close()
0 commit comments