-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgateway-buckaroo-afterpay.php
480 lines (413 loc) · 16.8 KB
/
gateway-buckaroo-afterpay.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
<?php
require_once __DIR__ . '/library/api/paymentmethods/afterpay/afterpay.php';
/**
* @package Buckaroo
*/
class WC_Gateway_Buckaroo_Afterpay extends WC_Gateway_Buckaroo {
const PAYMENT_CLASS = BuckarooAfterPay::class;
public $type;
public $b2b;
public $vattype;
public $country;
public $productQtyLoop = true;
public $afterpaypayauthorize;
public function __construct() {
$this->id = 'buckaroo_afterpay';
$this->title = 'Riverty';
$this->has_fields = false;
$this->method_title = 'Buckaroo Riverty (Old)';
$this->setIcon( 'afterpay.png', 'svg/afterpay.svg' );
$this->setCountry();
parent::__construct();
$this->addRefundSupport();
}
/** @inheritDoc */
protected function setProperties() {
parent::setProperties();
$this->afterpaypayauthorize = $this->get_option( 'afterpaypayauthorize', 'Pay' );
$this->type = $this->get_option( 'service' );
$this->b2b = $this->get_option( 'enable_bb' );
$this->vattype = $this->get_option( 'vattype' );
}
/**
* Process order
*
* @param integer $order_id
* @param integer $amount defaults to null
* @param string $reason
* @return callable|string function or error
*/
public function process_capture_refund( $order_id, $amount = null, $reason = '', $transaction_id = null ) {
return $this->processDefaultRefund(
$order_id,
$amount,
$reason,
false,
function ( $request ) use ( $transaction_id ) {
if ( $transaction_id != null ) {
$request->OriginalTransactionKey = $transaction_id;
}
}
);
}
/**
* 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->afterpaypayauthorize ) ? $this->afterpaypayauthorize : 'pay' );
return $this->process_refund_common( $action, $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 = '', $line_item_qtys = null, $line_item_totals = null, $line_item_tax_totals = null, $originalTransactionKey = 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 BuckarooAfterPay */
$afterpay = $this->createCreditRequest( $order, $amount, $reason );
$afterpay->channel = BuckarooConfig::CHANNEL_BACKOFFICE;
if ( $originalTransactionKey !== null ) {
$afterpay->OriginalTransactionKey = $originalTransactionKey;
}
// add items to refund call for afterpay
$issuer = get_post_meta( $order_id, '_wc_order_payment_issuer', true );
$products = array();
$items = $order->get_items();
$itemsTotalAmount = 0;
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' );
}
$orderDataForChecking = $afterpay->getOrderRefundData();
foreach ( $items as $item ) {
if ( isset( $line_item_qtys[ $item->get_id() ] ) && $line_item_qtys[ $item->get_id() ] > 0 ) {
$product = new WC_Product( $item['product_id'] );
$tax_class = $product->get_attribute( 'vat_category' );
if ( empty( $tax_class ) ) {
$tax_class = $this->vattype;
}
$tmp['ArticleDescription'] = $item['name'];
$tmp['ArticleId'] = $item['product_id'];
$tmp['ArticleQuantity'] = $line_item_qtys[ $item->get_id() ];
$tmp['ArticleUnitprice'] = number_format( number_format( $item['line_total'] + $item['line_tax'], 4 ) / $item['qty'], 2 );
$itemsTotalAmount += $tmp['ArticleUnitprice'] * $line_item_qtys[ $item->get_id() ];
$tmp['ArticleVatcategory'] = $tax_class;
$products[] = $tmp;
}
}
$fees = $order->get_fees();
foreach ( $fees as $key => $item ) {
if ( ! empty( $line_item_totals[ $key ] ) ) {
$tmp['ArticleDescription'] = $item['name'];
$tmp['ArticleId'] = $key;
$tmp['ArticleQuantity'] = 1;
$tmp['ArticleUnitprice'] = number_format( ( $item['line_total'] + $item['line_tax'] ), 2 );
$itemsTotalAmount += $tmp['ArticleUnitprice'];
$tmp['ArticleVatcategory'] = '4';
$products[] = $tmp;
}
}
// Add shippingCosts
$shippingInfo = $this->getAfterPayShippingInfo( 'afterpay', 'partial_refunds', $order, $line_item_totals, $line_item_tax_totals );
if ( $shippingInfo['costs'] > 0 ) {
$products[] = $shippingInfo['shipping_virtual_product'];
$itemsTotalAmount += $shippingInfo['costs'];
}
$ref_amount = $this->request( 'refund_amount' );
// end add items
if ( $ref_amount !== null && $itemsTotalAmount == 0 ) {
$afterpay->amountCredit = $ref_amount;
} else {
$amount = $itemsTotalAmount;
$afterpay->amountCredit = $amount;
}
if ( ! ( count( $products ) > 0 ) ) {
return true;
}
try {
$afterpay->checkRefundData( $orderDataForChecking );
$response = $afterpay->AfterPayRefund( $products, $issuer );
} catch ( exception $e ) {
update_post_meta( $order_id, '_pushallowed', 'ok' );
}
$final_response = fn_buckaroo_process_refund( $response, $order, $amount, $this->currency );
return $final_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' ) );
}
$previous_captures = get_post_meta( $order_id, '_wc_order_captures' ) ? get_post_meta( $order_id, '_wc_order_captures' ) : false;
$order = getWCOrder( $order_id );
/** @var BuckarooAfterPay */
$afterpay = $this->createDebitRequest( $order );
$afterpay->amountDedit = str_replace( ',', '.', $capture_amount );
$afterpay->OriginalTransactionKey = $order->get_transaction_id();
$afterpay->invoiceId = (string) getUniqInvoiceId( $order->get_order_number() ) . ( is_array( $previous_captures ) ? '-' . count( $previous_captures ) : '' );
if ( ! isset( $customVars ) ) {
$customVars = null;
}
// add items to capture call for afterpay
$customVars['payment_issuer'] = get_post_meta( $order_id, '_wc_order_payment_issuer', true );
$products = array();
$items = $order->get_items();
$itemsTotalAmount = 0;
$line_item_qtys = buckaroo_request_sanitized_json( 'line_item_qtys' );
$line_item_totals = buckaroo_request_sanitized_json( 'line_item_totals' );
$line_item_tax_totals = buckaroo_request_sanitized_json( 'line_item_tax_totals' );
foreach ( $items as $item ) {
if ( isset( $line_item_qtys[ $item->get_id() ] ) && $line_item_qtys[ $item->get_id() ] > 0 ) {
$product = new WC_Product( $item['product_id'] );
$tax_class = $product->get_attribute( 'vat_category' );
if ( empty( $tax_class ) ) {
$tax_class = $this->vattype;
}
$tmp['ArticleDescription'] = $item['name'];
$tmp['ArticleId'] = $item['product_id'];
$tmp['ArticleQuantity'] = $line_item_qtys[ $item->get_id() ];
$tmp['ArticleUnitprice'] = number_format( number_format( $item['line_total'] + $item['line_tax'], 4 ) / $item['qty'], 2 );
$itemsTotalAmount += $tmp['ArticleUnitprice'] * $item['qty'];
$tmp['ArticleVatcategory'] = $tax_class;
$products[] = $tmp;
}
}
if ( ! $previous_captures ) {
$fees = $order->get_fees();
foreach ( $fees as $key => $item ) {
$tmp['ArticleDescription'] = $item['name'];
$tmp['ArticleId'] = $key;
$tmp['ArticleQuantity'] = 1;
$tmp['ArticleUnitprice'] = number_format( ( $item['line_total'] + $item['line_tax'] ), 2 );
$itemsTotalAmount += $tmp['ArticleUnitprice'];
$tmp['ArticleVatcategory'] = '4';
$products[] = $tmp;
}
}
// Add shippingCosts
$shippingInfo = $this->getAfterPayShippingInfo( 'afterpay', 'capture', $order, $line_item_totals, $line_item_tax_totals );
if ( $shippingInfo['costs'] > 0 ) {
$products[] = $shippingInfo['shipping_virtual_product'];
}
// Merge products with same SKU
$mergedProducts = array();
foreach ( $products as $product ) {
if ( ! isset( $mergedProducts[ $product['ArticleId'] ] ) ) {
$mergedProducts[ $product['ArticleId'] ] = $product;
} else {
$mergedProducts[ $product['ArticleId'] ]['ArticleQuantity'] += 1;
}
}
$products = $mergedProducts;
// end add items
$response = $afterpay->Capture( $customVars, $products );
$process_response = fn_buckaroo_process_capture( $response, $order, $this->currency, $products );
return $process_response;
}
/**
* Validate payment fields on the frontend.
*
* @access public
* @return void
*/
public function validate_fields() {
if ( $this->request( 'buckaroo-afterpay-accept' ) === null ) {
wc_add_notice( __( 'Please accept licence agreements', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
$birthdate = $this->parseDate( $this->request( 'buckaroo-afterpay-birthdate' ) );
$b2b = $this->request( 'buckaroo-afterpay-b2b' );
if ( ! $this->validateDate( $birthdate, 'd-m-Y' ) && $b2b != 'ON' ) {
wc_add_notice( __( 'You must be at least 18 years old to use this payment method. Please enter your correct date of birth. Or choose another payment method to complete your order.', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
if ( $b2b == 'ON' ) {
if ( $this->request( 'buckaroo-afterpay-company-coc-registration' ) === null ) {
wc_add_notice( __( 'Company registration number is required (KvK)', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
if ( $this->request( 'buckaroo-afterpay-company-name' ) === null ) {
wc_add_notice( __( 'Company name is required', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
}
if ( $this->request( 'buckaroo-afterpay-phone' ) === null && $this->request( 'billing_phone' ) === null ) {
wc_add_notice( __( 'Please enter phone number', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
if ( $this->type == 'afterpayacceptgiro' ) {
if ( $this->request( 'buckaroo-afterpay-company-coc-registration' ) === null ) {
wc_add_notice( __( 'IBAN is required', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
}
parent::validate_fields();
}
/**
* Process payment
*
* @param integer $order_id
* @return callable|void fn_buckaroo_process_response() or void
*/
public function process_payment( $order_id ) {
$order = getWCOrder( $order_id );
$this->setOrderCapture( $order_id, 'Afterpay' );
/** @var BuckarooAfterPay */
$afterpay = $this->createDebitRequest( $order );
$afterpay->setType( $this->type );
$birthdate = $this->parseDate(
$this->request( 'buckaroo-afterpay-birthdate' )
);
$shippingCosts = $order->get_shipping_total( 'edit' );
$shippingCostsTax = (float) $order->get_shipping_tax( 'edit' );
if ( floatval( $shippingCosts ) > 0 ) {
$afterpay->ShippingCosts = number_format( $shippingCosts, 2 ) + number_format( $shippingCostsTax, 2 );
}
if ( $this->request( 'buckaroo-afterpay-b2b' ) == 'ON' ) {
$afterpay->B2B = 'TRUE';
$afterpay->CompanyCOCRegistration = $this->request( 'buckaroo-afterpay-company-coc-registration' );
$afterpay->CompanyName = $this->request( 'buckaroo-afterpay-company-name' );
}
$order_details = new Buckaroo_Order_Details( $order );
$afterpay = $this->getBillingInfo( $order_details, $afterpay, $birthdate );
$afterpay = $this->getShippingInfo( $order_details, $afterpay );
if ( $this->type == 'afterpayacceptgiro' ) {
$afterpay->CustomerAccountNumber = $this->request( 'buckaroo-afterpay-company-coc-registration' );
}
/** @var BuckarooAfterPay */
$afterpay = $this->handleThirdPartyShippings( $afterpay, $order, $this->country );
$afterpay->CustomerIPAddress = getClientIpBuckaroo();
$afterpay->Accept = 'TRUE';
$afterpay->returnUrl = $this->notify_url;
$action = ucfirst( isset( $this->afterpaypayauthorize ) ? $this->afterpaypayauthorize : 'pay' );
if ( $action == 'Authorize' ) {
update_post_meta( $order_id, '_wc_order_authorized', 'yes' );
}
$response = $afterpay->PayOrAuthorizeAfterpay(
$this->get_products_for_payment( $order_details ),
$action
);
// Save the original tranaction ID from the authorize or capture, we need it to do the refund
// JM REMOVE???
// update_post_meta( $order->get_id(), '_wc_order_payment_original_transaction_key', $this->type);
return fn_buckaroo_process_response( $this, $response, $this->mode );
}
/**
* Get billing info for pay request
*
* @param Buckaroo_Order_Details $order_details
* @param BuckarooAfterPay $method
* @param string $birthdate
*
* @return BuckarooAfterPay $method
*/
protected function getBillingInfo( $order_details, $method, $birthdate ) {
/** @var BuckarooAfterPay */
$method = $this->set_billing( $method, $order_details );
$method->BillingInitials = $order_details->getInitials(
$order_details->getBilling( 'first_name' )
);
$method->BillingBirthDate = date( 'Y-m-d', strtotime( $birthdate ) );
if ( empty( $method->BillingPhoneNumber ) ) {
$method->BillingPhoneNumber = $this->request( 'buckaroo-afterpay-phone' );
}
return $method;
}
/**
* Get shipping info for pay request
*
* @param Buckaroo_Order_Details $order_details
* @param BuckarooAfterPay $method
*
* @return BuckarooAfterPay $method
*/
protected function getShippingInfo( $order_details, $method ) {
$method->AddressesDiffer = 'FALSE';
if ( $this->request( 'buckaroo-afterpay-shipping-differ' ) !== null ) {
$method->AddressesDiffer = 'TRUE';
/** @var BuckarooAfterPay */
$method = $this->set_shipping( $method, $order_details );
$method->ShippingInitials = $order_details->getInitials(
$order_details->getShipping( 'first_name' )
);
}
return $method;
}
/**
* Check response data
*
* @access public
*/
public function response_handler() {
fn_buckaroo_process_response( $this );
exit;
}
/**
* Add fields to the form_fields() array, specific to this page.
*
* @access public
*/
public function init_form_fields() {
parent::init_form_fields();
$this->add_financial_warning_field();
$this->form_fields['service'] = array(
'title' => __( 'Select Riverty service', 'wc-buckaroo-bpe-gateway' ),
'type' => 'select',
'description' => __( 'Please select the service', 'wc-buckaroo-bpe-gateway' ),
'options' => array(
'afterpayacceptgiro' => __( 'Offer customer to pay afterwards by SEPA Direct Debit.', 'wc-buckaroo-bpe-gateway' ),
'afterpaydigiaccept' => __( 'Offer customer to pay afterwards by digital invoice.', 'wc-buckaroo-bpe-gateway' ),
),
'default' => 'afterpaydigiaccept',
);
$this->form_fields['enable_bb'] = array(
'title' => __( 'Enable B2B option for Riverty', 'wc-buckaroo-bpe-gateway' ),
'type' => 'select',
'description' => __( 'Enables or disables possibility to pay using company credentials', 'wc-buckaroo-bpe-gateway' ),
'options' => array(
'enable' => 'Enable',
'disable' => 'Disable',
),
'default' => 'disable',
);
$this->form_fields['vattype'] = array(
'title' => __( 'Default product VAT type', 'wc-buckaroo-bpe-gateway' ),
'type' => 'select',
'description' => __( 'Please select the default VAT type for your products', 'wc-buckaroo-bpe-gateway' ),
'options' => array(
'1' => '1 = High rate',
'2' => '2 = Low rate',
'3' => '3 = Zero rate',
'4' => '4 = Null rate',
'5' => '5 = middle rate',
),
'default' => '1',
);
$this->form_fields['afterpaypayauthorize'] = array(
'title' => __( 'Riverty Pay or Capture', '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',
);
}
}