forked from DaniWinter/bunq-request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbunq.php
203 lines (180 loc) · 8.32 KB
/
bunq.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/*
* bunq Requests Helper
* An easy way to create requests.
* Because the bunq API is a mess, this code can get you started without needing to guess how things work.
* Coded by Qarizma
* https://www.github.com/qarizma
*/
use bunq\Context\ApiContext;
use bunq\Http\ApiClient;
use bunq\Model\Generated\Endpoint\AttachmentPublic;
use bunq\Model\Generated\Endpoint\AttachmentTab;
use bunq\Model\Generated\Endpoint\Avatar;
use bunq\Model\Generated\Endpoint\CashRegister;
use bunq\Model\Generated\Endpoint\MonetaryAccount;
use bunq\Model\Generated\Endpoint\TabItemShop;
use bunq\Model\Generated\Endpoint\TabUsageSingle;
use bunq\Model\Generated\Object\Amount;
use bunq\Model\Generated\Object\Geolocation;
use bunq\Model\Generated\Object\TabVisibility;
use bunq\Util\BunqEnumApiEnvironmentType;
use bunq\Model\Generated\Endpoint\User;
use bunq\Model\Generated\Endpoint\UserCompany;
use bunq\Model\Generated\Endpoint\BunqMeTab;
use bunq\Model\Generated\Endpoint\BunqMeTabEntry;
require_once __DIR__ . '/vendor/autoload.php';
/*
* Creates a PRODUCTION bunq context which is required for API interaction.
* This should be executed once, then bunq.conf will be created for future use.
*/
function bunq_CreateContext($key, $device, $ips = []) {
$apiContext = ApiContext::create(BunqEnumApiEnvironmentType::PRODUCTION(), $key, $device);
$apiContext->save(ApiContext::FILENAME_CONFIG_DEFAULT);
}
/*
* Creates a SANDBOX bunq context which is required for API interaction.
* This should be executed once, then bunq.conf will be created for future use.
*/
function bunq_CreateContextSandbox($key, $device, $ips = []) {
$apiContext = ApiContext::create(BunqEnumApiEnvironmentType::SANDBOX(), $key, $device);
$apiContext->save(ApiContext::FILENAME_CONFIG_DEFAULT);
}
/*
* Creates a bunq payment request which can be shared with anyone.
* Requires the amount of payment, description and redirect url.
* Optionally you can pass user id and account id.
* Returns the array as seen below, including the raw object from the API.
*/
function bunq_CreateRequest($amount, $description, $redirect, $user = 0, $account = 0) {
$apiContext = ApiContext::restore(ApiContext::FILENAME_CONFIG_DEFAULT);
$users = User::listing($apiContext)->getValue();
//$user = $users[$user]->getUserPerson();
$user = $users[$user]->getUserCompany();
$userId = $user->getId();
$monetaryAccounts = MonetaryAccount::listing($apiContext, $userId)->getValue();
$monetaryAccount = $monetaryAccounts[$account]->getMonetaryAccountBank();
$monetaryAccountId = $monetaryAccount->getId();
$requestMap = [
BunqMeTab::FIELD_BUNQME_TAB_ENTRY => [
BunqMeTabEntry::FIELD_AMOUNT_INQUIRED => new Amount($amount, 'EUR'),
BunqMeTabEntry::FIELD_REDIRECT_URL => $redirect,
BunqMeTabEntry::FIELD_DESCRIPTION => $description
]
];
$createBunqMeTab = BunqMeTab::create($apiContext, $requestMap, $userId, $monetaryAccountId)->getValue();
$bunqMeRequest = BunqMeTab::get($apiContext, $userId, $monetaryAccountId, $createBunqMeTab)->getValue();
$r["id"] = $bunqMeRequest->getId();
$r["uuid"] = $bunqMeRequest->getBunqmeTabEntry()->getUuid();
$r["amount"] = $bunqMeRequest->getBunqmeTabEntry()->getAmountInquired()->getValue();
$r["paymentlink"] = $bunqMeRequest->getBunqmeTabShareUrl();
$r["status"] = $bunqMeRequest->getBunqmeTabEntry()->getStatus();
$r["description"] = $bunqMeRequest->getBunqmeTabEntry()->getDescription();
$r["redirecturl"] = $bunqMeRequest->getBunqmeTabEntry()->getRedirectUrl();
$r["raw"] = $bunqMeRequest;
return $r;
}
/*
* Gets the status of the payment request, so you can check if it's paid or nah.
* Requires the payment ID (not UUID), and optionally user id with account id.
* Returns the array as seen below, including the raw object from the API.
* TODO: Implement resultInquiries[] to get actual information about the payer.
*/
function bunq_StatusRequest($id, $user = 0, $account = 0) {
$apiContext = ApiContext::restore(ApiContext::FILENAME_CONFIG_DEFAULT);
$users = User::listing($apiContext)->getValue();
$user = $users[$user]->getUserCompany();
$userId = $user->getId();
$monetaryAccounts = MonetaryAccount::listing($apiContext, $userId)->getValue();
$monetaryAccount = $monetaryAccounts[$account]->getMonetaryAccountBank();
$monetaryAccountId = $monetaryAccount->getId();
$bunqMeRequest = BunqMeTab::get($apiContext, $userId, $monetaryAccountId, $id)->getValue();
$r["id"] = $bunqMeRequest->getId();
$r["uuid"] = $bunqMeRequest->getBunqmeTabEntry()->getUuid();
$r["amount"] = $bunqMeRequest->getBunqmeTabEntry()->getAmountInquired()->getValue();
$r["paymentlink"] = $bunqMeRequest->getBunqmeTabShareUrl();
$r["status"] = $bunqMeRequest->getBunqmeTabEntry()->getStatus();
$r["description"] = $bunqMeRequest->getBunqmeTabEntry()->getDescription();
$r["redirecturl"] = $bunqMeRequest->getBunqmeTabEntry()->getRedirectUrl();
// We still need to add the contents of resultInquiries[], but I can't test it so I didn't implement it.
$r["raw"] = $bunqMeRequest;
return $r;
}
/*
* EXAMPLE CODE
*/
// Run the following line just once:
//bunq_CreateContextSandbox("API-KEY-HERE", "My server name");
$create = bunq_CreateRequest("10", "My Payment", 0, 0, "http://bunq.com/");
echo "<pre>";
print_r($create);
echo "</pre>";
echo "<hr>";
$status = bunq_StatusRequest($create["id"]);
echo "<pre>";
print_r($status);
echo "</pre>";
/*
* BETA CODE
* WARNING THE FOLLOWING CODE HAS NOT BEEN TESTED OR OPTIMISED!!!
* I don't have bunq Premium to test this out, and the sandbox mode of bunq isn't really functional (it doesn't return real data).
* These functions have been ported from Javascript to work with PHP.
*/
/*
// generateId() generates a unique ID used for API calls.
// Ported from Javascript @ https://github.com/basst85/bunq_pay
function generateId() {
return bin2hex(random_bytes(8))."-".bin2hex(random_bytes(4))."-".bin2hex(random_bytes(4))."-".bin2hex(random_bytes(4))."-".bin2hex(random_bytes(12));
}
// getBanks() function to get all current banks from bunq for iDeal payment.
// Ported from Javascript @ https://github.com/basst85/bunq_pay
function getBanks() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.bunq.me/v1/bunqme-merchant-directory-ideal");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = ['X-Bunq-Client-Request-Id: '.generateId()];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = json_decode(curl_exec($ch), true);
curl_close ($ch);
return $server_output['Response'][0]['IdealDirectory']['country'][0]['issuer'];
}
// print_r(getBanks()); // prints all banks
// getiDeal() function should return the direct iDeal link based on the bank.
// Ported from Javascript @ https://github.com/basst85/bunq_pay
function getiDeal($uuid) {
$json = '{"amount_requested":{"currency":"EUR","value":"10"}, "bunqme_type":"TAB", "issuer":"RABONL2U","merchant_type":"IDEAL","bunqme_uuid":"'.$uuid.'"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.bunq.me/v1/bunqme-merchant-request");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = ['X-Bunq-Client-Request-Id: '.generateId()];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = json_decode(curl_exec($ch), true);
curl_close ($ch);
return $server_output;
}
//print_r(getiDeal($getUuid)); // prints the results from the ideal request
// getQR() function should echo a QR image for bunq users
// Ported from Javascript @ https://github.com/basst85/bunq_pay
function getQR($uuid) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.bunq.me/v1/bunqme-tab-entry/".$uuid."/qr-code-content");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, []);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = ['X-Bunq-Client-Request-Id: '.generateId()];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = json_decode(curl_exec($ch), true);
curl_close ($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.bunq.me/v1/bunqme-tab-entry/".$uuid."/qr-code-content/".$server_output['Response'][0]['Uuid']['uuid']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = ['X-Bunq-Client-Request-Id: '.generateId()];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = json_decode(curl_exec($ch), true);
curl_close ($ch);
echo "<img src='data:image/png;base64,".$server_output['Response'][0]['QrCodeImage']['base64']."'>";
}
//echo getQR($getUuid); // should display an image with the QR code
*/