-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-lyfePAY.php
465 lines (427 loc) · 12.9 KB
/
woocommerce-lyfePAY.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/**
* Plugin Name: lyfePAY
* Plugin URI: https://easymerchant.io/
* Description: Adds the lyfePAY gateway to your WooCommerce website.
* Version: 3.0.2
*
* Author: lyfePAY
* Author URI: https://easymerchant.io/
*
* Text Domain: woocommerce-easymerchant
* Domain Path: /i18n/languages/
*
* Requires at least: 4.2
* Tested up to: 4.9
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
if (!defined('LYFE_APP_NAME')) {
define('LYFE_APP_NAME', 'lyfePAYWooCommerce/3.0.2');
}
/**
* function to check if woocommerce is installed and activated
* @return string Returns error message if woocommerce not installed and activated
*/
function img_dependency_error_woo()
{
?>
<div class="notice notice-error is-dismissible">
<p>
<?php _e('lyfePAY requires Woocommerce plugin installed and activated!', 'woocommerce-lyfePAY'); ?>
</p>
</div>
<?php
}
/**
* function to check if php curl is installed and enabled
* @return string Returns error message if php curl not installed and enabled
*/
function img_dependency_error_curl()
{
?>
<div class="notice notice-error is-dismissible">
<p>
<?php _e('Easy Merchant requires PHP CURL installed on this server!', 'woocommerce-lyfePAY'); ?>
</p>
</div>
<?php
}
// function do_ssl_check()
// {
// if ('yes' != $this->stripe_sandbox && "no" == get_option('woocommerce_force_ssl_checkout') && "yes" == $this->enabled) {
// echo "<div class=\"error\"><p>" . sprintf(__("<strong>%s</strong> is enabled and WooCommerce is not forcing the SSL on your checkout page. Please ensure that you have a valid SSL certificate and that you are <a href=\"%s\">forcing the checkout pages to be secured.</a>"), $this->method_title, admin_url('admin.php?page=wc-settings&tab=checkout')) . "</p></div>";
// }
// }
/**
* WC lyfePAY gateway plugin class.
*
* @class WC_lyfePAY_Payments
*/
class WC_lyfePAY_Payments
{
/**
* @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()
{
}
/**
* Flag to indicate whether or not we need to load code for / support subscriptions.
*
* @var bool
*/
private $subscription_support_enabled = false;
/**
* Flag to indicate whether or not we need to load support for pre-orders.
*
* @since 3.0.3
*
* @var bool
*/
private $pre_order_enabled = false;
/**
* 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_notices', array($this, 'admin_notices'), 15);
add_action('plugins_loaded', array($this, 'init_easy_merchant'));
add_action('rest_api_init', function () {
register_rest_route('easymerchant/v1', '/cards', array(
'methods' => 'GET',
'callback' => 'fetch_user_cards',
));
});
// Register custom REST API endpoint to get current user ID and customer ID
add_action('rest_api_init', function () {
register_rest_route('wooeasy/wp-json/wp/v2', '/users', array(
'methods' => 'GET',
'callback' => 'get_user_meta_data',
'permission_callback' => function () {
return is_user_logged_in(); // Ensure the user is authenticated
}
));
});
}
public function init_easy_merchant()
{
/**
* Check if WooCommerce is active
**/
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
add_action('admin_notices', 'img_dependency_error_woo');
return;
}
/**
* Check if CURL is enabled
**/
if (!function_exists('curl_version')) {
add_action('admin_notices', 'img_dependency_error_curl');
return;
}
// Init the gateway itself
$this->init_gateways();
add_action('woocommerce_order_status_on-hold_to_processing', array($this, 'capture_payment'));
add_action('woocommerce_order_status_on-hold_to_completed', array($this, 'capture_payment'));
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'img_woocommerce_addon_settings_link'));
}
/**
* Allow this class and other classes to add slug keyed notices (to avoid duplication)
*/
public function add_admin_notice($slug, $class, $message)
{
$this->notices[$slug] = array(
'class' => $class,
'message' => $message
);
}
/**
* Display any notices we've collected thus far (e.g. for connection, disconnection)
*/
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>";
}
}
public function init_gateways()
{
if (class_exists('WC_Subscriptions_Order') && function_exists('wcs_create_renewal_order')) {
$this->subscription_support_enabled = true;
}
if (class_exists('WC_Pre_Orders_Order')) {
$this->pre_order_enabled = true;
}
if (class_exists('WC_Payment_Gateway')) {
include_once('includes/class-wc-gateway-lyfePAY.php');
include_once('includes/class-wc-gateway-ach-lyfePAY.php');
if ($this->subscription_support_enabled) {
include_once('includes/class-wc-gateway-lyfePAY-addons.php');
include_once('includes/class-wc-gateway-ach-lyfePAY-addons.php');
}
}
add_filter('woocommerce_payment_gateways', array($this, 'add_gateways'));
}
/**
* Plugin bootstrapping.
*/
public static function init()
{
//lyfePAY gateway class.
add_action('plugins_loaded', array(__CLASS__, 'includes'), 0);
// Make thelyfePAY gateway available to WC.
add_filter('woocommerce_payment_gateways', array(__CLASS__, 'add_gateway'));
// Registers WooCommerce Blocks integration.
add_action('woocommerce_blocks_loaded', array(__CLASS__, 'woocommerce_gateway_lyfePAY_woocommerce_block_support'));
}
/**
* Add the lyfePAY gateway to the list of available gateways.
*
* @param array
*/
public static function add_gateway($gateways)
{
$options = get_option('woocommerce_lyfePAY_settings', array());
if (isset($options['hide_for_non_admin_users'])) {
$hide_for_non_admin_users = $options['hide_for_non_admin_users'];
} else {
$hide_for_non_admin_users = 'no';
}
if (('yes' === $hide_for_non_admin_users && current_user_can('manage_options')) || 'no' === $hide_for_non_admin_users) {
$gateways[] = 'WC_Gateway_lyfePAY';
$gateways[] = 'WC_Gateway_ACH_lyfePAY';
}
return $gateways;
}
/**
* Add the gateways to WooCommerce
* @since 1.0.0
*/
public function add_gateways($methods)
{
if ($this->subscription_support_enabled || $this->pre_order_enabled) {
$methods[] = 'WC_Gateway_lyfePAY_Addons';
$methods[] = 'WC_Gateway_ACHlyfePAY_Addons';
} else {
$methods[] = 'WC_Gateway_lyfePAY';
$methods[] = 'WC_Gateway_ACH_lyfePAY';
}
return $methods;
}
public function get_curl2()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_VERBOSE, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
return $curl;
}
/**
* Capture payment when the order is changed from on-hold to complete or processing
*
* @param int $order_id
*/
public function capture_payment($order_id)
{
$order = wc_get_order($order_id);
if ('lyfePAY' === $order->payment_method) {
$charge = get_post_meta($order_id, '_transaction_id', true);
if ($charge) {
$curl = $this->get_curl2();
$mode = get_option('test_mode');
if ($mode) {
$api_base_url = "https://stage-api.stage-easymerchant.io";
$api_key = get_option('test_api_key');
$secret_key = get_option('test_secret_key');
} else {
$api_base_url = "https://api.easymerchant.io";
$api_key = get_option('api_key');
$secret_key = get_option('api_secret');
}
$options = get_option('woocommerce_lyfePAY_settings');
curl_setopt($curl, CURLOPT_URL, $api_base_url . '/api/v1/capture');
curl_setopt($curl, CURLOPT_POST, 'true');
curl_setopt($curl, CURLOPT_POSTFIELDS, array('charge_id' => $charge));
curl_setopt(
$curl,
CURLOPT_HTTPHEADER,
array(
'X-Api-Key: ' . $api_key,
'X-Api-Secret: ' . $secret_key,
'User-Agent: ' . LYFE_APP_NAME,
)
);
$resp = json_decode(curl_exec($curl));
if (is_wp_error($resp)) {
$order->add_order_note(__('Unable to capture charge!', 'woocommerce-lyfePAY') . ' ' . $resp->get_error_message());
} else {
$order->add_order_note(sprintf(__('lyfePAY charge complete (Charge ID: %s)', 'woocommerce-lyfePAY'), $resp->charge_id));
// Store other data such as fees
update_post_meta($order->id, '_transaction_id', $resp->charge_id);
}
}
}
}
/*Plugin Settings Link*/
public function img_woocommerce_addon_settings_link($actions)
{
$settinglinks = array(
'<a href="' . admin_url('admin.php?page=wc-settings&tab=checkout§ion=lyfePAY') . '">Settings</a>',
);
$actions = array_merge($actions, $settinglinks);
return $actions;
}
/**
* Plugin includes.
*/
public static function includes()
{
// Make the WC_Gateway_lyfePAY class available.
if (class_exists('WC_Payment_Gateway')) {
require_once 'includes/class-wc-gateway-lyfePAY.php';
require_once 'includes/class-wc-gateway-ach-lyfePAY.php';
}
}
/**
* Plugin url.
*
* @return string
*/
public static function plugin_url()
{
return untrailingslashit(plugins_url('/', __FILE__));
}
/**
* Plugin url.
*
* @return string
*/
public static function plugin_abspath()
{
return trailingslashit(plugin_dir_path(__FILE__));
}
/**
* Callback function to get user ID and customer ID
*/
function get_user_meta_data()
{
// Get the current user ID
$user_id = get_current_user_id();
if (!$user_id) {
return new WP_Error('no_user', __('No user is logged in', 'woocommerce-lyfePAY'), array('status' => 401));
}
// Get the customer ID from user meta
$customer_id = get_user_meta($user_id, '_customer_id', true);
// Return the data as a JSON response
return rest_ensure_response(array(
'user_id' => $user_id,
'customer_id' => $customer_id,
));
}
function fetch_user_cards()
{
$user_id = get_current_user_id();
$im_cus_id = get_user_meta($user_id, '_customer_id', true);
$mode = get_option('test_mode');
if ($mode) {
$api_base_url = "https://stage-api.stage-easymerchant.io";
$api_key = get_option('test_api_key');
$secret_key = get_option('test_secret_key');
} else {
$api_base_url = "https://api.easymerchant.io";
$api_key = get_option('api_key');
$secret_key = get_option('api_secret');
}
$cards = [];
if ($im_cus_id) {
$curl = curl_init();
$params = http_build_query(array('customer' => $im_cus_id));
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_URL, $api_base_url . "/api/v1/card?$params");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'X-Api-Key: ' . $api_key,
'X-Api-Secret: ' . $secret_key,
'User-Agent: ' . LYFE_APP_NAME,
));
$response = curl_exec($curl);
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
curl_close($curl);
return new WP_Error('curl_error', __('Error fetching cards: ' . $error_msg, 'your-text-domain'));
}
curl_close($curl);
$resp = json_decode($response, true);
if ($resp && isset($resp['Cards'])) {
$cards = $resp['Cards'];
}
}
return rest_ensure_response(['cards' => $cards]);
}
/**
* Registers WooCommerce Blocks integration.
*/
public static function woocommerce_gateway_lyfePAY_woocommerce_block_support()
{
if (class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) {
require_once 'includes/blocks/class-wc-lyfePAY-payments-blocks.php';
require_once 'includes/blocks/class-wc-ach-lyfePAY-payments-blocks.php';
add_action(
'woocommerce_blocks_payment_method_type_registration',
function (Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry) {
$payment_method_registry->register(new WC_Gateway_lyfePAY_Blocks_Support());
$payment_method_registry->register(new WC_Gateway_ACH_lyfePAY_Blocks_Support());
}
);
}
}
}
WC_lyfePAY_Payments::init();