|
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(), |
|
|
|
|
|
|
41
|
|
|
'third_party' => ! empty( $_POST['third_party'] ) ? (array) $_POST['third_party'] : array(), |
|
|
|
|
|
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
$data['contact']['firstname'] = ! empty( $_POST['contact']['firstname'] ) ? sanitize_text_field( $_POST['contact']['firstname'] ) : ''; |
|
|
|
|
|
|
45
|
|
|
$data['contact']['lastname'] = ! empty( $_POST['contact']['lastname'] ) ? sanitize_text_field( $_POST['contact']['lastname'] ) : ''; |
|
|
|
|
|
|
46
|
|
|
$data['contact']['phone'] = ! empty( $_POST['contact']['phone'] ) ? sanitize_text_field( $_POST['contact']['phone'] ) : ''; |
|
|
|
|
|
|
47
|
|
|
$data['contact']['email'] = ! empty( $_POST['contact']['email'] ) ? sanitize_email( $_POST['contact']['email'] ) : ''; |
|
|
|
|
|
|
48
|
|
|
$data['contact']['password'] = ! empty( $_POST['contact']['password'] ) ? (string) ( $_POST['contact']['password'] ) : ''; |
|
|
|
|
|
|
49
|
|
|
$data['third_party']['country_id'] = ! empty( $_POST['third_party']['country_id'] ) ? (int) ( $_POST['third_party']['country_id'] ) : ''; |
|
|
|
|
|
|
50
|
|
|
$data['third_party']['address'] = ! empty( $_POST['third_party']['address'] ) ? sanitize_text_field( $_POST['third_party']['address'] ) : ''; |
|
|
|
|
|
|
51
|
|
|
$data['third_party']['zip'] = ! empty( $_POST['third_party']['zip'] ) ? sanitize_text_field( $_POST['third_party']['zip'] ) : ''; |
|
|
|
|
|
|
52
|
|
|
$data['third_party']['town'] = ! empty( $_POST['third_party']['town'] ) ? sanitize_text_field( $_POST['third_party']['town'] ) : ''; |
|
|
|
|
|
|
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'] : ''; |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
switch ( $type ) { |
|
169
|
|
View Code Duplication |
case 'cheque': |
|
|
|
|
|
|
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': |
|
|
|
|
|
|
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': |
|
|
|
|
|
|
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': |
|
|
|
|
|
|
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
|
|
|
|