forked from ekyna/Dpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-shipment-with-labels.php
146 lines (113 loc) · 4.67 KB
/
create-shipment-with-labels.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
require __DIR__ . '/../vendor/autoload.php';
use Ekyna\Component\Dpd\Exception;
use Ekyna\Component\Dpd\EPrint;
/* ---------------- Client and API ---------------- */
require __DIR__ . '/config.php';
$api = new EPrint\Api($ePrintConfig);
$relayPoint = "P50124";
/* ---------------- Create request ---------------- */
// Shipment request
$request = new EPrint\Request\StdShipmentLabelRequest();
$request->customer_centernumber = $centerNumber;
$request->customer_countrycode = $countryCode;
$request->services = new EPrint\Model\StdServices();
if ($usePredict) {
// Predict
$request->customer_number = $predictNumber;
// Predict contact
$request->services->contact = new EPrint\Model\Contact();
$request->services->contact->type = EPrint\Enum\ETypeContact::PREDICT;
$request->services->contact->sms = '0611111111';
} else {
// Classic
$request->customer_number = $classicNumber;
}
// (Optional) Label type: PNG, PDF, PDF_A6
$request->labelType = new EPrint\Model\LabelType();
$request->labelType->type = $type = EPrint\Enum\ELabelType::PDF_A6;
// Receiver address
$request->receiveraddress = new EPrint\Model\Address();
$request->receiveraddress->name = 'John Doe';
$request->receiveraddress->countryPrefix = 'FR';
$request->receiveraddress->zipCode = '35000';
$request->receiveraddress->city = 'Rennes';
$request->receiveraddress->street = '2 rue saint-louis';
$request->receiveraddress->phoneNumber = '0622222222';
// (Optional) Receiver address optional info
$request->receiverinfo = new EPrint\Model\AddressInfo();
$request->receiverinfo->vinfo1 = 'Complément adresse';
// Shipper address
$request->shipperaddress = new EPrint\Model\Address();
$request->shipperaddress->name = 'Example';
$request->shipperaddress->countryPrefix = 'FR';
$request->shipperaddress->zipCode = '22100';
$request->shipperaddress->city = 'Dinan';
$request->shipperaddress->street = '3 rue sainte-clare';
$request->shipperaddress->phoneNumber = '0633333333';
// Shipment weight
$request->weight = 1.2; // kg
// (Optional) Theoretical shipment date ('d/m/Y' or 'd.m.Y')
$request->shippingdate = date('d/m/Y');
// (Optional) Insurance
$request->services->extraInsurance = new EPrint\Model\ExtraInsurance();
$request->services->extraInsurance->type = EPrint\Enum\ETypeInsurance::BY_SHIPMENTS; // Always use this const
$request->services->extraInsurance->value = '100'; // 22k euros max
// (Optional) References and comment
$request->referencenumber = 'my_ref_1';
$request->reference2 = 'my_ref_2';
$request->reference3 = 'my_ref_3';
$request->customLabelText = 'Shipping comment...';
// Relay point
if (!$usePredict && !empty($relayPoint)) {
$request->services = new EPrint\Model\StdServices();
$request->services->parcelshop = new EPrint\Model\ParcelShop();
$request->services->parcelshop->shopaddress = new EPrint\Model\ShopAddress();
$request->services->parcelshop->shopaddress->shopid = $relayPoint;
}
/* ---------------- Get response ---------------- */
// Use API helper
try {
/** @var \Ekyna\Component\Dpd\EPrint\Response\CreateShipmentWithLabelsResponse $response */
$response = $api->CreateShipmentWithLabels($request);
} catch (Exception\ExceptionInterface $e) {
echo "Error: " . $e->getMessage();
if ($debug && $e instanceof Exception\ClientException) {
echo "\nRequest:\n" . $e->request;
echo "\nResponse:\n" . $e->response;
}
exit();
}
echo get_class($response) . "\n";
// Get result model
/** @var \Ekyna\Component\Dpd\EPrint\Model\ShipmentsWithLabels $result */
$result = $response->CreateShipmentWithLabelsResult;
echo get_class($result) . "\n";
// Get shipments
$idx = 1;
/** @var \Ekyna\Component\Dpd\EPrint\Model\Shipment $shipment */
foreach ($result->shipments as $shipment) {
echo get_class($shipment) . "\n";
// Tracking url:
echo "Shipment#$idx tracking url: {$shipment->getTrackingUrl()}\n";
}
// Get label model
/** @var \Ekyna\Component\Dpd\EPrint\Model\Label $label */
$idx = 0;
foreach ($result->labels as $label) {
$idx++;
echo "Label#$idx: " . strlen($label->label) . " (" . $label->type . ")\n";
if ($type === EPrint\Enum\ELabelType::PNG) {
if (false === $im = imagecreatefromstring($label->label)) {
throw new \Exception("Failed to retrieve the shipment label data.");
}
$filename = $labelDir . DIRECTORY_SEPARATOR . sprintf('reference_%s.png', $idx);
if (file_exists($filename)) unlink($filename);
imagepng($im, $filename);
imagedestroy($im);
} else {
$filename = $labelDir . DIRECTORY_SEPARATOR . sprintf('reference_%s.pdf', $idx);
if (file_exists($filename)) unlink($filename);
file_put_contents($filename, $label->label);
}
}