forked from chtipepere/woocommerceAtosPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
396 lines (345 loc) · 15.4 KB
/
index.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
<?php
/**
* Plugin Name: WoocommerceAtos
* Text Domain: woocommerce-atos
* Plugin URI: https://github.com/chtipepere/woocommerceAtosPlugin
* Description: Extends Woocommerce with Atos SIPS gateway (French bank).
* Version: 1.2.3
* Author: πR
**/
// Exit if accessed directly
if (false === defined( 'ABSPATH' )) {
exit;
}
if ( ! class_exists( 'WooCommerce' )) {
function woocommerce_required()
{
echo sprintf(
'<div class="error"><p>%s</p></div>',
__( '<strong>Error!</strong> Woocommerce is mandatory. Please install it.', 'woocommerce-atos' )
);
return;
}
add_action( 'admin_notices', 'woocommerce_required' );
}
define( 'WOOCOMMERCEATOS_PHP_VERSION', '5.3' );
define( 'WOOCOMMERCE_MINIMUM_VERSION', '2.3.5' );
if ( ! version_compare( PHP_VERSION, WOOCOMMERCEATOS_PHP_VERSION, '>=' )) {
function woocommerce_required_version()
{
echo sprintf(
'<div class="error"><p>%s</p></div>',
sprintf(
__( '<strong>Error!</strong> WoocommerceAtos requires at least PHP %s! Your version is: %s. Please upgrade.',
'woocommerce-atos' ),
WOOCOMMERCEATOS_PHP_VERSION,
PHP_VERSION
)
);
return;
}
add_action( 'admin_notices', 'woocommerce_required_version' );
}
if ( ! version_compare( Woocommerce::instance()->version, WOOCOMMERCE_MINIMUM_VERSION, '>=' )) {
function woocommerce_minimum_version()
{
echo sprintf(
'<div class="error"><p>%s</p></div>',
sprintf( __( '<strong>Error!</strong> WoocommerceAtos requires at least Woocommerce %s! Your version is: %s. Please upgrade.',
'woocommerce-atos' ),
WOOCOMMERCE_MINIMUM_VERSION,
Woocommerce::instance()->version
)
);
return;
}
add_action( 'admin_notices', 'woocommerce_minimum_version' );
}
if (function_exists( 'add_action' )) {
add_action( 'plugins_loaded', 'woocommerce_atos_init', 0 );
}
function woocommerce_atos_init()
{
if ( ! class_exists( 'WC_Payment_Gateway' )) {
return;
}
/** Translations */
$plugin_dir = basename( dirname( __FILE__ ) );
load_plugin_textdomain( 'woocommerce-atos', false, sprintf( '%s/languages/', $plugin_dir ) );
/**
* Add the gateway to Woocommerce
*/
add_filter( 'woocommerce_payment_gateways', function ( $methods ) {
$methods[] = 'Woocommerce_atos';
return $methods;
} );
include_once( 'automatic_response.php' );
add_filter( sprintf( 'plugin_action_links_%s', plugin_basename( __FILE__ ) ), 'add_action_links' );
/**
* @param $links
*
* @return array
*/
function add_action_links( $links )
{
$mylinks = array(
sprintf( '<a href="%s">%s</a>',
admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=woocommerce_atos'),
__( 'Settings', 'woocommerce-atos' ) ),
sprintf( '<a href="https://github.com/chtipepere/woocommerceAtosPlugin/blob/master/README.md">%s</a>',
__( 'Docs', 'woocommerce-atos' ) )
);
return array_merge( $links, $mylinks );
}
/**
* Gateway class
*/
class Woocommerce_atos extends WC_Payment_Gateway
{
protected $params;
public $msg = array();
public $settings = array();
public $form_fields = array();
public function __construct()
{
// Go wild in here
$this->id = 'woocommerce_atos';
$this->icon = sprintf( '%s/%s/images/logo.gif', WP_PLUGIN_URL,
plugin_basename( dirname( __FILE__ ) ) );
$this->has_fields = false;
$this->method_title = 'Atos';
$this->method_description = __( 'France based ATOS Worldline SIPS is the leading secure payment solution in Europe. Atos works by sending the user to your bank to enter their payment information.',
'woocommerce-atos' );
$this->init_form_fields();
$this->init_settings();
$this->description = $this->get_option( 'woocommerce_atos_description' );
$this->enabled = $this->get_option( 'woocommerce_atos_is_enabled' );
$this->title = $this->get_option( 'woocommerce_atos_title' );
$this->merchant_id = $this->get_option( 'woocommerce_atos_merchant_id' );
$this->merchant_name = $this->get_option( 'woocommerce_atos_merchant_name' );
$this->pathfile = $this->get_option( 'woocommerce_atos_pathfile' );
$this->path_bin_request = $this->get_option( 'woocommerce_atos_path_bin_request' );
$this->path_bin_response = $this->get_option( 'woocommerce_atos_path_bin_response' );
$this->cancel_return_url = $this->get_option( 'woocommerce_atos_cancel_return_url' );
$this->automatic_response_url = $this->get_option( 'woocommerce_atos_automatic_response_url' );
$this->normal_return_url = $this->get_option( 'woocommerce_atos_normal_return_url' );
$this->logo_id = $this->get_option( 'woocommerce_atos_logo_id' );
$this->logo_id2 = $this->get_option( 'woocommerce_atos_logo_id2' );
$this->advert = $this->get_option( 'woocommerce_atos_advert' );
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id,
array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_receipt_' . $this->id, array( $this, 'receipt_page' ) );
}
public function init_form_fields()
{
$this->form_fields = array(
'woocommerce_atos_is_enabled' => array(
'title' => __( 'Enable Atos', 'woocommerce-atos' ),
'type' => 'checkbox',
'label' => __( 'Enable Atos SIPS Module.', 'woocommerce-atos' ),
'default' => 'no'
),
'woocommerce_atos_title' => array(
'title' => sprintf( __( 'Atos Standard %s', 'woocommerce-atos' ),
'<img style="vertical-align:middle;margin-top:-4px;margin-left:10px;" src="' . WP_PLUGIN_URL . '/' . plugin_basename( dirname( __FILE__ ) ) . '/images/logo.gif" alt="Atos">' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.',
'woocommerce-atos' ),
'default' => __( 'Credit card', 'woocommerce-atos' )
),
'woocommerce_atos_description' => array(
'title' => __( 'Description:', 'woocommerce-atos' ),
'type' => 'textarea',
'description' => __( 'Atos works by sending the user to your bank to enter their payment information.',
'woocommerce-atos' ),
'default' => __( 'Paiement sécurisé par Carte Bancaire (Atos)', 'woocommerce-atos' )
),
'woocommerce_atos_merchant_id' => array(
'title' => __( 'Merchant id', 'woocommerce-atos' ),
'type' => 'text',
'description' => __( 'Merchant id given by your bank', 'woocommerce-atos' ),
'default' => '014022286611112'
),
'woocommerce_atos_pathfile' => array(
'title' => __( 'Pathfile file', 'woocommerce-atos' ),
'type' => 'text',
'description' => __( 'Path to the pathfile file given by your bank', 'woocommerce-atos' ),
'default' => '/var/www/site/param/pathfile'
),
'woocommerce_atos_path_bin_request' => array(
'title' => __( 'Request bin file path', 'woocommerce-atos' ),
'type' => 'text',
'description' => __( 'Path to the request bin file given by your bank', 'woocommerce-atos' ),
'default' => '/var/www/site/bin/static/request'
),
'woocommerce_atos_path_bin_response' => array(
'title' => __( 'Response bin file path', 'woocommerce-atos' ),
'type' => 'text',
'description' => __( 'Path to the response bin file given by your bank', 'woocommerce-atos' ),
'default' => '/var/www/site/bin/static/response'
),
'woocommerce_atos_cancel_return_url' => array(
'title' => __( 'Cancel return url', 'woocommerce-atos' ),
'type' => 'text',
'description' => __( 'Return url in case of canceled transaction', 'woocommerce-atos' ),
'default' => site_url( '/cancel' )
),
'woocommerce_atos_normal_return_url' => array(
'title' => __( 'Normal return url', 'woocommerce-atos' ),
'type' => 'text',
'description' => __( 'Return url when a user click on the << Back to the shop >> button',
'woocommerce-atos' ),
'default' => site_url( '/thankyou' )
),
'woocommerce_atos_logo_id' => array(
'title' => __( 'Logo id', 'woocommerce-atos' ),
'type' => 'text',
'description' => __( 'Left image on Atos page', 'woocommerce-atos' ),
'default' => 'logo_id.gif'
),
'woocommerce_atos_logo_id2' => array(
'title' => __( 'Logo id2', 'woocommerce-atos' ),
'type' => 'text',
'description' => __( 'Right image on Atos page', 'woocommerce-atos' ),
'default' => 'logo_id2.gif'
),
'woocommerce_atos_advert' => array(
'title' => __( 'Advert', 'woocommerce-atos' ),
'type' => 'text',
'description' => __( 'Center image on Atos page', 'woocommerce-atos' ),
'default' => 'advert.gif'
),
'woocommerce_atos_automatic_response_url' => array(
'title' => __( 'Automatic response url', 'woocommerce-atos' ),
'type' => 'text',
'description' => __( 'URL called in case of success payment', 'woocommerce-atos' ),
'default' => site_url( '?page=12' )
)
);
}
/**
* Process the payment and return the result
*
* @param $order_id
*
* @return array
*/
public function process_payment( $order_id )
{
$order = new WC_order( $order_id );
return array(
'result' => 'success',
'redirect' => $order->get_checkout_payment_url( true )
);
}
/**
* There are no payment fields for atos, but we want to show the description if set.
**/
public function payment_fields()
{
if ($this->description) {
echo wpautop( wptexturize( $this->description ) );
}
}
/**
* @param $order_id
*/
public function receipt_page( $order_id )
{
echo sprintf(
'<p>%s</p>',
__( 'Thank you for your order, please click the button below to pay.', 'woocommerce-atos' )
);
echo $this->generate_atos_form( $order_id );
}
public function thankyou_page()
{
if ($this->description) {
echo wpautop( wptexturize( $this->mercitxt ) );
}
}
/**
* @param $content
*
* @return string
*/
public function showMessage( $content )
{
return sprintf(
'<div class="box %s-box">%s</div>%s',
$this->msg['class'],
$this->msg['message'],
$content
);
}
/**
* Generate atos button link
*
* @param $order_id
*
* @return string
*/
public function generate_atos_form( $order_id )
{
// Contains every informations about the basket and the customer
$order = new WC_order( $order_id );
$pathfile = $this->pathfile;
$path_bin_request = $this->path_bin_request;
$this->addParam( 'merchant_id', $this->merchant_id );
$this->addParam( 'merchant_country', 'fr' );
$this->addParam( 'amount', $this->calcAmount( $order->order_total ) );
$this->addParam( 'currency_code', 978 );
$this->addParam( 'pathfile', $pathfile );
$this->addParam( 'normal_return_url', $this->normal_return_url );
$this->addParam( 'cancel_return_url', $this->cancel_return_url );
$this->addParam( 'automatic_response_url', $this->automatic_response_url );
$this->addParam( 'language', 'fr' );
$this->addParam( 'payment_means', 'CB,2,VISA,2,MASTERCARD,2' );
$this->addParam( 'header_flag', 'no' );
$this->addParam( 'order_id', $order_id );
$this->addParam( 'logo_id', $this->logo_id );
$this->addParam( 'logo_id2', $this->logo_id2 );
$this->addParam( 'advert', $this->advert );
$parm = escapeshellcmd( $this->getParams() );
$result = exec( "$path_bin_request $parm" );
$codeAndError = explode( '!', "$result" );
$code = $codeAndError[1];
$error = $codeAndError[2];
if (( $code == '' ) && ( $error == '' )) {
return sprintf( '<p>%s %s</p>', __( 'Error calling the atos api: exec request not found',
'woocommerce-atos' ), $path_bin_request );
} elseif ($code != 0) {
return sprintf( '<p>%s %s</p>', __( 'Atos API error:', 'woocommerce-atos' ), $error );
} else {
// Display form with bank cards list
return $codeAndError[3];
}
}
/**
* @param $key
* @param $value
*/
protected function addParam( $key, $value )
{
$param = sprintf( '%s=%s', $key, $value );
$this->params .= sprintf( ' %s', $param );
}
/**
* @return mixed
*/
protected function getParams()
{
return $this->params;
}
/**
* @param $total
*
* @return string
*/
private function calcAmount( $total )
{
$amount = ( $total ) * 100;
return str_pad( $amount, 3, '0', STR_PAD_LEFT );
}
}
}