Repository navigation
Expand file tree
/
Copy pathcustomer-onboarding.php
More file actions
165 lines (135 loc) · 5.23 KB
/
Copy pathcustomer-onboarding.php
File metadata and controls
165 lines (135 loc) · 5.23 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
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
<?php declare(strict_types=1);
/**
* Complete customer onboarding flow.
*
* This example walks through a realistic scenario: creating a new customer,
* setting up a hosting service, registering a domain, creating an invoice,
* and sending it by email.
*
* See: https://www.hostfact.nl/developer/api/
*/
use Hyperized\Hostfact\Api\Controllers\Debtor;
use Hyperized\Hostfact\Api\Controllers\Domain;
use Hyperized\Hostfact\Api\Controllers\Hosting;
use Hyperized\Hostfact\Api\Controllers\Invoice;
use Hyperized\Hostfact\Api\Entity\Debtor as DebtorEntity;
use Hyperized\Hostfact\Api\Entity\Hosting as HostingEntity;
use Hyperized\Hostfact\Api\Entity\Invoice as InvoiceEntity;
use Hyperized\Hostfact\Api\Response\ErrorResponse;
use Hyperized\Hostfact\Api\Response\ShowResponse;
use Hyperized\Hostfact\Exceptions\InvalidArgumentException;
// ---------------------------------------------------------------------------
// 1. Create the customer (debtor)
// ---------------------------------------------------------------------------
$debtorResponse = Debtor::new()->add([
'CompanyName' => 'Webshop BV',
'Sex' => 'm',
'Initials' => 'J.',
'SurName' => 'de Vries',
'EmailAddress' => 'jan@webshop-bv.test',
'Street' => 'Keizersgracht',
'HouseNumber' => '100',
'ZipCode' => '1015AA',
'City' => 'Amsterdam',
'Country' => 'NL',
'TaxNumber' => 'NL123456789B01',
]);
if ($debtorResponse instanceof ErrorResponse) {
echo 'Failed to create customer: ' . implode(', ', $debtorResponse->errors);
exit(1);
}
assert($debtorResponse instanceof ShowResponse);
$debtor = $debtorResponse->entity;
assert($debtor instanceof DebtorEntity);
$debtorCode = $debtor->DebtorCode;
echo "Customer created: {$debtorCode}\n";
// ---------------------------------------------------------------------------
// 2. Register a domain for the customer
// ---------------------------------------------------------------------------
// First check if the domain is available
$checkResponse = Domain::new()->check([
'Domain' => 'webshop-bv.nl',
]);
$domainResponse = Domain::new()->register([
'Domain' => 'webshop-bv.nl',
'DebtorCode' => $debtorCode,
]);
if ($domainResponse instanceof ErrorResponse) {
echo 'Domain registration failed: ' . implode(', ', $domainResponse->errors);
// Continue anyway - the hosting setup doesn't depend on the domain
}
// ---------------------------------------------------------------------------
// 3. Set up hosting for the customer
// ---------------------------------------------------------------------------
$hostingResponse = Hosting::new()->add([
'DebtorCode' => $debtorCode,
'HostingName' => 'webshop-bv.nl',
'ProductCode' => 'P0001',
]);
if ($hostingResponse instanceof ErrorResponse) {
echo 'Failed to create hosting: ' . implode(', ', $hostingResponse->errors);
exit(1);
}
assert($hostingResponse instanceof ShowResponse);
$hosting = $hostingResponse->entity;
assert($hosting instanceof HostingEntity);
$hostingCode = $hosting->bag->string('HostingCode');
echo "Hosting created: {$hostingCode}\n";
// Provision the account on the server
$createResponse = Hosting::new()->create([
'HostingCode' => $hostingCode,
]);
if ($createResponse->isError()) {
echo "Warning: could not provision hosting on server\n";
}
// Send the login credentials to the customer
Hosting::new()->emailAccountData([
'HostingCode' => $hostingCode,
]);
// ---------------------------------------------------------------------------
// 4. Create an invoice for the setup
// ---------------------------------------------------------------------------
$invoiceResponse = Invoice::new()->add([
'DebtorCode' => $debtorCode,
]);
if ($invoiceResponse instanceof ErrorResponse) {
echo 'Failed to create invoice: ' . implode(', ', $invoiceResponse->errors);
exit(1);
}
assert($invoiceResponse instanceof ShowResponse);
$invoice = $invoiceResponse->entity;
assert($invoice instanceof InvoiceEntity);
$invoiceCode = $invoice->InvoiceCode;
// Add the hosting line
Invoice::new()->lineAdd([
'InvoiceCode' => $invoiceCode,
'Description' => 'Webhosting - webshop-bv.nl (1 year)',
'PriceExcl' => '99.00',
'TaxPercentage' => '21',
]);
// Add the domain registration line
Invoice::new()->lineAdd([
'InvoiceCode' => $invoiceCode,
'Description' => 'Domain registration - webshop-bv.nl (1 year)',
'PriceExcl' => '9.95',
'TaxPercentage' => '21',
]);
echo "Invoice created: {$invoiceCode}\n";
// ---------------------------------------------------------------------------
// 5. Send the invoice to the customer
// ---------------------------------------------------------------------------
$sendResponse = Invoice::new()->sendByEmail([
'InvoiceCode' => $invoiceCode,
]);
if ($sendResponse->isSuccess()) {
echo "Invoice {$invoiceCode} sent to jan@webshop-bv.test\n";
}
// ---------------------------------------------------------------------------
// 6. Later: mark the invoice as paid when payment arrives
// ---------------------------------------------------------------------------
$paidResponse = Invoice::new()->markAsPaid([
'InvoiceCode' => $invoiceCode,
]);
if ($paidResponse->isSuccess()) {
echo "Invoice {$invoiceCode} marked as paid\n";
}