PluginProbe
MONEI Payments for WooCommerce / 3.1.1
MONEI Payments for WooCommerce v3.1.1
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 3.1.1, at monei.php

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