This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
woocommerce-heidelpay.php
303 lines (261 loc) · 10.4 KB
/
woocommerce-heidelpay.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
use Heidelpay\MessageCodeMapper\MessageCodeMapper;
/**
* Plugin Name: heidelpay WooCommerce
* Plugin URI: https://dev.heidelpay.com
* Description: heidelpay payment integration for WooCommerce
* Version: 1.7.0
* Author: heidelpay
* Author URI: https://www.heidelpay.com
* Developer: heidelpay
* Developer URI: https://dev.heidelpay.com
* Text Domain: woocommerce-heidelpay
* Domain Path: /languages
*
* WC requires at least: 3.0
* WC tested up to: 4.3
*
* Copyright: © 2018-present heidelpay GmbH
* License: see LICENSE.txt
*/
/**
* Check if WooCommerce is active
**/
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
/**
* Required minimums and constants
*/
define('WC_HEIDELPAY_VERSION', '1.7.0');
define('WC_HEIDELPAY_MIN_PHP_VER', '5.6.0');
define('WC_HEIDELPAY_MIN_WC_VER', '3.0.0');
define('WC_HEIDELPAY_MAIN_FILE', __FILE__);
define('WC_HEIDELPAY_PLUGIN_URL', untrailingslashit(plugins_url(basename(plugin_dir_path(__FILE__)), basename(__FILE__))));
define('WC_HEIDELPAY_PLUGIN_PATH', untrailingslashit(plugin_dir_path(__FILE__)));
if (!class_exists('WC_Heidelpay')) :
class WC_Heidelpay
{
/**
* @var Singleton The reference the *Singleton* instance of this class
*/
private static $instance;
/**
* @var Reference to logging class.
*/
private static $log;
/**
* Returns the *Singleton* instance of this class.
*
* @return Singleton The *Singleton* instance.
*/
public static function get_instance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Private clone method to prevent cloning of the instance of the
* *Singleton* instance.
*
* @return void
*/
private function __clone()
{
}
/**
* Private unserialize method to prevent unserializing of the *Singleton*
* instance.
*
* @return void
*/
public function __wakeup()
{
}
/**
* Notices (array)
* @var array
*/
public $notices = array();
/**
* Protected constructor to prevent creating a new instance of the
* *Singleton* via the `new` operator from outside of this class.
*/
protected function __construct()
{
add_action('admin_init', array($this, 'check_environment'));
add_action('admin_notices', array($this, 'admin_notices'), 15);
add_action('plugins_loaded', array($this, 'init'));
add_action('woocommerce_before_cart', array($this, 'errorNotice'));
}
public function errorNotice()
{
if (isset($_GET['errorCode'])) {
$mapper = new MessageCodeMapper(get_locale());
$message = $mapper->getMessage($_GET['errorCode']);
wc_print_notice($message, 'error');
}
}
/**
* Init the plugin after plugins_loaded so environment variables are set.
*/
public function init()
{
// Don't hook anything else in the plugin if we're in an incompatible environment
if (self::get_environment_warning()) {
return;
}
load_plugin_textdomain(
'woocommerce-heidelpay',
false,
plugin_basename(__DIR__) . '/languages'
);
require_once __DIR__ . '/includes/abstracts/abstract-wc-heidelpay-payment-gateway.php';
require_once __DIR__ . '/includes/gateways/class-wc-heidelpay-gateway-cc.php';
require_once __DIR__ . '/includes/gateways/class-wc-heidelpay-gateway-pp.php';
require_once __DIR__ . '/includes/gateways/class-wc-heidelpay-gateway-dc.php';
require_once __DIR__ . '/includes/gateways/class-wc-heidelpay-gateway-idl.php';
require_once __DIR__ . '/includes/gateways/class-wc-heidelpay-gateway-dd.php';
require_once __DIR__ . '/includes/gateways/class-wc-heidelpay-gateway-gp.php';
require_once __DIR__ . '/includes/gateways/class-wc-heidelpay-gateway-iv.php';
require_once __DIR__ . '/includes/gateways/class-wc-heidelpay-gateway-ivpg.php';
require_once __DIR__ . '/includes/gateways/class-wc-heidelpay-gateway-so.php';
require_once __DIR__ . '/includes/gateways/class-wc-heidelpay-gateway-va.php';
require_once __DIR__ . '/includes/class-wc-heidelpay-response.php';
require_once __DIR__ . '/includes/class-wc-heidelpay-push.php';
add_filter('woocommerce_payment_gateways', array($this, 'add_gateways'));
}
/**
* Add the gateways to WooCommerce.
* @param $methods
* @return array
*/
public function add_gateways($methods)
{
$methods[] = 'WC_Gateway_HP_CC';
$methods[] = 'WC_Gateway_HP_DC';
$methods[] = 'WC_Gateway_HP_IDL';
$methods[] = 'WC_Gateway_HP_DD';
$methods[] = 'WC_Gateway_HP_GP';
$methods[] = 'WC_Gateway_HP_IV';
$methods[] = 'WC_Gateway_HP_IVPG';
$methods[] = 'WC_Gateway_HP_SO';
$methods[] = 'WC_Gateway_HP_VA';
$methods[] = 'WC_Gateway_HP_PP';
return $methods;
}
/**
* The backup sanity check, in case the plugin is activated in a weird way,
* or the environment changes after activation. Also handles upgrade routines.
*/
public function check_environment()
{
if (!defined('IFRAME_REQUEST') && (WC_HEIDELPAY_VERSION !== get_option('wc_heidelpay_version'))) {
$this->install();
do_action('woocommerce_heidelpay_updated');
}
$environment_warning = self::get_environment_warning();
}
/**
* Allow this class and other classes to add slug keyed notices (to avoid duplication)
* @param $slug
* @param $class
* @param $message
*/
public function add_admin_notice($slug, $class, $message)
{
$this->notices[$slug] = array(
'class' => $class,
'message' => $message,
);
}
/**
* Updates the plugin version in db
*
* @since 1.0.0
* @version 1.2.0
* @return bool
*/
private static function _update_plugin_version()
{
delete_option('wc_heidelpay_version');
update_option('wc_heidelpay_version', WC_HEIDELPAY_VERSION);
return true;
}
/**
* Handles upgrade routines.
*
* @since 1.0.0
* @version 1.2.0
*/
public function install()
{
if (!defined('WC_HEIDELPAY_INSTALLING')) {
define('WC_HEIDELPAY_INSTALLING', true);
}
static::_update_plugin_version();
}
/**
* Checks the environment for compatibility problems. Returns a string with the first incompatibility
* found or false if the environment has no problems.
*/
public static function get_environment_warning()
{
if (version_compare(PHP_VERSION, WC_HEIDELPAY_MIN_PHP_VER, '<')) {
$message = __('minimal PHP version error', 'woocommerce-heidelpay');
return sprintf($message, WC_HEIDELPAY_MIN_PHP_VER, PHP_VERSION);
}
if (!defined('WC_VERSION')) {
return __('WooCommerce plugin not activated', 'woocommerce-heidelpay');
}
if (version_compare(WC_VERSION, WC_HEIDELPAY_MIN_WC_VER, '<')) {
$message = __('minimal WC version error', 'woocommerce-heidelpay');
return sprintf($message, WC_HEIDELPAY_MIN_WC_VER, WC_VERSION);
}
if (!function_exists('curl_init')) {
return __('cURL is not installed.', 'woocommerce-heidelpay');
}
return false;
}
/**
* Get setting link.
*
* @since 1.0.0
*
* @return string Setting link
*/
public function get_setting_link()
{
$use_id_as_section = function_exists('WC') ? version_compare(wc()->version, '2.6', '>=') : false;
$section_slug = $use_id_as_section ? 'heidelpay' : strtolower('WC_Heidelpay');
return admin_url('admin.php?page=wc-settings&tab=checkout§ion=' . $section_slug);
}
/**
* Display any notices
*/
public function admin_notices()
{
foreach ((array)$this->notices as $notice_key => $notice) {
echo "<div class='" . esc_attr($notice['class']) . "'><p>";
echo wp_kses($notice['message'], array('a' => array('href' => array())));
echo '</p></div>';
}
}
/**
* logging
* @param $message
*/
public static function log($message)
{
if (null === self::$log) {
self::$log = new WC_Logger();
}
self::$log->add('heidelpay-woocommerce', $message);
}
}
WC_Heidelpay::get_instance();
endif;
}