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

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