SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.http import HttpResponse
  2. from django.http import JsonResponse
  3. import logging
  4. import sys
  5. import datetime
  6.  
  7. from django.forms.models import model_to_dict
  8. from urllib.error import URLError
  9. from fedex.config import FedexConfig
  10. from fedex.services.availability_commitment_service import FedexAvailabilityCommitmentRequest
  11. from fedex.services.address_validation_service import FedexAddressValidationRequest
  12. from fedex.services.rate_service import FedexRateServiceRequest
  13. from fedex.tools.conversion import sobject_to_dict
  14. from fedex.tools.conversion import sobject_to_json
  15. from fedex.services.ship_service import FedexProcessShipmentRequest, FedexDeleteShipmentRequest
  16. from django.forms.models import model_to_dict
  17. from fedex.services.track_service import FedexTrackRequest
  18. from fedex.base_service import FedexError, FedexFailure, FedexBaseServiceException
  19. from fedex.tools.conversion import sobject_to_json
  20.  
  21.  
  22. kitlender_address = {
  23.     'line1': '350 Mountain Rd',
  24.     'line2': '2sdadsadssssss',
  25.     'line3': '3dsads',
  26.     'line4': 'Stowe',
  27.     'state': 'VT',
  28.     'first_name': 'FIRST NAME',
  29.     'last_name': 'LAST NAME EXTAAAAAACHARSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSs',
  30.     'company_name': 'Kitlender',
  31.     'postcode': '05672',
  32.     'phone_number': '888-473-3111'
  33. }
  34.  
  35. class CourierWrapper(object):
  36.  
  37.     CONFIG_OBJ = FedexConfig(key='EKB0VPic6IRsXKfl',
  38.                              password='Xkc1hTbWGA9dhCS2mTgNREfNR',
  39.                              account_number='510087160',
  40.                              meter_number='113998879',
  41.                              use_test_server=True)
  42.  
  43.     def add_courier_event(self, event_type, recipient_address):
  44.         if event_type.name == "To Customer":
  45.            return self.add_shipment(kitlender_address, recipient_address)
  46.         if event_type.name == "To Kitlender":
  47.            return self.add_shipment(recipient_address, kitlender_address)
  48.         if event_type.name == "To Customer - Force":
  49.            return self.add_shipment(kitlender_address, recipient_address, force=True)
  50.  
  51.     def add_shipment(self, s_a, r_a, force=False):
  52.         # rember about UTF8 stuff
  53.  
  54.         shipment = FedexProcessShipmentRequest(self.CONFIG_OBJ)
  55.  
  56.         shipment.RequestedShipment.DropoffType = 'REGULAR_PICKUP'
  57.         shipment.RequestedShipment.ServiceType = 'FEDEX_GROUND'
  58.         shipment.RequestedShipment.PackagingType = 'YOUR_PACKAGING'
  59.         # add Contact.CompanyName
  60.         shipment.RequestedShipment.Shipper.Contact.CompanyName = s_a['company_name']
  61.         shipment.RequestedShipment.Shipper.Contact.PersonName = '{} {}'.format(s_a['first_name'], s_a['last_name'])
  62.         shipment.RequestedShipment.Shipper.Contact.PhoneNumber = '123456757'# s_a['phone_number']
  63.         shipment.RequestedShipment.Shipper.Address.StreetLines = '{}, {}'.format(s_a['line1'], s_a['line2'])
  64.         shipment.RequestedShipment.Shipper.Address.City = s_a['line4']
  65.         shipment.RequestedShipment.Shipper.Address.StateOrProvinceCode = s_a['state']
  66.         shipment.RequestedShipment.Shipper.Address.PostalCode = s_a['postcode']
  67.         shipment.RequestedShipment.Shipper.Address.CountryCode = 'US'
  68.         # shipment.RequestedShipment.Shipper.AssociatedShipmentDetail.TrackingId = '794655731416'
  69.         # shipment.RequestedShipment.PackageCount = 2
  70.  
  71.         shipment.RequestedShipment.Recipient.Contact.CompanyName = 'REALY LONG COMAPNY NAMEEEEEEEEEEEEEEEEEEEEEEEEXXXXXXXXXXXXXXXXXXXXXXXXXEEEEEEEEEEEEXXXXXXXXEEEEEEEEE' # r_a['company_name']
  72.         shipment.RequestedShipment.Recipient.Contact.PersonName = 'LONG  PERSON NAMEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEESADSADSADSADSADSADSADSADSA' #'{} {}'.format(r_a['first_name'], r_a['last_name'])
  73.         shipment.RequestedShipment.Recipient.Contact.PhoneNumber = '123456757' #r_a['phone_number']
  74.  
  75.         shipment.RequestedShipment.Recipient.Address.StreetLines = ['line1_dabFDKULYHSGAFLUDBSLFG DHNFNFNFNFNFNFNFSADSDAGDFSAGFSDGFDSGFDSGFSDGFDSGFDSGFSDGFSDGFDSGFDSGFDSGFDSGFDSGFDSGFDSGFDSGFFDSGFDSG', 'line2dabFDKULYHSGAFLUDBSLFG DHNFNFNFNFNFNFNFSADSDAGDFSAGFSDGFDSGFDSGFSDGFDSGFDSGFSDGFSDGFDSGFDSGFDSGFDSGFDSGFDSGFDSGFDSGFFDSGFDSG']
  76.         shipment.RequestedShipment.Recipient.Address.City = r_a['line4']
  77.         shipment.RequestedShipment.Recipient.Address.StateOrProvinceCode = r_a['state']
  78.         shipment.RequestedShipment.Recipient.Address.PostalCode = r_a['postcode']
  79.         shipment.RequestedShipment.Recipient.Address.CountryCode = 'US'
  80.         shipment.RequestedShipment.EdtRequestType = 'NONE'
  81.         # shipment.RequestedShipment.MasterTrackingId = "794655731416"
  82.         shipment.RequestedShipment.ShippingChargesPayment.Payor.ResponsibleParty.AccountNumber \
  83.             = self.CONFIG_OBJ.account_number
  84.  
  85.         shipment.RequestedShipment.ShippingChargesPayment.PaymentType = 'SENDER'
  86.         shipment.RequestedShipment.LabelSpecification.LabelFormatType = 'COMMON2D'
  87.         shipment.RequestedShipment.LabelSpecification.ImageType = 'PNG'
  88.         shipment.RequestedShipment.LabelSpecification.LabelStockType = 'PAPER_7X4.75'
  89.         shipment.RequestedShipment.LabelSpecification.LabelPrintingOrientation = 'BOTTOM_EDGE_OF_TEXT_FIRST'
  90.  
  91.         # Use order if setting multiple labels or delete
  92.         del shipment.RequestedShipment.LabelSpecification.LabelOrder
  93.  
  94.         # package1_weight = shipment.create_wsdl_object_of_type('Weight')
  95.         # package1_weight.Value = 12.0
  96.         # package1_weight.Units = "LB"
  97.         # package1 = shipment.create_wsdl_object_of_type('RequestedPackageLineItem')
  98.         # package1.PhysicalPackaging = 'ENVELOPE'
  99.         # package1.Weight = package1_weight
  100.         # # customer_reference = shipment.create_wsdl_object_of_type('CustomerReference')
  101.         # # customer_reference.CustomerReferenceType="CUSTOMER_REFERENCE"
  102.         # # customer_reference.Value = "dajesz malina, firmowy samochod"
  103.         # # package1.CustomerReferences.append(customer_reference)
  104.         # print(package1)
  105.         # shipment.add_package(package1)
  106.         # shipment.add_package(package1)
  107.         shipment.RequestedShipment.PackageCount = '2'
  108.        
  109.         package1 = self.create_package(shipment)
  110.         # package2 = self.create_package(shipment)
  111.  
  112.         shipment.RequestedShipment.RequestedPackageLineItems.append(package1)
  113.         shipment.RequestedShipment.RequestedPackageLineItems.append(package1)
  114.  
  115.  
  116.  
  117.         # shipment.add_package(package1)
  118.         # shipment.add_package(package2)
  119.  
  120.         try:
  121.             shipment.send_validation_request()
  122.             shipment.send_request()
  123.             json_response = sobject_to_dict(shipment.response)
  124.             if force is False:
  125.                 assert shipment.response.HighestSeverity == 'SUCCESS'
  126.             return (True, json_response)
  127.         except URLError:
  128.             return (False, "Courier service has a problem. Please repeat action")
  129.         except AssertionError:
  130.             error_message = ""
  131.             for message in json_response["Notifications"]:
  132.                 error_message += "{}: {}\n".format(message['Severity'], message['Message'])
  133.             return (False, error_message)
  134.         except (FedexError, FedexFailure, FedexBaseServiceException) as e:
  135.             return (False, str(e))
  136.         except Exception as e:
  137.             return (False, str(e))
  138.         #
  139.         # assert shipment.response
  140.         # assert shipment.response.HighestSeverity == 'SUCCESS'
  141.         track_id = shipment.response.CompletedShipmentDetail.CompletedPackageDetails[0].TrackingIds[0].TrackingNumber
  142.         # assert track_id
  143.  
  144.         # json_response = sobject_to_dict(shipment.response)
  145.         # print(json_response)
  146.         # return json_response
  147.  
  148.     def address_validation(self, r_a):
  149.         # r_a = kitlender_address
  150.         avs_request = FedexAddressValidationRequest(self.CONFIG_OBJ)
  151.  
  152.         address1 = avs_request.create_wsdl_object_of_type('AddressToValidate')
  153.         address1.Address.StreetLines = [r_a['line1'], r_a['line2'], r_a['line3']]
  154.         address1.Address.City = r_a['line4']
  155.         address1.Address.StateOrProvinceCode = r_a['state']
  156.         address1.Address.PostalCode = r_a['postcode']
  157.         address1.Address.CountryCode = 'US'
  158.         address1.Address.Residential = False
  159.         avs_request.add_address(address1)
  160.  
  161.         avs_request.send_request()
  162.         print(avs_request.response)
  163.         assert avs_request.response
  164.  
  165.     def delete_package(self, tracking_number):
  166.         del_shipment = FedexDeleteShipmentRequest(self.CONFIG_OBJ)
  167.         del_shipment.DeletionControlType = "DELETE_ALL_PACKAGES"
  168.         del_shipment.TrackingId.TrackingNumber = tracking_number
  169.         del_shipment.TrackingId.TrackingIdType = 'EXPRESS'
  170.         try:
  171.             del_shipment.send_request()
  172.             assert del_shipment.response.HighestSeverity == 'SUCCESS'
  173.             print(del_shipment.response)
  174.             return (True, True)
  175.         except URLError:
  176.             return (False, "Courier service has a problem. Please repeat action")
  177.         except AssertionError:
  178.             return (False, "")
  179.         except FedexError as e:
  180.             return (False, str(e))
  181.  
  182.     def create_package(self, shipment):
  183.         # Create Weight, in pounds.
  184.         package1_weight = shipment.create_wsdl_object_of_type('Weight')
  185.         package1_weight.Value = 1.0
  186.         package1_weight.Units = "LB"
  187.  
  188.         # Insured Value
  189.         # package1_insure = shipment.create_wsdl_object_of_type('Money')
  190.         # package1_insure.Currency = 'USD'
  191.         # package1_insure.Amount = 1.0
  192.  
  193.         # Create PackageLineItem
  194.         package1 = shipment.create_wsdl_object_of_type('RequestedPackageLineItem')
  195.         # BAG, BARREL, BASKET, BOX, BUCKET, BUNDLE, CARTON, CASE, CONTAINER, ENVELOPE etc..
  196.         package1.PhysicalPackaging = 'ENVELOPE'
  197.         package1.Weight = package1_weight
  198.         # package1.MasterTrackingId = '794655731416'
  199.  
  200.         # Add Insured and Total Insured values.
  201.         # package1.InsuredValue = package1_insure
  202.         # shipment.RequestedShipment.TotalInsuredValue = package1_insure
  203.  
  204.         # Add customer reference
  205.         # customer_reference = shipment.create_wsdl_object_of_type('CustomerReference')
  206.         # customer_reference.CustomerReferenceType="CUSTOMER_REFERENCE"
  207.         # customer_reference.Value = "your customer reference number"
  208.         # package1.CustomerReferences.append(customer_reference)
  209.  
  210.         # Add department number
  211.         # department_number = shipment.create_wsdl_object_of_type('CustomerReference')
  212.         # department_number.CustomerReferenceType="DEPARTMENT_NUMBER"
  213.         # department_number.Value = "your department number"
  214.         # package1.CustomerReferences.append(department_number)
  215.  
  216.         # Add invoice number
  217.         # invoice_number = shipment.create_wsdl_object_of_type('CustomerReference')
  218.         # invoice_number.CustomerReferenceType="INVOICE_NUMBER"
  219.         # invoice_number.Value = "your invoice number"
  220.         # package1.CustomerReferences.append(invoice_number)
  221.  
  222.         # Add a signature option for the package using SpecialServicesRequested or comment out.
  223.         # SpecialServiceTypes can be APPOINTMENT_DELIVERY, COD, DANGEROUS_GOODS, DRY_ICE, SIGNATURE_OPTION etc..
  224.         package1.SpecialServicesRequested.SpecialServiceTypes = 'SIGNATURE_OPTION'
  225.         # SignatureOptionType can be ADULT, DIRECT, INDIRECT, NO_SIGNATURE_REQUIRED, SERVICE_DEFAULT
  226.         package1.SpecialServicesRequested.SignatureOptionDetail.OptionType = 'SERVICE_DEFAULT'
  227.  
  228.         return package1
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top