PluginProbe
MONEI Payments for WooCommerce / 4.2.1
MONEI Payments for WooCommerce v4.2.1
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 5.2.4 All 86 releases
monei / monei.php

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

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