-
Notifications
You must be signed in to change notification settings - Fork 2
/
woocommerce_paybox_gateway.class.php
413 lines (380 loc) · 22.3 KB
/
woocommerce_paybox_gateway.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
/*
* Paybox Commerce Gateway Class
*/
class WC_Paybox extends WC_Payment_Gateway {
function __construct() {
$this->id = 'paybox';
$this->text_domain = 'paybox_gateway';
$this->icon = PLUGIN_DIR . '/images/paybox.png';
$this->has_fields = false;
$this->method_title = 'PayBox';
// Load the form fields
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Get setting values
foreach ($this->settings as $key => $val){
$this->$key = $val;
//echo $key .' = '. $val.'<br>';
}
// Chargement des traductions
$this->return_url = get_bloginfo('url' );
load_plugin_textdomain($this->text_domain, false, dirname(plugin_basename(__FILE__)).'/lang/');
// Ajout des Hooks
add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('woocommerce_receipt_' . $this->id, array($this, 'getParamPaybox'));
//add_action('init', array($this,'woocommerce_paybox_check_response'));
}
/*
* Admin tools.
*/
public function admin_options() {
echo '<h3>' . __('PayBox Gateway', $this->text_domain) . '</h3>';
$install_url = '';
if (!get_option('woocommerce_pbx_order_received_page_id') || !get_page(get_option('woocommerce_pbx_order_received_page_id')))
$install_url .= '&install_pbx_received_page=true';
if (!get_option('woocommerce_pbx_order_refused_page_id') || !get_page(get_option('woocommerce_pbx_order_refused_page_id')))
$install_url .= '&install_pbx_refused_page=true';
if (!get_option('woocommerce_pbx_order_canceled_page_id') || !get_page(get_option('woocommerce_pbx_order_canceled_page_id')))
$install_url .= '&install_pbx_canceled_page=true';
if ($install_url != '' && !isset($_GET['install_pbx_received_page']) && !isset($_GET['install_pbx_refused_page']) && !isset($_GET['install_pbx_canceled_page'])) {
echo
'<p>' .
__('We have detected that Paybox return pages are not currently installed on your system', $this->text_domain) . '<br/>' . __('Press the install button to prevent 404 from users whom transaction would have been received, canceled or refused.', $this->text_domain) .
'</p>
<p>
<a class="button" target="_self" href="./admin.php?page=wc-settings&tab=checkout§ion=wc_paybox' . $install_url . '">' . __('Install return pages', $this->text_domain) . '</a>
</p>';
} else {
echo
'<p>' .
__('Paybox return pages are installed', $this->text_domain) . ' :
<a target="_self" href="./post.php?post=' . get_option('woocommerce_pbx_order_received_page_id') . '&action=edit">' . __('received', $this->text_domain) . '</a> |
<a target="_self" href="./post.php?post=' . get_option('woocommerce_pbx_order_canceled_page_id') . '&action=edit">' . __('canceled', $this->text_domain) . '</a> |
<a target="_self" href="./post.php?post=' . get_option('woocommerce_pbx_order_refused_page_id') . '&action=edit">' . __('refused', $this->text_domain) . '</a>
</p>';
}
echo '<p>'.__('Paybox return url (IPN) is by default your website home url, currently set to' , $this->text_domain).' <em>'.$this->return_url.'</em></p>';
echo '<table class="form-table">';
$this->generate_settings_html();
echo '</table>';
// Page paiement reçu -> Shortcode
if (!empty($_GET['install_pbx_received_page']) && !get_page(get_option('woocommerce_pbx_order_received_page_id')))
$this->create_page(esc_sql('order-pbx-received'), 'woocommerce_pbx_order_received_page_id', __('Order PBX Received', $this->text_domain), '[' . THANKS_SHORTCODE . ']', woocommerce_get_page_id('checkout'));
// Page paiement refusé -> A venir shortcode pour interpretation du code retour
if (!empty($_GET['install_pbx_refused_page']) && !get_page(get_option('woocommerce_pbx_order_refused_page_id')))
$this->create_page(esc_sql('order-pbx-refused'), 'woocommerce_pbx_order_refused_page_id', __('Order PBX Refused', $this->text_domain), __('Your order has been refused', $this->text_domain), woocommerce_get_page_id('checkout'));
// Page paiement annulé par le client
if (!empty($_GET['install_pbx_canceled_page']) && !get_page(get_option('woocommerce_pbx_order_canceled_page_id')))
$this->create_page(esc_sql('order-pbx-canceled'), 'woocommerce_pbx_order_canceled_page_id', __('Order PBX Canceled', $this->text_domain), __('Your order has been cancelled', $this->text_domain), woocommerce_get_page_id('checkout'));
}
function create_page($slug, $option, $page_title = '', $page_content = '', $post_parent = 0) {
global $wpdb;
$option_value = get_option($option);
if ($option_value > 0 && get_post($option_value))
return;
$page_found = $wpdb->get_var($wpdb->prepare("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = %s LIMIT 1;", $slug));
if ($page_found && !$option_value) {
update_option($option, $page_found);
return;
}
$page_data = array(
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
'post_name' => $slug,
'post_title' => $page_title,
'post_content' => $page_content,
'post_parent' => $post_parent,
'comment_status' => 'closed'
);
$page_id = wp_insert_post($page_data);
update_option($option, $page_id);
}
/*
* Initialize Gateway Settings Form Fields.
*/
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable', $this->text_domain),
'type' => 'checkbox',
'label' => __('Enable Paybox Payment', $this->text_domain),
'default' => 'yes'
),
'title' => array(
'title' => __('Title', $this->text_domain),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', $this->text_domain),
'default' => __('Paybox Payment', $this->text_domain)
),
'description' => array(
'title' => __('Customer Message', $this->text_domain),
'type' => 'textarea',
'description' => __('Let the customer know the payee and where they should be sending the Paybox to and that their order won\'t be shipping until you receive it.', $this->text_domain),
'default' => __('Credit card payment by PayBox.', $this->text_domain)
),
'paybox_site_id' => array(
'title' => __('Site ID Paybox', $this->text_domain),
'type' => 'text',
'description' => __('Please enter you ID Site provided by PayBox.', $this->text_domain),
'default' => '1999888'
),
'paybox_identifiant' => array(
'title' => __('Paybox ID', $this->text_domain),
'type' => 'text',
'description' => __('Please enter you Paybox ID provided by PayBox.', $this->text_domain),
'default' => '2'
),
'paybox_rang' => array(
'title' => __('Paybox Rank', $this->text_domain),
'type' => 'text',
'description' => __('Please enter Paybox Rank provided by PayBox.', $this->text_domain),
'default' => '32'
),
'paybox_wait_time' => array(
'title' => __('Paybox Checkout waiting time', $this->text_domain),
'type' => 'text',
'description' => __('Time to wait before to redirect to Paybox gateway (in milliseconds).', $this->text_domain),
'default' => '2000'
),
// 'return_url' => array(
// 'title' => __('Paybox return URL', $this->text_domain),
// 'type' => 'text',
// 'description' => __('Please enter the autoreponse URL for PayBox.', $this->text_domain),
// 'default' => '/paybox_autoresponse'
// ),
'callback_success_url' => array(
'title' => __('Successful Return Link', $this->text_domain),
'type' => 'text',
'description' => __('Please enter callback link from PayBox when transaction succeed', $this->text_domain) . ' (' . __('where you need to put the', $this->text_domain) . ' [' . THANKS_SHORTCODE . ']' . __('shortcode', $this->text_domain) . ')',
'default' => '/checkout/order-pbx-received/'
),
'callback_refused_url' => array(
'title' => __('Failed Return Link', $this->text_domain),
'type' => 'text',
'description' => __('Please enter callback link from PayBox when transaction is refused by gateway.', $this->text_domain),
'default' => '/checkout/order-pbx-refused/'
),
'callback_cancel_url' => array(
'title' => __('Cancel Return Link', $this->text_domain),
'type' => 'text',
'description' => __('Please enter back link from PayBox when enduser cancel transaction.', $this->text_domain),
'default' => '/checkout/order-pbx-canceled/'
),
'paybox_url' => array(
'title' => __('Paybox URL', $this->text_domain),
'type' => 'text',
'description' => __('Please enter the posting URL for paybox Form', $this->text_domain) . '<br/>' . __('For testing', $this->text_domain) . ' : https://preprod-tpeweb.paybox.com/cgi/MYchoix_pagepaiement.cgi<br/>' . __('For production', $this->text_domain) . ' : https://tpeweb.paybox.com/cgi/MYchoix_pagepaiement.cgi',
'default' => 'https://preprod-tpeweb.paybox.com/cgi/MYchoix_pagepaiement.cgi'
),
'prepost_message' => array(
'title' => __('Customer Message', $this->text_domain),
'type' => 'textarea',
'description' => __('Message to the user before redirecting to PayBox.', $this->text_domain),
'default' => __('You will be redirect to Paybox System payment gatway in a few seconds ... Please wait ...', $this->text_domain)
),
'paybox_key' => array(
'title' => __('Paybox Key for HMAC (optional if you use CGI)', $this->text_domain),
'type' => 'textarea',
'description' => __('Please enter the private secret Key generated at PayBox Backoffice.', $this->text_domain),
'default' => '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'
),
'paybox_exe' => array(
'title' => __('Complete path to PayBox CGI (optional if you use HMAC)', $this->text_domain),
'type' => 'textarea',
'description' => __('Location for Paybox executable', $this->text_domain) . ' (http://www1.paybox.com/telechargement_focus.aspx?cat=3)',
'default' => '/the/path/to/paybox.cgi'
)
);
}
/**
* Process the payment and return the result
*
* @access public
* @param int $order_id
* @return array
*/
function process_payment($order_id) {
global $woocommerce ;
$order = new WC_Order($order_id);
if(version_compare($woocommerce->version, '2.1.0', '<')) {
$pay_url = add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(woocommerce_get_page_id('pay'))));
} else {
$pay_url = $order->get_checkout_payment_url(true);
}
return array(
'result' => 'success',
'redirect' => $pay_url
);
}
function getParamPaybox($order_id) {
$exe = $this->paybox_exe;
$order = new WC_Order($order_id);
if (!empty($exe) && file_exists($exe)) {
$param = 'PBX_MODE=4'; // Envoi en ligne de commande
$param .= ' PBX_OUTPUT=B';
$param .= ' PBX_SITE=' . $this->paybox_site_id;
$param .= ' PBX_IDENTIFIANT=' . $this->paybox_identifiant;
$param .= ' PBX_RANG=' . $this->paybox_rang;
$param .= ' PBX_TOTAL=' . 100 * $order->get_total();
$param .= ' PBX_CMD=' . $order->id;
$param .= ' PBX_REPONDRE_A= '.$this->return_url;
$param .= ' PBX_PAYBOX= '.$this->paybox_url;
$param .= ' PBX_EFFECTUE='.$this->return_url . '/' . $this->callback_success_url;
$param .= ' PBX_REFUSE='.$this->return_url . '/' . $this->callback_refused_url;
$param .= ' PBX_ANNULE='.$this->return_url . '/' . $this->callback_cancel_url;
$param .= ' PBX_DEVISE=978';
$param .= ' PBX_DEVISE=SHA512'; // SHA512 (à paramétriser avec hash_algos() qd j'ai 2 min)
$param .= ' PBX_TIME=' . date('c');
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
$param .= ' PBX_RETOUR=order:R;erreur:E;carte:C;numauto:A;numtrans:S;numabo:B;montantbanque:M;sign:K';
else // Pour linux
$param .= ' PBX_RETOUR=order:R\\;erreur:E\\;carte:C\\;numauto:A\\;numtrans:S\\;numabo:B\\;montantbanque:M\\;sign:K';
$param .= ' PBX_PORTEUR=' . $order->billing_email; //. $order->customer_user;
$form_output = shell_exec($exe . ' ' . $param);
if(NULL == $form_output) $form_output = __('Error processing cgi execution... Check .cgi file permissions...',$this->text_domain);
} else { // No CGI -> Let's try HMAC
if (!empty($this->paybox_key)) { // Test à afiner ...
$param = 'PBX_SITE=' . $this->paybox_site_id;
$param .= '&PBX_RANG=' . $this->paybox_rang;
$param .= '&PBX_IDENTIFIANT=' . $this->paybox_identifiant;
$param .= '&PBX_TOTAL=' . 100 * $order->get_total();
$param .= '&PBX_DEVISE=978';
$param .= '&PBX_TYPEPAIEMENT=CARTE';
$param .= '&PBX_TYPECARTE=CB';
$param .= '&PBX_REPONDRE_A=' . $this->return_url;
$param .= '&PBX_EFFECTUE='.$this->return_url . '/' . $this->callback_success_url;
$param .= '&PBX_REFUSE='.$this->return_url . '/' . $this->callback_refused_url;
$param .= '&PBX_ANNULE='.$this->return_url . '/' . $this->callback_cancel_url;
$param .= '&PBX_CMD=' . $order->id;
$param .= '&PBX_PORTEUR=' . $order->billing_email; //. $order->customer_user;
$param .= '&PBX_RETOUR=order:R;erreur:E;carte:C;numauto:A;numtrans:S;numabo:B;montantbanque:M;sign:K';
$param .= '&PBX_HASH=SHA512'; // SHA512 (à paramétriser avec hash_algos() qd j'ai 2 min)
$param .= '&PBX_TIME=' . date('c');
$binKey = pack("H*", $this->paybox_key);
$hmac = strtoupper(hash_hmac('sha512', $param, $binKey));
$form_output = '<p>'.__('You will be redirected on Paybox plateform. If nothing happens please click on "Paybox" button below.', $this->text_domain).'</p>';
$form_output .= '<form method="POST" action="' . $this->paybox_url . '" name="PAYBOX">
<input type="hidden" name="PBX_SITE" value="' . $this->paybox_site_id . '">
<input type="hidden" name="PBX_RANG" value="' . $this->paybox_rang . '">
<input type="hidden" name="PBX_IDENTIFIANT" value="' . $this->paybox_identifiant . '">
<input type="hidden" name="PBX_TOTAL" value="' . (100 * $order->get_total()) . '">
<input type="hidden" name="PBX_DEVISE" value="978">
<input type="hidden" name="PBX_TYPEPAIEMENT" value="CARTE">
<input type="hidden" name="PBX_TYPECARTE" value="CB">
<input type="hidden" name="PBX_REPONDRE_A" value="'.$this->return_url .'">
<input type="hidden" name="PBX_EFFECTUE" value="'.$this->return_url . '/' . $this->callback_success_url .'">
<input type="hidden" name="PBX_REFUSE" value="'.$this->return_url . '/' . $this->callback_refused_url .'">
<input type="hidden" name="PBX_ANNULE" value="'.$this->return_url . '/' . $this->callback_cancel_url .'">
<input type="hidden" name="PBX_CMD" value="' . $order->id . '">
<input type="hidden" name="PBX_PORTEUR" value="' . $order->billing_email . '">
<input type="hidden" name="PBX_RETOUR" value="order:R;erreur:E;carte:C;numauto:A;numtrans:S;numabo:B;montantbanque:M;sign:K">
<input type="hidden" name="PBX_HASH" value="SHA512">
<input type="hidden" name="PBX_TIME" value="' . date('c') . '">
<input type="hidden" name="PBX_HMAC" value="'. $hmac. '">
<input type="submit" value="Paybox">
</form>';
}
else{
$form_output = __('Paybox Key must be setup for HMAC', $this->text_domain);
}
}
//form autosubmission
$form_output .= '<script>
function launchPaybox(){';
if (!empty($exe) && file_exists($exe)) {
//allow preprod url using CGI : dynamic replace form action and PBX_PAYBOX values
$form_output .= 'document.PAYBOX.action=\''.$this->paybox_url.'\';';
$form_output .= 'document.PAYBOX.PBX_PAYBOX.value=\''.$this->paybox_url.'\';';
}
$form_output .= 'document.PAYBOX.submit();
}
t=setTimeout("launchPaybox()",' . ((isset($this->paybox_wait_time) && is_numeric($this->paybox_wait_time)) ? $this->paybox_wait_time : '100') . ');
</script>';
echo $form_output ;
}
static function getRealIpAddr() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
$ip = $_SERVER['REMOTE_ADDR'];
return $ip;
}
function getErreurMsg($code_erreur) {
switch ($code_erreur) {
case '00000':
$erreur_msg = __('Opération réussie.', 'paybox_gateway');
break;
case '00001':
$erreur_msg = __('La connexion au centre d\'autorisation a échoué. Vous pouvez dans ce cas là effectuer les redirections des internautes vers le FQDN', 'paybox_gateway') . ' tpeweb1.paybox.com.';
break;
case '00002':
$erreur_msg = __('Une erreur de cohérence est survenue.', 'paybox_gateway');
break;
case '00003':
$erreur_msg = __('Erreur Paybox.', 'paybox_gateway');
break;
case '00004':
$erreur_msg = __('Numéro de porteur ou crytogramme visuel invalide.', 'paybox_gateway');
break;
case '00006':
$erreur_msg = __('Accès refusé ou site/rang/identifiant incorrect.', 'paybox_gateway');
break;
case '00008':
$erreur_msg = __('Date de fin de validité incorrecte.', 'paybox_gateway');
break;
case '00009':
$erreur_msg = __('Erreur de création d\'un abonnement.', 'paybox_gateway');
break;
case '00010':
$erreur_msg = __('Devise inconnue.', 'paybox_gateway');
break;
case '00011':
$erreur_msg = __('Montant incorrect.', 'paybox_gateway');
break;
case '00015':
$erreur_msg = __('Paiement déjà effectué', 'paybox_gateway');
break;
case '00016':
$erreur_msg = __('Abonné déjà existant (inscription nouvel abonné). Valeur \'U\' de la variable PBX_RETOUR.', 'paybox_gateway');
break;
case '00021':
$erreur_msg = __('Carte non autorisée.', 'paybox_gateway');
break;
case '00029':
$erreur_msg = __('Carte non conforme. Code erreur renvoyé lors de la documentation de la variable « PBX_EMPREINTE ».', 'paybox_gateway');
break;
case '00030':
$erreur_msg = __('Temps d\'attente > 15 mn par l\'internaute/acheteur au niveau de la page de paiements.', 'paybox_gateway');
break;
case '00031':
case '00032':
$erreur_msg = __('Réservé', 'paybox_gateway');
break;
case '00033':
$erreur_msg = __('Code pays de l\'adresse IP du navigateur de l\'acheteur non autorisé.', 'paybox_gateway');
break;
// Nouveaux codes : 11/2013 (v6.1)
case '00040':
$erreur_msg = __('Opération sans authentification 3-DSecure, bloquée par le filtre', 'paybox_gateway');
break;
case '99999':
$erreur_msg = __('Opération en attente de validation par l\'emmetteur du moyen de paiement.', 'paybox_gateway');
break;
default:
if (substr($code_erreur, 0, 3) == '001')
$erreur_msg = __('Paiement refusé par le centre d\'autorisation. En cas d\'autorisation de la transaction par le centre d\'autorisation de la banque, le code erreur \'00100\' sera en fait remplacé directement par \'00000\'.', 'paybox_gateway');
else
$erreur_msg = __('Pas de message', 'paybox_gateway');
break;
}
return $erreur_msg;
}
}
// Fin de la classe
?>