-
Notifications
You must be signed in to change notification settings - Fork 19
/
Financial.php
143 lines (114 loc) · 4.67 KB
/
Financial.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
<?php declare(strict_types=1);
use MpApiClient\Common\Authenticators\ClientIdAuthenticator;
use MpApiClient\Exception\MpApiException;
use MpApiClient\MpApiClient;
use MpApiClient\MpApiClientOptions;
require '../vendor/autoload.php';
$options = new MpApiClientOptions(
new ClientIdAuthenticator('my-client-id')
);
$client = MpApiClient::createFromOptions('my-app-name', $options);
//
// Get invoice list
//
try {
$invoiceList = $client->financial()->listInvoices(null);
// Print all invoices as json object
echo json_encode($invoiceList, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// Get all invoices as array
var_dump($invoiceList->jsonSerialize());
// Iterate over the returned list
foreach ($invoiceList as $invoice) {
echo 'Invoice number: ' . $invoice->getInvoiceNumber() . PHP_EOL;
echo 'Created at: ' . $invoice->getCreatedAt()->format(DATE_RFC3339) . PHP_EOL;
echo 'Total: ' . $invoice->getTotal() . PHP_EOL;
echo 'Currency: ' . $invoice->getCurrency() . PHP_EOL;
echo 'Item count: ' . $invoice->getItems()->count() . PHP_EOL;
echo PHP_EOL;
}
} catch (MpApiException | Exception $e) {
echo 'Unexpected error occurred while loading invoice list: ' . $e->getMessage();
}
//
// Get invoice detail
//
try {
$invoice = $client->financial()->getInvoice('invoice-id');
// Print invoice as json object
echo json_encode($invoice, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// Get invoice as array
var_dump($invoice->jsonSerialize());
// Show some invoice data
echo 'Created at: ' . $invoice->getCreatedAt()->format(DATE_RFC3339) . PHP_EOL;
echo 'Total: ' . $invoice->getTotal() . PHP_EOL;
echo 'Currency: ' . $invoice->getCurrency() . PHP_EOL;
echo 'Item count: ' . $invoice->getItems()->count() . PHP_EOL;
echo PHP_EOL;
} catch (MpApiException | Exception $e) {
echo 'Unexpected error occurred while loading invoice: ' . $e->getMessage();
}
//
// Download invoice
//
try {
$response = $client->financial()->downloadInvoice('invoice-id');
header('Content-Type: ' . $response->getHeaderLine('Content-Type'));
// this header forces browser to download the file, comment out to display attachment in a browser
header('Content-Disposition: attachment; filename="invoice-id.pdf"');
echo $response->getBody()->getContents();
} catch (MpApiException $e) {
echo 'Unexpected error occurred while downloading invoice: ' . $e->getMessage();
}
//
// Get offset list
//
try {
$offsetsList = $client->financial()->listOffsets(null);
// Print all offsets as json object
echo json_encode($offsetsList, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// Get all invoices as array
var_dump($offsetsList->jsonSerialize());
// Iterate over the returned list
foreach ($offsetsList as $offset) {
echo 'Document number: ' . $offset->getDocumentNumber() . PHP_EOL;
echo 'Created at: ' . $offset->getCreatedAt()->format(DATE_RFC3339) . PHP_EOL;
echo 'Diff price: ' . $offset->getDiffPrice() . PHP_EOL;
echo 'Currency: ' . $offset->getCurrency() . PHP_EOL;
echo 'Invoice count: ' . $offset->getInvoices()->count() . PHP_EOL;
echo 'Order count: ' . $offset->getOrders()->count() . PHP_EOL;
echo PHP_EOL;
}
} catch (MpApiException | Exception $e) {
echo 'Unexpected error occurred while loading offset list: ' . $e->getMessage();
}
//
// Get offset detail
//
try {
$offset = $client->financial()->getOffset('offset-id');
// Print offset as json object
echo json_encode($offset, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// Get offset as array
var_dump($offset->jsonSerialize());
// Show some offset data
echo 'Document number: ' . $offset->getDocumentNumber() . PHP_EOL;
echo 'Created at: ' . $offset->getCreatedAt()->format(DATE_RFC3339) . PHP_EOL;
echo 'Diff price: ' . $offset->getDiffPrice() . PHP_EOL;
echo 'Currency: ' . $offset->getCurrency() . PHP_EOL;
echo 'Attachment: ' . $offset->getAttachment()->getFilename() . PHP_EOL;
echo PHP_EOL;
} catch (MpApiException | Exception $e) {
echo 'Unexpected error occurred while loading offset: ' . $e->getMessage();
}
//
// Download offset
//
try {
$response = $client->financial()->downloadOffset('offset-id');
header('Content-Type: ' . $response->getHeaderLine('Content-Type'));
// this header forces browser to download the file, comment out to display attachment in a browser
header('Content-Disposition: attachment; filename="offset-id.pdf"');
echo $response->getBody()->getContents();
} catch (MpApiException $e) {
echo 'Unexpected error occurred while downloading offset: ' . $e->getMessage();
}