-
Notifications
You must be signed in to change notification settings - Fork 3
/
iugu-woocommerce.php
311 lines (284 loc) · 9.84 KB
/
iugu-woocommerce.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
<?php
/**
* Plugin Name: WooCommerce iugu
* Plugin URI: https://github.com/iugu/iugu-woocommerce
* Description: iugu payment gateway for WooCommerce.
* Author: iugu
* Author URI: https://iugu.com/
* Version: 3.1.0
* Requires at least: 5.6
* Requires PHP: 7.0
* License: GPLv2 or later
* Text Domain: iugu-woocommerce
* Domain Path: lang/
*/
if (!defined('ABSPATH')) {
exit;
} // end if;
if (!class_exists('WC_Iugu')) {
/**
* WooCommerce Iugu main class.
*/
class WC_Iugu {
/**
* API constants. Plugin name and version.
*
* @var string
*/
const CLIENT_NAME = 'plugin-iugu-woocommerce';
const CLIENT_VERSION = '3.1.0';
/**
* Instance of this class.
*
* @var WC_Iugu_Hooks
*/
public $iugu_hooks = null;
/**
* Instance of this class.
*
* @var WC_Iugu
*/
protected static $instance = null;
/**
* @var WC_IUGU_Settings
*/
public function settings() {
if ($this->_settings == null) {
$this->_settings = new WC_IUGU_Settings();
}
return $this->_settings;
}
private $_settings = null;
/**
* Initialize the plugin actions.
*/
public function __construct() {
if (!defined('WC_IUGU_PLUGIN_FILE')) {
define('WC_IUGU_PLUGIN_FILE', __FILE__);
} // end if;
if (!defined('IUGU')) {
define('IUGU', 'iugu-woocommerce');
} // end if;
if (class_exists('WooCommerce')) {
/**
* Load plugin text domain.
*/
$this->load_plugin_textdomain();
WC()->frontend_includes();
if (!class_exists('WC_Iugu2')) {
/**
* Checks with WooCommerce and WooCommerce Extra Checkout Fields for Brazil is installed.
*/
if (class_exists('WC_Payment_Gateway') && class_exists('Extra_Checkout_Fields_For_Brazil')) {
$this->includes();
/**
* Hook to add Iugu Gateway to WooCommerce.
*/
add_filter('woocommerce_payment_gateways', array($this, 'add_gateway'));
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugin_action_links'));
}
add_action('admin_notices', array($this, 'dependencies_notices'));
add_filter('woocommerce_get_customer_payment_tokens', array($this, 'woocommerce_get_customer_payment_tokens'), 10, 3);
if (function_exists('bua_login_authenticate')) {
add_action('woocommerce_edit_account_form', array($this, 'woocommerce_edit_account_form_block_user'));
add_action('woocommerce_save_account_details', array($this, 'woocommerce_save_account_details_block_user'));
}
} else {
include_once 'views/admin-notices/html-notice-remove-old-version.php';
} // end if;
}
} // end __construct;
function woocommerce_get_customer_payment_tokens($tokens, $customer_id, $gateway_id) {
if (WC()->session != null &&
!WC()->session->get('in_woocommerce_get_customer_payment_tokens', false)) {
WC()->session->set('in_woocommerce_get_customer_payment_tokens', true);
try {
$api = new WC_Iugu_API(null, 'credit-card');
if (strlen(get_user_meta(get_current_user_id(), '_iugu_customer_id', true)) > 0) {
$api->get_customer_id();
$tokens = $api->get_payment_methods();
}
if (is_array($tokens) && count($tokens) == 0) {
$customer_id_iugu = $api->get_customer_id();
if ($customer_id_iugu) {
$payment_methods_iugu = $api->get_iugu_customer_payment_methods($customer_id_iugu);
if (is_array($payment_methods_iugu) && count($payment_methods_iugu) > 0) {
foreach ($payment_methods_iugu as $token_info) {
if (isset($token_info['item_type']) && $token_info['item_type'] == 'credit_card') {
$api->set_wc_payment_method($token_info, false, $customer_id_iugu);
}
}
$tokens = WC_Iugu_API::get_payment_methods();
}
}
}
} finally {
WC()->session->set('in_woocommerce_get_customer_payment_tokens', false);
}
}
return $tokens;
}
function woocommerce_edit_account_form_block_user() {
if (defined('DOING_AJAX') && DOING_AJAX) {
return;
}
wp_enqueue_style('user_status_style', BUA_CSS_URL . '/style.css');
$user_id = wp_get_current_user()->ID;
?>
<label class="tgl">
<input type="checkbox" name="user_status" value="deactive" id="user_status" <?php checked(get_user_meta($user_id, 'user_status', true), 'deactive'); ?>>
<span data-off="<?php echo __('Active account', IUGU) ?>" data-on="<?php echo __('Request deletion', IUGU) ?>"></span>
</label>
</p>
<?php
}
function woocommerce_save_account_details_block_user($user_ID) {
if (defined('DOING_AJAX') && DOING_AJAX) {
return;
}
$user_status = !empty($_POST['user_status']) ? wc_clean($_POST['user_status']) : '';
$user_status_message = '';
if ($user_status == 'deactive') {
$user_status_message = __('Deletion request made. Wait for the process to complete.', IUGU);
}
update_user_meta($user_ID, 'user_status', $user_status);
update_user_meta($user_ID, 'user_status_message', $user_status_message);
}
/**
* Return an instance of this class.
*
* @return WC_Iugu A single instance of this class.
*/
public static function get_instance() {
/**
* If the single instance hasn't been set, set it now.
*/
if (null == self::$instance) {
self::$instance = new self;
} // end if;
return self::$instance;
} // end get_instance;
/**
* Get templates path.
*
* @return string Templates dir path.
*/
public static function get_templates_path() {
return plugin_dir_path(__FILE__) . 'views/';
} // end get_templates_path;
/**
* Load the plugin text domain for translation.
*
* @return void.
*/
public function load_plugin_textdomain() {
load_plugin_textdomain(IUGU, false, dirname(plugin_basename(__FILE__)) . '/lang');
} // end load_plugin_textdomain;
/**
* Include all the necessary files.
*
* @return void
*/
private function includes() {
if (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) {
if (file_exists(dirname(__FILE__) . '/../../../ChromePhp.php')) {
require_once(dirname(__FILE__) . '/../../../ChromePhp.php');
}
}
include_once 'services/service-iugu-logger.php';
include_once 'inc/classes/class-wc-iugu-settings.php';
include_once 'inc/classes/class-wc-iugu-product.php';
include_once 'inc/classes/class-wc-iugu-user-profile.php';
include_once 'inc/gateways/class-wc-iugu-gateway.php';
include_once 'inc/gateways/class-wc-iugu-gateway-woocommerce-subscription.php';
/**
* Main API file.
*/
include_once 'inc/class-wc-iugu-api.php';
/**
* Bank Slip Gateway.
*/
include_once 'inc/gateways/bank-slip/class-wc-iugu-bank-slip-gateway.php';
/**
* Credit Card Gateway.
*/
include_once 'inc/gateways/credit-card/class-wc-iugu-credit-card-gateway-woocommerce-subscription.php';
include_once 'inc/gateways/credit-card/class-wc-iugu-credit-card-gateway.php';
/**
* PIX Gateway.
*/
include_once 'inc/gateways/pix/class-wc-iugu-pix-gateway.php';
// /**
// * My Account hooks.
// */
include_once 'inc/class-wc-iugu-hooks.php';
$this->settings();
$this->iugu_hooks = new WC_Iugu_Hooks();
WC_IUGU_Product::init();
WC_IUGU_UserProfile::init();
} // end includes;
/**
* Add all gateways to WooCommerce.
*
* @param array $methods WooCommerce payment methods.
* @return array Payment methods with Iugu.
*/
public function add_gateway($methods) {
$methods[] = 'WC_Iugu_Credit_Card_Gateway';
$methods[] = 'WC_Iugu_Bank_Slip_Gateway';
$methods[] = 'WC_Iugu_Pix_Gateway';
return $methods;
} // end add_gateway;
/**
* Add dependencies notices, if needed.
*
* @return void.
*/
public function dependencies_notices() {
if (!class_exists('WC_Payment_Gateway')) {
include_once 'views/admin-notices/html-notice-woocommerce-missing.php';
} // end if;
if (!class_exists('Extra_Checkout_Fields_For_Brazil')) {
include_once 'views/admin-notices/html-notice-ecfb-missing.php';
} // end if;
include_once 'views/admin-notices/html-notice-woocommerce-account.php';
} // end dependencies_notices;
/**
* Get log view.
*
* @return string Log view.
*/
public static function get_log_view($gateway_id) {
if (defined('WC_VERSION') && version_compare(WC_VERSION, '2.2', '>=')) {
return '<a href="' . esc_url(admin_url('admin.php?page=wc-status&tab=logs&log_file=' . esc_attr($gateway_id) . '-' . sanitize_file_name(wp_hash($gateway_id)) . '.log')) . '">' . __('System status > logs', IUGU) . '</a>';
} // end if;
return '<code>woocommerce/logs/' . esc_attr($gateway_id) . '-' . sanitize_file_name(wp_hash($gateway_id)) . '.txt</code>';
} // end get_log_view;
/**
* Action links.
*
* @param array $links
* @return array
*/
public function plugin_action_links($links) {
$plugin_links = array();
if (defined('WC_VERSION') && version_compare(WC_VERSION, '2.1', '>=')) {
$settings_url = admin_url('admin.php?page=wc-settings&tab=checkout§ion=');
} else {
$settings_url = admin_url('admin.php?page=woocommerce_settings&tab=payment_gateways§ion=');
} // end if;
$credit_card = 'WC_Iugu_Credit_Card_Gateway';
$bank_slip = 'WC_Iugu_Bank_Slip_Gateway';
$pix = 'WC_Iugu_Pix_Gateway';
$plugin_links[] = '<a href="' . admin_url('admin.php?page=wc-settings&tab=' . IUGU) . '">' . __('Integration settings', IUGU) . '</a>';
$plugin_links[] = '<a href="' . esc_url($settings_url . $credit_card) . '">' . __('Credit card settings', IUGU) . '</a>';
$plugin_links[] = '<a href="' . esc_url($settings_url . $bank_slip) . '">' . __('Bank slip settings', IUGU) . '</a>';
$plugin_links[] = '<a href="' . esc_url($settings_url . $pix) . '">' . __('PIX settings', IUGU) . '</a>';
return array_merge($plugin_links, $links);
} // end plugin_action_links;
} // end WC_Iugu;
/**
* Add get instance to the plugins loaded hook;
*/
add_action('plugins_loaded', array('WC_Iugu', 'get_instance'));
} // end if;