-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsend-letter.php
More file actions
79 lines (60 loc) · 2.44 KB
/
Copy pathsend-letter.php
File metadata and controls
79 lines (60 loc) · 2.44 KB
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
<?php
/**
* Create a letter as a concept, inspect it, then delete it again.
*
* Nothing is sent: the item is created with send(false) and removed at the end,
* so this is safe to run against a sandbox mailbox.
*
* POSTBODE_API_KEY=your-key php examples/send-letter.php PSBD
*/
declare(strict_types=1);
use Postbode\Enum\PostalPlex;
use Postbode\Enum\PostalPrinting;
use Postbode\Enum\ShippingType;
use Postbode\Exception\PostbodeException;
use Postbode\Exception\ValidationException;
use Postbode\Request\PostalRequest;
/** @var \Postbode\PostbodeApiClient $postbode */
$postbode = require __DIR__ . '/bootstrap.php';
$customerCode = $argv[1] ?? null;
if ($customerCode === null) {
fwrite(STDERR, "Usage: php examples/send-letter.php <customer-code>\n");
exit(1);
}
$envelope = $postbode->envelopes->listForMailbox($customerCode)->first();
if ($envelope === null) {
fwrite(STDERR, "Mailbox {$customerCode} has no envelopes yet.\n");
exit(1);
}
$request = PostalRequest::make($customerCode, $envelope->uuid)
->addDocumentFromFile(__DIR__ . '/../tests/pdf/Letter.pdf')
->shipping(ShippingType::NL_FAST)
->printing(PostalPrinting::BLACK)
->plex(PostalPlex::DUPLEX)
->customerReference('EXAMPLE-' . bin2hex(random_bytes(4)))
->metadata(['source' => 'postbode-api examples'])
->send(false);
try {
$postal = $postbode->postals->create($request);
} catch (ValidationException $exception) {
fwrite(STDERR, $exception->getMessage() . "\n");
foreach ($exception->getErrors() as $field => $messages) {
fwrite(STDERR, sprintf(" %s: %s\n", $field, implode(' ', $messages)));
}
exit(1);
} catch (PostbodeException $exception) {
fwrite(STDERR, $exception->getMessage() . "\n");
exit(1);
}
printf("Created %s\n", $postal->reference);
printf("UUID %s\n", $postal->uuid);
printf("Status %s (%d)\n", $postal->status->name, $postal->status->rawCode);
printf("Pages %d on %d sheets, %d grams\n", $postal->pages, $postal->sheets, $postal->weight);
printf("Price € %.2f incl. VAT\n", $postal->financial->price->amountInclVat);
// Retrieving it again proves the item exists on the server, not just in this response.
$fetched = $postbode->postals->get($postal->uuid);
printf("Fetched %s\n", $fetched->reference);
// To actually ship it, call this instead of the delete below:
// $postbode->postals->send($postal->uuid);
$postbode->postals->delete($postal->uuid);
echo "Deleted the concept again.\n";