/
easy-digital-downloads-paybox-gayteway.php
352 lines (317 loc) · 13.5 KB
/
easy-digital-downloads-paybox-gayteway.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
<?php
/*
Plugin Name: Easy Digital Downloads Paybox
Plugin URI: https://github.com/romainberger/easy-digital-downloads-paybox-gateway
Description: Paybox payment gateway for the Easy Digital Downloads plugin
Version: 0.1.0
Author: Romain Berger <romain@romainberger.com>
Author URI: http://romainberger.com
License: MIT
*/
class EasyDigitalDownloadsPaybox {
private $purchaseDatas;
private $payment;
private $gateway;
public function __construct() {
add_action('edd_payment_gateways', array($this, 'displayPayboxPayment'));
add_action('edd_paybox_cc_form', array($this, 'displayCreditCardForm'));
add_action('edd_gateway_paybox', array($this, 'processPayboxPurchase'));
add_filter('edd_settings_gateways', array($this, 'displaySettings'));
}
/**
* Add the payment gateway on the admin panel
*/
public function displayPayboxPayment($gateways) {
$gateways['paybox'] = array(
'admin_label' => __('Paybox', 'edd'),
'checkout_label' => __('Paybox', 'edd')
);
return $gateways;
}
/**
* Display the credit card form on the checkout page
* Basically a duplicate of the default form but cleaned
*/
public function displayCreditCardForm() {
ob_start();
do_action('edd_before_cc_fields');
?>
<fieldset id="edd_cc_fields" class="edd-do-validate">
<span><legend><?php _e( 'Credit Card Info', 'edd' ); ?></legend></span>
<?php if( is_ssl() ) : ?>
<div id="edd_secure_site_wrapper">
<span class="padlock"></span>
<span><?php _e( 'This is a secure SSL encrypted payment.', 'edd' ); ?></span>
</div>
<?php endif; ?>
<p id="edd-card-number-wrap">
<label for="card_number" class="edd-label">
<?php _e( 'Card Number', 'edd' ); ?>
<span class="edd-required-indicator">*</span>
<span class="card-type"></span>
</label>
<span class="edd-description"><?php _e( 'The (typically) 16 digits on the front of your credit card.', 'edd' ); ?></span>
<input type="text" autocomplete="off" name="card_number" id="card_number" class="card-number edd-input required" placeholder="<?php _e( 'Card number', 'edd' ); ?>" />
</p>
<p id="edd-card-cvc-wrap">
<label for="card_cvc" class="edd-label">
<?php _e( 'CVC', 'edd' ); ?>
<span class="edd-required-indicator">*</span>
</label>
<span class="edd-description"><?php _e( 'The 3 digit (back) or 4 digit (front) value on your card.', 'edd' ); ?></span>
<input type="text" size="4" autocomplete="off" name="card_cvc" id="card_cvc" class="card-cvc edd-input required" placeholder="<?php _e( 'Security code', 'edd' ); ?>" />
</p>
<p id="edd-card-name-wrap">
<label for="card_name" class="edd-label">
<?php _e( 'Name on the Card', 'edd' ); ?>
<span class="edd-required-indicator">*</span>
</label>
<span class="edd-description"><?php _e( 'The name printed on the front of your credit card.', 'edd' ); ?></span>
<input type="text" autocomplete="off" name="card_name" id="card_name" class="card-name edd-input required" placeholder="<?php _e( 'Card name', 'edd' ); ?>" />
</p>
<?php do_action( 'edd_before_cc_expiration' ); ?>
<p class="card-expiration">
<label for="card_exp_month" class="edd-label">
<?php _e( 'Expiration (MM/YY)', 'edd' ); ?>
<span class="edd-required-indicator">*</span>
</label>
<span class="edd-description"><?php _e( 'The date your credit card expires, typically on the front of the card.', 'edd' ); ?></span>
<select id="card_exp_month" name="card_exp_month" class="card-expiry-month edd-select edd-select-small required">
<?php for( $i = 1; $i <= 12; $i++ ) { echo '<option value="' . $i . '">' . sprintf ('%02d', $i ) . '</option>'; } ?>
</select>
<span class="exp-divider"> / </span>
<select id="card_exp_year" name="card_exp_year" class="card-expiry-year edd-select edd-select-small required">
<?php for( $i = date('Y'); $i <= date('Y') + 10; $i++ ) { echo '<option value="' . $i . '">' . substr( $i, 2 ) . '</option>'; } ?>
</select>
</p>
<?php do_action( 'edd_after_cc_expiration' ); ?>
</fieldset>
<?php
echo ob_get_clean();
}
/**
* Process the credit card form
*
* @param array $purchaseDatas
*/
public function processPayboxPurchase($purchaseDatas) {
$this->purchaseDatas = $purchaseDatas;
$this->gateway = $purchaseDatas['post_data']['edd-gateway'];
$paymentDatas = array(
'price' => $purchaseDatas['price'],
'date' => $purchaseDatas['date'],
'user_email' => $purchaseDatas['user_email'],
'purchase_key' => $purchaseDatas['purchase_key'],
'currency' => edd_get_currency(),
'downloads' => $purchaseDatas['downloads'],
'user_info' => $purchaseDatas['user_info'],
'cart_details' => $purchaseDatas['cart_details'],
'gateway' => 'paybox',
'status' => 'pending'
);
// Record the pending payment
$this->payment = edd_insert_payment($paymentDatas);
$orderTotal = $purchaseDatas['price'];
$id = $purchaseDatas['user_info']['id'].intval($purchaseDatas['subtotal']);
$cardNumber = $purchaseDatas['card_info']['card_number'];
$cvv = $purchaseDatas['card_info']['card_cvc'];
$monthExpire = $purchaseDatas['card_info']['card_exp_month'];
$yearExpire = $purchaseDatas['card_info']['card_exp_year'];
$settings = $this->getSettings();
$url = $settings['preprod'] ? $settings['url_preprod'] : $settings['url_prod'];
$fields = array(
'DATEQ' => date('Ydm'),
'TYPE' => '00001',
'NUMQUESTION' => time(),
'MONTANT' => intval($orderTotal),
'SITE' => $settings['site'],
'RANG' => $settings['rang'],
'REFERENCE' => 'test',
'VERSION' => $settings['version'],
'CLE' => $settings['key'],
'IDENTIFIANT' => $settings['id'],
'DEVISE' => '978',
'PORTEUR' => $cardNumber,
'DATEVAL' => str_pad($monthExpire, 2, '0', STR_PAD_LEFT).substr($yearExpire, 2, 4),
'CVV' => $cvv,
'ACTIVITE' => '024',
'ARCHIVAGE' => 'AXZ130968CT2',
'DIFFERE' => '000',
'NUMAPPEL' => '',
'NUMTRANS' => '',
'AUTORISATION' => '',
'PAYS' => ''
);
$this->performPayment($url, $fields);
}
/**
* Settings for the admin panel
*
* @param array $settings
* @return array
*/
public function displaySettings($settings) {
$payboxSettings = array(
'paybox' => array(
'id' => 'paybox',
'name' => '<strong>' . __('Paybox Settings', 'edd') . '</strong>',
'desc' => __('Configure the Paybox settings', 'edd'),
'type' => 'header'
),
'paybox_site' => array(
'id' => 'paybox_site',
'name' => __('Site', 'edd'),
'type' => 'text',
'size' => 'regular'
),
'paybox_rang' => array(
'id' => 'paybox_rang',
'name' => __('Rang', 'edd'),
'type' => 'text',
'size' => 'regular'
),
'paybox_id' => array(
'id' => 'paybox_id',
'name' => __('Id', 'edd'),
'type' => 'text',
'size' => 'regular'
),
'paybox_key' => array(
'id' => 'paybox_key',
'name' => __('Key', 'edd'),
'type' => 'text',
'size' => 'regular'
),
'paybox_version' => array(
'id' => 'paybox_version',
'name' => __('Version', 'edd'),
'type' => 'text',
'size' => 'regular'
),
'paybox_url_prod' => array(
'id' => 'paybox_url_prod',
'name' => __('Payment url (production)', 'edd'),
'type' => 'text',
'size' => 'regular'
),
'paybox_url_prod_2' => array(
'id' => 'paybox_url_prod_2',
'name' => __('Payment url (production backup)', 'edd'),
'type' => 'text',
'size' => 'regular',
'desc' => '<p class="description">'.__('Url to use in case the first url is not available', 'edd').'</p>'
),
'paybox_url_preprod' => array(
'id' => 'paybox_url_preprod',
'name' => __('Payment url (preprod)', 'edd'),
'type' => 'text',
'size' => 'regular'
),
'paybox_preprod' => array(
'id' => 'paybox_preprod',
'name' => __('Preprod', 'edd'),
'type' => 'checkbox'
)
);
return array_merge($settings, $payboxSettings);
}
/**
* Get the settings for the plugin
*
* @return array
*/
private function getSettings() {
$eddSettings = get_option('edd_settings');
$preprod = isset($eddSettings['paybox_preprod']) && $eddSettings['paybox_preprod'] ? true : false;
return array(
'rang' => $eddSettings['paybox_rang'],
'site' => $eddSettings['paybox_site'],
'id' => $eddSettings['paybox_id'],
'key' => $eddSettings['paybox_key'],
'version' => $eddSettings['paybox_version'],
'url_prod' => $eddSettings['paybox_url_prod'],
'url_prod_2' => $eddSettings['paybox_url_prod_2'],
'url_preprod' => $eddSettings['paybox_url_preprod'],
'preprod' => $preprod,
);
}
/**
* Simple wrapper for the api call / response treatement
*
* @param string $url
* @param array $fields
*/
private function performPayment($url, $fields) {
$result = $this->curlAction($url, $fields);
$result = $this->getDataTransaction($result);
if ($result['CODEREPONSE'] == '00000') {
edd_update_payment_status($this->payment, 'publish');
edd_complete_purchase($this->payment, 'publish', 'pending');
foreach ($this->purchaseDatas['downloads'] as $download) {
$log = edd_record_log('Payment', 'Payment', $download['id'], 'sale');
update_post_meta($log, '_edd_log_payment_id', $this->payment);
}
edd_empty_cart();
edd_send_to_success_page();
}
else if ($result['CODEREPONSE'] == '00001' || $result['CODEREPONSE'] == '00003') {
$settings = $this->getSettings();
// if the first prod url failed try the second
if (!$settings['preprod'] && $url !== $settings['url_prod_2'] && isset($settings['url_prod_2'])) {
$this->performPayment($settings['url_prod_2'], $fields);
}
edd_record_gateway_error(__('Payment Error', 'edd'), __('Payment gateways unavailable', 'edd'));
edd_send_back_to_checkout('?payment-mode='.$this->gateway);
}
else {
edd_record_gateway_error(__('Payment Error', 'edd'), $result['COMMENTAIRE']);
edd_send_back_to_checkout('?payment-mode='.$this->gateway);
}
}
/**
* Simple wrapper to perform curl
*
* @param string $url
* @param array $fields
* @return curl response
*/
private function curlAction($url, $fields) {
$ch = curl_init();
$fields_string = '';
foreach ($fields as $key => $value) {
$fields_string .= $key.'='.$value.'&';
}
$fields_string = rtrim($fields_string, '&');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
if (curl_errno($ch)) {
die(curl_error($ch));
}
else {
curl_close($ch);
}
return $result;
}
/**
* Decode the response to an array
*
* @param string $result
* @return array
*/
private function getDataTransaction($result) {
$results = explode('&', $result);
$transaction = array();
foreach ($results as $result) {
$resultTmp = explode('=', $result);
if (isset($resultTmp[1])) {
$transaction[$resultTmp[0]] = $resultTmp[1];
}
}
return $transaction;
}
}
new EasyDigitalDownloadsPaybox;