-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathexample.php
More file actions
133 lines (112 loc) · 3.91 KB
/
example.php
File metadata and controls
133 lines (112 loc) · 3.91 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
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
<?php
// Config bitflags
const BYSQUARE_DEBURR = 0b00000001; // Bit 0: Enable diacritics removal
// Version values (in high byte, bits 24-31)
const BYSQUARE_VERSION_110 = 1 << 24; // v1.1.0
// Special config value for default
// PAY defaults: v1.2.0 + deburr + validate
// Invoice defaults: v1.0.0 + validate (no deburr)
const BYSQUARE_CONFIG_DEFAULT = -1;
$libExt = match (PHP_OS_FAMILY) {
'Darwin' => 'dylib',
'Windows' => 'dll',
default => 'so',
};
$ffi = FFI::cdef("
char* bysquare_pay_encode(char* jsonData, int config);
char* bysquare_pay_decode(char* qrString);
char* bysquare_invoice_encode(char* jsonData, int config);
char* bysquare_invoice_decode(char* qrString);
int bysquare_detect_type(char* qrString);
void bysquare_free(char* ptr);
", __DIR__ . "/../../../go/bin/libbysquare.{$libExt}");
$paymentData = [
'payments' => [[
'type' => 1,
'amount' => 123.45,
'currencyCode' => 'EUR',
'variableSymbol' => '987654',
'beneficiary' => ['name' => 'John Doe'],
'bankAccounts' => [['iban' => 'SK9611000000002918599669']]
]]
];
$invoiceData = [
'documentType' => 0,
'invoiceId' => 'FV2024001',
'issueDate' => '20240115',
'localCurrencyCode' => 'EUR',
'supplierParty' => [
'partyName' => 'Supplier s.r.o.',
'postalAddress' => [
'streetName' => 'Hlavna 1',
'cityName' => 'Bratislava',
'postalZone' => '81101',
'country' => 'SVK'
]
],
'customerParty' => [
'partyName' => 'Customer a.s.'
],
'numberOfInvoiceLines' => 1,
'taxCategorySummaries' => [[
'classifiedTaxCategory' => 0.2,
'taxExclusiveAmount' => 100,
'taxAmount' => 20
]],
'monetarySummary' => [
'taxExclusiveAmount' => 100,
'taxInclusiveAmount' => 120
]
];
$payJson = json_encode($paymentData);
$invoiceJson = json_encode($invoiceData);
// Helper function for PAY encode + error handling
$encodePay = function($config) use ($ffi, $payJson) {
$result = $ffi->bysquare_pay_encode($payJson, $config);
$qr = FFI::string($result);
$ffi->bysquare_free($result);
if (str_starts_with($qr, 'ERROR:')) {
die("PAY encoding error: " . substr($qr, 6) . "\n");
}
return $qr;
};
// Helper function for invoice encode + error handling
$encodeInvoice = function($config) use ($ffi, $invoiceJson) {
$result = $ffi->bysquare_invoice_encode($invoiceJson, $config);
$qr = FFI::string($result);
$ffi->bysquare_free($result);
if (str_starts_with($qr, 'ERROR:')) {
die("Invoice encoding error: " . substr($qr, 6) . "\n");
}
return $qr;
};
// PAY: Default config (v1.2.0 + deburr + validate)
$qrDefault = $encodePay(BYSQUARE_CONFIG_DEFAULT);
echo "PAY default config: {$qrDefault}\n";
// PAY: Custom config - version 1.1.0 with deburr only
$qrCustom = $encodePay(BYSQUARE_DEBURR | BYSQUARE_VERSION_110);
echo "PAY custom config: {$qrCustom}\n";
// PAY: Decode
$result = $ffi->bysquare_pay_decode($qrDefault);
$decodedJson = FFI::string($result);
$ffi->bysquare_free($result);
if (str_starts_with($decodedJson, 'ERROR:')) {
die("PAY decoding error: " . substr($decodedJson, 6) . "\n");
}
echo "PAY decoded: {$decodedJson}\n";
// Invoice: Encode with defaults (v1.0.0 + validate)
$qrInvoice = $encodeInvoice(BYSQUARE_CONFIG_DEFAULT);
echo "Invoice: {$qrInvoice}\n";
// Invoice: Decode
$result = $ffi->bysquare_invoice_decode($qrInvoice);
$decodedInvoice = FFI::string($result);
$ffi->bysquare_free($result);
if (str_starts_with($decodedInvoice, 'ERROR:')) {
die("Invoice decoding error: " . substr($decodedInvoice, 6) . "\n");
}
echo "Invoice decoded: {$decodedInvoice}\n";
// Detect type (0=PAY, 1=Invoice, -1=error)
$payType = $ffi->bysquare_detect_type($qrDefault);
$invoiceType = $ffi->bysquare_detect_type($qrInvoice);
echo "QR type (PAY): {$payType}\n";
echo "QR type (Invoice): {$invoiceType}\n";