wpshop_payment   F
last analyzed

Complexity

Total Complexity 217

Size/Duplication

Total Lines 708
Duplicated Lines 10.88 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
dl 77
loc 708
rs 1.0434
c 0
b 0
f 0
wmc 217
lcom 0
cbo 8

20 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 12 5
A get_success_payment_url() 0 4 1
A get_cancel_payment_url() 0 4 1
A construct_url_parameters() 0 6 2
B wpshop_payment_result() 0 33 6
D __display_payment_methods_choice_form() 30 94 27
D display_payment_methods_choice_form() 6 30 18
D the_order_payment_is_completed() 6 28 9
D setOrderPaymentStatus() 3 41 9
B get_payment_method() 0 24 6
F display_payment_receiver_interface() 16 49 14
C partial_payment_calcul() 0 45 12
A get_payment_transaction_number() 0 10 4
A set_payment_transaction_number() 0 11 3
A save_payment_return_data() 0 8 1
D add_new_payment_to_order() 0 22 12
A get_order_waiting_payment_array_id() 0 13 4
B get_payment_transaction_number_old_way() 0 22 5
C reverify_payment_invoice_ref() 0 44 15
F check_order_payment_total_amount() 16 110 63

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like wpshop_payment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use wpshop_payment, and based on these observations, apply Extract Interface, too.

1
<?php if ( !defined( 'ABSPATH' ) ) exit;
2
3
/*	Check if file is include. No direct access possible with file url	*/
4
if ( !defined( 'WPSHOP_VERSION' ) ) {
5
	die( __('Access is not allowed by this way', 'wpshop') );
6
}
7
8
/**
9
 * Wpshop Payment Gateway
10
 *
11
 * @class 		wpshop_payment
12
 * @package		WP-Shop
13
 * @category	Payment Gateway
14
 * @author		Eoxia
15
 */
16
class wpshop_payment {
17
18
	public function __construct() {
19
		global $wpshop;
20
21
		$wpshop_paypal = new wpshop_paypal();
0 ignored issues
show
Unused Code introduced by
$wpshop_paypal is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
		// If the CIC payment method is active
23
		$wpshop_paymentMethod = get_option( 'wps_payment_mode' );
24
		if(WPSHOP_PAYMENT_METHOD_CIC && ( !empty($wpshop_paymentMethod['mode']) && !empty($wpshop_paymentMethod['mode']['cic']) && !empty($wpshop_paymentMethod['mode']['cic']['active']) ) ) {
25
			$wpshop_cic = new wpshop_CIC();
0 ignored issues
show
Unused Code introduced by
$wpshop_cic is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
		}
27
		wpshop_tools::create_custom_hook ('wpshop_bankserver_reponse');
28
29
	}
30
31
	public static function get_success_payment_url() {
32
		$url = get_permalink( wpshop_tools::get_page_id( get_option('wpshop_payment_return_page_id') ) );
33
		return self::construct_url_parameters($url, 'paymentResult', 'success');
34
	}
35
36
	public static function get_cancel_payment_url() {
37
		$url = get_permalink( wpshop_tools::get_page_id( get_option('wpshop_payment_return_nok_page_id') ) );
38
		return $url;
39
	}
40
41
	public static function construct_url_parameters($url, $param, $value) {
42
		$interoguation_marker_pos = strpos($url, '?');
43
		if($interoguation_marker_pos===false)
44
			return $url.'?'.$param.'='.$value;
45
		else return $url.'&'.$param.'='.$value;
46
	}
47
48
	/**
49
	 * Shortcode : Manage payment result
50
	 */
51
	public static function wpshop_payment_result() {
52
		global $wpdb;
53
		$user_ID = get_current_user_id();
54
		$query = $wpdb->prepare('SELECT MAX(ID) FROM ' .$wpdb->posts. ' WHERE post_type = %s AND post_author = %d', WPSHOP_NEWTYPE_IDENTIFIER_ORDER, $user_ID);
55
		$order_post_id = $wpdb->get_var( $query );
56
		if ( !empty($order_post_id) ) {
57
			$order_postmeta = get_post_meta($order_post_id , '_wpshop_order_status', true);
58
			if ( !empty($order_postmeta) ) {
59
				switch ( $order_postmeta ) {
60
					case 'awaiting_payment':
61
						echo __('We wait your payment.','wpshop');
62
					break;
63
					case 'completed':
64
						echo __('Thank you ! Your payment has been recorded.','wpshop');
65
					break;
66
					case 'partially_paid':
67
						echo __('Thank you ! Your first payment has been recorded.','wpshop');
68
					break;
69
					default:
70
						echo __('Your payment and your order has been cancelled.','wpshop');
71
					break;
72
				}
73
			}
74
		}
75
// 		if(!empty($_GET['paymentResult'])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
// 			if($_GET['paymentResult']=='success') {
77
// 				echo __('Thank you ! Your payment has been recorded.','wpshop');
78
// 			}
79
// 			elseif($_GET['paymentResult']=='cancel') {
80
// 				echo __('Your payment and your order has been cancelled.','wpshop');
81
// 			}
82
// 		}
83
	}
84
85
	/**
86
	 * Display the list of payment methods available
87
	 *
88
	 * @param integer $order_id The order id if existing - Useful when user does not finish its order and want to validateit later
89
	 * @return string The different payment method
90
	 */
91
	function __display_payment_methods_choice_form($order_id=0, $cart_type = 'cart') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
92
		$output = '';
93
		/**	Get available payment method	*/
94
		$paymentMethod = get_option('wpshop_paymentMethod', array());
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
96
		if(!empty($order_id) && is_numeric($order_id)) {
97
			$output .= '<input type="hidden" name="order_id" value="'.$order_id.'" />';
98
		}
99
100
		if ($cart_type == 'cart') {
101
			$payment_methods = array();
102 View Code Duplication
			if(!empty($paymentMethod['paypal'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
				$payment_methods['paypal'] = array('payment_method_name' => __('CB with Paypal', 'wpshop'),
104
														'payment_method_icon' => WPSHOP_TEMPLATES_URL . 'wpshop/medias/paypal.png',
105
														'payment_method_explanation' => __('<strong>Tips</strong> : If you have a Paypal account, by choosing this payment method, you will be redirected to the secure payment site Paypal to make your payment. Debit your PayPal account, immediate booking products.','wpshop')
106
								 );
107
			}
108 View Code Duplication
			if(!empty($paymentMethod['checks'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
				$payment_methods['check'] = array('payment_method_name' => __('Check', 'wpshop'),
110
						'payment_method_icon' => WPSHOP_TEMPLATES_URL . 'wpshop/medias/cheque.png',
111
						'payment_method_explanation' => __('Reservation of products upon receipt of the check.','wpshop')
112
				);
113
			}
114 View Code Duplication
			if(!empty($paymentMethod['banktransfer'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
				$payment_methods['banktransfer'] = array('payment_method_name' => __('Bank transfer', 'wpshop'),
116
						'payment_method_icon' => WPSHOP_TEMPLATES_URL . 'wpshop/medias/cheque.png',
117
						'payment_method_explanation' =>__('Reservation of product receipt of payment.','wpshop')
118
				);
119
			}
120 View Code Duplication
			if(WPSHOP_PAYMENT_METHOD_CIC || !empty($paymentMethod['cic'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
				$payment_methods['cic'] = array('payment_method_name' =>__('Credit card', 'wpshop'),
122
						'payment_method_icon' => WPSHOP_TEMPLATES_URL . 'wpshop/medias/cic_payment_logo.jpg',
123
						'payment_method_explanation' =>__('Reservation of products upon confirmation of payment.','wpshop')
124
				);
125
			}
126
			$payment_methods = apply_filters('wpshop_payment_method', $payment_methods);
127
128
			$payment_method_table = array();
129
130
			if ( !empty( $paymentMethod['display_position'] ) ) {
131
				$position_determinated = false;
132
				foreach ( $paymentMethod['display_position'] as $key => $position) {
0 ignored issues
show
Bug introduced by
The expression $paymentMethod['display_position'] of type string is not traversable.
Loading history...
133
					if ( $position != null) {
134
						$position_determinated = true;
135
					}
136
				}
137
				if ( $position_determinated ) {
138
					for ( $i = 1; $i < count( $paymentMethod['display_position'] ) + 1; $i++) {
139
						foreach ( $paymentMethod['display_position'] as $key => $position ) {
0 ignored issues
show
Bug introduced by
The expression $paymentMethod['display_position'] of type string is not traversable.
Loading history...
140
							if ( $position == $i  && !empty($paymentMethod[$key])) {
141
								if ($key == 'checks') {
142
									$key = 'check';
143
								}
144
								$payment_method_table[$key] = $payment_methods[$key];
145
							}
146
						}
147
					}
148
					$payment_methods = $payment_method_table;
149
				}
150
			}
151
			if (!empty($payment_methods) ) {
152
153
				foreach( $payment_methods as  $payment_method_identifier =>  $payment_method_def ) {
154
					$tpl_component = array();
155
					$checked = $active = '';
156
					$payment_identifier_for_test = $payment_method_identifier;
157
					if ($payment_method_identifier == 'check') {
158
						$payment_identifier_for_test = 'checks';
159
					}
160
					if ( !empty($paymentMethod['default_method']) && $paymentMethod['default_method'] == $payment_identifier_for_test) {
161
						$checked = ' checked="checked"';
162
						$active = ' active';
163
164
					}
165
					$tpl_component['CHECKOUT_PAYMENT_METHOD_STATE_CLASS'] = $active;
166
					$tpl_component['CHECKOUT_PAYMENT_METHOD_INPUT_STATE'] = $checked;
167
					$tpl_component['CHECKOUT_PAYMENT_METHOD_IDENTIFIER'] = $payment_method_identifier;
168 View Code Duplication
					if ( !empty($payment_mode['logo']) && (int)$payment_mode['logo'] != 0 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
						$tpl_component['CHECKOUT_PAYMENT_METHOD_ICON'] = ( !empty($payment_method_def['payment_method_icon']) ) ? wp_get_attachment_image( $payment_method_def['payment_method_icon'], 'thumbnail', false, array('class' => 'wps_shipping_mode_logo') ) : '';
170
					}
171
					else {
172
						$tpl_component['CHECKOUT_PAYMENT_METHOD_ICON'] = ( !empty($payment_method_def['payment_method_icon']) ) ? '<img src="' .$payment_method_def['payment_method_icon']. '" alt="" />' : '';
173
					}
174
					//$tpl_component['CHECKOUT_PAYMENT_METHOD_ICON'] = $payment_method_def['payment_method_icon'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
175
					$tpl_component['CHECKOUT_PAYMENT_METHOD_NAME'] = $payment_method_def['payment_method_name'];
176
					$tpl_component['CHECKOUT_PAYMENT_METHOD_EXPLANATION'] = $payment_method_def['payment_method_explanation'];
177
					$output .= wpshop_display::display_template_element('wpshop_checkout_page_payment_method_bloc', $tpl_component, array('type' => 'payment_method', 'id' => $payment_method_identifier));
178
					unset($tpl_component);
179
				}
180
			}
181
		}
182
183
		return array( $output, $paymentMethod['mode'] );
184
	}
185
186
187
	public static function display_payment_methods_choice_form($order_id=0, $cart_type = 'cart') {
188
		$payment_option = get_option( 'wps_payment_mode' );
189
		$output = '';
190
		if(!empty($order_id) && is_numeric($order_id)) {
191
			$output .= '<input type="hidden" name="order_id" value="'.$order_id.'" />';
192
		}
193
		if( $cart_type == 'cart' ) {
194
			if ( !empty($payment_option) && !empty($payment_option['mode']) ) {
195
				foreach( $payment_option['mode'] as $payment_id => $payment_config ) {
0 ignored issues
show
Bug introduced by
The expression $payment_option['mode'] of type string is not traversable.
Loading history...
196
					if( !empty($payment_config['active']) ) {
197
						$tpl_component['CHECKOUT_PAYMENT_METHOD_STATE_CLASS'] = ( ( !empty($payment_option['default_choice']) && $payment_option['default_choice'] == $payment_id ) ? ' active' : '');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tpl_component was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tpl_component = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
198
						$tpl_component['CHECKOUT_PAYMENT_METHOD_INPUT_STATE'] = ( ( !empty($payment_option['default_choice']) && $payment_option['default_choice'] == $payment_id ) ? 'checked="checked"' : '');
0 ignored issues
show
Bug introduced by
The variable $tpl_component does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
199
						$tpl_component['CHECKOUT_PAYMENT_METHOD_IDENTIFIER'] = $payment_id;
200
201 View Code Duplication
						if ( !empty($payment_config['logo']) && (int)$payment_config['logo'] != 0 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
							$tpl_component['CHECKOUT_PAYMENT_METHOD_ICON'] = ( !empty($payment_config['logo']) ) ? wp_get_attachment_image( $payment_config['logo'], 'thumbnail', false ) : '';
203
						}
204
						else {
205
							$tpl_component['CHECKOUT_PAYMENT_METHOD_ICON'] = ( !empty($payment_config['logo']) ) ? '<img src="' .$payment_config['logo']. '" alt="' .$payment_config['name']. '" />' : '';
206
						}
207
						$tpl_component['CHECKOUT_PAYMENT_METHOD_NAME'] = ( !empty($payment_config['name']) ) ? $payment_config['name'] : '';
208
						$tpl_component['CHECKOUT_PAYMENT_METHOD_EXPLANATION'] = ( !empty($payment_config['description']) ) ? $payment_config['description'] : '';
209
						$output .= wpshop_display::display_template_element('wpshop_checkout_page_payment_method_bloc', $tpl_component, array('type' => 'payment_method', 'id' => $payment_id));
210
						unset($tpl_component);
211
					}
212
				}
213
			}
214
		}
215
		return array($output, $payment_option);
216
	}
217
218
219
220
	/**
221
	* Reduce the stock regarding the order
222
	*/
223
	function the_order_payment_is_completed($order_id, $txn_id = null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
224
		// Donnees commandes
225
		$order = get_post_meta($order_id, '_order_postmeta', true);
226
		$order_info = get_post_meta($order_id, '_order_info', true);
227
228
		$wps_message = new wps_message_ctr();
229
230
		if(!empty($order) && !empty($order_info) && empty($order['order_invoice_ref'])) {
231
			$email = (!empty($order_info['billing']['address']['address_user_email']) ? $order_info['billing']['address']['address_user_email'] : '' );
232
			$first_name = ( !empty($order_info['billing']['address']['address_first_name']) ? $order_info['billing']['address']['address_first_name'] : '' );
233
			$last_name = ( !empty($order_info['billing']['address']['address_last_name']) ? $order_info['billing']['address']['address_last_name'] : '' );
234
235
			// Envoie du message de confirmation de paiement au client
236
			switch($order['payment_method']) {
237
				case 'check':
238
					$wps_message->wpshop_prepared_email($email, 'WPSHOP_OTHERS_PAYMENT_CONFIRMATION_MESSAGE', array('order_key' => $order['order_key'], 'customer_first_name' => $first_name, 'customer_last_name' => $last_name, 'order_date' => $order['order_date']));
239
				break;
240
241 View Code Duplication
				case 'paypal':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
					$wps_message->wpshop_prepared_email($email, 'WPSHOP_PAYPAL_PAYMENT_CONFIRMATION_MESSAGE', array('paypal_order_key' => $txn_id, 'customer_first_name' => $first_name, 'customer_last_name' => $last_name, 'order_date' => $order['order_date']));
243
				break;
244
245 View Code Duplication
				default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
246
					$wps_message->wpshop_prepared_email($email, 'WPSHOP_OTHERS_PAYMENT_CONFIRMATION_MESSAGE', array('order_key' => $order['order_key'], 'customer_first_name' => $first_name, 'customer_last_name' => $last_name, 'order_date' => $order['order_date']));
247
				break;
248
			}
249
		}
250
	}
251
252
253
	function setOrderPaymentStatus( $order_id, $payment_status ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
254
		/**	Get order main information	*/
255
		$order = get_post_meta($order_id, '_order_postmeta', true);
256
		$send_email = false;
257
258
		if ( !empty($order) ) {
259
			/**	Change order status to given status	*/
260
			$order['order_status'] = strtolower($payment_status);
261
			/**	Put order status into a single meta, allowing to use it easily later	*/
262
			update_post_meta($order_id, '_wpshop_order_status', $order['order_status']);
263
264
			/**	In case the set status is completed, make specific treatment: add the completed date	*/
265
			if ( $payment_status == 'completed' ) {
266
				/**	Read order items list, if not empty and check if each item is set to manage stock or not */
267
				if (!empty($order['order_items'])) {
268
					foreach ($order['order_items'] as $o) {
0 ignored issues
show
Bug introduced by
The expression $order['order_items'] of type string is not traversable.
Loading history...
269
						$product = wpshop_products::get_product_data( $o['item_id'] );
270 View Code Duplication
						if (!empty($product) && !empty($product['manage_stock']) && __($product['manage_stock'], 'wpshop') == __('Yes', 'wpshop') ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271
							wpshop_products::reduce_product_stock_qty($o['item_id'], $o['item_qty']);
272
						}
273
					}
274
				}
275
276
				/** Add information about the order completed date */
277
				update_post_meta($order_id, '_' . WPSHOP_NEWTYPE_IDENTIFIER_ORDER . '_completed_date', current_time('mysql', 0));
278
279
				/**	Set a variable to know when send an email to the customer	*/
280
				$send_email = true;
281
			}
282
283
			/**	Send email to customer when specific case need it	*/
284
			if ( $send_email ) {
285
				/**	Get information about customer that make the order	*/
286
				$order_info = get_post_meta($order_id, '_order_info', true);
0 ignored issues
show
Unused Code introduced by
$order_info is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
287
				$mail_tpl_component = array('order_key' => $order['order_key'], 'customer_first_name' => $first_name, 'customer_last_name' => $last_name, 'order_date' => $order['order_date']);
0 ignored issues
show
Bug introduced by
The variable $first_name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $last_name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
$mail_tpl_component is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
288
			}
289
290
			/**	Update order with new informations	*/
291
			update_post_meta($order_id, '_order_postmeta', $order);
292
		}
293
	}
294
295
	/**
296
	* Get payment method
297
	*/
298
	function get_payment_method($post_id){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
299
		$pm = __('Nc','wpshop');
300
		$order_postmeta = get_post_meta($post_id, '_order_postmeta', true);
301
		if ( !empty($order_postmeta['payment_method']) ) {
302
			switch($order_postmeta['payment_method']){
303
				case 'check':
304
					$pm = __('Check','wpshop');
305
				break;
306
				case 'paypal':
307
					$pm = __('Paypal','wpshop');
308
				break;
309
				case 'banktransfer':
310
					$pm = __('Bank transfer','wpshop');
311
				break;
312
				case 'cic':
313
					$pm = __('Credit card','wpshop');
314
				break;
315
				default:
316
					$pm = __('Nc','wpshop');
317
				break;
318
			}
319
		}
320
		return $pm;
321
	}
322
323
	/**
324
	* Set payment transaction number
325
	*/
326
	function display_payment_receiver_interface($post_id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
327
		$payment_validation = '';
328
		$display_button = false;
329
330
		$order_postmeta = get_post_meta($post_id, '_order_postmeta', true);
331
332
		$transaction_indentifier = self::get_payment_transaction_number($post_id);
333
334
		$paymentMethod = $paymentMethod['mode'];
0 ignored issues
show
Bug introduced by
The variable $paymentMethod seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
335
		$payment_validation .= '
336
<div id="order_payment_method_'.$post_id.'" class="wpshop_cls wpshopHide" >
337
	<input type="hidden" id="used_method_payment_'.$post_id.'" value="' . (!empty($order_postmeta['payment_method']) ? $order_postmeta['payment_method'] : 'no_method') . '"/>
338
	<input type="hidden" id="used_method_payment_transaction_id_'.$post_id.'" value="' . (!empty($transaction_indentifier) ? $transaction_indentifier : 0) . '"/>';
339
340
		if(!empty($order_postmeta['payment_method'])){
341
			$payment_validation .= sprintf(__('Selected payment method: %s', 'wpshop'), __($order_postmeta['payment_method'], 'wpshop')) . '<br/>';
342
		}
343
344 View Code Duplication
		if(!empty($paymentMethod['paypal']) && empty($order_postmeta['payment_method'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
345
			$payment_validation .= '<input type="radio" class="payment_method" name="payment_method" value="paypal" id="payment_method_paypal" /><label for="payment_method_paypal" >' . __('Paypal', 'wpshop') . '</label><br/>';
346
			$display_button = true;
347
		}
348
349 View Code Duplication
		if(!empty($paymentMethod['checks']) && empty($order_postmeta['payment_method'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
350
			$payment_validation .= '<input type="radio" class="payment_method" name="payment_method" value="check" id="payment_method_check" /><label for="payment_method_check" >' . __('Check', 'wpshop') . '</label><br/>';
351
			$display_button = true;
352
		}
353
354
		$wpshop_paymentMethod = get_option( 'wps_payment_mode' );
355
		if((WPSHOP_PAYMENT_METHOD_CIC || (!empty($wpshop_paymentMethod['mode']) && !empty($wpshop_paymentMethod['mode']['cic'])) ) && empty($order_postmeta['payment_method'])) {
356
			$payment_validation .= '<input type="radio" class="payment_method" name="payment_method" value="cb" id="payment_method_cb" /><label for="payment_method_cb" >' . __('Credit card', 'wpshop') . '</label><br/>';
357
			$display_button = true;
358
		}
359
360 View Code Duplication
		if(empty($payment_transaction)){
0 ignored issues
show
Bug introduced by
The variable $payment_transaction seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
361
			$payment_validation .= '<hr/>' . __('Transaction number', 'wpshop') . '&nbsp;:&nbsp;<input type="text" value="" name="payment_method_transaction_number" id="payment_method_transaction_number_'.$post_id.'" />';
362
			$display_button = true;
363
		}
364
365 View Code Duplication
		if($display_button){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
366
			$payment_validation .= '
367
		<br/><br/><a class="button payment_method_validate order_'.$post_id.' wpshop_clear" >'.__('Validate payment method', 'wpshop').'</a>';
368
		}
369
370
		$payment_validation .= '
371
</div>';
372
373
		return $payment_validation;
374
	}
375
376
	/**
377
	 * Allows to inform customer that he would pay a partial amount on this order
378
	 *
379
	 * @param float $current_order_total The current order total to pay before partial amount calcul
380
	 * @return array The amount to pay / A html output with amount to pay and different information
381
	 */
382
	function partial_payment_calcul( $current_order_total, $for = 'for_all' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
383
		$output = '';
384
		$tpl_component = array();
385
386
		/**	Get current configuration	*/
387
		$partial_payment_configuration = get_option('wpshop_payment_partial', array($for => array(), 'for_quotation' => array()));
0 ignored issues
show
Documentation introduced by
array($for => array(), '..._quotation' => array()) is of type array<string,array,{"for_quotation":"array"}>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
388
		if ( !empty($partial_payment_configuration[$for]) && (!empty($partial_payment_configuration[$for]['activate'])) && ($partial_payment_configuration[$for]['activate'] == 'on') ) {
389
			$amount_of_partial_payment = 0;
390
			if ( !empty($partial_payment_configuration[$for]['value']) && !empty($partial_payment_configuration[$for]['activate']) ) {
391
				$amount_of_partial_payment = $partial_payment_configuration[$for]['value'];
392
			}
393
394
			$partial_amount_to_pay = 0;
395
			$type_of_partial_payment = null;
396
			if (!empty($partial_payment_configuration[$for]) && !empty($partial_payment_configuration[$for]['type']) ) {
397
				switch ($partial_payment_configuration[$for]['type']) {
398
					case 'percentage':
399
						$type_of_partial_payment = '%';
400
						$partial_amount_to_pay = (($current_order_total * $amount_of_partial_payment) / 100);
401
					break;
402
					case 'amount':
403
						$type_of_partial_payment = wpshop_tools::wpshop_get_currency();
404
						$partial_amount_to_pay = ($current_order_total - $amount_of_partial_payment);
405
					break;
406
					default:
407
						$type_of_partial_payment = wpshop_tools::wpshop_get_currency();
408
						$partial_amount_to_pay = ($current_order_total - $amount_of_partial_payment);
409
					break;
410
				}
411
			}
412
			$output['amount_of_partial_payment'] = $amount_of_partial_payment;
413
			$output['type_of_partial_payment'] = $type_of_partial_payment;
414
			$output['amount_to_pay'] = $partial_amount_to_pay;
415
416
			$tpl_component['CURRENT_ORDER_TOTAL_AMOUNT'] = $current_order_total;
417
			$tpl_component['PARTIAL_PAYMENT_CONFIG_AMOUNT'] = !empty($amount_of_partial_payment) ? $amount_of_partial_payment : '';
418
			$tpl_component['PARTIAL_PAYMENT_CONFIG_TYPE'] = !empty($type_of_partial_payment) ? $type_of_partial_payment : '';
419
			$tpl_component['PARTIAL_PAYMENT_AMOUNT'] = $partial_amount_to_pay;
420
421
			$output['display'] = wpshop_display::display_template_element('wpshop_partial_payment_display', $tpl_component);
422
			unset($tpl_component);
423
		}
424
425
		return $output;
426
	}
427
428
	/**
429
	 * Return the new transaction reference for an order payment
430
	 * @since 1.3.3.7
431
	 *
432
	 * @param integer $order_id The order identifer we want to get payment reference for
433
	 *
434
	 * @return mixed The payment reference for current order
435
	 */
436
	function get_payment_transaction_number($order_id, $payment_index = 0) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
437
		$order_postmeta = get_post_meta($order_id, '_order_postmeta', true);
0 ignored issues
show
Unused Code introduced by
$order_postmeta is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
438
		$transaction_indentifier = '';
439
440
		if (!empty($order_meta['order_payment']['received']) && !empty($order_meta['order_payment']['received'][$payment_index]) && !empty($order_meta['order_payment']['received'][$payment_index]['payment_reference'])) {
0 ignored issues
show
Bug introduced by
The variable $order_meta seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
441
			$transaction_indentifier = $order_meta['order_payment']['received'][$payment_index]['payment_reference'];
442
		}
443
444
		return $transaction_indentifier;
445
	}
446
447
	/**
448
	 * Set the transaction identifier for a given order
449
	 *
450
	 * @param integer $order_id
451
	 * @param mixed $transaction_number The identifier of transaction. Used for all the payment method
452
	 */
453
	function set_payment_transaction_number($order_id, $transaction_number) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
454
		$order_postmeta = get_post_meta($order_id, '_order_postmeta', true);
455
456
		if ( !empty($order_postmeta['order_payment']['received']) ) {
457
			if (count($order_postmeta['order_payment']['received']) == 1) {
458
				$order_postmeta['order_payment']['received'][0]['payment_reference'] = $transaction_number;
459
			}
460
		}
461
462
		update_post_meta($order_id, '_order_postmeta', $order_postmeta);
463
	}
464
465
	/**
466
	 * Save the payment data returned by the payment server
467
	 *
468
	 * @param integer $order_id
469
	 */
470
	function save_payment_return_data( $order_id ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
471
		$data = wpshop_tools::getMethode();
472
473
		$current_payment_return = get_post_meta( $order_id, '_wpshop_payment_return_data', true);
474
		$current_payment_return[] = $data;
475
476
		update_post_meta($order_id, '_wpshop_payment_return_data', $current_payment_return);
477
	}
478
479
	/**
480
	 * Add a new payment to a given order
481
	 *
482
	 * @param array $order_meta The complete order meta informations
483
	 * @param integer $payment_index The payment to add/update data for
484
	 * @param array $params : infos sended by the bank, array structure : ('method', 'waited amount', 'status', 'author', 'payment reference', 'date', 'received amount')
485
	 * @return array The order new meta informations
486
	 */
487
	public static function add_new_payment_to_order( $order_id, $order_meta, $payment_index, $params, $bank_response ) {
488
489
		$order_meta['order_payment']['received'][$payment_index]['method'] = ( !empty($params['method']) ) ? $params['method'] : null;
490
		$order_meta['order_payment']['received'][$payment_index]['waited_amount'] = ( !empty($params['waited_amount']) ) ? $params['waited_amount'] : null;
491
		$order_meta['order_payment']['received'][$payment_index]['status'] = ( !empty($params['status']) ) ? $params['status'] : null;
492
		$order_meta['order_payment']['received'][$payment_index]['author'] = ( !empty($params['author']) ) ? $params['author'] : get_current_user_id();
493
		$order_meta['order_payment']['received'][$payment_index]['payment_reference'] =( !empty($params['payment_reference']) ) ? $params['payment_reference'] : null;
494
		$order_meta['order_payment']['received'][$payment_index]['date'] = ( !empty($params['date']) ) ? $params['date'] : null;
495
		$order_meta['order_payment']['received'][$payment_index]['received_amount'] = ( !empty($params['received_amount']) ) ? $params['received_amount'] : null;
496
		$order_meta['order_payment']['received'][$payment_index]['comment'] = '';
497
498
		$order_info = get_post_meta($order_id, '_order_info', true);
499
		if(!empty($order_meta) && !empty($order_info) && ($bank_response == 'completed')) {
500
501
			/**	Generate an invoice number for the current payment. Check if the payment is complete or not	*/
502
			if ( empty($order_meta['order_invoice_ref']) ) {
503
				$order_meta['order_payment']['received'][$payment_index]['invoice_ref'] = wpshop_modules_billing::generate_invoice_number( $order_id );
504
			}
505
		}
506
507
		return $order_meta['order_payment']['received'][$payment_index];
508
	}
509
510
	/**
511
	 * Return the array id of the last waited paylent for an payment method
512
	 * @param integer $oid
513
	 * @param string $payment_method
514
	 * @return integer $key : array id of [order_payment][received] in the order postmeta
515
	 */
516
	public static function get_order_waiting_payment_array_id ( $oid, $payment_method ) {
0 ignored issues
show
Unused Code introduced by
The parameter $payment_method is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
517
		$key = 0;
518
		$order_meta = get_post_meta( $oid, '_order_postmeta', true);
519
		if ( !empty($order_meta) ) {
520
			$key = count( $order_meta['order_payment']['received'] );
521
			foreach ( $order_meta['order_payment']['received'] as $k => $payment_test) {
522
				if ( !array_key_exists('received_amount', $payment_test) /* && $order_meta['order_payment']['received'][$k]['method'] == $payment_method */ ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
523
					$key = $k;
524
				}
525
			}
526
		}
527
		return $key;
528
	}
529
530
531
	/**
532
	 * Update th receive payment part in order postmeta and return "Complete" if the shop have received the total amount of the order
533
	 * @param int $order_id
534
	 * @param array $params_array
535
	 * @return string
536
	 */
537
	public static function check_order_payment_total_amount($order_id, $params_array, $bank_response, $order_meta = array(), $save_metadata = true ) {
538
		global $wpshop_payment; global $wpdb;
539
		$order_meta = ( !empty($order_meta) ) ? $order_meta : get_post_meta( $order_id, '_order_postmeta', true);
540
541
		$wps_message = new wps_message_ctr();
542
		if ( !empty($order_meta) ) {
543
			$order_info = get_post_meta($order_id, '_order_info', true);
544
			$user_data = get_userdata( $order_meta['customer_id'] );
545
			$email = ( !empty($user_data) && !empty($user_data->user_email) ) ? $user_data->user_email : '';
546
// 			$email = ( !empty($order_info) &&  !empty($order_info['billing']) && !empty($order_info['billing']['address']['address_user_email']) ) ? $order_info['billing']['address']['address_user_email'] : '' ;
0 ignored issues
show
Unused Code Comprehensibility introduced by
76% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
547
			$first_name = (!empty($order_info) && !empty($order_info['billing']) &&  !empty($order_info['billing']['address']['address_first_name']) ? $order_info['billing']['address']['address_first_name'] : '' );
548
			$last_name = ( !empty($order_info) && !empty($order_info['billing']) && !empty($order_info['billing']['address']['address_last_name']) ? $order_info['billing']['address']['address_last_name'] : '' );
549
550
			$key = self::get_order_waiting_payment_array_id( $order_id, $params_array['method']);
551
			$order_grand_total = $order_meta['order_grand_total'];
552
			$total_received = ( ( !empty($params_array['status']) && ( $params_array['status'] == 'payment_received') && ($bank_response == 'completed') && !empty($params_array['received_amount']) ) ? $params_array['received_amount'] : 0 );
553 View Code Duplication
			foreach ( $order_meta['order_payment']['received'] as $received ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
554
				$total_received += ( ( !empty($received['status']) && ( $received['status'] == 'payment_received') && ($bank_response == 'completed') && !empty($received['received_amount']) ) ? $received['received_amount'] : 0 );
555
			}
556
			$order_meta['order_amount_to_pay_now'] = $order_grand_total - $total_received;
557
			$order_meta['order_payment']['received'][$key] = self::add_new_payment_to_order( $order_id, $order_meta, $key, $params_array, $bank_response );
558
559
			if ($bank_response == 'completed') {
560
561
				if ( number_format((float)$total_received, 2, '.', '') >= number_format((float)$order_grand_total,2, '.', '') ) {
562
					$payment_status = 'completed';
563
564
					$order_meta['order_invoice_ref'] = ( empty ($order_meta['order_invoice_ref'] ) && !empty($order_meta['order_payment']['received'][$key]) && !empty($order_meta['order_payment']['received'][$key]['invoice_ref']) ) ? $order_meta['order_payment']['received'][$key]['invoice_ref'] : ( empty($order_meta['order_invoice_ref']) ? null : $order_meta['order_invoice_ref'] ) ;
565
					$order_meta['order_invoice_date'] = current_time('mysql', 0);
566
567
					if (!empty($order_meta['order_items'])) {
568
						foreach ($order_meta['order_items'] as $item_id => $o) {
569
							$pid = $o['item_id'];
570 View Code Duplication
							if (strpos($item_id,'__') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
571
								$product_data_id = explode( '__', $item_id );
572
								$pid = ( !empty($product_data_id) && !empty($product_data_id[1]) ) ? $product_data_id[1] : $pid;
573
							}
574
							$product = wpshop_products::get_product_data( $pid );
575
							if ( get_post_type( $pid ) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION ) {
576
								$parent_def = wpshop_products::get_parent_variation ( $pid );
577
								$parent_post = $parent_def['parent_post'];
578
								$product = wpshop_products::get_product_data( $parent_post->ID );
579
							}
580
581 View Code Duplication
							if (!empty($product) && !empty($product['manage_stock']) && strtolower( __($product['manage_stock'], 'wpshop') ) == strtolower( __('Yes', 'wpshop') ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
582
								wpshop_products::reduce_product_stock_qty($product['product_id'], $o['item_qty'], $pid );
583
							}
584
						}
585
					}
586
587
					/** Add information about the order completed date */
588
					update_post_meta($order_id, '_' . WPSHOP_NEWTYPE_IDENTIFIER_ORDER . '_completed_date', current_time('mysql', 0));
589
590
					// Send confirmation e-mail to administrator
591 View Code Duplication
					if ( empty($_SESSION['wps-pos-addon']) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
592
						$email_option = get_option('wpshop_emails');
593
						if(  !empty($email_option) && !empty($email_option['send_confirmation_order_message']) ){
594
							wpshop_checkout::send_order_email_to_administrator( $order_id, $user_data );
595
						}
596
					}
597
598
					// POS Status
599
					if( !empty($order_meta['order_payment']) && !empty($order_meta['order_payment']['shipping_method']) && $order_meta['order_payment']['shipping_method'] == 'default_shipping_mode_for_pos' ) {
600
						$payment_status = 'pos';
601
					}
602
				}
603
				else {
604
					$payment_status = 'partially_paid';
605
				}
606
607
				$order_meta['order_status'] = $payment_status;
608
				update_post_meta( $order_id, '_order_postmeta', $order_meta);
609
				$save_metadata = false;
610
611
				$allow_send_invoice = get_option( 'wpshop_send_invoice' );
612
				$invoice_attachment_file = ( !empty($allow_send_invoice) ) ? wpshop_modules_billing::generate_invoice_for_email( $order_id, empty( $order_meta['order_payment']['received'][$key]['invoice_ref'] ) ? $order_meta['order_invoice_ref'] : $order_meta['order_payment']['received'][$key]['invoice_ref'] ) : '';
613
614
				$email_option = get_option( 'wpshop_emails' );
615
616
				$shipping_mode_option = get_option( 'wps_shipping_mode' );
617
				$shipping_method = ( !empty($order_meta['order_payment']['shipping_method']) && !empty($shipping_mode_option) && !empty($shipping_mode_option['modes']) && is_array($shipping_mode_option['modes']) && array_key_exists($order_meta['order_payment']['shipping_method'], $shipping_mode_option['modes'])) ? $shipping_mode_option['modes'][$order_meta['order_payment']['shipping_method']]['name'] : ( (!empty($order_meta['order_payment']['shipping_method']) ) ? $order_meta['order_payment']['shipping_method'] : '' );
618
619
				$payment_method_option = get_option( 'wps_payment_mode' );
620
				$order_payment_method = ( !empty($payment_method_option) && !empty($payment_method_option['mode'])  && !empty($order_meta['order_payment']['customer_choice']['method'])  && !empty($payment_method_option['mode'][$order_meta['order_payment']['customer_choice']['method']])  ) ? $payment_method_option['mode'][$order_meta['order_payment']['customer_choice']['method']]['name'] : $order_meta['order_payment']['customer_choice']['method'];
621
622
				if ( !empty( $email_option ) && !empty( $email_option['send_confirmation_order_message'] ) && $payment_status == 'completed'
623
						&& ( !isset( $params_array[ 'send_received_payment_email' ] ) || ( true == $params_array[ 'send_received_payment_email' ] ) ) ) {
624
					$wps_message->wpshop_prepared_email($email, 'WPSHOP_ORDER_CONFIRMATION_MESSAGE', array('order_id' => $order_id,'customer_first_name' => $first_name, 'customer_last_name' => $last_name, 'customer_email' => $email, 'order_key' => ( ( !empty($order_meta['order_key']) ) ? $order_meta['order_key'] : ''),'order_date' => current_time('mysql', 0),  'order_payment_method' => $order_payment_method, 'order_content' => '', 'order_addresses' => '', 'order_customer_comments' => '', 'order_billing_address' => '', 'order_shipping_address' => '',  'order_shipping_method' => $shipping_method ) );
625
				}
626
627
				if ( !isset( $params_array[ 'send_received_payment_email' ] ) || ( true == $params_array[ 'send_received_payment_email' ] ) ) {
628
					$wps_message->wpshop_prepared_email( $email, 'WPSHOP_OTHERS_PAYMENT_CONFIRMATION_MESSAGE', array('order_key' => $order_meta['order_key'], 'customer_first_name' => $first_name, 'customer_last_name' => $last_name, 'order_date' => $order_meta['order_date'], 'order_shipping_method' => $shipping_method), array(), $invoice_attachment_file);
629
				}
630
			}
631
			else {
632
				$payment_status = $bank_response;
633
			}
634
635
			$order_meta['order_status'] = $payment_status;
636
637
			update_post_meta( $order_id, '_wpshop_order_status', $payment_status);
638
			if( $save_metadata ) {
639
				update_post_meta( $order_id, '_order_postmeta', $order_meta);
640
			}
641
		}
642
		do_action( 'wps_after_check_order_payment_total_amount', $order_id );
643
		if ( ! $save_metadata ) {
644
			return $order_meta;
645
		}
646
	}
647
648
	/**
649
	 * Return the transaction of an order payment transaction.
650
	 *
651
	 * @deprecated deprecated since version 1.3.3.7
652
	 *
653
	 * @param integer $order_id The order identifier we want to get the old transaction reference for
654
	 * @return integer
655
	 */
656
	public static function get_payment_transaction_number_old_way($order_id){
657
		$order_postmeta = get_post_meta($order_id, '_order_postmeta', true);
658
		$transaction_indentifier = 0;
659
		if(!empty($order_postmeta['payment_method'])){
660
			switch($order_postmeta['payment_method']){
661
				case 'check':
662
					$transaction_indentifier = get_post_meta($order_id, '_order_check_number', true);
663
					break;
664
				case 'paypal':
665
					$transaction_indentifier = get_post_meta($order_id, '_order_paypal_txn_id', true);
666
					break;
667
				case 'cic':
668
					$transaction_indentifier = get_post_meta($order_id, '_order_cic_txn_id', true);
669
					break;
670
				default:
671
					$transaction_indentifier = 0;
672
					break;
673
			}
674
		}
675
676
		return $transaction_indentifier;
677
	}
678
679
	public static function reverify_payment_invoice_ref( $order_id, $index_payment ) {
680
		$status = false;
681
		$order_meta = get_post_meta( $order_id, '_order_postmeta', true );
682
		if( !empty( $order_meta ) && !empty( $order_meta['order_payment'] ) && !empty( $order_meta['order_payment']['received'] ) && !empty( $order_meta['order_payment']['received'][$index_payment] ) && empty( $order_meta['order_payment']['received'][$index_payment]['invoice_ref'] ) ) {
683
			$order_invoice = $invoice_ref = false;
684
685
			end( $order_meta['order_payment']['received'] );
686
			$last_payment = key( $order_meta['order_payment']['received'] );
687
			if( $last_payment == $index_payment ) {
688
				$payments = 0;
689
				foreach( $order_meta['order_payment']['received'] as $payment ) {
690
					$payments += ( $payment['status'] == 'payment_received' ) ? $payment['received_amount'] : 0;
691
				}
692
				if( $order_meta['order_grand_total'] <= $payments ) {
693
					if( $order_meta['order_status'] == 'partially_paid' ) {
694
						$order_status_meta = get_post_meta( $order_id, '_wpshop_order_status', true );
0 ignored issues
show
Unused Code introduced by
$order_status_meta is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
695
						$order_meta['order_status'] = 'completed';
696
						$status = (bool)update_post_meta( $order_id, '_wpshop_order_status', 'completed' );
0 ignored issues
show
Unused Code introduced by
$status is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
697
					}
698
					if( !empty( $order_meta['order_invoice_ref'] ) ) {
699
						$invoice_ref = $order_meta['order_invoice_ref'];
700
						$status = true;
701
					} else {
702
						$status = $order_invoice = true;
703
					}
704
				}
705
			} else {
706
				$status = true;
707
			}
708
709
			if( $status ) {
710
				if( empty( $invoice_ref ) ) {
711
					$invoice_ref = wpshop_modules_billing::generate_invoice_number( $order_id );
712
					if( $order_invoice ) {
713
						$order_meta['order_invoice_ref'] = $invoice_ref;
714
					}
715
				}
716
717
				$order_meta['order_payment']['received'][$index_payment]['invoice_ref'] = $invoice_ref;
718
				$status = (bool)update_post_meta( $order_id, '_order_postmeta', $order_meta );
719
			}
720
		}
721
		return $status;
722
	}
723
}
724
725
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
726