UPS makes available to generate labels on your own. They have XML API to do this. We provide you with nice .NET wrapper of it.
The whole process has two steps:
Requesting label
Accepting it.
UPS allows you also to void your request.
These funcionalities are covered by three methods of UPSShipmentProvider class:
Confirm, where you pass three parameters:
UPSAuthTicket with authentication details
UPSShipmentConfirmRequest with your shipment details
boolean value indicating whether you want to validate your request before sending to UPS.
And it returns UPSShipmentConfirmResponse object with confirmation code and tracking number.
Accept, where you pass three parameters:
UPSAuthTicket with authentication details
UPSShipmentAcceptRequest with your confirmation code.
boolean value indicating whether you want to validate your request before sending to UPS.
And it returns UPSShipmentAcceptResponse object with 64-encoded gif image of your label.
Void, where you pass three parameters:
UPSAuthTicket
UPSShipmentVoidRequest with your tracking number.
boolean value indicating whether you want to validate your request before sending to UPS.
So to generate the label request you need to create UPSShipmentConfirmRequest object:
UPSShipmentConfirmRequest confirmRequest = new UPSShipmentConfirmRequest()
and it:
Specify label type:
UPSLabelSpecification labelType = new UPSLabelSpecification();
confirmRequest.LabelSpecification = labelType;
labelType.LabelImageFormat = new UPSLabelImageFormat(UPSLabelImageFormatCode.Gif);
labelType.LabelPrintMethod = new UPSLabelImageFormat(UPSLabelImageFormatCode.Gif);
Create new UPSShipment representing your shipment:
UPSShipment shipment = new UPSShipment();
confirmRequest.Shipment = shipment;
Fill your shipment details:
Original address:
shipment.ShipFrom = new UPSShipFrom();
shipment.ShipFrom.Address = new UPSAddress();
shipment.ShipFrom.Address.PostalCode = "33131";
// ...
Destination address:
shipment.ShipTo= new UPSShipFrom();
shipment.ShipTo.Address = new UPSAddress();
shipment.ShipTo.Address.PostalCode = "12345";
// ...
Service type:
shipment.Service = new UPSService( UPSServiceCode.Ground );
Fill your package details:
UPSPackage package = new UPSPackage();
shipment.Package = package;
package.PackagingType = new UPSPackaging(UPSPackagingCode.UPSLetter);
Choose payment method:
result.Shipment.PaymentInformation = new UPSPaymentInformation();
UPSPrepaid prepaid = new UPSPrepaid(); // for example: payment by credit card
result.Shipment.PaymentInformation.Prepaid = prepaid;
prepaid.BillShipper = new UPSBillShipper();
UPSCreditCard cc = new UPSCreditCard();
prepaid.BillShipper.CreditCard = cc;
cc.Type = UPSCreditCardType.VISA;
//cc.Address = ... //Fill address data
cc.ExpirationDate = new DateTime(2010, 10, 1);
cc.Number = "4213521200167658";
cc.SecurityCode = "045";
That's it. You specifed what and where you want to send and you can continue with the rest of the process:
UPSShipmentProvider provider = new UPSShipmentProvider();
UPSShipmentConfirmResponse confirmResponse =
provider.Confirm(Ticket, confirmRequest, false);
UPSShipmentAcceptRequest acceptRequest = new UPSShipmentAcceptRequest();
acceptRequest.ShipmentDigest = confirmResponse.ShipmentDigest;
UPSShipmentAcceptResponse acceptResponse = provider.Accept(Ticket, acceptRequest, false);
string label = acceptResponse.ShipmentResults.PackageResults.LabelImage.GraphicImage;
In label variable 64-encoded label is stored. You are also provided with tools to display or save it:
Image image =GraphicsUtil.StringToGif(label);
image.Save("c:/My first label.gif");
To void your shipment request you need to fill UPSShipmentVoidRequest object:
UPSShipmentVoidRequest voidRequest = new UPSShipmentVoidRequest();
voidRequest.ExpandedVoidShipment = new UPSExpandedVoidShipment();
voidRequest.ExpandedVoidShipment.ShipmentIdentificationNumber = trackingNubmer;
UPSShipmentVoidResponse voidResponse = provider.Void(Ticket, voidRequest, false);