-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhooks.php
205 lines (176 loc) · 7.57 KB
/
hooks.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
204
205
<?php /** @noinspection PhpInconsistentReturnPointsInspection */
if(!defined('WHMCS'))
die('This file cannot be accessed directly');
use Illuminate\Database\Capsule\Manager as Capsule;
require_once __DIR__ . '/vendor/autoload.php';
/**
* This hook will validate the additional fields that are required for each registrant type by Ficora
*
* An error will be returned if the necessary fields are not filled by the user
*/
add_hook('ShoppingCartValidateDomainsConfig', 50, function ($vars)
{
global $smarty;
$errors = [];
foreach($_SESSION['cart']['domains'] as $key => $domain) {
if(isset($vars['domainfield'][$key]['registrant_type']) &&
(substr($domain['domain'], -strlen('.fi')) === '.fi')) {
switch($vars['domainfield'][$key]['registrant_type']) {
case '0':
if(!trim($vars['domainfield'][$key]['idNumber'])) {
$errors[] = 'ID number is required for Finnish residents';
} elseif(!Validate_FI::pin(trim($vars['domainfield'][$key]['idNumber']))) {
$errors[] = 'Given ID number is invalid';
}
break;
case '10':
if(!$vars['domainfield'][$key]['birthdate']) {
$errors[] = 'Birth date is required for foreign private persons';
}
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
if(!trim($vars['domainfield'][$key]['registerNumber'])) {
$errors[] = 'VAT/Register number is a required field for corporate bodies';
}
break;
default:
$errors[] = 'Registrant type is missing';
break;
}
}
}
return $errors;
});
/**
* This hook adds additional fields to transfer orders when the domains are being configured
*
* By default WHMCS does not provide additional fields for Transfer order type, but Ficora requires the additional info
*/
add_hook('ClientAreaPage', 1, function ($vars)
{
if($vars['templatefile'] !== 'configuredomains')
return;
if(!array_key_exists('domains', $vars))
return;
foreach($vars['domains'] as $key => $domain) {
if(substr($domain['domain'], -strlen('.fi')) !== '.fi')
continue;
if($domain['fields'])
continue;
$additflds = new WHMCS\Domains\AdditionalFields();
$additflds->setTLD('fi');
$vars['domains'][$key]['fields'] = $additflds->getFieldsForOutput($key);
$vars['domains'][$key]['configtoshow'] = true;
}
return $vars;
});
/**
* This hook will save additional fields filled on a Transfer order domain configuration screen to $_SESSION so the
* field data can be used at hook CartTotalAdjustment to save it to the database
*
* By default WHMCS does not provide additional fields for Transfer order type, but Ficora requires the additional info
*/
add_hook('ShoppingCartValidateDomainsConfig', 1, function ($vars)
{
if(!array_key_exists('domainfield', $_POST))
return;
foreach ($_SESSION['cart']['domains'] as $key => $domain) {
if(substr($domain['domain'], -strlen('.fi')) !== '.fi')
continue;
if($domain['fields'])
continue;
if($domain['type'] !== 'transfer')
continue;
if(!array_key_exists($key, $_POST['domainfield']))
continue;
$_SESSION['cart']['domains'][$key]['fields'] = $_POST['domainfield'][$key];
}
});
/**
* This hook will save additional fields from $_SESSION to the database when the order type is Transfer
*
* By default WHMCS does not provide additional fields for Transfer order type, but Ficora requires the additional info
*/
add_hook('CartTotalAdjustment', 1, function ()
{
foreach ($_SESSION['cart']['domains'] as $key => $domain) {
if(substr($domain['domain'], -strlen('.fi')) !== '.fi')
continue;
if($domain['type'] !== 'transfer')
continue;
if(!$domain['fields'])
continue;
$id = Capsule::table('tbldomains')
->where('domain', $domain['domain'])
->value('id');
if(!$id)
continue;
$fields = new WHMCS\Domains\AdditionalFields();
$fields->setTLD('fi');
$fields->setFieldValues($domain['fields']);
$fields->saveToDatabase($id);
}
});
/**
* This hook will hide the unnecessary additional fields for different registrant types
*
* For example a private person does not need to fill in a company VAT ID. This way the order process is much more clear
* and simple for customers
*/
add_hook('ClientAreaHeadOutput', 50, function($vars) {
if($vars['templatefile'] !== 'configuredomains')
return;
foreach($_SESSION['cart']['domains'] as $key => $domain) {
if(substr($domain['domain'], -strlen('.fi')) !== '.fi')
continue;
$domains[] = $key;
}
ob_start();
?>
<script type="text/javascript">
$(document).ready(function() {
<?php foreach($domains as $id): ?>
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][1]"]').closest('.row').show();
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][2]"]').closest('.row').hide();
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][3]"]').closest('.row').hide();
$('#frmConfigureDomains select[name="domainfield[<?= $id ?>][0]"]').change(function() {
if(this.value === '10') {
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][1]"]').closest('.row').hide();
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][2]"]').closest('.row').hide();
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][3]"]').closest('.row').show();
} else if(this.value === '0') {
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][1]"]').closest('.row').show();
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][2]"]').closest('.row').hide();
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][3]"]').closest('.row').hide();
} else {
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][1]"]').closest('.row').hide();
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][2]"]').closest('.row').show();
$('#frmConfigureDomains input[name="domainfield[<?= $id ?>][3]"]').closest('.row').hide();
}
});
<?php endforeach; ?>
});
</script>
<?php
return ob_get_clean();
});
/**
* Ficora transfers execute immediately and they either fail or succeed. This is why default WHMCS Transfer Sync
* functionality does not suffice.
*
* Instead on successful transfer, FicoraEPP For WHMCS will automatically update the domain from Pending Transfer to
* state Active
*/
add_hook('AfterRegistrarTransfer', 50, function($vars) {
if(@$vars['params']['registrar'] !== 'ficoraepp') {
return;
}
/** @noinspection UnusedFunctionResultInspection */
RegCallFunction($vars['params'], 'CompleteTransfer');
});