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

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

1,010 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 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.1.0
15 * Author: MONEI
16 * Author URI: https://www.monei.net/
17 * Tested up to: 5.4
18 * WC requires at least: 3.0
19 * WC tested up to: 4.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.1.0' );
28 define( 'MONEI_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
29 define( 'MONEI_SIGNUP', 'https://dashboard.monei.net/?action=signUp' );
30 define( 'MONEI_WEB', 'https://monei.net/' );
31 define( 'MONEI_REVIEW', 'https://wordpress.org/support/plugin/monei/reviews/?rate=5#new-post' );
32 define( 'MONEI_SUPPORT', 'https://support.monei.net/' );
33
34 add_action( 'plugins_loaded', 'woocommerce_gateway_monei_init', 0 );
35
36
37 function woocommerce_gateway_monei_init() {
38 if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
39 return;
40 }
41 /**
42 * Localisation
43 */
44 load_plugin_textdomain( 'monei', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
45 /**
46 * Gateway class
47 */
48 class WC_Gateway_Monei extends WC_Payment_Gateway {
49 var $notify_url;
50 /**
51 * Constructor for the gateway.
52 *
53 * @access public
54 * @return void
55 */
56 public function __construct() {
57 global $woocommerce;
58 $this->id = 'monei';
59 $logo_url = $this->get_option( 'logo' );
60 if ( ! empty( $logo_url ) ) {
61 $logo_url = $this->get_option( 'logo' );
62 $this->icon = apply_filters( 'woocommerce_monei_icon', $logo_url );
63 } else {
64 $this->icon = apply_filters( 'woocommerce_monei_icon', plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) . '/assets/images/MONEI-logo.png' );
65 }
66 $this->has_fields = 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 $currency = get_woocommerce_currency();
545 $account_id = $this->accountid;
546 $transaction_id = str_pad( $order_id, 12, '0', STR_PAD_LEFT );
547 $transaction_id1 = wp_rand( 1, 999 ); // lets to create a random number.
548 $transaction_id2 = substr_replace( $transaction_id, $transaction_id1, 0, -9 ); // new order number.
549 $shop_name = $this->commercename;
550 $test = $this->test_mode();
551 $url_callback = $this->notify_url;
552 $url_cancel = html_entity_decode( $order->get_cancel_order_url() );
553 $url_complete = utf8_encode( add_query_arg( 'utm_nooverride', '1', $this->get_return_url( $order ) ) );
554 $userip = WC_Geolocation::get_ip_address();
555 $useragent = wc_get_user_agent();
556 $monei_adr = $this->charge_url;
557 $amount = $this->amount_format( $order->get_total() );
558 $apikey = $this->apikey;
559
560 $body = array(
561 'amount' => $amount,
562 'currency' => $currency,
563 'orderId' => $transaction_id2,
564 'description' => 'Descripcion del pedido',
565 'customer' => array(
566 'email' => 'j.conti@joseconti.com',
567 ),
568 'callbackUrl' => $url_callback,
569 'completeUrl' => $url_complete,
570 'cancelUrl' => $url_cancel,
571 );
572
573 $data_string = json_encode( $body );
574 $options = array(
575 'headers' => array(
576 'Content-Type' => 'application/json',
577 'Authorization' => $apikey,
578 ),
579 'body' => $data_string,
580 );
581 if ( 'yes' === $this->debug ) {
582 $this->log->add( 'monei', print_r( $options, true ) );
583 }
584 $monei_adr = 'https://api.monei.net/v1/payments';
585 $response = wp_remote_post( $monei_adr, $options );
586 $response_code = wp_remote_retrieve_response_code( $response );
587 $response_body = wp_remote_retrieve_body( $response );
588 $result = json_decode( $response_body );
589 $urlchallenge = $result->redirect_url;
590 $refultmonei = $result->result;
591
592 if ( 'yes' === $this->debug ) {
593 $this->log->add( 'monei', '/*************************/');
594 $this->log->add( 'monei', ' Get URL To redirect ');
595 $this->log->add( 'monei', '/*************************/');
596 $this->log->add( 'monei', '$response_body: ' . $response_body );
597 $this->log->add( 'monei', 'URL: ' . $result->nextAction->redirectUrl );
598 }
599
600 return array(
601 'result' => 'success',
602 'redirect' => $result->nextAction->redirectUrl,
603 );
604 }
605 }
606 /**
607 * Output for the order received page.
608 *
609 * @access public
610 * @return void
611 */
612 function receipt_page( $order ) {
613 echo '<p>' . esc_html__( 'Thank you for your order, please click the button below to pay with Credit Card via MONEI.', 'monei' ) . '</p>';
614 echo $this->generate_monei_form( $order );
615 }
616
617 /**
618 * Check for Monei HTTP Notification
619 *
620 * @access public
621 * @return void
622 */
623 function check_ipn_response() {
624 if ( 'yes' === $this->debug ) {
625 $this->log->add( 'monei', ' ' );
626 $this->log->add( 'monei', '/****************************/' );
627 $this->log->add( 'monei', ' check_ipn_response ' );
628 $this->log->add( 'monei', '/****************************/' );
629 $this->log->add( 'monei', ' ' );
630 }
631 @ob_clean();
632 $json = file_get_contents( 'php://input' );
633 $data = json_decode( $json );
634 $result = $data->status;
635 if ( 'yes' === $this->debug ) {
636 $this->log->add( 'monei', $json );
637 $this->log->add( 'monei', '$result: ' . $result );
638 }
639
640 if ( 'SUCCEEDED' === $result ) {
641 header( 'HTTP/1.1 200 OK' );
642 do_action( 'valid_monei_standard_ipn_request', $data );
643 } else {
644 wp_die( 'MONEI Notification Request Failure' );
645 }
646 }
647 /**
648 * Successful Payment!
649 *
650 * @access public
651 * @param array $posted
652 * @return void
653 */
654 function successful_request( $data ) {
655 global $woocommerce;
656
657 $monei_order_id = sanitize_text_field( $data->id );
658 $order_id = sanitize_text_field( $data->orderId );
659 $message = sanitize_text_field( $data->message );
660 $order2 = substr( $order_id, 3 ); // cojo los 9 digitos del final.
661 $order = $this->get_monei_order( (int) $order2 );
662 $status = sanitize_text_field( $data->status );
663 $amount = floatval($data->amount)/100;
664
665 if ( 'yes' === $this->debug ) {
666 $this->log->add( 'monei', '$monei_order_id: ' . $monei_order_id );
667 $this->log->add( 'monei', '$order_id: ' . $order_id );
668 $this->log->add( 'monei', '$status: ' . $status );
669 $this->log->add( 'monei', '$message: ' . $message );
670 }
671
672 if ( 'SUCCEEDED' === $status ) {
673 // authorized.
674 $order2 = substr( $order_id, 3 ); //cojo los 9 digitos del final
675 $order = new WC_Order( $order2 );
676 $amountwoo = floatval($order->get_total());
677
678 if ( $amountwoo !== $amount ) {
679 // amount does not match.
680 if ( 'yes' === $this->debug ) {
681 $this->log->add( 'monei', 'Payment error: Amounts do not match (order: ' . $amountwoo . ' - received: ' . $amount . ')' );
682 }
683 // Put this order on-hold for manual checking.
684 /* translators: order an received are the amount */
685 $order->update_status( 'on-hold', sprintf( __( 'Validation error: Order vs. Notification amounts do not match (order: %1$s - received: %2&s).', 'monei' ), $amountwoo, $amount ) );
686 exit;
687 }
688 /*
689 $parts = explode( 'T', $timestamp );
690 $part2 = explode( '.', $parts[1] );
691 $date = $parts[0];
692 $hour = $part2[0];
693
694 if ( 'yes' === $this->debug ) {
695 $this->log->add( 'monei', 'Timestamp: ' . $timestamp );
696 $this->log->add( 'monei', 'Date: ' . $date );
697 $this->log->add( 'monei', 'Hour: ' . $hour );
698 }
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