Skip to content

Commit d86f915

Browse files
Add support for Reseller.createClientAccount and demo
- Update example read me - Expand validation example
1 parent c9adefa commit d86f915

File tree

4 files changed

+189
-16
lines changed

4 files changed

+189
-16
lines changed

example/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ PHP Api proxy for the [Postcode.nl Api](https://developer.postcode.eu/documentat
77
Usage
88
=============
99

10-
This directory serves as an extremely simple proxy example for the supplied client.
10+
This directory contains extremely simple examples for the supplied client.
1111
Set the correct `API_KEY` and `API_SECRET` and optional `PLATFORM` and you can call
1212
any supported method, for example:
1313

14+
## Autocomplete API example
15+
To run the autocomplete example `autocomplete.html` set the correct credentials in `example/index.php`.
16+
1417
### Show all valid data from a valid postal code
1518
`example/index.php?p=dutchAddressByPostcode/2012es/30`
1619

@@ -32,6 +35,12 @@ What you should add:
3235
* Configure your webserver to send all requests under a certain directory to your proxy
3336
so you don't need to use `?p=`
3437

38+
## Validate API example
39+
Set the correct credentials in `example/validate.php` and open the file in your browser.
40+
41+
## Reseller createClientAccount API example
42+
Set the correct credentials in `example/createClientAccount.php` and open the file in your browser.
43+
3544
License
3645
=============
3746

example/createClientAccount.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
define('API_KEY', '** insert key here **');
3+
define('API_SECRET', '** insert secret here **');
4+
define('PLATFORM', 'example proxy');
5+
6+
7+
// this will load exceptions when they occur
8+
spl_autoload_register(function($class){
9+
if (0 === strpos($class, 'PostcodeNl\\'))
10+
require(__DIR__ .'/../src/'. str_replace('\\', '/', $class) .'.php');
11+
});
12+
$result = null;
13+
if (isset($_GET['createResellerAccount']))
14+
{
15+
$client = new PostcodeNl\Api\Client(API_KEY, API_SECRET, PLATFORM);
16+
$result = $client->createClientAccount(
17+
$_GET['companyName'] ?? '',
18+
$_GET['countryIso'] ?? '',
19+
$_GET['vatNumber'] ?? '',
20+
$_GET['contactEmail'] ?? '',
21+
$_GET['subscriptionAmount'] ?? 1,
22+
explode("\n", $_GET['siteUrls'] ?? ''),
23+
$_GET['invoiceEmail'] ?? '',
24+
$_GET['invoiceReference'] ?? '',
25+
$_GET['invoiceAddressLine1'] ?? '',
26+
$_GET['invoiceAddressLine2'] ?? '',
27+
$_GET['invoiceAddressPostalCode'] ?? '',
28+
$_GET['invoiceAddressLocality'] ?? '',
29+
$_GET['invoiceAddressRegion'] ?? '',
30+
$_GET['invoiceAddressCountryIso'] ?? '',
31+
$_GET['invoiceContactName'] ?? null,
32+
true
33+
);
34+
}
35+
36+
?>
37+
<!DOCTYPE html>
38+
<html lang="nl-NL">
39+
<head>
40+
<title>Reseller.createClientAccount example</title>
41+
<style>
42+
input:not([type="submit"]), textarea {
43+
width: 400px;
44+
margin-left: 10px;
45+
}
46+
47+
label {
48+
display: grid;
49+
grid-template-columns: 200px 1fr;
50+
margin-bottom: 15px;
51+
}
52+
53+
.create-client-account-result {
54+
padding: 1em;
55+
}
56+
</style>
57+
</head>
58+
<body>
59+
<h3>Reseller.createClientAccount</h3>
60+
<p>For more information see README.md</p>
61+
<form action="createClientAccount.php" method="GET">
62+
<label>Company Name <input type="text" name="companyName" placeholder="Company name" value="<?php print($_GET['companyName'] ?? ''); ?>"></label><br>
63+
<label>Country ISO <input type="text" name="countryIso" placeholder="Country ISO" value="<?php print($_GET['countryIso'] ?? ''); ?>"></label><br>
64+
<label>VAT Number <input type="text" name="vatNumber" placeholder="VAT Number" value="<?php print($_GET['vatNumber'] ?? ''); ?>"></label><br>
65+
<label>Contact Email <input type="email" name="contactEmail" placeholder="Contact Email" value="<?php print($_GET['contactEmail'] ?? ''); ?>"></label><br>
66+
<label>Subscription Amount <input type="number" name="subscriptionAmount" placeholder="Subscription Amount" value="<?php print($_GET['subscriptionAmount'] ?? ''); ?>"></label><br>
67+
<label>Site Urls <textarea name="siteUrls" placeholder="Site URLs, 1 per line"><?php print($_GET['siteUrls'] ?? ''); ?></textarea></label><br>
68+
<label>Invoice Email" <input type="email" name="invoiceEmail" placeholder="Invoice Email" value="<?php print($_GET['invoiceEmail'] ?? ''); ?>"></label><br>
69+
<label>Invoice Reference <input type="text" name="invoiceReference" placeholder="Invoice Reference" value="<?php print($_GET['invoiceReference'] ?? ''); ?>"></label><br>
70+
<label>Invoice Address Line 1 <input type="text" name="invoiceAddressLine1" placeholder="Invoice Address Line 1" value="<?php print($_GET['invoiceAddressLine1'] ?? ''); ?>"></label><br>
71+
<label>Invoice Address Line 2 <input type="text" name="invoiceAddressLine2" placeholder="Invoice Address Line 2" value="<?php print($_GET['invoiceAddressLine2'] ?? ''); ?>"></label><br>
72+
<label>Invoice Address Postal Code <input type="text" name="invoiceAddressPostalCode" placeholder="Invoice Address Postal Code" value="<?php print($_GET['invoiceAddressPostalCode'] ?? ''); ?>"></label><br>
73+
<label>Invoice Address Locality <input type="text" name="invoiceAddressLocality" placeholder="Invoice Address Locality" value="<?php print($_GET['invoiceAddressLocality'] ?? ''); ?>"></label><br>
74+
<label>Invoice Address Region <input type="text" name="invoiceAddressRegion" placeholder="Invoice Address Region" value="<?php print($_GET['invoiceAddressRegion'] ?? ''); ?>"></label><br>
75+
<label>Invoice Contact Name <input type="text" name="invoiceContactName" placeholder="Invoice Contact Name" value="<?php print($_GET['invoiceContactName'] ?? ''); ?>"></label><br>
76+
77+
<input type="submit" value="Create reseller account" name="createResellerAccount">
78+
</form>
79+
80+
<pre class="create-client-account-result"><?php print($result === null ? '' : var_export($result, true)); ?></pre>
81+
</body>

example/validate.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,38 @@
1313
if (isset($_GET['validate']))
1414
{
1515
$client = new PostcodeNl\Api\Client(API_KEY, API_SECRET, PLATFORM);
16-
$result = $client->validate(
17-
$_GET['country'],
18-
$_GET['postcode'],
19-
$_GET['locality'],
20-
$_GET['street'],
21-
$_GET['building'],
22-
$_GET['region'],
23-
$_GET['streetAndBuilding']
24-
);
16+
$country = $_GET['country'];
17+
// If it is 3 characters it is probably already an iso3 code
18+
if (strlen($country) === 3)
19+
{
20+
$countryIso = strtolower($country);
21+
}
22+
else
23+
{
24+
// Request an iso3 code for the country
25+
$result = $client->getCountry($country);
26+
$countryIso = strtolower($result['iso3']);
27+
}
28+
29+
if (isset($countryIso))
30+
{
31+
$result = $client->validate(
32+
$countryIso,
33+
$_GET['postcode'],
34+
$_GET['locality'],
35+
$_GET['street'],
36+
$_GET['building'],
37+
$_GET['region'],
38+
$_GET['streetAndBuilding']
39+
);
40+
}
2541
}
2642

2743
?>
2844
<!DOCTYPE html>
2945
<html lang="nl-NL">
3046
<head>
47+
<title>Validate example</title>
3148
<style>
3249
input[type=text]{
3350
width: 500px;

src/PostcodeNl/Api/Client.php

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public function __construct(string $key, string $secret, string $platform)
5555
}
5656

5757
$this->_curlHandle = curl_init();
58-
curl_setopt($this->_curlHandle, CURLOPT_CUSTOMREQUEST, 'GET');
5958
curl_setopt($this->_curlHandle, CURLOPT_RETURNTRANSFER, true);
6059
curl_setopt($this->_curlHandle, CURLOPT_CONNECTTIMEOUT, 2);
6160
curl_setopt($this->_curlHandle, CURLOPT_TIMEOUT, 5);
@@ -275,6 +274,57 @@ public function getCountry(string $country): array
275274
return $this->_performApiCall(implode('/', $urlParts), null);
276275
}
277276

277+
/**
278+
* @see https://developer.postcode.eu/documentation/reseller/v1/Reseller/createClientAccount
279+
*/
280+
public function createClientAccount(
281+
string $companyName,
282+
string $countryIso,
283+
string $vatNumber,
284+
string $contactEmail,
285+
int $subscriptionAmount,
286+
array $siteUrls,
287+
string $invoiceEmail,
288+
string $invoiceReference,
289+
string $invoiceAddressLine1,
290+
string $invoiceAddressLine2,
291+
string $invoiceAddressPostalCode,
292+
string $invoiceAddressLocality,
293+
string $invoiceAddressRegion,
294+
string $invoiceAddressCountryIso,
295+
?string $invoiceContactName = null,
296+
bool $isTest = false
297+
): array
298+
{
299+
$postData = [
300+
'companyName' => $companyName,
301+
'countryIso' => $countryIso,
302+
'vatNumber' => $vatNumber,
303+
'contactEmail' => $contactEmail,
304+
'subscriptionAmount' => $subscriptionAmount,
305+
'siteUrls' => $siteUrls,
306+
'invoiceEmail' => $invoiceEmail,
307+
'invoiceReference' => $invoiceReference,
308+
'invoiceAddressLine1' => $invoiceAddressLine1,
309+
'invoiceAddressLine2' => $invoiceAddressLine2,
310+
'invoiceAddressPostalCode' => $invoiceAddressPostalCode,
311+
'invoiceAddressLocality' => $invoiceAddressLocality,
312+
'invoiceAddressRegion' => $invoiceAddressRegion,
313+
'invoiceAddressCountryIso' => $invoiceAddressCountryIso,
314+
];
315+
316+
if ($invoiceContactName !== null)
317+
{
318+
$postData['invoiceContactName'] = $invoiceContactName;
319+
}
320+
if ($isTest)
321+
{
322+
$postData['isTest'] = true;
323+
}
324+
325+
return $this->_performPostApiCall('reseller/v1/client', $postData);
326+
}
327+
278328
/**
279329
* @see https://developer.postcode.eu/documentation/account/v1/Account/getInfo
280330
*/
@@ -325,15 +375,31 @@ protected function _validateSessionHeader(string $session): void
325375
protected function _performApiCall(string $path, ?string $session): array
326376
{
327377
$url = static::SERVER_URL . $path;
328-
curl_setopt($this->_curlHandle, CURLOPT_URL, $url);
329-
curl_setopt($this->_curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
330-
curl_setopt($this->_curlHandle, CURLOPT_USERPWD, $this->_key .':'. $this->_secret);
331378
if ($session !== null)
332379
{
333380
curl_setopt($this->_curlHandle, CURLOPT_HTTPHEADER, [
334381
static::SESSION_HEADER_KEY . ': ' . $session,
335382
]);
336383
}
384+
curl_setopt($this->_curlHandle, CURLOPT_HTTPGET, true);
385+
386+
return $this->_performCurlCall($url);
387+
}
388+
389+
protected function _performPostApiCall(string $path, array $postData = []): array
390+
{
391+
$url = static::SERVER_URL . $path;
392+
curl_setopt($this->_curlHandle, CURLOPT_POST, true);
393+
curl_setopt($this->_curlHandle, CURLOPT_POSTFIELDS, http_build_query($postData));
394+
395+
return $this->_performCurlCall($url);
396+
}
397+
398+
protected function _performCurlCall(string $url): array
399+
{
400+
curl_setopt($this->_curlHandle, CURLOPT_URL, $url);
401+
curl_setopt($this->_curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
402+
curl_setopt($this->_curlHandle, CURLOPT_USERPWD, $this->_key .':'. $this->_secret);
337403

338404
$this->_mostRecentResponseHeaders = [];
339405
$response = curl_exec($this->_curlHandle);
@@ -362,9 +428,9 @@ protected function _performApiCall(string $path, ?string $session): array
362428
case 401:
363429
throw new AuthenticationException('Could not authenticate your request, please make sure your API credentials are correct.');
364430
case 403:
365-
throw new ForbiddenException('Your account currently has no access to the international API, make sure you have an active subscription.');
431+
throw new ForbiddenException(sprintf('API access not allowed: `%s`', $jsonResponse['exception']));
366432
case 404:
367-
throw new NotFoundException('The requested address could not be found.');
433+
throw new NotFoundException('The request was valid, but nothing could be found.');
368434
case 429:
369435
throw new TooManyRequestsException('Too many requests made, please slow down: ' . $response);
370436
case 503:

0 commit comments

Comments
 (0)