-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWC_Gateway_LHV_Hire_Purchase.php
executable file
·252 lines (214 loc) · 6.32 KB
/
WC_Gateway_LHV_Hire_Purchase.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
namespace Codelight\LHV;
if (!defined('WPINC')) {
die;
}
/**
* Class WC_Gateway_LHV_Hire_Purchase
* @package Codelight\LHV
*
* WooCommerce's own coding standards conflict with PSR-2,
* so we'll have to ignore some warnings.
*
* @codingStandardsIgnoreStart
* @SuppressWarnings(PHPMD.CamelCaseClassName)
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
* @SuppressWarnings(PHPMD.CamelCaseParameterName)
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
class WC_Gateway_LHV_Hire_Purchase extends \WC_Payment_Gateway
{
/* @var string */
public $id = 'lhv_hire_purchase';
/* @var string */
public $icon;
/* @var string */
public $method_title;
/* @var string */
public $method_description;
/* @var string */
public $title;
/* @var string */
public $description;
/* @var callable */
protected $process_payment_callback;
/* @var boolean */
protected $is_available;
/**
* Initialize fields, set up actions.
*/
public function __construct()
{
$this->method_title = apply_filters(
'lhv/hire-purchase/method_title',
__(
'LHV hire-purchase',
'lhv-hire-purchase'
)
);
$this->method_description = apply_filters(
'lhv/hire-purchase/method_description',
__(
'Payment with LHV hire-purchase takes only a few minutes. Quick response to your application, affordable interest, down-payment from 0 euros. Premature payment free of charge.',
'lhv-hire-purchase'
)
);
$this->icon = apply_filters(
'lhv/hire-purchase/method_icon',
GatewayManager::getInstance()->getAssetUrl('img/lhv-hire-purchase-badge.png')
);
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
}
/**
* Set the callback function for process_payment().
*
* @param callable $callback
*/
public function set_process_payment_callback($callback)
{
$this->process_payment_callback = $callback;
}
/**
* Return settings defined in WooCommerce admin.
*
* @return array
*/
public function get_settings()
{
return [
'enabled' => $this->get_option('enabled'),
'testmode' => $this->get_option('testmode'),
'testmode-admin' => $this->get_option('testmode-admin'),
'merchant_id' => $this->get_option('merchant_id'),
'private_key' => $this->get_option('private_key'),
'private_key_pass' => $this->get_option('private_key_pass'),
'public_key' => $this->get_option('public_key'),
];
}
/**
* Get required field keys.
*
* @return array
*/
public function get_required_fields()
{
return [
'enabled',
'testmode',
'testmode-admin',
'merchant_id',
'private_key',
'public_key',
];
}
/**
* Disable gateway
*/
public function set_disabled()
{
$this->enabled = 'no';
}
/**
* Check if the gateway is available in the frontend.
*
* @return bool
*/
public function is_available()
{
$available = parent::is_available();
if ('yes' === $this->get_option('testmode-admin') && !is_super_admin()) {
$available = false;
}
return apply_filters(
'lhv/hire-purchase/is_available',
$available
);
}
/**
* Check if the settings are filled in correctly.
*
* @return array
*/
public function validate_settings()
{
$settings = $this->get_settings();
$required = $this->get_required_fields();
$errors = [];
foreach ($settings as $key => $value) {
if (!in_array($key, $required)) {
continue;
}
if (empty($value)) {
$errors[] = $this->form_fields[$key]['title'];
}
}
if (empty($errors)) {
return [
'status' => 'success',
];
} else {
return [
'status' => 'error',
'fields' => $errors,
];
}
}
/**
* Handle the payment action
*
* @param int $order_id
* @return array
*/
public function process_payment($order_id)
{
return call_user_func(
apply_filters('lhv/hire-purchase/process_payment_callback', $this->process_payment_callback),
$order_id
);
}
/**
* Set up the form fields for WooCommerce admin.
*/
public function init_form_fields()
{
$defaultPublicKey = $this->get_default_public_key();
$this->form_fields = apply_filters('lhv/hire-purchase/form_fields', include('src/woocommerce-settings.php'));
}
/**
* Get the default public key from assets folder.
*
* @return string
*/
protected function get_default_public_key()
{
try {
$key = file_get_contents(GatewayManager::getInstance()->getAssetPath('lhv.pub'));
if (empty($key)) {
wc_get_logger()->error('Error opening default public key file!', 'lhv');
return '';
}
return $key;
} catch (\Exception $e) {
wc_get_logger()->error('Error opening default public key file: ' . $e->getMessage(), 'lhv');
return '';
}
}
/**
* Generate the HTML to be displayed in WooCommerce settings page
*
* @return string
*/
public function generate_lhv_hire_purchase_support_html()
{
$documentationUrl = GatewayManager::getInstance()->getDocumentationUrl();
$supportUrl = GatewayManager::getInstance()->getSupportUrl();
ob_start();
include('templates/admin-support.php');
return ob_get_clean();
}
}