-
Notifications
You must be signed in to change notification settings - Fork 3
/
gateway-buckaroo-creditcard.php
519 lines (456 loc) · 16 KB
/
gateway-buckaroo-creditcard.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<?php
require_once __DIR__ . '/library/api/paymentmethods/creditcard/creditcard.php';
/**
* @package Buckaroo
*/
class WC_Gateway_Buckaroo_Creditcard extends WC_Gateway_Buckaroo {
const PAYMENT_CLASS = BuckarooCreditCard::class;
public const SHOW_IN_CHECKOUT_FIELD = 'show_in_checkout';
public $creditCardProvider = array();
protected $creditcardmethod;
protected $creditcardpayauthorize;
public function __construct() {
$this->setParameters();
$this->setCreditcardIcon();
$this->has_fields = true;
parent::__construct();
$this->addRefundSupport();
}
/**
* Set gateway parameters
*
* @return void
*/
public function setParameters() {
$this->id = 'buckaroo_creditcard';
$this->title = 'Credit and debit card';
$this->method_title = 'Buckaroo Credit and debit card';
}
/**
* Set credicard icon
*
* @return void
*/
public function setCreditcardIcon() {
$this->setIcon( '24x24/cc.gif', 'svg/creditcards.svg' );
}
/** @inheritDoc */
protected function setProperties() {
parent::setProperties();
$this->creditCardProvider = $this->get_option( 'AllowedProvider', array() );
$this->creditcardmethod = $this->get_option( 'creditcardmethod', 'redirect' );
$this->creditcardpayauthorize = $this->get_option( 'creditcardpayauthorize', 'Pay' );
}
/**
* Can the order be refunded
*
* @param integer $order_id
* @param integer $amount defaults to null
* @param string $reason
* @return callable|string function or error
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
$action = ucfirst( isset( $this->creditcardpayauthorize ) ? $this->creditcardpayauthorize : 'pay' );
if ( $action == 'Authorize' ) {
$captures = get_post_meta( $order_id, 'buckaroo_capture', false );
$previous_refunds = get_post_meta( $order_id, 'buckaroo_refund', false );
if ( $captures == false || count( $captures ) < 1 ) {
return new WP_Error( 'error_refund_trid', __( 'Order is not captured yet, you can only refund captured orders' ) );
}
// Merge captures with previous refunds
foreach ( $captures as &$captureJson ) {
$capture = json_decode( $captureJson, true );
foreach ( $previous_refunds as &$refundJson ) {
$refund = json_decode( $refundJson, true );
if ( isset( $refund['OriginalCaptureTransactionKey'] ) && $capture['OriginalTransactionKey'] == $refund['OriginalCaptureTransactionKey'] ) {
if ( $capture['amount'] >= $refund['amount'] ) {
$capture['amount'] -= $refund['amount'];
$refund['amount'] = 0;
} else {
$refund['amount'] -= $capture['amount'];
$capture['amount'] = 0;
}
}
$refundJson = json_encode( $refund );
}
$captureJson = json_encode( $capture );
}
$captures = json_decode( json_encode( $captures ), true );
$refundQueue = array();
// Find free `slots` in captures
foreach ( $captures as $captureJson ) {
$capture = json_decode( $captureJson, true );
if ( $amount > 0 ) {
if ( $amount > $capture['amount'] ) {
$refundQueue[ $capture['OriginalTransactionKey'] ] = $capture['amount'];
$amount -= $capture['amount'];
} else {
$refundQueue[ $capture['OriginalTransactionKey'] ] = $amount;
$amount = 0;
}
}
}
// Check if something cannot be refunded
$NotRefundable = false;
if ( $amount > 0 ) {
$NotRefundable = true;
}
if ( $NotRefundable ) {
return new WP_Error( 'error_refund_trid', __( 'Refund amount cannot be bigger than the amount you have captured' ) );
}
$refund_result = array();
foreach ( $refundQueue as $OriginalTransactionKey => $amount ) {
if ( $amount > 0 ) {
$refund_result[] = $this->process_partial_refunds( $order_id, $amount, $reason, $OriginalTransactionKey );
}
}
foreach ( $refund_result as $result ) {
if ( $result !== true ) {
if ( isset( $result->errors['error_refund'][0] ) ) {
return new WP_Error( 'error_refund_trid', __( $result->errors['error_refund'][0] ) );
} else {
return new WP_Error( 'error_refund_trid', __( 'Unexpected error occured while processing refund, please check your transactions in the Buckaroo plaza.' ) );
}
}
}
return true;
} else {
return $this->process_partial_refunds( $order_id, $amount, $reason );
}
}
/**
* Can the order be refunded
*
* @param integer $order_id
* @param integer $amount defaults to null
* @param string $reason
* @return callable|string function or error
*/
public function process_partial_refunds(
$order_id,
$amount = null,
$reason = '',
$OriginalTransactionKey = null,
$line_item_totals = null,
$line_item_tax_totals = null,
$line_item_qtys = null
) {
$order = wc_get_order( $order_id );
if ( ! $this->can_refund_order( $order ) ) {
return new WP_Error( 'error_refund_trid', __( 'Refund failed: Order not in ready state, Buckaroo transaction ID do not exists.' ) );
}
update_post_meta( $order_id, '_pushallowed', 'busy' );
/** @var BuckarooCreditCard */
$creditcard = $this->createCreditRequest( $order, $amount, $reason );
if ( $line_item_qtys === null ) {
$line_item_qtys = buckaroo_request_sanitized_json( 'line_item_qtys' );
}
if ( $line_item_totals === null ) {
$line_item_totals = buckaroo_request_sanitized_json( 'line_item_totals' );
}
if ( $line_item_tax_totals === null ) {
$line_item_tax_totals = buckaroo_request_sanitized_json( 'line_item_tax_totals' );
}
if ( $OriginalTransactionKey !== null ) {
$creditcard->OriginalTransactionKey = $OriginalTransactionKey;
}
$creditcard->setType(
get_post_meta(
$order->get_id(),
'_payment_method_transaction',
true
)
);
try {
$creditcard->checkRefundData(
$creditcard->getOrderRefundData()
);
$response = $creditcard->Refund();
} catch ( exception $e ) {
update_post_meta( $order_id, '_pushallowed', 'ok' );
return new WP_Error( 'refund_error', __( $e->getMessage() ) );
}
$final_response = fn_buckaroo_process_refund( $response ?? null, $order, $amount, $this->currency );
if ( $final_response === true ) {
// Store the transaction_key together with refunded products, we need this for later refunding actions
$refund_data = json_encode(
array(
'OriginalTransactionKey' => $response->transactions,
'OriginalCaptureTransactionKey' => $creditcard->OriginalTransactionKey,
'amount' => $amount,
)
);
add_post_meta( $order_id, 'buckaroo_refund', $refund_data, false );
}
return $final_response;
}
/**
* Validate fields
*
* @return void;
*/
public function validate_fields() {
parent::validate_fields();
$issuer = $this->request( $this->id . '-creditcard-issuer' );
if ( $issuer === null ) {
wc_add_notice( __( 'Select a credit or debit card.', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
if ( ! in_array(
$issuer,
array(
'amex',
'cartebancaire',
'cartebleuevisa',
'dankort',
'maestro',
'mastercard',
'nexi',
'postepay',
'visa',
'visaelectron',
'vpay',
)
) ) {
wc_add_notice( __( 'A valid credit card is required.', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
if ( $this->get_option( 'creditcardmethod' ) == 'encrypt' && $this->isSecure() ) {
$card_year = $this->request( $this->id . '-cardyear' );
if ( $card_year === null ) {
wc_add_notice( __( 'Enter expiration year field', 'wc-buckaroo-bpe-gateway' ), 'error' );
return;
}
$fullYear = date( 'Y' );
$year = date( 'y' );
if ( (int) $card_year < (int) $fullYear && strlen( $card_year ) === 4 ) {
wc_add_notice( __( 'Enter valid expiration year', 'wc-buckaroo-bpe-gateway' ), 'error' );
return;
}
if ( (int) $card_year < (int) $year && strlen( $card_year ) !== 4 ) {
wc_add_notice( __( 'Enter valid expiration year', 'wc-buckaroo-bpe-gateway' ), 'error' );
return;
}
}
return;
}
/**
* Process payment
*
* @param integer $order_id
* @return callable fn_buckaroo_process_response()
*/
public function process_payment( $order_id ) {
$this->setOrderCapture(
$order_id,
'Creditcard',
$this->request( $this->id . '-creditcard-issuer' )
);
$order = getWCOrder( $order_id );
/** @var BuckarooCreditCard */
$creditcard = $this->createDebitRequest( $order );
$creditCardMethod = isset( $this->creditcardmethod ) ? $this->creditcardmethod : 'redirect';
$creditCardPayAuthorize = isset( $this->creditcardpayauthorize ) ? $this->creditcardpayauthorize : 'pay';
$customVars = array();
if ( $this->request( $this->id . '-encrypted-data' ) !== null ) {
$customVars['CreditCardDataEncrypted'] = $this->request( $this->id . '-encrypted-data' );
} else {
$customVars['CreditCardDataEncrypted'] = null;
}
if ( $this->request( $this->id . '-creditcard-issuer' ) !== null ) {
$customVars['CreditCardIssuer'] = $this->request( $this->id . '-creditcard-issuer' );
} else {
$customVars['CreditCardIssuer'] = null;
}
if ( $creditCardMethod == 'encrypt' && $this->isSecure() ) {
if ( $creditCardPayAuthorize == 'pay' ) {
$response = $creditcard->PayEncrypt( $customVars );
} elseif ( $creditCardPayAuthorize == 'authorize' ) {
$response = $creditcard->AuthorizeEncrypt( $customVars, $order );
} else {
wc_add_notice( __( 'The type of credit card request is not defined. Contact Buckaroo.', 'wc-buckaroo-bpe-gateway' ), 'error' );
return;
}
return fn_buckaroo_process_response( $this, $response );
}
if ( $creditCardPayAuthorize == 'pay' ) {
$response = $creditcard->Pay( $customVars );
} elseif ( $creditCardPayAuthorize == 'authorize' ) {
$response = $creditcard->AuthorizeCC( $customVars, $order );
} else {
wc_add_notice( __( 'The type of credit card request is not defined. Contact Buckaroo.', 'wc-buckaroo-bpe-gateway' ), 'error' );
return;
}
return fn_buckaroo_process_response( $this, $response );
}
public function process_capture() {
$order_id = $this->request( 'order_id' );
if ( $order_id === null || ! is_numeric( $order_id ) ) {
return $this->create_capture_error( __( 'A valid order number is required' ) );
}
$capture_amount = $this->request( 'capture_amount' );
if ( $capture_amount === null || ! is_scalar( $capture_amount ) ) {
return $this->create_capture_error( __( 'A valid capture amount is required' ) );
}
$order = getWCOrder( $order_id );
/** @var BuckarooCreditCard */
$creditcard = $this->createDebitRequest( $order );
$creditcard->amountDedit = str_replace( ',', '.', $capture_amount );
$creditcard->OriginalTransactionKey = $order->get_transaction_id();
$creditcard->channel = BuckarooConfig::CHANNEL_BACKOFFICE;
$customVars['CreditCardIssuer'] = get_post_meta( $order->get_id(), '_wc_order_payment_issuer', true );
$response = $creditcard->Capture( $customVars );
// Store the transaction_key together with captured amount, we need this for refunding
$capture_data = json_encode(
array(
'OriginalTransactionKey' => $response->transactions,
'amount' => $creditcard->amountDedit,
)
);
add_post_meta( $order_id, 'buckaroo_capture', $capture_data, false );
return fn_buckaroo_process_capture( $response, $order, $this->currency );
}
public function getCardsList() {
$cards = array();
$cardsDesc = array(
'amex' => 'American Express',
'cartebancaire' => 'Carte Bancaire',
'cartebleuevisa' => 'Carte Bleue',
'dankort' => 'Dankort',
'maestro' => 'Maestro',
'mastercard' => 'Mastercard',
'nexi' => 'Nexi',
'postepay' => 'PostePay',
'visa' => 'Visa',
'visaelectron' => 'Visa Electron',
'vpay' => 'Vpay',
);
if ( is_array( $this->creditCardProvider ) ) {
foreach ( $this->creditCardProvider as $value ) {
$cards[] = array(
'servicename' => $value,
'displayname' => $cardsDesc[ $value ],
);
}
}
return $cards;
}
/**
* Add fields to the form_fields() array, specific to this page.
*
* @access public
*/
public function init_form_fields() {
parent::init_form_fields();
$this->form_fields['creditcardmethod'] = array(
'title' => __( 'Credit and debit card method', 'wc-buckaroo-bpe-gateway' ),
'type' => 'select',
'description' => __( 'Redirect user to Buckaroo or enter credit or debit card information (directly) inline in the checkout. SSL is required to enable inline credit or debit card information.', 'wc-buckaroo-bpe-gateway' ),
'options' => array(
'redirect' => 'Redirect',
'encrypt' => 'Inline',
),
'default' => 'redirect',
'desc_tip' => __( 'Check with Buckaroo whether Client Side Encryption is enabled, otherwise transactions will fail. If in doubt, please contact us.', 'wc-buckaroo-bpe-gateway' ),
);
$this->form_fields['creditcardpayauthorize'] = array(
'title' => __( 'Credit and debit card flow', 'wc-buckaroo-bpe-gateway' ),
'type' => 'select',
'description' => __( 'Choose to execute Pay or Capture call', 'wc-buckaroo-bpe-gateway' ),
'options' => array(
'pay' => 'Pay',
'authorize' => 'Authorize',
),
'default' => 'pay',
);
$this->form_fields['AllowedProvider'] = array(
'title' => __( 'Allowed provider', 'wc-buckaroo-bpe-gateway' ),
'type' => 'multiselect',
'options' => array(
'amex' => 'American Express',
'cartebancaire' => 'Carte Bancaire',
'cartebleuevisa' => 'Carte Bleue',
'dankort' => 'Dankort',
'maestro' => 'Maestro',
'mastercard' => 'Mastercard',
'nexi' => 'Nexi',
'postepay' => 'PostePay',
'visa' => 'Visa',
'visaelectron' => 'Visa Electron',
'vpay' => 'Vpay',
),
'description' => __( 'Select which credit or debit card providers will be visible to customer', 'wc-buckaroo-bpe-gateway' ),
'default' => array(
'amex',
'cartebancaire',
'cartebleuevisa',
'dankort',
'mastercard',
'maestro',
'nexi',
'postepay',
'visa',
'visaelectron',
'vpay',
),
);
$this->form_fields[ self::SHOW_IN_CHECKOUT_FIELD ] = array(
'title' => __( 'Show separate in checkout', 'wc-buckaroo-bpe-gateway' ),
'type' => 'multiselect',
'options' => array(
'' => __( 'None', 'wc-buckaroo-bpe-gateway' ),
'amex' => 'American Express',
'cartebancaire' => 'Carte Bancaire',
'cartebleuevisa' => 'Carte Bleue',
'dankort' => 'Dankort',
'maestro' => 'Maestro',
'mastercard' => 'Mastercard',
'nexi' => 'Nexi',
'postepay' => 'PostePay',
'visa' => 'Visa',
'visaelectron' => 'Visa Electron',
'vpay' => 'Vpay',
),
'description' => __( 'Select which credit or debit card providers will be shown separately in the checkout', 'wc-buckaroo-bpe-gateway' ),
'default' => array(),
);
}
/** @inheritDoc */
public function process_admin_options() {
parent::process_admin_options();
$this->after_admin_options_update();
}
/**
* Do code after admin options update
*
* @return void
*/
public function after_admin_options_update() {
set_transient( 'buckaroo_credicard_updated', true );
}
/**
* Returns true if secure (https), false if not (http)
*/
public function isSecure() {
return ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' )
|| ! empty( $_SERVER['SERVER_PORT'] ) && $_SERVER['SERVER_PORT'] == 443;
}
/**
* Save only creditcards that are allowed
*
* @param string $key
* @param mixed $value
*
* @return mixed
*/
public function validate_show_in_checkout_field( $key, $value ) {
$allowed = $this->settings['AllowedProvider'];
if ( is_array( $value ) ) {
return array_filter(
$value,
function ( $provider ) use ( $allowed ) {
return in_array( $provider, $allowed );
}
);
}
return $value;
}
}