PluginProbe
MONEI Payments for WooCommerce / 2.1.0
MONEI Payments for WooCommerce v2.1.0
7.3.3 7.3.2 7.3.1 7.3.0 7.2.4 7.2.3 7.2.2 7.2.0 7.2.1 7.1.3 2.1.0 3.0.0 3.1.0 3.1.1 4.0.0 4.1.0 4.1.1 4.2.0 4.2.1 5.0 5.1.0 5.1.1 5.1.2 5.2.2 5.2.3 All 87 releases
monei / monei.php

monei.php in MONEI Payments for WooCommerce 2.1.0, at monei.php

813 lines 30.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WooCommerce MONEI Gateway
5 *
6 * @package WooCommerce MONEI Gateway
7 * @author José Conti
8 * @copyright 2020-2020 MONEI Conti
9 * @license GPL-3.0+
10 *
11 * Plugin Name: WooCommerce MONEI Gateway
12 * Plugin URI: https://wordpress.org/plugins/monei/
13 * Description: Extends WooCommerce with a MONEI gateway. Best payment gateway rates. The perfect solution to manage your digital payments.
14 * Version: 2.1.0
15 * Author: MONEI
16 * Author URI: https://www.monei.net/
17 * Tested up to: 5.4
18 * WC requires at least: 3.0
19 * WC tested up to: 4.1
20 * Text Domain: monei
21 * Domain Path: /languages/
22 * Copyright: (C) 2017 MONEI.
23 * License: GNU General Public License v3.0
24 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
25 */
26
27 define( 'MONEI_VERSION', '2.1.0' );
28 define( 'MONEI_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
29 define( 'MONEI_SIGNUP', 'https://dashboard.monei.net/?action=signUp' );
30 define( 'MONEI_WEB', 'https://monei.net/' );
31 define( 'MONEI_REVIEW', 'https://wordpress.org/support/plugin/monei/reviews/?rate=5#new-post' );
32 define( 'MONEI_SUPPORT', 'https://support.monei.net/' );
33
34 add_action( 'plugins_loaded', 'woocommerce_gateway_monei_init', 0 );
35
36
37 function woocommerce_gateway_monei_init() {
38 if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
39 return;
40 }
41 /**
42 * Localisation
43 */
44 load_plugin_textdomain( 'monei', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
45 /**
46 * Gateway class
47 */
48 class WC_Gateway_Monei extends WC_Payment_Gateway {
49 var $notify_url;
50 /**
51 * Constructor for the gateway.
52 *
53 * @access public
54 * @return void
55 */
56 public function __construct() {
57 global $woocommerce;
58 $this->id = 'monei';
59 $logo_url = $this->get_option( 'logo' );
60 if ( ! empty( $logo_url ) ) {
61 $logo_url = $this->get_option( 'logo' );
62 $this->icon = apply_filters( 'woocommerce_monei_icon', $logo_url );
63 } else {
64 $this->icon = apply_filters( 'woocommerce_monei_icon', plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) . '/assets/images/MONEI-logo.png' );
65 }
66 $this->has_fields = false;
67 $this->liveurl = 'https://pay.monei.net/checkout/v1';
68 $this->refund_url = 'https://pay.monei.net/checkout/v1/refund';
69 $this->testmode = $this->get_option( 'testmode' );
70 $this->method_title = __( 'MONEI', 'monei' );
71 $this->notify_url = add_query_arg( 'wc-api', 'WC_Gateway_monei', home_url( '/' ) );
72 $this->notify_url_not_https = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'WC_Gateway_monei', home_url( '/' ) ) );
73 // Define user set variables.
74 $this->title = $this->get_option( 'title' );
75 $this->description = $this->get_option( 'description' );
76 $this->logo = $this->get_option( 'logo' );
77 $this->orderdo = $this->get_option( 'orderdo' );
78 $this->accountid = $this->get_option( 'accountid' );
79 $this->commercename = $this->get_option( 'commercename' );
80 $this->secret = $this->get_option( 'secret' );
81 $this->password = $this->get_option( 'password' );
82 $this->debug = $this->get_option( 'debug' );
83 $this->log = new WC_Logger();
84 $this->supports = array(
85 'products',
86 'refunds',
87 );
88 $this->init_form_fields();
89 $this->init_settings();
90 // Actions.
91 add_action( 'valid_monei_standard_ipn_request', array( $this, 'successful_request' ) );
92 add_action( 'woocommerce_receipt_monei', array( $this, 'receipt_page' ) );
93 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
94 // Payment listener/API hook.
95 add_action( 'woocommerce_api_wc_gateway_' . $this->id, array( $this, 'check_ipn_response' ) );
96 if ( ! $this->is_valid_for_use() ) {
97 $this->enabled = false;
98 }
99 }
100
101 /**
102 * Check if this gateway is enabled and available in the user's country
103 *
104 * @access public
105 * @return bool
106 */
107 function is_valid_for_use() {
108
109 if ( ! in_array( get_woocommerce_currency(), array( 'EUR', 'USD', 'GBP' ), true ) ) {
110 return false;
111 } else {
112 return true;
113 }
114 }
115 /**
116 * Admin Panel Options
117 *
118 * @since 1.0.0
119 */
120 public function admin_options() {
121 ?>
122 <h3><?php esc_html_e( 'MONEI', 'monei' ); ?></h3>
123 <p><?php esc_html_e( 'Best payment gateway rates. The perfect solution to manage your digital payments.', 'monei' ); ?></p>
124 <?php if ( $this->is_valid_for_use() ) : ?>
125 <table class="form-table">
126 <?php
127 // Generate the HTML For the settings form.
128 $this->generate_settings_html();
129 ?>
130 </table><!--/.form-table-->
131 <?php else : ?>
132 <div class="inline error"><p><strong><?php esc_html_e( 'Gateway Disabled', 'monei' ); ?></strong>: <?php esc_html_e( 'MONEI only support EUROS, USD & GBP currencies.', 'monei' ); ?></p></div>
133 <?php
134 endif;
135 }
136 /**
137 * Initialise Gateway Settings Form Fields
138 *
139 * @access public
140 * @return void
141 */
142 function init_form_fields() {
143 $this->form_fields = array(
144 'enabled' => array(
145 'title' => __( 'Enable/Disable', 'monei' ),
146 'type' => 'checkbox',
147 'label' => __( 'Enable MONEI', 'monei' ),
148 'default' => 'no',
149 ),
150 'title' => array(
151 'title' => __( 'Title', 'monei' ),
152 'type' => 'text',
153 'description' => __( 'This controls the title which the user sees during checkout.', 'monei' ),
154 'default' => __( 'MONEI', 'monei' ),
155 'desc_tip' => true,
156 ),
157 'description' => array(
158 'title' => __( 'Description', 'monei' ),
159 'type' => 'textarea',
160 'description' => __( 'This controls the description which the user sees during checkout.', 'monei' ),
161 'default' => __( 'Pay via MONEI; you can pay with your credit card.', 'monei' ),
162 ),
163 'logo' => array(
164 'title' => __( 'Logo', 'monei' ),
165 'type' => 'text',
166 'description' => __( 'Add link to image logo.', 'monei' ),
167 'desc_tip' => true,
168 ),
169 'commercename' => array(
170 'title' => __( 'Shop Name', 'monei' ),
171 'type' => 'text',
172 'description' => __( 'Shop Name', 'monei' ),
173 'desc_tip' => true,
174 ),
175 'accountid' => array(
176 'title' => __( 'Account ID', 'monei' ),
177 'type' => 'text',
178 'description' => __( 'Account ID', 'monei' ),
179 'desc_tip' => true,
180 ),
181 'password' => array(
182 'title' => __( 'Password', 'monei' ),
183 'type' => 'text',
184 'description' => __( 'MONEI Password', 'monei' ),
185 'desc_tip' => true,
186 ),
187 'orderdo' => array(
188 'title' => __( 'What to do after payment?', 'monei' ),
189 'type' => 'select',
190 'description' => __( 'Chose what to do after the customer pay the order.', 'monei' ),
191 'default' => 'processing',
192 'options' => array(
193 'processing' => __( 'Mark as Processing (default & recomended)', 'monei' ),
194 'completed' => __( 'Mark as Complete', 'monei' ),
195 ),
196 ),
197 'testmode' => array(
198 'title' => __( 'Running in test mode', 'monei' ),
199 'type' => 'checkbox',
200 'label' => __( 'Running in test mode', 'monei' ),
201 'default' => 'yes',
202 'description' => sprintf( __( 'Select this option for the initial testing required by MONEI, deselect this option once you pass the required test phase and your production environment is active.', 'monei' ) ),
203 ),
204 'debug' => array(
205 'title' => __( 'Debug Log', 'monei' ),
206 'type' => 'checkbox',
207 'label' => __( 'Enable logging', 'monei' ),
208 'default' => 'no',
209 'description' => __( 'Log MONEY events, such as notifications requests, inside <code>WooCommerce > Status > Logs > Select MONEI Logs</code>', 'monei' ),
210 ),
211 );
212 }
213
214 function test_mode() {
215 if ( 'yes' === $this->testmode ) {
216 $test = 'true';
217 } else {
218 $test = 'false';
219 }
220 return $test;
221 }
222 function get_monei_args( $order ) {
223 global $woocommerce;
224
225 $order_id = $order->get_id();
226 $currency = get_woocommerce_currency();
227 $account_id = $this->accountid;
228 $transaction_id = str_pad( $order_id, 12, '0', STR_PAD_LEFT );
229 $transaction_id1 = wp_rand( 1, 999 ); // lets to create a random number.
230 $transaction_id2 = substr_replace( $transaction_id, $transaction_id1, 0, -9 ); // new order number.
231 $amount = $order->get_total();
232 $country = new WC_Countries();
233 $shop_country = $country->get_base_country();
234 $shop_name = $this->commercename;
235 $url_callback = $this->notify_url;
236 $url_cancel = html_entity_decode( $order->get_cancel_order_url() );
237 $url_complete = utf8_encode( add_query_arg( 'utm_nooverride', '1', $this->get_return_url( $order ) ) );
238 $transaction_type = 'sale';
239 $password = $this->password;
240 $test = $this->test_mode();
241
242 $message = 'account_id' . $account_id . 'amount' . $amount . 'currency' . $currency . 'order_id' . $transaction_id2 . 'shop_name' . $shop_name . 'test' . $test . 'transaction_type' . $transaction_type . 'url_callback' . $url_callback . 'url_cancel' . $url_cancel . 'url_complete' . $url_complete;
243
244 $sign = hash_hmac('sha256', $message, $password );
245
246 if ( 'yes' === $this->debug ) {
247 $this->log->add( 'monei', 'Generating payment form for order ' . $order->get_order_number() );
248 $this->log->add( 'monei', 'Helping to understand the encrypted code: ' );
249 $this->log->add( 'monei', 'account_id: ' . $account_id );
250 $this->log->add( 'monei', 'amount: ' . $amount );
251 $this->log->add( 'monei', 'currency: ' . $currency );
252 $this->log->add( 'monei', 'order_id: ' . $transaction_id2 );
253 $this->log->add( 'monei', 'shop_name: ' . $shop_name );
254 $this->log->add( 'monei', 'test: ' . $test );
255 $this->log->add( 'monei', 'url_callback: ' . $url_callback );
256 $this->log->add( 'monei', 'url_cancel: ' . $url_cancel );
257 $this->log->add( 'monei', 'url_complete: ' . $url_complete );
258 $this->log->add( 'monei', 'Password: ' . $password );
259 $this->log->add( 'monei', 'Shop country: ' . $shop_country );
260 $this->log->add( 'monei', 'concatenated: ' . $message );
261 $this->log->add( 'monei', 'sign: ' . $sign );
262 }
263
264 $monei_args = array(
265 'account_id' => $account_id,
266 'amount' => $amount,
267 'currency' => $currency,
268 'order_id' => $transaction_id2,
269 'shop_name' => $shop_name,
270 'test' => $test,
271 'transaction_type' => $transaction_type,
272 'url_callback' => $url_callback,
273 'url_cancel' => $url_cancel,
274 'url_complete' => $url_complete,
275 'signature' => $sign,
276
277 );
278
279 $monei_args = apply_filters( 'woocommerce_monei_args', $monei_args );
280 return $monei_args;
281 }
282 /**
283 * Generate the monei form
284 *
285 * @access public
286 * @param mixed $order_id
287 * @return string
288 */
289 function generate_monei_form( $order_id ) {
290 global $woocommerce;
291
292 $order = new WC_Order( $order_id );
293 $monei_adr = $this->liveurl . '?';
294 $monei_args = $this->get_monei_args( $order );
295 $form_inputs = '';
296 foreach ( $monei_args as $key => $value ) {
297 $form_inputs .= '<input type="hidden" name="' . $key . '" value="' . esc_attr( $value ) . '" />';
298 }
299 wc_enqueue_js( '
300 $("body").block({
301 message: "<img src=\"' . esc_url( apply_filters( 'woocommerce_ajax_loader_url', $woocommerce->plugin_url() . '/assets/images/select2-spinner.gif' ) ) . '\" alt=\"Redirecting&hellip;\" style=\"float:left; margin-right: 10px;\" />' . __( 'Thank you for your order. We are now redirecting you to MONEI to make the payment.', 'monei' ) . '",
302 overlayCSS:
303 {
304 background: "#fff",
305 opacity: 0.6
306 },
307 css: {
308 padding: 20,
309 textAlign: "center",
310 color: "#555",
311 border: "3px solid #aaa",
312 backgroundColor:"#fff",
313 cursor: "wait",
314 lineHeight: "32px"
315 }
316 });
317 jQuery("#submit_monei_payment_form").click();
318 ' );
319 return '<form action="' . esc_url( $monei_adr ) . '" method="post" id="monei_payment_form" target="_top">
320 ' . $form_inputs . '
321 <input type="submit" class="button-alt" id="submit_monei_payment_form" value="' . __( 'Pay with Credit Card via MONEI', 'monei' ) . '" /> <a class="button cancel" href="' . esc_url( $order->get_cancel_order_url() ) . '">' . __( 'Cancel order &amp; restore cart', 'monei' ) . '</a>
322 </form>';
323 }
324 /**
325 * Process the payment and return the result
326 *
327 * @access public
328 * @param int $order_id
329 * @return array
330 */
331 function process_payment( $order_id ) {
332 $order = new WC_Order( $order_id );
333 return array(
334 'result' => 'success',
335 'redirect' => $order->get_checkout_payment_url( true ),
336 );
337 }
338 /**
339 * Output for the order received page.
340 *
341 * @access public
342 * @return void
343 */
344 function receipt_page( $order ) {
345 echo '<p>' . esc_html__( 'Thank you for your order, please click the button below to pay with Credit Card via MONEI.', 'monei' ) . '</p>';
346 echo $this->generate_monei_form( $order );
347 }
348 /**
349 * Check monei IPN validity
350 **/
351 function check_ipn_request_is_valid() {
352
353 if ( 'yes' === $this->debug ) {
354 $this->log->add( 'monei', 'checking notification' );
355 }
356
357 if ( isset( $_POST['signature'] ) ) {
358 $signature = $_POST['signature'];
359 $posted = array();
360 foreach ( $_POST as $key => $value ) {
361 if ( 'signature' !== $key ) {
362 $posted[$key] = $value;
363 }
364 continue;
365 }
366 ksort( $posted );
367
368 foreach ( $posted as $key => $value ) {
369 $content .= $key . $value;
370 }
371 $password = $this->password;
372
373 if ( ! empty( $content ) && ! empty( $password ) && ! empty( $signature ) ) {
374 $sign = hash_hmac('sha256', $content, $password );
375 if ( 'yes' === $this->debug ) {
376 $this->log->add( 'monei', 'data: ' . $content );
377 $this->log->add( 'monei', 'signature form Manei: ' . $signature );
378 $this->log->add( 'monei', 'signature at Plugin: ' . $sign );
379 }
380 if ( $sign !== $signature ) {
381 if ( 'yes' === $this->debug ) {
382 $this->log->add( 'monei', 'Received INVALID notification from MONEI' );
383 }
384 return false;
385 } else {
386 if ( 'yes' === $this->debug ) {
387 $this->log->add( 'monei', 'Correct signature' );
388 }
389 $amountmonei = $_POST['amount'];
390 $order_id = $_POST['order_id'];
391 $order2 = substr( $order_id, 3 ); //cojo los 9 digitos del final
392 $order = new WC_Order( $order2 );
393 $amountwoo = $order->get_total();
394
395 if ( $amountmonei === $amountwoo ) {
396 if ( 'yes' === $this->debug ) {
397 $this->log->add( 'monei', 'The amount match' );
398 }
399 return true;
400 } else {
401 if ( 'yes' === $this->debug ) {
402 $this->log->add( 'monei', 'The amount does not match' );
403 }
404 return false;
405 }
406 }
407 } else {
408 if ( 'yes' === $this->debug ) {
409 $this->log->add( 'monei', 'Received INVALID notification from MONEI' );
410 }
411 return false;
412 }
413 } else {
414 if ( 'yes' === $this->debug ) {
415 $this->log->add( 'monei', 'Received INVALID notification from MONEI' );
416 }
417 return false;
418 }
419 }
420 /**
421 * Check for Monei HTTP Notification
422 *
423 * @access public
424 * @return void
425 */
426 function check_ipn_response() {
427 if ( 'yes' === $this->debug ) {
428 $this->log->add( 'monei', ' ' );
429 $this->log->add( 'monei', '/****************************/' );
430 $this->log->add( 'monei', ' check_ipn_response ' );
431 $this->log->add( 'monei', '/****************************/' );
432 $this->log->add( 'monei', ' ' );
433 }
434 @ob_clean();
435 $_POST = stripslashes_deep( $_POST );
436
437 if ( $this->check_ipn_request_is_valid() ) {
438 header( 'HTTP/1.1 200 OK' );
439 do_action( 'valid_monei_standard_ipn_request', $_POST );
440 } else {
441 wp_die( 'MONEI Notification Request Failure' );
442 }
443 }
444 /**
445 * Successful Payment!
446 *
447 * @access public
448 * @param array $posted
449 * @return void
450 */
451 function successful_request( $posted ) {
452 global $woocommerce;
453
454 $account_id = sanitize_text_field( $_POST['account_id'] );
455 $amount = sanitize_text_field( $_POST['amount'] );
456 $currency = sanitize_text_field( $_POST['currency'] );
457 $monei_order_id = sanitize_text_field( $_POST['monei_order_id'] );
458 $order_id = sanitize_text_field( $_POST['order_id'] );
459 $result = sanitize_text_field( $_POST['result'] ); // completed, failed and pending
460 $signature = sanitize_text_field( $_POST['signature'] );
461 $test = sanitize_text_field( $_POST['test'] );
462 $timestamp = sanitize_text_field( $_POST['timestamp'] );
463 $message = sanitize_text_field( $_POST['message'] );
464 $transaction_type = sanitize_text_field( $_POST['transaction_type'] ); //sale, authorization, capture, refund and void
465 $order2 = substr( $order_id, 3 ); // cojo los 9 digitos del final.
466 $order = $this->get_monei_order( (int) $order2 );
467
468 if ( 'yes' === $this->debug ) {
469 $this->log->add( 'monei', '$account_id: ' . $account_id );
470 $this->log->add( 'monei', '$amount: ' . $amount );
471 $this->log->add( 'monei', '$currency: ' . $currency );
472 $this->log->add( 'monei', '$monei_order_id: ' . $monei_order_id );
473 $this->log->add( 'monei', '$order_id: ' . $order_id );
474 $this->log->add( 'monei', '$result: ' . $result );
475 $this->log->add( 'monei', '$signature: ' . $signature );
476 $this->log->add( 'monei', '$test: ' . $test );
477 $this->log->add( 'monei', '$timestamp: ' . $timestamp );
478 $this->log->add( 'monei', '$message: ' . $message );
479 $this->log->add( 'monei', '$transaction_type: ' . $transaction_type );
480 }
481
482 if ( 'Transaction Approved' === $message ) {
483 // authorized.
484 $order2 = substr( $order_id, 3 ); //cojo los 9 digitos del final
485 $order = new WC_Order( $order2 );
486 $amountwoo = $order->get_total();
487
488 if ( $amountwoo !== $amount ) {
489 // amount does not match.
490 if ( 'yes' === $this->debug ) {
491 $this->log->add( 'monei', 'Payment error: Amounts do not match (order: ' . $amountwoo . ' - received: ' . $amount . ')' );
492 }
493 // Put this order on-hold for manual checking.
494 /* translators: order an received are the amount */
495 $order->update_status( 'on-hold', sprintf( __( 'Validation error: Order vs. Notification amounts do not match (order: %1$s - received: %2&s).', 'monei' ), $amountwoo, $amount ) );
496 exit;
497 }
498
499 $parts = explode( 'T', $timestamp );
500 $part2 = explode( '.', $parts[1] );
501 $date = $parts[0];
502 $hour = $part2[0];
503
504 if ( 'yes' === $this->debug ) {
505 $this->log->add( 'monei', 'Timestamp: ' . $timestamp );
506 $this->log->add( 'monei', 'Date: ' . $date );
507 $this->log->add( 'monei', 'Hour: ' . $hour );
508 }
509
510 if ( ! empty( $monei_order_id ) ) {
511 update_post_meta( $order->get_id(), '_payment_order_number_monei', $monei_order_id );
512 }
513
514 if ( ! empty( $date ) ) {
515 update_post_meta( $order->get_id(), '_payment_date_monei', $date );
516 }
517
518 if ( ! empty( $hour ) ) {
519 update_post_meta( $order->get_id(), '_payment_hour_monei', $hour );
520 }
521
522 if ( ! empty( $order_id ) ) {
523 update_post_meta( $order->get_id(), '_payment_wc_order_id_monei', $order_id );
524 }
525
526 // Payment completed.
527 $order->add_order_note( __( 'HTTP Notification received - payment completed', 'monei' ) );
528 $order->add_order_note( __( 'MONEI Order Number: ', 'monei' ) . $monei_order_id );
529 $order->payment_complete();
530 if ( 'completed' === $this->orderdo ) {
531 $order->update_status( 'completed', __( 'Order Completed by MONEI', 'monei' ) );
532 }
533
534 if ( 'yes' === $this->debug ) {
535 $this->log->add( 'monei', 'Payment complete.' );
536 }
537 } else {
538 // Tarjeta caducada.
539 if ( 'yes' === $this->debug ) {
540 $this->log->add( 'monei', 'Order cancelled by MONEI: ' . $message );
541 }
542 // Order cancelled.
543 $order->update_status( 'cancelled', 'Cancelled by MONEI: ' . $message);
544 $order->add_order_note( 'Order cancelled by MONEI: ' . $message );
545 WC()->cart->empty_cart();
546 }
547 }
548
549 /**
550 * get_monei_order function.
551 *
552 * @access public
553 * @param mixed $order_id
554 * @return void
555 */
556 function get_monei_order( $order_id ) {
557 $order = new WC_Order( $order_id );
558 return $order;
559 }
560
561 /**
562 * Refund
563 **/
564
565 function ask_for_refund( $order_id, $transaction_id, $amount ) {
566
567 //post code to MONEI
568 $order2 = get_post_meta( $order_id, '_payment_wc_order_id_monei', true );
569 $monei_order_number = $transaction_id;
570 $currency_codes = get_woocommerce_currency();
571 $account_id = $this->accountid;
572 $test = $this->test_mode();
573 $transaction_type = 'refund';
574 $shop_name = $this->commercename;
575 $password = $this->password;
576 $country = new WC_Countries();
577 $shop_country = $country->get_base_country();
578 $monei_adr = $this->refund_url;
579
580 $message = 'account_id' . $account_id .
581 'amount' . $amount .
582 'currency' . $currency_codes .
583 'monei_order_id' . $monei_order_number .
584 'order_id' . $order2 .
585 'shop_name' . $shop_name .
586 'test' . $test .
587 'transaction_type' . $transaction_type;
588
589 $sign = hash_hmac('sha256', $message, $password );
590
591 if ( 'yes' === $this->debug ) {
592 $this->log->add( 'monei', ' ' );
593 $this->log->add( 'monei', '$message ' . $message );
594 $this->log->add( 'monei', '$password ' . $password );
595 $this->log->add( 'monei', '$sign: ' . $sign );
596 $this->log->add( 'monei', __( 'Order Number MONEI : ', 'monei' ) . $monei_order_number );
597 }
598
599 if ( 'yes' === $this->debug ) {
600 $this->log->add( 'monei', ' ' );
601 $this->log->add( 'monei', ' ' );
602 $this->log->add( 'monei', '/**************************/' );
603 $this->log->add( 'monei', __( 'Starting asking for Refund', 'monei' ) );
604 $this->log->add( 'monei', '/**************************/' );
605 $this->log->add( 'monei', ' ' );
606 }
607
608 if ( 'yes' === $this->debug ) {
609 $this->log->add( 'monei', ' ' );
610 $this->log->add( 'monei', __( 'All data from meta', 'monei' ) );
611 $this->log->add( 'monei', '**********************' );
612 $this->log->add( 'monei', ' ' );
613 $this->log->add( 'monei', __( 'If something is empty, the data was not saved', 'monei' ) );
614 $this->log->add( 'monei', ' ' );
615 $this->log->add( 'monei', __( 'All data from meta', 'monei' ) );
616 $this->log->add( 'monei', __( 'Order Number MONEI : ', 'monei' ) . $monei_order_number );
617 }
618
619 if ( 'yes' === $this->debug ) {
620 $this->log->add( 'monei', ' ' );
621 $this->log->add( 'monei', __( 'Data sent to MONEI for refund', 'monei' ) );
622 $this->log->add( 'monei', '*********************************' );
623 $this->log->add( 'monei', ' ' );
624 $this->log->add( 'monei', __( 'URL to MONEI : ', 'monei' ) . $monei_adr );
625 $this->log->add( 'monei', __( 'account_id : ', 'monei' ) . $account_id );
626 $this->log->add( 'monei', __( 'amount : ', 'monei' ) . $amount );
627 $this->log->add( 'monei', __( 'currency : ', 'monei' ) . $currency_codes );
628 $this->log->add( 'monei', __( 'order_id : ', 'monei' ) . $order2 );
629 $this->log->add( 'monei', __( 'monei_order_id : ', 'monei' ) . $monei_order_number );
630 $this->log->add( 'monei', __( 'signature : ', 'monei' ) . $sign );
631 $this->log->add( 'monei', __( 'test : ', 'monei' ) . $test );
632 $this->log->add( 'monei', __( 'transaction_type : refund', 'monei' ) );
633 $this->log->add( 'monei', __( 'ask_for_refund Asking for order #: ', 'monei' ) . $order_id );
634 $this->log->add( 'monei', ' ' );
635 }
636
637 $body = array(
638 'account_id' => $account_id,
639 'amount' => $amount,
640 'currency' => $currency_codes,
641 'monei_order_id' => $monei_order_number,
642 'order_id' => $order2,
643 'shop_name' => $shop_name,
644 'signature' => $sign,
645 'test' => $test,
646 'transaction_type' => 'refund',
647 );
648
649 $data_string = json_encode( $body );
650
651 $options = array(
652 'headers' => array(
653 'Content-Type' => 'application/json',
654 ),
655 'body' => $data_string,
656 );
657
658 $response = wp_remote_post( $monei_adr, $options );
659 $response_code = wp_remote_retrieve_response_code( $response );
660 $response_body = wp_remote_retrieve_body( $response );
661
662 if ( is_wp_error( $response ) ) {
663 $error_string = $response->get_error_message();
664 if ( 'yes' === $this->debug ) {
665 $this->log->add( 'monei', ' ' );
666 $this->log->add( 'monei', __( 'There is an error', 'monei' ) );
667 $this->log->add( 'monei', '*********************************' );
668 $this->log->add( 'monei', ' ' );
669 $this->log->add( 'monei', __( 'The error is : ', 'monei' ) . $error_string );
670 }
671 return $error_string;
672 }
673
674 $result = json_decode( $response_body );
675 $end = $result->result;
676
677 if ( 'yes' === $this->debug ) {
678 $this->log->add( 'monei', ' ' );
679 $this->log->add( 'monei', '$result: ' . $end );
680 $this->log->add( 'monei', ' ' );
681 }
682
683 if ( 'completed' === $end ) {
684 return true;
685 } else {
686 return $end;
687 }
688 }
689
690 public function process_refund( $order_id, $amount = null, $reason = '' ) {
691
692 // Do your refund here. Refund $amount for the order with ID $order_id _transaction_id
693 set_time_limit( 0 );
694 $order = $this->get_monei_order( $order_id );
695 $order2 = get_post_meta( $order_id, '_payment_wc_order_id_monei', true );
696 $monei_order_number = get_post_meta( $order_id, '_payment_order_number_monei', true );
697
698
699 if ( ! $amount ) {
700 $order_total_sign = $order->get_total();
701 } else {
702 $order_total_sign = $amount;
703 }
704
705 if ( ! empty( $order2 ) ) {
706 if ( 'yes' === $this->debug ) {
707 $this->log->add( 'monei', ' ' );
708 $this->log->add( 'monei', '/****************************/' );
709 $this->log->add( 'monei', ' Once upon a time ' );
710 $this->log->add( 'monei', '/****************************/' );
711 $this->log->add( 'monei', ' ' );
712 $this->log->add( 'monei', __( 'check_monei_refund Asking for order #: ', 'monei' ) . $order_id );
713 }
714
715 $refund_asked = $this->ask_for_refund( $order_id, $monei_order_number, $order_total_sign );
716
717 if ( $refund_asked ) {
718 if ( 'yes' === $this->debug && $result ) {
719 $this->log->add( 'monei', __( 'check_monei_refund = true ', 'monei' ) );
720 $this->log->add( 'monei', ' ' );
721 $this->log->add( 'monei', '/********************************/' );
722 $this->log->add( 'monei', ' Refund complete by MONEI ' );
723 $this->log->add( 'monei', '/********************************/' );
724 $this->log->add( 'monei', ' ' );
725 $this->log->add( 'monei', '/******************************************/' );
726 $this->log->add( 'monei', ' The final has come, this story has ended ' );
727 $this->log->add( 'monei', '/******************************************/' );
728 $this->log->add( 'monei', ' ' );
729 }
730 return true;
731 } else {
732 if ( is_wp_error( $refund_asked ) ) {
733 if ( 'yes' === $this->debug ) {
734 $this->log->add( 'redsys', __( 'Refund Failed: ', 'monei' ) . $refund_asked->get_error_message() );
735 }
736 return new WP_Error( 'error', $refund_asked->get_error_message() );
737 }
738 }
739
740 if ( is_wp_error( $refund_asked ) ) {
741 if ( 'yes' === $this->debug ) {
742 $this->log->add( 'redsys', __( 'Refund Failed: ', 'monei' ) . $refund_asked->get_error_message() );
743 }
744 return new WP_Error( 'error', $refund_asked->get_error_message() );
745 }
746 } else {
747 if ( 'yes' === $this->debug && $result ) {
748 $this->log->add( 'monei', __( 'Refund Failed: No transaction ID', 'monei' ) );
749 }
750 return new WP_Error( 'monei', __( 'Refund Failed: No transaction ID', 'monei' ) );
751 }
752 }
753 }
754
755 function monei_add_notice_new_version() {
756
757 $version = get_option( 'hide-new-version-monei-notice' );
758
759 if ( ! $version ) {
760 if ( isset( $_REQUEST['monei-hide-new-version'] ) && 'hide-new-version-monei' === $_REQUEST['monei-hide-new-version'] ) {
761 $nonce = sanitize_text_field( $_REQUEST['_monei_hide_new_version_nonce'] );
762 if ( wp_verify_nonce( $nonce, 'monei_hide_new_version_nonce' ) ) {
763 update_option( 'hide-new-version-monei-notice', MONEI_VERSION );
764 }
765 } else {
766 ?>
767 <div id="message" class="updated woocommerce-message woocommerce-monei-messages">
768 <div class="contenido-monei-notice">
769 <a class="woocommerce-message-close notice-dismiss" style="top:0;" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'monei-hide-new-version', 'hide-new-version-monei' ), 'monei_hide_new_version_nonce', '_monei_hide_new_version_nonce' ) ); ?>"><?php esc_html_e( 'Dismiss', 'monei' ); ?></a>
770 <p>
771 <h3>
772 <?php esc_html_e( 'Thank you for install MONEI for WooCommerce. Version: ', 'monei' ) . ' ' . esc_html_e( MONEI_VERSION ); ?>
773 </h3>
774 </p>
775 <p>
776 <?php esc_html_e( 'The best payment gateway rates. The perfect solution to manage your digital payments.,', 'monei' ); ?>
777 </p>
778 <p class="submit">
779 <a href="<?php esc_html_e( MONEI_SIGNUP ); ?>" class="button-primary" target="_blank"><?php esc_html_e( 'Signup', 'monei' ); ?></a>
780 <a href="<?php esc_html_e( MONEI_WEB ); ?>" class="button-primary" target="_blank"><?php esc_html_e( 'MONEI website', 'monei' ); ?></a>
781 <a href="<?php esc_html_e( MONEI_REVIEW ); ?>" class="button-primary" target="_blank"><?php esc_html_e( 'Leave a review', 'monei' ); ?></a>
782 <a href="<?php esc_html_e( MONEI_SUPPORT ); ?>" class="button-primary" target="_blank"><?php esc_html_e( 'Support', 'monei' ); ?></a>
783 </p>
784 </div>
785 </div>
786 <?php }
787 }
788 }
789 add_action( 'admin_notices', 'monei_add_notice_new_version' );
790
791 function monei_notice_style() {
792 wp_register_style( 'monei_notice_css', MONEI_PLUGIN_URL . 'assets/css/monei-notice.css', false, MONEI_VERSION );
793 wp_enqueue_style( 'monei_notice_css' );
794 }
795 add_action( 'admin_enqueue_scripts', 'monei_notice_style' );
796
797 function woocommerce_add_gateway_monei_gateway( $methods ) {
798 $methods[] = 'WC_Gateway_monei';
799 return $methods;
800 }
801 add_filter( 'woocommerce_payment_gateways', 'woocommerce_add_gateway_monei_gateway' );
802
803 function add_monei_meta_box() {
804 $date_decoded = get_post_meta( get_the_ID(), '_payment_date_monei', true );
805 $hour_decoded = get_post_meta( get_the_ID(), '_payment_hour_monei', true );
806 echo '<h4>' . esc_html__( 'Payment Details', 'monei' ) . '</h4>';
807 echo '<p><strong>' . esc_html__( 'MONEI Date', 'monei' ) . ': </strong><br />' . esc_html( $date_decoded ) . '</p>';
808 echo '<p><strong>' . esc_html__( 'MONEI Hour', 'monei' ) . ': </strong><br />' . esc_html( $hour_decoded ) . '</p>';
809 echo '<p><strong>' . esc_html__( 'MONEI Order Number', 'monei' ) . ': </strong><br />' . esc_attr( get_post_meta( get_the_ID(), '_payment_order_number_monei', true ) ) . '</p>';
810 }
811 add_action( 'woocommerce_admin_order_data_after_billing_address', 'add_monei_meta_box' );
812 }
813