-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExampleWebhookReceiver.php
84 lines (63 loc) · 2.69 KB
/
ExampleWebhookReceiver.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
<?php
require __DIR__ . '/vendor/autoload.php';
$payload = file_get_contents("php://input");
$expectedHash = hash_hmac('sha256', $payload, "secretKey");
if (empty($_SERVER["HTTP_X_ECONNECT_SIGNATURE"]) || $_SERVER["HTTP_X_ECONNECT_SIGNATURE"] != "sha256=" . $expectedHash) {
header("HTTP/1.1 403 Forbidden");
die("Forbidden (Signature invalid)");
}
$webhook = json_decode($payload);
if ($webhook->topic != "InvoiceReceived") {
header("HTTP/1.1 501 Not Implemented");
die("Not Implemented (Topic not supported)");
}
$sentDate = new \DateTime($webhook->sentOn);
$minDate = new \DateTime();
$minDate->sub(new \DateInterval('PT300S'));
$maxDate = new \DateTime();
$maxDate->add(new \DateInterval('PT300S'));
if ($sentDate < $minDate) {
header("HTTP/1.1 400 Bad Request");
die("Bad Request (Too old ".$sentDate->format('c')." < ".$minDate->format('c').")");
}
if ($sentDate > $maxDate) {
header("HTTP/1.1 400 Bad Request");
die("Bad Request (Too new ".$sentDate->format('c')." > ".$maxDate->format('c').")");
}
$httpClient = new GuzzleHttp\Client();
$config = \EConnect\Psb\Configuration::getDefaultConfiguration();
$config
->setUsername("{username}")
->setPassword("{password}")
->setClientId("2210f77eed3a4ab2") // For testing only
->setClientSecret("ddded83702534a6c9cadde3d1bf3e94a") // For testing only
->setHost("https://accp-psb.econnect.eu") // For testing only
->setApiKey('Subscription-Key', 'Sandbox.Accp.W2NmWFRINXokdA'); // For testing only
$purchaseInvoiceApi = new EConnect\Psb\Api\PurchaseInvoiceApi(
$httpClient,
$config
);
$authn = new \EConnect\Psb\Authentication($config);
$token = $authn->login();
// Download as NLCUIS and print file
$format = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2::Invoice##urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0::2.1";
header('Content-Type: application/xml; charset=utf-8');
$file = $purchaseInvoiceApi->downloadPurchaseInvoice($webhook->partyId, $webhook->documentId, $format);
while (!$file->eof()) {
echo $file->fgets();
}
// Invoice response
$invoice_response = new \EConnect\Psb\Model\InvoiceResponse();
// Acknowledgement
$invoice_response->setStatus("AB");
// Reject
// $reasons = new \EConnect\Psb\Model\InvoiceResponseReasons();
// $reasons->setRef("Purchase order number is invalid. The format should be POnnnnnn.");
// $actions = new \EConnect\Psb\Model\InvoiceResponseActions();
// $actions->setNin("Please send a new invoice.");
// $invoice_response
// ->setStatus("RE")
// ->setReasons($reasons)
// ->setActions($actions)
// ->setNote("Send form php");
$purchaseInvoiceApi->sendInvoiceResponse($webhook->partyId, $webhook->documentId, $invoice_response);