forked from cristianrennella/gocuotas-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-gocuotas.php
341 lines (278 loc) · 12 KB
/
class-gocuotas.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
<?php
class WC_Gateway_GoCuotas extends WC_Payment_Gateway
{
public function __construct()
{
$this->id = 'gocuotas';
$this->icon = plugin_dir_url(__FILE__).'logo-mobile.svg';
$this->has_fields = false;
$this->method_title = 'Go Cuotas';
$this->method_description = 'Plugin para integración de Go Cuotas en WooCommerce';
$this->supports = array(
'products'
);
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->enabled = $this->get_option('enabled');
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('wp_enqueue_scripts', array($this, 'payment_scripts'));
add_action('woocommerce_api_gocuotas', array($this, 'webhook'));
add_action('woocommerce_thankyou_' . $this->id, array($this, 'thankyou_page'));
add_action('woocommerce_api_gocuotas_webhook', array($this, 'webhook'));
add_filter('woocommerce_available_payment_gateways', array($this, 'restrict_payment_option'));
}
public function init_form_fields()
{
$this->form_fields = array(
'enabled' => [
'title' => 'Activar/Desactivar',
'label' => 'Activar GOcuotas',
'type' => 'checkbox',
'description' => '',
'default' => 'no'
],
'title' => [
'title' => 'Titulo',
'type' => 'text',
'description' => 'Titulo a mostrar al finalizar compra.',
'default' => 'Pagar CUOTAS con DEBITO sin interés',
'desc_tip' => true,
],
'description' => [
'title' => 'Descripción',
'type' => 'textarea',
'description' => 'Descripcion a mostrar al finalizar compra.',
'default' => 'Ahora podes pagar en CUOTAS sin interés con tu tarjeta de DEBITO!',
],
'email_go' => [
'title' => 'Email API Comercio',
'type' => 'text'
],
'password_go' => [
'title' => 'Password API Comercio',
'type' => 'password'
],
'show_fees_category' => [
'title' => 'Mostrar cuotas en las categoría de productos',
'label' => 'Activar',
'type' => 'checkbox',
'description' => '',
'default' => 'no'
],
'show_fees_product' => [
'title' => 'Mostrar cuotas en la página de producto',
'label' => 'Activar',
'type' => 'checkbox',
'description' => '',
'default' => 'no'
],
'fees_text' => [
'title' => 'Texto para mostrar las cuotas en el producto',
'label' => 'Texto',
'type' => 'text',
'default' => 'o en 4 cuotas con tarjeta de DÉBITO SIN interés de: ',
'description' => 'Ejemplo: o en 4 cuotas con tarjeta de DÉBITO SIN interés de: '
],
'fees_number' => [
'title' => 'Cuotas a mostrar',
'label' => 'Cuotas',
'type' => 'number',
'default' => 4,
'description' => 'Si en el texto de arriba se muestran 4 (cuatro) cuotas, este campo debe ser 4. Si se muestran 3 (tres) cuotas, este campo debe ser 3. etc'
],
'max_total' => [
'title' => 'Mostrar solo si el total es menor a: ',
'lable' => 'Máximo',
'type' => 'number',
'description' => 'Configurar el costo máximo de la orden para utilizar el plugin, este total también muestra o no las cuotas en el producto, deje el campo vacio para desactivar'
],
'logg' => [
'title' => 'Activar Log',
'label' => 'Activar',
'type' => 'checkbox',
'description' => 'Guarda un log de operaciones en un archivo dentro de la carpeta del plugin.',
'default' => 'no'
]
);
}
public function admin_options()
{
echo '<h3>' . $this->method_title . '</h3>';
echo '<table class="form-table">';
$this->generate_settings_html();
echo '</table>';
}
public function payment_fields()
{
if ($this->description) {
echo wpautop(wp_kses_post($this->description));
}
}
public function restrict_payment_option($available_gateways)
{
if (is_admin()) {
return $available_gateways;
}
if (is_wc_endpoint_url('order_pay')) {
$order_id = wc_get_order_id_by_order_key($_GET['key']);
$order = wc_get_order($order_id);
$order_total = $order->get_total();
} else {
$order_total = WC()->cart->total;
}
$gocuotas_settings = get_option('woocommerce_gocuotas_settings', true);
if (is_array($gocuotas_settings) && isset($gocuotas_settings['max_total']) && $gocuotas_settings['max_total'] < $order_total && $gocuotas_settings['max_total']!= '') {
unset($available_gateways['gocuotas']);
}
return $available_gateways;
}
public function payment_scripts()
{
if (!is_cart() && !is_checkout() && !isset($_GET['pay_for_order'])) {
return;
}
if ('no' === $this->enabled) {
return;
}
if (empty($this->comercio_key) || empty($this->publishable_key)) {
return;
}
wp_localize_script('woocommerce_gocuotas', 'gocuotas_params', array(
'publishableKey' => $this->publishable_key
));
wp_enqueue_script('woocommerce_gocuotas');
}
public function process_payment($order_id)
{
global $woocommerce;
$order = wc_get_order($order_id);
$godata = get_option('woocommerce_gocuotas_settings', true);
$authentication = wp_remote_post('https://www.gocuotas.com/api_redirect/v1/authentication', [
'method' => 'POST',
'body' => [
'email' => $godata['email_go'],
'password' => $godata['password_go']
]
]);
if ($godata['logg'] == 'yes') {
GoCuotas_Helper::go_log('auth-info.txt', json_encode($authentication) . PHP_EOL);
}
$auth_response = $authentication['response'];
if ($auth_response['code'] != 200) {
wc_add_notice($auth_response['message'] == 'Unauthorized' ? 'Error de autenticación de comercio.' : $auth_response['message'], 'error');
return;
}
$token = $authentication['body'];
$token = json_decode($token)->token;
$total = $order->get_total();
$total = str_replace(".", "", $total);
$total = str_replace(",", "", $total);
$order_received_url = wc_get_endpoint_url('order-received', $order->get_id(), wc_get_checkout_url());
$order_received_url_ok = $order_received_url . "done/";
$order_received_url_fail = $order->get_checkout_payment_url($on_checkout = false);
$order_received_url_okk = add_query_arg('key', $order->get_order_key(), $order_received_url_ok);
$payment_init = wp_remote_post('https://www.gocuotas.com/api_redirect/v1/checkouts', [
'method' => 'POST',
'headers' => [
'Authorization' => $token
],
'body' => [
'amount_in_cents' => $total,
'order_reference_id' => $order_id,
'url_success' => $order_received_url_okk,
'url_failure' => $order_received_url_fail,
'webhook_url' => home_url() . '/wc-api/gocuotas_webhook',
'email' => $order->get_billing_email(),
'phone_number' => $order->get_billing_phone(),
]
]);
if (is_wp_error($payment_init)) {
wc_add_notice('Error de pago: Ocurrio un error al conectar con la API.', 'error');
return;
};
$response = $payment_init['response'];
if ($response['code'] != 201) {
wc_add_notice('Error de pago: Ocurrio un error al pagar. "' . $response['message'] . '"', 'error');
return;
}
if ($godata['logg'] == 'yes') {
GoCuotas_Helper::go_log('payment_info.txt', json_encode($payment_init['body']) . PHP_EOL);
}
$url_init = $payment_init['body'];
$url_init = json_decode($url_init)->url_init;
update_post_meta($order_id, 'gocuotas_response', $payment_init['body']);
wc_reduce_stock_levels($order_id);
WC()->cart->empty_cart();
return array(
'result' => 'success',
'redirect' => $url_init
);
}
public function thankyou_page($order_id)
{
if ($this->instructions) {
echo wpautop(wptexturize($this->instructions));
}
$r = $_SERVER['REQUEST_URI'];
$r = explode('/', $r);
if (in_array("done", $r)) {
$order = wc_get_order($order_id);
$order->payment_complete();
$order->add_order_note(
'GO Cuotas: ' .
__('Payment APPROVED. IPN', 'gocuotas')
);
}
}
public function webhook()
{
$data = json_decode(file_get_contents('php://input'), true);
GoCuotas_Helper::go_log(date('Y-m-d') . 'webhook.txt', json_encode($data) . PHP_EOL);
$order = wc_get_order($data['order_reference_id']);
if (!$order) return;
$status = ['completed', 'processing', 'cancelled', 'refunded'];
if (in_array($order->get_status(), $status)) return;
if ($data['status'] != 'approved') {
$order->update_status('failed');
$order->add_order_note(
'GO Cuotas: ' .
__('ERROR EN PAGO, DENEGADO. IPN', 'gocuotas')
);
return;
}
$order->payment_complete();
wc_reduce_stock_levels($data['order_reference_id']);
update_option('webhook_debug', $_GET);
}
}
remove_filter('woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders');
add_filter('woocommerce_cancel_unpaid_orders', 'override_cancel_unpaid_orders');
function override_cancel_unpaid_orders()
{
$held_duration = get_option('woocommerce_hold_stock_minutes');
if ($held_duration < 1 || 'yes' !== get_option('woocommerce_manage_stock')) {
return;
}
$data_store = WC_Data_Store::load('order');
$unpaid_orders = $data_store->get_unpaid_orders(strtotime('-' . absint($held_duration) . ' MINUTES', current_time('timestamp')));
if ($unpaid_orders) {
foreach ($unpaid_orders as $unpaid_order) {
$order = wc_get_order($unpaid_order);
if ($order->get_payment_method() == 'gocuotas') {
if (apply_filters('woocommerce_cancel_unpaid_order', 'checkout' === $order->get_created_via(), $order)) {
$order->update_status('cancelled', __('Unpaid order cancelled - time limit reached.', 'woocommerce'));
foreach ($order->get_items() as $item_id => $item) {
$product = $item->get_product();
$qty = $item->get_quantity();
wc_update_product_stock($product, $qty, 'increase');
}
}
}
}
}
wp_clear_scheduled_hook('woocommerce_cancel_unpaid_orders');
wp_schedule_single_event(time() + (absint($held_duration) * 60), 'woocommerce_cancel_unpaid_orders');
}
add_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );