PluginProbe
MONEI Payments for WooCommerce / 4.0.0
MONEI Payments for WooCommerce v4.0.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 4.0.0, at monei.php

1,005 lines 38.2 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: 4.0.0
15 * Author: MONEI
16 * Author URI: https://www.monei.net/
17 * Tested up to: 5.6
18 * WC requires at least: 3.0
19 * WC tested up to: 4.7
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', '4.0.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 require_once 'includes/vendor/autoload.php';
37
38
39 function woocommerce_gateway_monei_init() {
40 if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
41 return;
42 }
43 /**
44 * Localisation
45 */
46 load_plugin_textdomain( 'monei', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
47 /**
48 * Gateway class
49 */
50 class WC_Gateway_Monei extends WC_Payment_Gateway {
51 var $notify_url;
52 /**
53 * Constructor for the gateway.
54 *
55 * @access public
56 * @return void
57 */
58 public function __construct() {
59 global $woocommerce;
60 $this->id = 'monei';
61 $logo_url = $this->get_option( 'logo' );
62 if ( ! empty( $logo_url ) ) {
63 $logo_url = $this->get_option( 'logo' );
64 $this->icon = apply_filters( 'woocommerce_monei_icon', $logo_url );
65 } else {
66 $this->icon = apply_filters( 'woocommerce_monei_icon', plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) . '/assets/images/MONEI-logo.png' );
67 }
68 $this->has_fields = true;
69 $this->liveurl = 'https://pay.monei.net/checkout';
70 $this->refund_url = 'https://api.monei.net/v1/refund';
71 $this->charge_url = 'https://api.monei.net/v1/charge';
72 $this->testmode = $this->get_option( 'testmode' );
73 $this->method_title = __( 'MONEI', 'monei' );
74 $this->notify_url = add_query_arg( 'wc-api', 'WC_Gateway_monei', home_url( '/' ) );
75 $this->notify_url_not_https = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'WC_Gateway_monei', home_url( '/' ) ) );
76 // Define user set variables.
77 $this->title = $this->get_option( 'title' );
78 $this->description = $this->get_option( 'description' );
79 $this->logo = $this->get_option( 'logo' );
80 $this->orderdo = $this->get_option( 'orderdo' );
81 $this->accountid = $this->get_option( 'accountid' );
82 $this->apikey = $this->get_option( 'apikey' );
83 $this->commercename = $this->get_option( 'commercename' );
84 $this->secret = $this->get_option( 'secret' );
85 $this->password = $this->get_option( 'password' );
86 $this->tokenization = $this->get_option( 'tokenization' );
87 $this->debug = $this->get_option( 'debug' );
88 $this->log = new WC_Logger();
89 $this->supports = array(
90 'products',
91 'tokenization',
92 'refunds',
93 );
94 $this->init_form_fields();
95 $this->init_settings();
96 // Actions.
97 add_action( 'valid_monei_standard_ipn_request', array( $this, 'successful_request' ) );
98 add_action( 'woocommerce_receipt_monei', array( $this, 'receipt_page' ) );
99 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
100 // Payment listener/API hook.
101 add_action( 'woocommerce_api_wc_gateway_' . $this->id, array( $this, 'check_ipn_response' ) );
102 if ( ! $this->is_valid_for_use() ) {
103 $this->enabled = false;
104 }
105 }
106
107 /**
108 * Check if this gateway is enabled and available in the user's country
109 *
110 * @access public
111 * @return bool
112 */
113 function is_valid_for_use() {
114
115 if ( ! in_array( get_woocommerce_currency(), array( 'EUR', 'USD', 'GBP' ), true ) ) {
116 return false;
117 } else {
118 return true;
119 }
120 }
121 /**
122 * Admin Panel Options
123 *
124 * @since 1.0.0
125 */
126 public function admin_options() {
127 ?>
128 <h3><?php esc_html_e( 'MONEI', 'monei' ); ?></h3>
129 <p><?php esc_html_e( 'Best payment gateway rates. The perfect solution to manage your digital payments.', 'monei' ); ?></p>
130 <?php if ( $this->is_valid_for_use() ) : ?>
131 <table class="form-table">
132 <?php
133 // Generate the HTML For the settings form.
134 $this->generate_settings_html();
135 ?>
136 </table><!--/.form-table-->
137 <?php else : ?>
138 <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>
139 <?php
140 endif;
141 }
142 /**
143 * Initialise Gateway Settings Form Fields
144 *
145 * @access public
146 * @return void
147 */
148 function init_form_fields() {
149 $this->form_fields = array(
150 'enabled' => array(
151 'title' => __( 'Enable/Disable', 'monei' ),
152 'type' => 'checkbox',
153 'label' => __( 'Enable MONEI', 'monei' ),
154 'default' => 'no',
155 ),
156 'title' => array(
157 'title' => __( 'Title', 'monei' ),
158 'type' => 'text',
159 'description' => __( 'This controls the title which the user sees during checkout.', 'monei' ),
160 'default' => __( 'MONEI', 'monei' ),
161 'desc_tip' => true,
162 ),
163 'description' => array(
164 'title' => __( 'Description', 'monei' ),
165 'type' => 'textarea',
166 'description' => __( 'This controls the description which the user sees during checkout.', 'monei' ),
167 'default' => __( 'Pay via MONEI; you can pay with your credit card.', 'monei' ),
168 ),
169 'logo' => array(
170 'title' => __( 'Logo', 'monei' ),
171 'type' => 'text',
172 'description' => __( 'Add link to image logo.', 'monei' ),
173 'desc_tip' => true,
174 ),
175 'commercename' => array(
176 'title' => __( 'Shop Name', 'monei' ),
177 'type' => 'text',
178 'description' => __( 'Shop Name', 'monei' ),
179 'desc_tip' => true,
180 ),
181 'accountid' => array(
182 'title' => __( 'Account ID', 'monei' ),
183 'type' => 'text',
184 'description' => __( 'Account ID', 'monei' ),
185 'desc_tip' => true,
186 ),
187 'apikey' => array(
188 'title' => __( 'API Key', 'monei' ),
189 'type' => 'text',
190 'description' => __( 'API Key', 'monei' ),
191 ),
192 'password' => array(
193 'title' => __( 'Password', 'monei' ),
194 'type' => 'text',
195 'description' => __( 'MONEI Password', 'monei' ),
196 'desc_tip' => true,
197 ),
198 'tokenization' => array(
199 'title' => __( 'Enable/Disable', 'monei' ),
200 'type' => 'checkbox',
201 'label' => __( 'Enable Tokenization', 'monei' ),
202 'default' => 'no',
203 ),
204 'orderdo' => array(
205 'title' => __( 'What to do after payment?', 'monei' ),
206 'type' => 'select',
207 'description' => __( 'Chose what to do after the customer pay the order.', 'monei' ),
208 'default' => 'processing',
209 'options' => array(
210 'processing' => __( 'Mark as Processing (default & recomended)', 'monei' ),
211 'completed' => __( 'Mark as Complete', 'monei' ),
212 ),
213 ),
214 'testmode' => array(
215 'title' => __( 'Running in test mode', 'monei' ),
216 'type' => 'checkbox',
217 'label' => __( 'Running in test mode', 'monei' ),
218 'default' => 'yes',
219 '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' ) ),
220 ),
221 'debug' => array(
222 'title' => __( 'Debug Log', 'monei' ),
223 'type' => 'checkbox',
224 'label' => __( 'Enable logging', 'monei' ),
225 'default' => 'no',
226 'description' => __( 'Log MONEY events, such as notifications requests, inside <code>WooCommerce > Status > Logs > Select MONEI Logs</code>', 'monei' ),
227 ),
228 );
229 }
230
231 function test_mode() {
232 if ( 'yes' === $this->testmode ) {
233 $test = 'true';
234 } else {
235 $test = 'false';
236 }
237 return $test;
238 }
239
240 function amount_format( $total ) {
241 $order_total_sign = number_format( $total, 2, '', '' );
242 return $order_total_sign;
243 }
244
245 function get_monei_args( $order ) {
246 global $woocommerce;
247
248 $order_id = $order->get_id();
249 $url_challenge = get_transient( 'monei_url_challenge_' . sanitize_title( $order_id ) );
250 $param_md = get_transient( 'monei_param_md_challenge_' . sanitize_title( $order_id ) );
251 $param_pareq = get_transient( 'monei_param_pareq_challenge_' . sanitize_title( $order_id ) );
252 $param_termurl = get_transient( 'monei_param_termurl_challenge_' . sanitize_title( $$order_id ) );
253
254 if ( $url_challenge ) {
255
256 $monei_args = array();
257
258 } else {
259
260 $currency = get_woocommerce_currency();
261 $account_id = $this->accountid;
262 $transaction_id = str_pad( $order_id, 12, '0', STR_PAD_LEFT );
263 $transaction_id1 = wp_rand( 1, 999 ); // lets to create a random number.
264 $transaction_id2 = substr_replace( $transaction_id, $transaction_id1, 0, -9 ); // new order number.
265 $amount = $order->get_total();
266 $country = new WC_Countries();
267 $shop_country = $country->get_base_country();
268 $shop_name = $this->commercename;
269 $url_callback = $this->notify_url;
270 $url_cancel = html_entity_decode( $order->get_cancel_order_url() );
271 $url_complete = utf8_encode( add_query_arg( 'utm_nooverride', '1', $this->get_return_url( $order ) ) );
272 $transaction_type = 'sale';
273 $password = $this->password;
274 $test = $this->test_mode();
275
276 $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;
277
278 $sign = hash_hmac('sha256', $message, $password );
279
280 if ( 'yes' === $this->debug ) {
281 $this->log->add( 'monei', 'Generating payment form for order ' . $order->get_order_number() );
282 $this->log->add( 'monei', 'Helping to understand the encrypted code: ' );
283 $this->log->add( 'monei', 'account_id: ' . $account_id );
284 $this->log->add( 'monei', 'amount: ' . $amount );
285 $this->log->add( 'monei', 'currency: ' . $currency );
286 $this->log->add( 'monei', 'order_id: ' . $transaction_id2 );
287 $this->log->add( 'monei', 'shop_name: ' . $shop_name );
288 $this->log->add( 'monei', 'test: ' . $test );
289 $this->log->add( 'monei', 'url_callback: ' . $url_callback );
290 $this->log->add( 'monei', 'url_cancel: ' . $url_cancel );
291 $this->log->add( 'monei', 'url_complete: ' . $url_complete );
292 $this->log->add( 'monei', 'Password: ' . $password );
293 $this->log->add( 'monei', 'Shop country: ' . $shop_country );
294 $this->log->add( 'monei', 'concatenated: ' . $message );
295 $this->log->add( 'monei', 'sign: ' . $sign );
296 }
297 $monei_args = array(
298 'account_id' => $account_id,
299 'amount' => $amount,
300 'currency' => $currency,
301 'order_id' => $transaction_id2,
302 'shop_name' => $shop_name,
303 'test' => $test,
304 'transaction_type' => $transaction_type,
305 'url_callback' => $url_callback,
306 'url_cancel' => $url_cancel,
307 'url_complete' => $url_complete,
308 'signature' => $sign,
309 );
310 }
311 $monei_args = apply_filters( 'woocommerce_monei_args', $monei_args );
312 return $monei_args;
313 }
314
315 /**
316 * Generate the monei form
317 *
318 * @access public
319 * @param mixed $order_id
320 * @return string
321 */
322 function generate_monei_form( $order_id ) {
323 global $woocommerce;
324
325 $order = new WC_Order( $order_id );
326 $monei_args = $this->get_monei_args( $order );
327 $form_inputs = '';
328 $url_challenge = get_transient( 'monei_url_challenge_' . sanitize_title( $order_id ) );
329 if ( $url_challenge ) {
330 $monei_adr = $url_challenge;
331 } else {
332 $monei_adr = $this->liveurl . '?';
333 }
334
335 foreach ( $monei_args as $key => $value ) {
336 $form_inputs .= '<input type="hidden" name="' . $key . '" value="' . esc_attr( $value ) . '" />';
337 }
338 wc_enqueue_js( '
339 $("body").block({
340 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' ) . '",
341 overlayCSS:
342 {
343 background: "#fff",
344 opacity: 1.0
345 },
346 css: {
347 padding: 20,
348 textAlign: "center",
349 color: "#555",
350 border: "3px solid #aaa",
351 backgroundColor:"#fff",
352 cursor: "wait",
353 lineHeight: "32px"
354 }
355 });
356 jQuery("#submit_monei_payment_form").click();
357 ' );
358 return '<form action="' . esc_url( $monei_adr ) . '" method="post" id="monei_payment_form" target="_top">
359 ' . $form_inputs . '
360 <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>
361 </form>';
362 }
363
364 function get_monei_users_token() {
365 $customer_token = null;
366 if ( is_user_logged_in() ) {
367 $tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id(), 'monei' );
368 foreach ( $tokens as $token ) {
369 if ( $token->get_gateway_id() === 'monei' ) {
370 $customer_token = $token->get_token();
371 }
372 }
373 }
374 return $customer_token;
375 }
376
377 function get_users_token_bulk( $user_id ) {
378 $customer_token = null;
379 $tokens = WC_Payment_Tokens::get_customer_tokens( $user_id, 'monei' );
380 foreach ( $tokens as $token ) {
381 if ( $token->get_gateway_id() === 'monei' ) {
382 $customer_token = $token->get_token();
383 }
384 }
385 return $customer_token;
386 }
387 /**
388 * Process the payment and return the result
389 *
390 * @access public
391 * @param int $order_id
392 * @return array
393 */
394 function process_payment( $order_id ) {
395
396 $order = new WC_Order( $order_id );
397 $transaction_type = 'sale';
398 $currency = get_woocommerce_currency();
399 $order_id = $order->get_id();
400 $user_id = $order->get_user_id();
401 $currency = get_woocommerce_currency();
402 $account_id = $this->accountid;
403 $transaction_id = str_pad( $order_id, 12, '0', STR_PAD_LEFT );
404 $transaction_id1 = wp_rand( 1, 999 ); // lets to create a random number.
405 $transaction_id2 = substr_replace( $transaction_id, $transaction_id1, 0, -9 ); // new order number.
406 $shop_name = $this->commercename;
407 $test = $this->test_mode();
408 $url_callback = $this->notify_url;
409 $url_cancel = html_entity_decode( $order->get_cancel_order_url() );
410 $url_complete = utf8_encode( add_query_arg( 'utm_nooverride', '1', $this->get_return_url( $order ) ) );
411 $userip = WC_Geolocation::get_ip_address();
412 $useragent = wc_get_user_agent();
413 $monei_adr = $this->charge_url;
414 $amount = $this->amount_format( $order->get_total() );
415 $apikey = $this->apikey;
416 $customer_emial = $order->billing_email;
417 $token = false;
418 $token_post_id = false;
419
420 if ( isset( $_POST['moneitoken'] ) ) {
421 $token_post_id = sanitize_text_field( $_POST['moneitoken'] );
422 }
423
424 if ( $token_post_id && ( 'no' !== $token_post_id && 'yes' !== $token_post_id ) ) {
425 $token_ob = WC_Payment_Tokens::get( $token_post_id );
426 $token = $token_ob->get_token();
427 }
428
429 if ( 'yes' === $this->debug ) {
430 $this->log->add( 'monei', '$token_post_id: ' . $token_post_id );
431 $this->log->add( 'monei', '$token: ' . $token );
432 //$this->log->add( 'monei', '$token_ob: ' . print_r( $token_ob, true ) );
433 }
434
435 if ( 'yes' === $this->tokenization && 'yes' === $token_post_id ) {
436 update_post_meta( $order_id, 'get_token', 'yes' );
437 $get_token = get_post_meta( $order_id, 'get_token', true );
438 if ( 'yes' === $this->debug ) {
439 $this->log->add( 'monei', '$get_token: ' . $get_token );
440 }
441 $body = array(
442 'amount' => $amount,
443 'currency' => $currency,
444 'orderId' => $transaction_id2,
445 'description' => 'Descripcion del pedido',
446 'customer' => array(
447 'email' => $customer_emial,
448 ),
449 'callbackUrl' => $url_callback,
450 'completeUrl' => $url_complete,
451 'cancelUrl' => $url_cancel,
452 'generatePaymentToken' => 'true',
453 );
454 } elseif ( 'yes' === $this->tokenization && $token ) {
455 $body = array(
456 'amount' => $amount,
457 'currency' => $currency,
458 'orderId' => $transaction_id2,
459 'description' => 'Descripcion del pedido',
460 'customer' => array(
461 'email' => $customer_emial,
462 ),
463 'callbackUrl' => $url_callback,
464 'completeUrl' => $url_complete,
465 'cancelUrl' => $url_cancel,
466 'paymentToken' => $token,
467 );
468 } else {
469 update_post_meta( $order_id, 'get_token', 'no' );
470 $get_token = get_post_meta( $order_id, 'get_token', true );
471 if ( 'yes' === $this->debug ) {
472 $this->log->add( 'monei', '$get_token: ' . $get_token );
473 }
474 $body = array(
475 'amount' => $amount,
476 'currency' => $currency,
477 'orderId' => $transaction_id2,
478 'description' => 'Descripcion del pedido',
479 'customer' => array(
480 'email' => $customer_emial,
481 ),
482 'callbackUrl' => $url_callback,
483 'completeUrl' => $url_complete,
484 'cancelUrl' => $url_cancel,
485 );
486 }
487
488 if ( 'yes' === $this->debug ) {
489 $this->log->add( 'monei', '$body: ' . print_r( json_decode( $body ), true ) );
490 }
491
492 $data_string = json_encode( $body );
493 $options = array(
494 'headers' => array(
495 'Content-Type' => 'application/json',
496 'Authorization' => $apikey,
497 ),
498 'body' => $data_string,
499 );
500 if ( 'yes' === $this->debug ) {
501 $this->log->add( 'monei', print_r( $options, true ) );
502 }
503 $monei_adr = 'https://api.monei.net/v1/payments';
504 $response = wp_remote_post( $monei_adr, $options );
505 $response_code = wp_remote_retrieve_response_code( $response );
506 $response_body = wp_remote_retrieve_body( $response );
507 $result = json_decode( $response_body );
508 $urlchallenge = $result->redirect_url;
509 $refultmonei = $result->result;
510 $status = $result->status;
511 $authorizationCode = $result->authorizationCode;
512 $id = $result->id;
513
514 if ( 'yes' === $this->debug ) {
515 $this->log->add( 'monei', '$response_body: ' . print_r( $response, true ) );
516 $this->log->add( 'monei', 'URL: ' . print_r( $result, true ) );
517 $this->log->add( 'monei', '/*************************/');
518 $this->log->add( 'monei', ' Get URL To redirect ');
519 $this->log->add( 'monei', '/*************************/');
520 $this->log->add( 'monei', '$response_body: ' . $response_body );
521 $this->log->add( 'monei', 'URL: ' . $result->nextAction->redirectUrl );
522 $this->log->add( 'monei', '$status: ' . $status );
523 $this->log->add( 'monei', '$authorizationCode: ' . $authorizationCode );
524 $this->log->add( 'monei', '$id: ' . $id );
525 }
526
527 return array(
528 'result' => 'success',
529 'redirect' => $result->nextAction->redirectUrl,
530 );
531 }
532 /**
533 * Output for the order received page.
534 *
535 * @access public
536 * @return void
537 */
538 function receipt_page( $order ) {
539 echo '<p>' . esc_html__( 'Thank you for your order, please click the button below to pay with Credit Card via MONEI.', 'monei' ) . '</p>';
540 echo $this->generate_monei_form( $order );
541 }
542
543 /**
544 * Check for Monei HTTP Notification
545 *
546 * @access public
547 * @return void
548 */
549 function check_ipn_response() {
550 if ( 'yes' === $this->debug ) {
551 $this->log->add( 'monei', ' ' );
552 $this->log->add( 'monei', '/****************************/' );
553 $this->log->add( 'monei', ' check_ipn_response ' );
554 $this->log->add( 'monei', '/****************************/' );
555 $this->log->add( 'monei', ' ' );
556 }
557 @ob_clean();
558 $json = file_get_contents( 'php://input' );
559 $data = json_decode( $json );
560 $result = $data->status;
561 if ( 'yes' === $this->debug ) {
562 $this->log->add( 'monei', $json );
563 $this->log->add( 'monei', '$result: ' . $result );
564 }
565
566 if ( 'SUCCEEDED' === $result ) {
567 header( 'HTTP/1.1 200 OK' );
568 do_action( 'valid_monei_standard_ipn_request', $data );
569 } else {
570 wp_die( 'MONEI Notification Request Failure' );
571 }
572 }
573 /**
574 * Successful Payment!
575 *
576 * @access public
577 * @param array $posted
578 * @return void
579 */
580 function successful_request( $data ) {
581 global $woocommerce;
582
583 $monei_order_id = sanitize_text_field( $data->id );
584 $order_id = sanitize_text_field( $data->orderId );
585 $message = sanitize_text_field( $data->message );
586 $order2 = substr( $order_id, 3 ); // cojo los 9 digitos del final.
587 $order = $this->get_monei_order( (int) $order2 );
588 $status = sanitize_text_field( $data->status );
589 $amount = floatval( $data->amount ) / 100;
590 $json = file_get_contents( 'php://input' );
591 $data = json_decode( $json );
592
593 if ( 'yes' === $this->debug ) {
594 $this->log->add( 'monei', '$monei_order_id: ' . $monei_order_id );
595 $this->log->add( 'monei', '$order_id: ' . $order_id );
596 $this->log->add( 'monei', '$status: ' . $status );
597 $this->log->add( 'monei', '$message: ' . $message );
598 }
599
600 if ( 'SUCCEEDED' === $status ) {
601 // authorized.
602 $order2 = substr( $order_id, 3 ); //cojo los 9 digitos del final
603 $order = new WC_Order( $order2 );
604 $amountwoo = floatval($order->get_total());
605
606 if ( $amountwoo !== $amount ) {
607 // amount does not match.
608 if ( 'yes' === $this->debug ) {
609 $this->log->add( 'monei', 'Payment error: Amounts do not match (order: ' . $amountwoo . ' - received: ' . $amount . ')' );
610 }
611 // Put this order on-hold for manual checking.
612 /* translators: order an received are the amount */
613 $order->update_status( 'on-hold', sprintf( __( 'Validation error: Order vs. Notification amounts do not match (order: %1$s - received: %2&s).', 'monei' ), $amountwoo, $amount ) );
614 exit;
615 }
616
617 if ( ! empty( $monei_order_id ) ) {
618 update_post_meta( $order->get_id(), '_payment_order_number_monei', $monei_order_id );
619 }
620
621 if ( ! empty( $order_id ) ) {
622 update_post_meta( $order->get_id(), '_payment_wc_order_id_monei', $order_id );
623 }
624
625 // Payment completed.
626 $order->add_order_note( __( 'HTTP Notification received - payment completed', 'monei' ) );
627 $order->add_order_note( __( 'MONEI Order Number: ', 'monei' ) . $monei_order_id );
628 $order->payment_complete();
629 if ( 'completed' === $this->orderdo ) {
630 $order->update_status( 'completed', __( 'Order Completed by MONEI', 'monei' ) );
631 }
632
633 $get_token = get_post_meta( $order->get_id(), 'get_token', true );
634
635 if ( 'yes' === $this->debug ) {
636 $this->log->add( 'monei', '$get_token: ' . $get_token );
637 }
638
639 if ( 'yes' === $get_token ) {
640 $monei = new Monei\MoneiClient( $this->apikey );
641 $data_payment = $monei->payments->get( $monei_order_id );
642
643 $data_array = json_decode( $data_payment );
644
645 if ( isset( $data_array->paymentToken ) ) {
646 if ( 'yes' === $this->debug ) {
647 $this->log->add( 'monei', '$token: ' . $data_array->paymentToken );
648 $this->log->add( 'monei', '$brand: ' .$data->paymentMethod->card->brand );
649 $this->log->add( 'monei', '$lastfour: ' . $data->paymentMethod->card->last4 );
650 }
651 $token_n = $data_array->paymentToken;
652 $brand = $data->paymentMethod->card->brand;
653 $lastfour = $data->paymentMethod->card->last4;
654 $token = new WC_Payment_Token_CC();
655 $token->set_token( $token_n );
656 $token->set_gateway_id( 'monei' );
657 $token->set_user_id( $order->get_user_id() );
658 $token->set_card_type( $brand );
659 $token->set_last4( $lastfour );
660 $token->set_expiry_month( '12' );
661 $token->set_expiry_year( '2040' );
662 $token->set_default( true );
663 $token->save();
664 }
665 }
666
667 if ( 'yes' === $this->debug ) {
668 $this->log->add( 'monei', '$data_payment: ' . $data_payment );
669 }
670
671 if ( 'yes' === $this->debug ) {
672 $this->log->add( 'monei', 'Payment complete.' );
673 }
674 } else {
675 // Tarjeta caducada.
676 if ( 'yes' === $this->debug ) {
677 $this->log->add( 'monei', 'Order cancelled by MONEI: ' . $message );
678 }
679 // Order cancelled.
680 $order->update_status( 'cancelled', 'Cancelled by MONEI: ' . $message);
681 $order->add_order_note( 'Order cancelled by MONEI: ' . $message );
682 WC()->cart->empty_cart();
683 }
684 }
685
686 /**
687 * get_monei_order function.
688 *
689 * @access public
690 * @param mixed $order_id
691 * @return void
692 */
693 function get_monei_order( $order_id ) {
694 $order = new WC_Order( $order_id );
695 return $order;
696 }
697
698 function payment_fields() {
699
700 if ( is_user_logged_in() && 'yes' === $this->tokenization ) {
701 $user_id = get_current_user_id();
702 $tokens = WC_Payment_Tokens::get_customer_tokens( $user_id, $this->id );
703 if ( ! empty( $tokens ) ) {
704 echo '<h4>Select a Credit Card</h4>';
705 echo '<div class="credit-cards-monei">';
706 foreach ( $tokens as $token ) {
707 $is_default = $token->is_default();
708 if ( $is_default ) {
709 $checked = 'checked="checked"';
710 } else {
711 $checked = '';
712 }
713 echo '<div class="moneicreditcards">';
714 echo '<input id="' . $token->get_id() . '" name="moneitoken" type="radio" ' . $checked . ' value="' . $token->get_id() . '"/>';
715 echo '<label for="' . $token->get_id() . '">' . $token->get_card_type() . ' ended in ' . $token->get_last4() . ' ' . $token->get_expiry_month() . '/' . $token->get_expiry_year() . '</label>';
716 echo '</div>';
717 continue;
718 }
719 echo '<div class="moneicreditcards">';
720 echo '<input id="yes" name="moneitoken" type="radio" value="yes"/>';
721 echo '<label for="yes">Add new Credit Card</label>';
722 echo '</div>';
723 echo '<div class="moneicreditcards">';
724 echo '<input id="no" name="moneitoken" type="radio" value="no"/>';
725 echo '<label for="no">Do not use any Credit Card</label>';
726 echo '</div>';
727 echo '</div>';
728 } else {
729 echo '<div class="credit-cards-monei">
730 <h4>Do we save your credit card?</h4>
731 <p>We won\'t keep your card, we\'ll keep a token that MONEI will provide. It\'s totally safe.</p>
732 <div class="moneicreditcards">
733 <input id="yes" name="moneitoken" type="radio" value="yes"/>
734 <label for="yes">Yes</label>
735 </div>
736 <div class="moneicreditcards">
737 <input id="no" name="moneitoken" type="radio" value="no"/>
738 <label for="no">No</label>
739 </div>
740 </div>';
741 }
742 } else {
743 echo $this->description;
744 }
745 }
746
747 /**
748 * Refund
749 **/
750
751 function ask_for_refund( $order_id, $transaction_id, $amount ) {
752
753 //post code to MONEI
754 $order2 = get_post_meta( $order_id, '_payment_wc_order_id_monei', true );
755 $monei_order_number = $transaction_id;
756 $currency_codes = get_woocommerce_currency();
757 $account_id = $this->accountid;
758 $test = $this->test_mode();
759 $transaction_type = 'refund';
760 $shop_name = $this->commercename;
761 $password = $this->password;
762 $country = new WC_Countries();
763 $shop_country = $country->get_base_country();
764 $monei_adr = $this->refund_url;
765
766 $message = 'account_id' . $account_id .
767 'amount' . $amount .
768 'currency' . $currency_codes .
769 'monei_order_id' . $monei_order_number .
770 'order_id' . $order2 .
771 'shop_name' . $shop_name .
772 'test' . $test .
773 'transaction_type' . $transaction_type;
774
775 $sign = hash_hmac('sha256', $message, $password );
776
777 if ( 'yes' === $this->debug ) {
778 $this->log->add( 'monei', ' ' );
779 $this->log->add( 'monei', '$message ' . $message );
780 $this->log->add( 'monei', '$password ' . $password );
781 $this->log->add( 'monei', '$sign: ' . $sign );
782 $this->log->add( 'monei', __( 'Order Number MONEI : ', 'monei' ) . $monei_order_number );
783 }
784
785 if ( 'yes' === $this->debug ) {
786 $this->log->add( 'monei', ' ' );
787 $this->log->add( 'monei', ' ' );
788 $this->log->add( 'monei', '/**************************/' );
789 $this->log->add( 'monei', __( 'Starting asking for Refund', 'monei' ) );
790 $this->log->add( 'monei', '/**************************/' );
791 $this->log->add( 'monei', ' ' );
792 }
793
794 if ( 'yes' === $this->debug ) {
795 $this->log->add( 'monei', ' ' );
796 $this->log->add( 'monei', __( 'All data from meta', 'monei' ) );
797 $this->log->add( 'monei', '**********************' );
798 $this->log->add( 'monei', ' ' );
799 $this->log->add( 'monei', __( 'If something is empty, the data was not saved', 'monei' ) );
800 $this->log->add( 'monei', ' ' );
801 $this->log->add( 'monei', __( 'All data from meta', 'monei' ) );
802 $this->log->add( 'monei', __( 'Order Number MONEI : ', 'monei' ) . $monei_order_number );
803 }
804
805 if ( 'yes' === $this->debug ) {
806 $this->log->add( 'monei', ' ' );
807 $this->log->add( 'monei', __( 'Data sent to MONEI for refund', 'monei' ) );
808 $this->log->add( 'monei', '*********************************' );
809 $this->log->add( 'monei', ' ' );
810 $this->log->add( 'monei', __( 'URL to MONEI : ', 'monei' ) . $monei_adr );
811 $this->log->add( 'monei', __( 'account_id : ', 'monei' ) . $account_id );
812 $this->log->add( 'monei', __( 'amount : ', 'monei' ) . $amount );
813 $this->log->add( 'monei', __( 'currency : ', 'monei' ) . $currency_codes );
814 $this->log->add( 'monei', __( 'order_id : ', 'monei' ) . $order2 );
815 $this->log->add( 'monei', __( 'monei_order_id : ', 'monei' ) . $monei_order_number );
816 $this->log->add( 'monei', __( 'signature : ', 'monei' ) . $sign );
817 $this->log->add( 'monei', __( 'test : ', 'monei' ) . $test );
818 $this->log->add( 'monei', __( 'transaction_type : refund', 'monei' ) );
819 $this->log->add( 'monei', __( 'ask_for_refund Asking for order #: ', 'monei' ) . $order_id );
820 $this->log->add( 'monei', ' ' );
821 }
822
823 $body = array(
824 'account_id' => $account_id,
825 'amount' => $amount,
826 'currency' => $currency_codes,
827 'monei_order_id' => $monei_order_number,
828 'order_id' => $order2,
829 'shop_name' => $shop_name,
830 'signature' => $sign,
831 'test' => $test,
832 'transaction_type' => 'refund',
833 );
834
835 $data_string = json_encode( $body );
836
837 $options = array(
838 'headers' => array(
839 'Content-Type' => 'application/json',
840 ),
841 'body' => $data_string,
842 );
843
844 $response = wp_remote_post( $monei_adr, $options );
845 $response_code = wp_remote_retrieve_response_code( $response );
846 $response_body = wp_remote_retrieve_body( $response );
847
848 if ( is_wp_error( $response ) ) {
849 $error_string = $response->get_error_message();
850 if ( 'yes' === $this->debug ) {
851 $this->log->add( 'monei', ' ' );
852 $this->log->add( 'monei', __( 'There is an error', 'monei' ) );
853 $this->log->add( 'monei', '*********************************' );
854 $this->log->add( 'monei', ' ' );
855 $this->log->add( 'monei', __( 'The error is : ', 'monei' ) . $error_string );
856 }
857 return $error_string;
858 }
859
860 $result = json_decode( $response_body );
861 $end = $result->result;
862
863 if ( 'yes' === $this->debug ) {
864 $this->log->add( 'monei', ' ' );
865 $this->log->add( 'monei', '$result: ' . $end );
866 $this->log->add( 'monei', ' ' );
867 }
868
869 if ( 'completed' === $end ) {
870 return true;
871 } else {
872 return $end;
873 }
874 }
875
876 public function process_refund( $order_id, $amount = null, $reason = '' ) {
877
878 // Do your refund here. Refund $amount for the order with ID $order_id _transaction_id
879 set_time_limit( 0 );
880 $order = $this->get_monei_order( $order_id );
881 $order2 = get_post_meta( $order_id, '_payment_wc_order_id_monei', true );
882 $monei_order_number = get_post_meta( $order_id, '_payment_order_number_monei', true );
883
884
885 if ( ! $amount ) {
886 $order_total_sign = $order->get_total();
887 } else {
888 $order_total_sign = $amount;
889 }
890
891 if ( ! empty( $order2 ) ) {
892 if ( 'yes' === $this->debug ) {
893 $this->log->add( 'monei', ' ' );
894 $this->log->add( 'monei', '/****************************/' );
895 $this->log->add( 'monei', ' Once upon a time ' );
896 $this->log->add( 'monei', '/****************************/' );
897 $this->log->add( 'monei', ' ' );
898 $this->log->add( 'monei', __( 'check_monei_refund Asking for order #: ', 'monei' ) . $order_id );
899 }
900
901 $refund_asked = $this->ask_for_refund( $order_id, $monei_order_number, $order_total_sign );
902
903 if ( $refund_asked ) {
904 if ( 'yes' === $this->debug && $result ) {
905 $this->log->add( 'monei', __( 'check_monei_refund = true ', 'monei' ) );
906 $this->log->add( 'monei', ' ' );
907 $this->log->add( 'monei', '/********************************/' );
908 $this->log->add( 'monei', ' Refund complete by MONEI ' );
909 $this->log->add( 'monei', '/********************************/' );
910 $this->log->add( 'monei', ' ' );
911 $this->log->add( 'monei', '/******************************************/' );
912 $this->log->add( 'monei', ' The final has come, this story has ended ' );
913 $this->log->add( 'monei', '/******************************************/' );
914 $this->log->add( 'monei', ' ' );
915 }
916 return true;
917 } else {
918 if ( is_wp_error( $refund_asked ) ) {
919 if ( 'yes' === $this->debug ) {
920 $this->log->add( 'monei', __( 'Refund Failed: ', 'monei' ) . $refund_asked->get_error_message() );
921 }
922 return new WP_Error( 'error', $refund_asked->get_error_message() );
923 }
924 }
925
926 if ( is_wp_error( $refund_asked ) ) {
927 if ( 'yes' === $this->debug ) {
928 $this->log->add( 'monei', __( 'Refund Failed: ', 'monei' ) . $refund_asked->get_error_message() );
929 }
930 return new WP_Error( 'error', $refund_asked->get_error_message() );
931 }
932 } else {
933 if ( 'yes' === $this->debug && $result ) {
934 $this->log->add( 'monei', __( 'Refund Failed: No transaction ID', 'monei' ) );
935 }
936 return new WP_Error( 'monei', __( 'Refund Failed: No transaction ID', 'monei' ) );
937 }
938 }
939 }
940
941 function monei_add_notice_new_version() {
942
943 $version = get_option( 'hide-new-version-monei-notice' );
944
945 if ( ! $version ) {
946 if ( isset( $_REQUEST['monei-hide-new-version'] ) && 'hide-new-version-monei' === $_REQUEST['monei-hide-new-version'] ) {
947 $nonce = sanitize_text_field( $_REQUEST['_monei_hide_new_version_nonce'] );
948 if ( wp_verify_nonce( $nonce, 'monei_hide_new_version_nonce' ) ) {
949 update_option( 'hide-new-version-monei-notice', MONEI_VERSION );
950 }
951 } else {
952 ?>
953 <div id="message" class="updated woocommerce-message woocommerce-monei-messages">
954 <div class="contenido-monei-notice">
955 <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>
956 <p>
957 <h3>
958 <?php esc_html_e( 'Thank you for install MONEI for WooCommerce. Version: ', 'monei' ) . ' ' . esc_html_e( MONEI_VERSION ); ?>
959 </h3>
960 </p>
961 <p>
962 <?php esc_html_e( 'The best payment gateway rates. The perfect solution to manage your digital payments.,', 'monei' ); ?>
963 </p>
964 <p class="submit">
965 <a href="<?php esc_html_e( MONEI_SIGNUP ); ?>" class="button-primary" target="_blank"><?php esc_html_e( 'Signup', 'monei' ); ?></a>
966 <a href="<?php esc_html_e( MONEI_WEB ); ?>" class="button-primary" target="_blank"><?php esc_html_e( 'MONEI website', 'monei' ); ?></a>
967 <a href="<?php esc_html_e( MONEI_REVIEW ); ?>" class="button-primary" target="_blank"><?php esc_html_e( 'Leave a review', 'monei' ); ?></a>
968 <a href="<?php esc_html_e( MONEI_SUPPORT ); ?>" class="button-primary" target="_blank"><?php esc_html_e( 'Support', 'monei' ); ?></a>
969 </p>
970 </div>
971 </div>
972 <?php }
973 }
974 }
975 add_action( 'admin_notices', 'monei_add_notice_new_version' );
976
977 function monei_notice_style() {
978 wp_register_style( 'monei_notice_css', MONEI_PLUGIN_URL . 'assets/css/monei-notice.css', false, MONEI_VERSION );
979 wp_enqueue_style( 'monei_notice_css' );
980 }
981 add_action( 'admin_enqueue_scripts', 'monei_notice_style' );
982
983 function monei_style_checkout() {
984 wp_register_style( 'monei_checkput_css', MONEI_PLUGIN_URL . 'assets/css/monei-checkout-card.css', false, MONEI_VERSION );
985 wp_enqueue_style( 'monei_checkput_css' );
986 }
987 //add_action( 'wp_enqueue_scripts', 'monei_style_checkout' );
988
989 function woocommerce_add_gateway_monei_gateway( $methods ) {
990 $methods[] = 'WC_Gateway_monei';
991 return $methods;
992 }
993 add_filter( 'woocommerce_payment_gateways', 'woocommerce_add_gateway_monei_gateway' );
994
995 function add_monei_meta_box() {
996 $date_decoded = get_post_meta( get_the_ID(), '_payment_date_monei', true );
997 $hour_decoded = get_post_meta( get_the_ID(), '_payment_hour_monei', true );
998 echo '<h4>' . esc_html__( 'Payment Details', 'monei' ) . '</h4>';
999 echo '<p><strong>' . esc_html__( 'MONEI Date', 'monei' ) . ': </strong><br />' . esc_html( $date_decoded ) . '</p>';
1000 echo '<p><strong>' . esc_html__( 'MONEI Hour', 'monei' ) . ': </strong><br />' . esc_html( $hour_decoded ) . '</p>';
1001 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>';
1002 }
1003 add_action( 'woocommerce_admin_order_data_after_billing_address', 'add_monei_meta_box' );
1004 }
1005