|
| 1 | +import shippo |
| 2 | +from datetime import datetime, timedelta |
| 3 | + |
| 4 | +""" |
| 5 | +In this tutorial we have an order with a sender address, |
| 6 | +recipient address and parcel. The shipment is going from the |
| 7 | +United States to an international location. |
| 8 | +
|
| 9 | +In addition to that we know that the customer expects the |
| 10 | +shipment to arrive within 3 days. We now want to purchase |
| 11 | +the cheapest shipping label with a transit time <= 3 days. |
| 12 | +""" |
| 13 | + |
| 14 | +# replace <API-KEY> with your key |
| 15 | +shippo.config.api_key = "<API-KEY>" |
| 16 | + |
| 17 | +# Example address_from object dict |
| 18 | +# The complete reference for the address object is available here: https://goshippo.com/docs/reference#addresses |
| 19 | +address_from = { |
| 20 | + "company": "", |
| 21 | + "street_no": "", |
| 22 | + "name": "Shippo Friend", |
| 23 | + "street1": "1092 Indian Summer Ct", |
| 24 | + "city": "San Jose", |
| 25 | + "state": "CA", |
| 26 | + "zip": "95122", |
| 27 | + "country": "US", |
| 28 | + "phone": "+1 555 341 9393", |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +# Example address_to object dict |
| 33 | +# The complete reference for the address object is available here: https://goshippo.com/docs/reference#addresses |
| 34 | + |
| 35 | +address_to_international = { |
| 36 | + "name": "Mrs. Hippo", |
| 37 | + "street1": "200 University Ave W", |
| 38 | + "street2": "", |
| 39 | + "city": "Waterloo", |
| 40 | + "state": "ON", |
| 41 | + "zip": "N2L 3G1", |
| 42 | + "country": "CA", |
| 43 | + "phone": "+1 555 341 9393", |
| 44 | + |
| 45 | + "metadata": "For Order Number 123" |
| 46 | +} |
| 47 | + |
| 48 | +# parcel object dict |
| 49 | +# The complete reference for parcel object is here: https://goshippo.com/docs/reference#parcels |
| 50 | +parcel = { |
| 51 | + "length": "5", |
| 52 | + "width": "5", |
| 53 | + "height": "5", |
| 54 | + "distance_unit": "in", |
| 55 | + "weight": "2", |
| 56 | + "mass_unit": "lb" |
| 57 | +} |
| 58 | + |
| 59 | +# Example CustomsItems object. |
| 60 | +# The complete reference for customs object is here: https://goshippo.com/docs/reference#customsitems |
| 61 | +customs_item = { |
| 62 | + "description": "T-Shirt", |
| 63 | + "quantity": 2, |
| 64 | + "net_weight": "400", |
| 65 | + "mass_unit": "g", |
| 66 | + "value_amount": "20", |
| 67 | + "value_currency": "USD", |
| 68 | + "origin_country": "US", |
| 69 | + "tariff_number": "" |
| 70 | +} |
| 71 | + |
| 72 | +# Creating the CustomsDeclaration |
| 73 | +# The details on creating the CustomsDeclaration is here: https://goshippo.com/docs/reference#customsdeclarations |
| 74 | +customs_declaration = shippo.CustomsDeclaration.create( |
| 75 | + contents_type='MERCHANDISE', |
| 76 | + contents_explanation='T-Shirt purchase', |
| 77 | + non_delivery_option='RETURN', |
| 78 | + certify=True, |
| 79 | + certify_signer='Mr Hippo', |
| 80 | + items=[customs_item]) |
| 81 | + |
| 82 | +# Creating the shipment object. asynchronous=False indicates that the function will wait until all |
| 83 | +# rates are generated before it returns. |
| 84 | + |
| 85 | +# The reference for the shipment object is here: https://goshippo.com/docs/reference#shipments |
| 86 | +# By default Shippo API operates on an async basis. You can read about our async flow here: https://goshippo.com/docs/async |
| 87 | +shipment_international = shippo.Shipment.create( |
| 88 | + address_from=address_from, |
| 89 | + address_to=address_to_international, |
| 90 | + parcels=[parcel], |
| 91 | + customs_declaration=customs_declaration.object_id, |
| 92 | + asynchronous=False) |
| 93 | + |
| 94 | +# Get the first usps or dhl express rate. |
| 95 | +# The details on the returned object are here: https://goshippo.com/docs/reference#rates |
| 96 | +filtered_rates = [] |
| 97 | +for rate in shipment_international.rates: |
| 98 | + if rate.provider.lower() == "usps" or rate.provider.lower() == "dhl_express": |
| 99 | + filtered_rates.append(rate) |
| 100 | + # return strtolower($rate['provider']) == 'usps' Or strtolower($rate['provider']) == 'dhl_express'; |
| 101 | + |
| 102 | +rate_international = filtered_rates[0] |
| 103 | +selected_rate_carrier_account = rate_international.carrier_account |
| 104 | + |
| 105 | +# Purchase the desired rate. |
| 106 | +# The complete information about purchasing the label: https://goshippo.com/docs/reference#transaction-create |
| 107 | +transaction_international = shippo.Transaction.create( |
| 108 | + rate=rate_international.object_id, asynchronous=False) |
| 109 | + |
| 110 | +if transaction_international.status != "SUCCESS": |
| 111 | + print("Failed purchasing the label due to:") |
| 112 | + for message in transaction_international.messages: |
| 113 | + print("- %s" % message['text']) |
| 114 | + |
| 115 | +# $pickupTimeStart = date('Y-m-d H:i:s', time()); |
| 116 | +# $pickupTimeEnd = date('Y-m-d H:i:s', time() + 60*60*24); |
| 117 | +pickupTimeStart = datetime.now() + timedelta(hours=1) |
| 118 | +pickupTimeEnd = pickupTimeStart + timedelta(days=1) |
| 119 | + |
| 120 | +# Schedule the pickup |
| 121 | +# Only 1 pickup can be scheduled in a day |
| 122 | +try: |
| 123 | + pickup = shippo.Pickup.create( |
| 124 | + carrier_account= selected_rate_carrier_account, |
| 125 | + location= { |
| 126 | + "building_location_type" : "Knock on Door", |
| 127 | + "address" : address_from, |
| 128 | + }, |
| 129 | + transactions= [transaction_international.object_id], |
| 130 | + requested_start_time= pickupTimeStart.isoformat() + "Z", |
| 131 | + requested_end_time= pickupTimeEnd.isoformat() + "Z", |
| 132 | + is_test= False |
| 133 | + ) |
| 134 | +except shippo.error.InvalidRequestError as err: |
| 135 | + print("A pickup has already been scheduled for today.") |
| 136 | +else: |
| 137 | + if pickup.status == "SUCCESS": |
| 138 | + print("Pickup has been scheduled") |
| 139 | + else: |
| 140 | + print("Failed scheduling a pickup:") |
| 141 | + for message in pickup.messages: |
| 142 | + print("- %s" % message['text']) |
| 143 | + |
| 144 | +# For more tutorials of address validation, tracking, returns, refunds, and other functionality, check out our |
| 145 | +# complete documentation: https://goshippo.com/docs/ |
0 commit comments