Completed
Branch 2.0.0 (814c19)
by Jimmy
03:05
created

Checkout::get_posted_data()   F

Complexity

Conditions 12
Paths 2048

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 2048
nop 0
dl 0
loc 18
rs 2.8
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Les fonctions principales du tunnel de vente.
4
 *
5
 * @author    Eoxia <[email protected]>
6
 * @copyright (c) 2011-2018 Eoxia <[email protected]>.
7
 *
8
 * @license   AGPLv3 <https://spdx.org/licenses/AGPL-3.0-or-later.html>
9
 *
10
 * @package   WPshop\Classes
11
 *
12
 * @since     2.0.0
13
 */
14
15
namespace wpshop;
16
17
defined( 'ABSPATH' ) || exit;
18
19
/**
20
 * Checkout Class.
21
 */
22
class Checkout extends \eoxia\Singleton_Util {
23
24
	/**
25
	 * Constructeur pour la classe Checkout. Charge les options et les actions.
26
	 *
27
	 * @since 2.0.0
28
	 */
29
	protected function construct() {}
30
31
	/**
32
	 * Récupères les données postées
33
	 *
34
	 * @since 2.0.0
35
	 *
36
	 * @return array Les données postées filtrés et sécurisés.
37
	 */
38
	public function get_posted_data() {
39
		$data = array(
40
			'contact'     => ! empty( $_POST['contact'] ) ? (array) $_POST['contact'] : array(),
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
41
			'third_party' => ! empty( $_POST['third_party'] ) ? (array) $_POST['third_party'] : array(),
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
42
		);
43
44
		$data['contact']['firstname']      = ! empty( $_POST['contact']['firstname'] ) ? sanitize_text_field( $_POST['contact']['firstname'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
45
		$data['contact']['lastname']       = ! empty( $_POST['contact']['lastname'] ) ? sanitize_text_field( $_POST['contact']['lastname'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
46
		$data['contact']['phone']          = ! empty( $_POST['contact']['phone'] ) ? sanitize_text_field( $_POST['contact']['phone'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
47
		$data['contact']['email']          = ! empty( $_POST['contact']['email'] ) ? sanitize_email( $_POST['contact']['email'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
48
		$data['contact']['password']       = ! empty( $_POST['contact']['password'] ) ? (string) ( $_POST['contact']['password'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
49
		$data['third_party']['country_id'] = ! empty( $_POST['third_party']['country_id'] ) ? (int) ( $_POST['third_party']['country_id'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
50
		$data['third_party']['address']    = ! empty( $_POST['third_party']['address'] ) ? sanitize_text_field( $_POST['third_party']['address'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
51
		$data['third_party']['zip']        = ! empty( $_POST['third_party']['zip'] ) ? sanitize_text_field( $_POST['third_party']['zip'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
52
		$data['third_party']['town']       = ! empty( $_POST['third_party']['town'] ) ? sanitize_text_field( $_POST['third_party']['town'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
53
54
		return apply_filters( 'wps_checkout_posted_data', $data );
55
	}
56
57
	/**
58
	 * Définition du formulaire du tunnel de vente
59
	 *
60
	 * @since 2.0.0
61
	 *
62
	 * @return array Tableau contenant la définition des champs.
63
	 */
64
	private function get_checkout_fields() {
65
		return array(
66
			'contact'     => array(
67
				'firstname' => array(
68
					'label'    => __( 'First name', 'wpshop' ),
69
					'required' => false,
70
				),
71
				'lastname'  => array(
72
					'label'    => __( 'Last name', 'wpshop' ),
73
					'required' => false,
74
				),
75
				'phone'     => array(
76
					'label'    => __( 'Phone', 'wpshop' ),
77
					'required' => false,
78
				),
79
				'email'     => array(
80
					'label'    => __( 'Email address', 'wpshop' ),
81
					'required' => true,
82
				),
83
				'password'  => array(
84
					'label'    => __( 'Password', 'wpshop' ),
85
					'required' => false,
86
				),
87
			),
88
			'third_party' => array(
89
				'country_id' => array(
90
					'label'    => __( 'Country', 'wpshop' ),
91
					'required' => true,
92
				),
93
				'address'    => array(
94
					'label'    => __( 'Street Address', 'wpshop' ),
95
					'required' => true,
96
				),
97
				'zip'        => array(
98
					'label'    => __( 'Postcode / Zip', 'wpshop' ),
99
					'required' => true,
100
				),
101
				'town'       => array(
102
					'label'    => __( 'Town / City', 'wpshop' ),
103
					'required' => true,
104
				),
105
			),
106
		);
107
	}
108
109
	/**
110
	 * Vérifie les données reçu par le formulaire du tunnel de vente.
111
	 *
112
	 * @since 2.0.0
113
	 *
114
	 * @param  array    $data   Les données reçu du formulaire.
115
	 * @param  WP_Error $errors Gestion des erreurs du formulaire.
116
	 */
117
	protected function validate_posted_data( &$data, &$errors ) {
118
		foreach ( $this->get_checkout_fields() as $fieldset_key => $fieldset ) {
119
			foreach ( $fieldset as $field_key => $field ) {
120
				if ( $field['required'] && ( '' == $data[ $fieldset_key ][ $field_key ] || '0' == $data[ $fieldset_key ][ $field_key ] ) ) {
121
					/* translators: Lastname is a required field. */
122
					$errors->add( 'required-field', apply_filters( 'wps_checkout_required_field_notice', sprintf( __( '%s is a required field.', 'wpshop' ), '<strong>' . esc_html( $field['label'] ) . '</strong>' ), $field['label'] ) );
123
124
					$error_field = array(
125
						'required'    => true,
126
						'input_class' => $fieldset_key . '-' . $field_key,
127
					);
128
129
					$errors->add_data( $error_field, 'input_' . $fieldset_key . '_' . $field_key );
130
				}
131
132
				if ( ! is_user_logged_in() && 'email' === $field_key && false !== email_exists( $data['contact']['email'] ) ) {
133
					/* translators: [email protected] is already used. */
134
					$errors->add( 'email-exists', apply_filters( 'wps_checkout_email_exists_notice', sprintf( __( '%s is already used.', 'wpshop' ), '<strong>' . esc_html( $field['label'] ) . '</strong>' ), $field['label'] ) );
135
					$error_field = array(
136
						'email_exists' => true,
137
						'input_class'  => $fieldset_key . '-' . $field_key,
138
					);
139
140
					$errors->add_data( $error_field, 'input_' . $fieldset_key . '_' . $field_key );
141
				}
142
			}
143
		}
144
	}
145
146
	/**
147
	 * Appel la méthode pour valider le formulaire.
148
	 *
149
	 * @since 2.0.0
150
	 *
151
	 * @param  array    $data   Les données reçu du formulaire.
152
	 * @param  WP_Error $errors Gestion des erreurs du formulaire.
153
	 */
154
	public function validate_checkout( &$data, &$errors ) {
155
		$this->validate_posted_data( $data, $errors );
156
	}
157
158
	/**
159
	 * Procèdes au paiement
160
	 *
161
	 * @since 2.0.0
162
	 *
163
	 * @param Order_Model $order Les données de la commande.
164
	 */
165
	public function process_order_payment( $order ) {
166
		$type = ! empty( $_POST['type_payment'] ) ? $_POST['type_payment'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
167
168
		switch ( $type ) {
169 View Code Duplication
			case 'cheque':
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...
170
				update_post_meta( $order->data['id'], 'payment_method', 'cheque' );
171
				Cart_Session::g()->destroy();
172
				wp_send_json_success( array(
173
					'namespace'        => 'wpshopFrontend',
174
					'module'           => 'checkout',
175
					'callback_success' => 'redirect',
176
					'url'              => Pages::g()->get_valid_checkout_link() . '?order_id=' . $order->data['id'],
177
				) );
178
				break;
179 View Code Duplication
			case 'payment_in_shop':
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...
180
				update_post_meta( $order->data['id'], 'payment_method', 'payment_in_shop' );
181
				Cart_Session::g()->destroy();
182
				wp_send_json_success( array(
183
					'namespace'        => 'wpshopFrontend',
184
					'module'           => 'checkout',
185
					'callback_success' => 'redirect',
186
					'url'              => Pages::g()->get_valid_checkout_link() . '?order_id=' . $order->data['id'],
187
				) );
188
				break;
189 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...
190
				update_post_meta( $order->data['id'], 'payment_method', 'paypal' );
191
192
				$result = Paypal::g()->process_payment( $order );
193
				Cart_Session::g()->destroy();
194
				if ( ! empty( $result['url'] ) ) {
195
					wp_send_json_success( array(
196
						'namespace'        => 'wpshopFrontend',
197
						'module'           => 'checkout',
198
						'callback_success' => 'redirectToPayment',
199
						'url'              => $result['url'],
200
					) );
201
				}
202
				break;
203 View Code Duplication
			case 'stripe':
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...
204
				update_post_meta( $order->data['id'], 'payment_method', 'stripe' );
205
				$result = Stripe::g()->process_payment( $order );
206
				Cart_Session::g()->destroy();
207
208
				if ( ! empty( $result['id'] ) ) {
209
					wp_send_json_success( array(
210
						'namespace'        => 'wpshopFrontend',
211
						'module'           => 'stripe',
212
						'callback_success' => 'redirectToPayment',
213
						'id'               => $result['id'],
214
					) );
215
				}
216
				break;
217
		}
218
219
	}
220
}
221
222
Checkout::g();
223