-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAbstractGateway.php
537 lines (476 loc) · 15.4 KB
/
AbstractGateway.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
/**
* @author Open Source Team
* @copyright 2022 Pagar.me (https://pagar.me)
* @license https://pagar.me Copyright
*
* @link https://pagar.me
*/
declare(strict_types=1);
namespace Woocommerce\Pagarme\Controller\Gateways;
use Exception;
use WC_Admin_Settings;
use WC_Payment_Gateway;
use Woocommerce\Pagarme\Block\Template;
use Woocommerce\Pagarme\Controller\Gateways\Exceptions\InvalidOptionException;
use Woocommerce\Pagarme\Core;
use Woocommerce\Pagarme\Model\Charge;
use Woocommerce\Pagarme\Model\Checkout;
use Woocommerce\Pagarme\Model\Subscription;
use Woocommerce\Pagarme\Model\Config;
use Woocommerce\Pagarme\Model\Config\Source\Yesno;
use Woocommerce\Pagarme\Model\Gateway;
use Woocommerce\Pagarme\Model\Order;
use Woocommerce\Pagarme\Model\Payment\PostFormatter;
use Woocommerce\Pagarme\Model\WooOrderRepository;
use Woocommerce\Pagarme\Block\Checkout\Gateway as GatewayBlock;
use Woocommerce\Pagarme\Block\Order\EmailPaymentDetails;
use WP_Error;
defined('ABSPATH') || exit;
if (!function_exists('add_action')) {
exit(0);
}
/**
* Abstract Gateway
* @package Woocommerce\Pagarme\Controller\Gateways
*/
abstract class AbstractGateway extends WC_Payment_Gateway
{
/** @var string */
const PAGARME = 'Pagar.me';
const WC_PAYMENT_PAGARME = 'woo-pagarme-payments';
/** @var string */
const PAYMENT_OPTION_UPDATE_SLUG = 'woocommerce_update_options_payment_gateways_';
/** @var string */
const PAYMENT_OPTIONS_SETTINGS_NAME = 'woocommerce_%s_settings';
/** @var Gateway|null */
public $model;
/** @var string */
protected $method = 'payment';
/** @var string */
protected $vendor = self::PAGARME;
/** @var WooOrderRepository */
private $wooOrderRepository;
/** @var PostFormatter */
private $postFormatter;
/** @var Config */
protected $config;
/** @var Checkout */
private $checkout;
/** @var GatewayBlock */
private $gatewayBlock;
/** @var Template*/
private $template;
/** @var Yesno */
protected $yesnoOptions;
/** @var array */
protected $sendEmailStatus = ['pending', 'on-hold'];
/**
* @var Subscription
*/
private $subscription;
/**
* @param Yesno|null $yesnoOptions
* @param Checkout|null $checkout
* @param Gateway|null $gateway
* @param WooOrderRepository|null $wooOrderRepository
* @param PostFormatter|null $postFormatter
* @param Config|null $config
* @param GatewayBlock|null $gatewayBlock
* @param Template|null $template
*/
public function __construct(
Yesno $yesnoOptions = null,
Checkout $checkout = null,
Gateway $gateway = null,
WooOrderRepository $wooOrderRepository = null,
PostFormatter $postFormatter = null,
Config $config = null,
GatewayBlock $gatewayBlock = null,
Template $template = null
) {
$this->gatewayBlock = $gatewayBlock ?? new GatewayBlock;
$this->config = $config ?? new Config;
$this->postFormatter = $postFormatter ?? new PostFormatter;
$this->model = $gateway ?? new Gateway;
$this->checkout = $checkout ?? new Checkout;
$this->wooOrderRepository = $wooOrderRepository ?? new WooOrderRepository;
$this->template = $template ?? new Template;
$this->id = 'woo-pagarme-payments-' . $this->method;
$this->yesnoOptions = $yesnoOptions ?? new Yesno;
$this->method_title = $this->getPaymentMethodTitle();
$this->method_description = __('Payment Gateway Pagar.me', 'woo-pagarme-payments') . ' ' . $this->method_title;
$this->has_fields = false;
$this->init_form_fields();
$this->init_settings();
$this->enabled = $this->isEnabled();
$this->title = $this->getTitle();
$this->has_fields = true;
if (is_admin()) {
add_action("update_option", [$this, 'beforeUpdateAdminOptions'], 10, 3);
add_action("add_option", [$this, 'beforeAddAdminOptions'], 10, 2);
add_action(self::PAYMENT_OPTION_UPDATE_SLUG . $this->id, [$this, 'process_admin_options']);
}
add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
add_action('woocommerce_thankyou_' . $this->id, [$this, 'thank_you_page']);
add_action('admin_enqueue_scripts', array($this, 'payments_scripts'));
add_action('woocommerce_email_after_order_table', [$this, 'pagarme_email_payment_info'], 15, 2 );
$this->subscription = new Subscription($this);
$this->addRefundSupport();
}
/**
* @return boolean
*/
public function hasSubscriptionSupport(): bool
{
return false;
}
/**
* @return boolean
*/
public function isSubscriptionActive(): bool
{
return false;
}
public function payments_scripts()
{
wp_register_script('pagarme_payments', $this->jsUrl('pagarme_payments'), [], false, true);
wp_register_script('pagarme_payments_validation', $this->jsUrl('pagarme_payments_validation'), [], false, true);
wp_enqueue_script('pagarme_payments');
wp_enqueue_script('pagarme_payments_validation');
}
public function jsUrl($jsFileName)
{
return Core::plugins_url('assets/javascripts/admin/' . $jsFileName . '.js');
}
/**
* @param $orderId
* @return array
* @throws Exception
*/
public function process_payment($orderId): array
{
$wooOrder = $this->wooOrderRepository->getById($orderId);
if ($this->subscription->isChangePaymentSubscription()) {
return $this->subscription->processChangePaymentSubscription($wooOrder);
}
$this->postFormatter->formatReactCheckout();
$this->postFormatter->assemblePaymentRequest();
if ($this->subscription->hasSubscriptionFreeTrial()) {
return $this->subscription->processFreeTrialSubscription($wooOrder);
}
$this->checkout->process($wooOrder);
return [
'result' => 'success',
'redirect' => $this->get_return_url($wooOrder)
];
}
/**
* @return void
* @throws Exception
*/
public function payment_fields()
{
$this->model->payment = $this->method;
echo $this->gatewayBlock->setPaymentInstance($this->model->getPaymentInstance($this->method))->toHtml();
}
/**
* @param $orderId
* @return void
*/
public function receipt_page($orderId)
{
$this->checkout_transparent($orderId);
}
/**
* @param $order_id
* @return void
*/
public function checkout_transparent($order_id)
{
$wc_order = $this->wooOrderRepository->getById($order_id);
require_once Core::get_file_path($this->method . '-item.php', 'templates/checkout/');
}
/**
* @param $order_id
* @return void
* @throws Exception
*/
public function thank_you_page($order_id)
{
$order = $this->wooOrderRepository->getById($order_id);
$pagarmeOrder = new Order($order_id);
if ($this->method === $pagarmeOrder->get_meta('payment_method')) {
$this->template->createBlock(
'\Woocommerce\Pagarme\Block\Checkout\ThankYou',
'pagarme.checkout.thank-you',
[
'woo_order' => $order,
'pagarme_order' => $pagarmeOrder,
'payment_method' => $this->method,
'container' => true
]
)->toHtml();
}
}
/**
* @return false
*/
public function addRefundSupport()
{
return false;
}
/**
* @param int $order_id
* @param null $amount
* @param string $reason
*
* @return bool|WP_Error
*/
public function process_refund($order_id, $amount = null, $reason = '')
{
$order = new Order($order_id);
$charges = $order->get_charges();
if (empty($charges)) {
return false;
}
$charge = new Charge();
$chargeId = $charges[0]->getTransactions()[0]->getChargeId();
return $charge->processChargeRefund($chargeId, $amount);
}
/**
* @return string
*/
public function getTitle()
{
if ($title = $this->get_option('title')) {
return $title;
}
return $this->getPaymentMethodTitle();
}
/**
* @return string
*/
public function getPaymentMethodTitle()
{
return __(ucwords(str_replace('-', ' ', str_replace('_', ' ', $this->method))), 'woo-pagarme-payments');
}
/**
* @return void
*/
public function init_form_fields()
{
$this->form_fields['enabled'] = $this->field_enabled();
$this->form_fields['title'] = $this->field_title();
$this->form_fields = array_merge(
$this->form_fields,
$this->append_form_fields(),
$this->append_gateway_form_fields()
);
}
/**
* @return array
*/
public function append_form_fields()
{
return [];
}
/**
* @return array
*/
private function append_gateway_form_fields()
{
if ($this->isGatewayType()) {
return $this->gateway_form_fields();
}
return [];
}
/**
* @return array
*/
protected function gateway_form_fields()
{
return [];
}
/**
* @return bool
*/
public function isGatewayType()
{
$isPaymentGateway = $this->model->config->getIsPaymentGateway();
if (empty($isPaymentGateway) || !key_exists($this->method, $isPaymentGateway)) {
return $this->model->config->getIsGatewayIntegrationType();
}
return $isPaymentGateway[$this->method];
}
/**
* @return array
*/
public function field_enabled()
{
return [
'title' => __('Enable/Disable', 'woocommerce'),
'type' => 'select',
'options' => $this->yesnoOptions->toLabelsArray(true),
'label' => __('Enable', 'woo-pagarme-payments') . ' ' .
__($this->getPaymentMethodTitle(), 'woo-pagarme-payments'),
'default' => __(
$this->config->getData('enable_' . $this->method),
'woo-pagarme-payments'
) ?? strtolower(Yesno::NO),
];
}
/**
* @return array
*/
public function field_title()
{
return [
'title' => __('Checkout title', 'woo-pagarme-payments'),
'type' => 'text',
'description' => __('Name shown to the customer in the checkout page.', 'woo-pagarme-payments'),
'desc_tip' => true,
'default' => __($this->getPaymentMethodTitle(), 'woo-pagarme-payments'),
];
}
/**
* @param mixed $optionName
* @param mixed $oldValue
* @param mixed $values
* @return void
*/
public function beforeUpdateAdminOptions($optionName, $oldValue, $values)
{
$isValidOption = $optionName !== sprintf(self::PAYMENT_OPTIONS_SETTINGS_NAME, $this->id);
if ($isValidOption) {
return;
}
$this->saveAdminOptionsInCoreConfig($values);
}
/**
* @param mixed $optionName
* @param mixed $values
* @return void
*/
public function beforeAddAdminOptions($optionName, $values)
{
$isValidOption = $optionName !== sprintf(self::PAYMENT_OPTIONS_SETTINGS_NAME, $this->id);
if ($isValidOption) {
return;
}
$this->saveAdminOptionsInCoreConfig($values);
}
/**
* @param array $values
* @return void
*/
protected function saveAdminOptionsInCoreConfig($values)
{
foreach ($values as $field => $value) {
if ($field === 'title') {
$field = $this->method . '_' . $field;
}
if ($field === 'enabled') {
$field = $this->form_fields['enabled']['old_name'] ?? 'enable_' . $this->method;
}
$this->config->setData($field, $value);
}
$this->config->save();
}
/**
* @param mixed $order
* @return void
*/
public function pagarme_email_payment_info($order, $sent_to_admin)
{
if ($sent_to_admin
|| $this->id !== $order->get_meta('payment_method')
|| !in_array($order->get_status(), $this->sendEmailStatus)) {
return;
}
$paymentDetails = new EmailPaymentDetails();
$paymentDetails->render($order->get_id());
}
/**
* @throws InvalidOptionException
*/
protected function validateMaxLength($value, $fieldName, $maxLength)
{
$isValueLengthGreaterThanMaxLength = mb_strlen($value) > $maxLength;
if ($isValueLengthGreaterThanMaxLength) {
$maximumLengthErrorMessage = sprintf(
__('%s has exceeded the %d character limit.', 'woo-pagarme-payments'),
__($fieldName, 'woo-pagarme-payments'),
$maxLength
);
$this->addValidationError($maximumLengthErrorMessage);
}
}
/**
* @throws InvalidOptionException
*/
protected function validateRequired($value, $fieldName)
{
$isValueEmpty = empty($value) && $value !== "0";
if ($isValueEmpty) {
$requiredErrorMessage = sprintf(
__('%s is required.', 'woo-pagarme-payments'),
__($fieldName, 'woo-pagarme-payments')
);
$this->addValidationError($requiredErrorMessage);
}
}
/**
* @throws InvalidOptionException
*/
protected function validateMinValue($value, $fieldName, $minValue)
{
$isValueLesserThanMinimum = floatval($value) < $minValue;
if ($isValueLesserThanMinimum) {
$minimumValueErrorMessage = sprintf(
__('%s does not have the minimum value of %d.', 'woo-pagarme-payments'),
__($fieldName, 'woo-pagarme-payments'),
$minValue
);
$this->addValidationError($minimumValueErrorMessage);
}
}
/**
* @throws InvalidOptionException
*/
protected function validateAlphanumericAndSpacesAndPunctuation($value, $fieldName)
{
if (!empty($value) && !preg_match('/^[A-Za-z0-9À-ú \-:()%@*_.,!?$;]+$/', $value)) {
$alphanumericAndSpacesAndPunctuationErrorMessage = sprintf(
__(
'%s must only contain letters, numbers, spaces and punctuations (except quotation marks).',
'woo-pagarme-payments'
),
__($fieldName, 'woo-pagarme-payments')
);
$this->addValidationError($alphanumericAndSpacesAndPunctuationErrorMessage);
}
}
/**
* @throws InvalidOptionException
*/
protected function addValidationError($errorMessage)
{
WC_Admin_Settings::add_error($errorMessage);
throw new InvalidOptionException(InvalidOptionException::CODE, $errorMessage);
}
protected function isEnabled()
{
global $wp;
$enabled = $this->get_option('enabled', 'no');
if (!isset($wp->query_vars['order-pay'])) {
return $enabled;
}
$orderId = $wp->query_vars['order-pay'];
$order = wc_get_order($orderId);
if (empty($order->get_customer_id())) {
$enabled = 'no';
}
return $enabled;
}
}