-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwoo-pagarme-payments.php
577 lines (490 loc) · 18.6 KB
/
woo-pagarme-payments.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
<?php
/*
* Plugin Name: Pagar.me for WooCommerce
* Version: 3.5.0
* Author: Pagar.me
* Author URI: https://pagar.me
* License: GPL2
* Description: Enable Pagar.me Gateway for WooCommerce
* Requires at least: 4.1
* Tested up to: 6.6.1
* WC requires at least: 3.9.0
* WC tested up to: 9.1.4
* Domain Path: /languages
* Requires Plugins: woocommerce
* Text Domain: woo-pagarme-payments
*/
use Woocommerce\Pagarme\Model\Config;
use Woocommerce\Pagarme\Model\FeatureCompatibilization;
use Woocommerce\Pagarme\Action\CustomerFieldsActions;
const PAGARME_REQUIREMENTS_URL = 'https://docs.pagar.me/docs/requisitos-de-instala%C3%A7%C3%A3o-woocommerce';
if (!defined('ABSPATH') || !function_exists('add_action')) {
exit(0);
}
require_once dirname(__FILE__) . '/constants.php';
/**
* Renders custom Wordpress Notice on every admin pages.
*
* @param string $message Message displayed on the notice.
* @param array $buttons Optional. An array of buttons arrays, generated with the wcmpSingleButtonArray function.
* @param string $type Optional. The type of the notice.
* Possible options are: `'error'` (default), `'warning'`, `'success'` or `'info'`.
* @param bool $includeScript Optional. Message displayed on the notice.
*/
function wcmpRenderAdminNoticeHtml($message, $buttons = [], $type = 'error', $includeScript = false)
{
wp_enqueue_style(
'pagarme-notice-css',
plugins_url('/pagarme-payments-for-woocommerce/assets/stylesheets/admin/notice.css'),
array(),
"1.0.1"
);
if ($includeScript) {
$noticesL10n = array(
'accountInfoUrl' => admin_url('/wc-api/pagarme-account-info')
);
wp_enqueue_script(
'pagarme-notice-js',
plugins_url('/pagarme-payments-for-woocommerce/assets/javascripts/admin/pagarme_notices.js'),
array('jquery', 'wp-i18n'),
"1.0.0"
);
wp_set_script_translations(
'pagarme-notice-js',
'woo-pagarme-payments',
plugin_dir_path(__FILE__) . 'languages/'
);
wp_localize_script('pagarme-notice-js', 'pagarmeNotice', $noticesL10n);
}
?>
<div class="notice <?= esc_html($type); ?> is-dismissible">
<div class="pagarme-notice">
<div class="pagarme-notice-avatar-container">
<img alt="Pagarme Avatar" class="pagarme-notice-avatar"
src="<?= plugins_url('/pagarme-payments-for-woocommerce/assets/images/pagarme-avatar.svg') ?>">
</div>
<div class="pagarme-notice-message-container">
<p><strong><?= __('Pagar.me module for Woocommerce', 'woo-pagarme-payments'); ?>:</strong></p>
<p><?= $message ?></p>
<?php
if (!empty($buttons)) {
echo wcmpAddNoticeButton($buttons);
}
?>
</div>
</div>
</div>
<?php
}
/**
* @param string $label Name displayed on the button.
* @param string $url URL of destiny on click.
* If an empty string is provided, the link will have no 'href' attribute.
* @param string $type Optional. The type of button.
* Possible options are: `'primary'` (default), `'secondary'` or `''` (empty string).
* If an empty string is provided, the button will be rendered as a simple link.
* @param string $target Optional. Target of of the URL link.
* Possible options are: `'_blank'`, `'_self'`, `'_parent'`, `'_top'`, any framename or `''` (empty string - default).
* If an empty string is provided, the link will have no 'target' attribute.
* @param string $class Optional. Additional class value(s) for custom style or script purposes.
*
* @return array
*/
function wcmpSingleButtonArray($label, $url, $type = 'primary', $target = '', $class = '')
{
return array(
'label' => $label,
'url' => $url,
'type' => $type,
'target' => $target,
'class' => $class
);
}
/**
* @param array $buttons Array of arrays, each containing the keys `label`, `url`, `type`, `target` and `class`.
*
* @return string
*/
function wcmpAddNoticeButton($buttons)
{
$html = '<p>';
foreach ($buttons as $button) {
$buttonHtml = '<a%1$s%2$s class="%3$s%4$s">%5$s</a>';
$html .= sprintf(
$buttonHtml,
$button['url'] !== '' ? ' href="' . esc_url($button['url']) . '"' : '',
$button['target'] !== '' ? " target='{$button['target']}'" : '',
$button['type'] !== '' ? "button button-{$button['type']} " : '',
$button['class'],
__($button['label'], 'woo-pagarme-payments')
);
}
$html .= '</p>';
return $html;
}
function wcmpAdminNoticePhpVersion()
{
wcmpRenderAdminNoticeHtml(
__('Your PHP version is not supported. Required >= 7.1.', 'woo-pagarme-payments')
);
}
if (version_compare(PHP_VERSION, '7.1', '<')) {
wcmpLoadNotice('AdminNoticePhpVersion');
return;
}
function wcmpIsPluginInstalled($pluginBasename)
{
$isInstalled = false;
if (function_exists('get_plugins')) {
$allPlugins = get_plugins();
$isInstalled = !empty($allPlugins[$pluginBasename]);
}
return $isInstalled;
}
function wcmpGetPluginButton($pluginBasename, $pluginName)
{
$button = [];
if (wcmpIsPluginInstalled($pluginBasename) && current_user_can('install_plugins')) {
return array(
wcmpSingleButtonArray(
__("Activate", 'woo-pagarme-payments') . " {$pluginName}",
wp_nonce_url(
self_admin_url("plugins.php?action=activate&plugin={$pluginBasename}&plugin_status=active"),
"activate-plugin_{$pluginBasename}"
)
)
);
}
if (current_user_can('install_plugins')) {
$plugin = explode('/', $pluginBasename)[0];
$button = array(
wcmpSingleButtonArray(
__("Install", 'woo-pagarme-payments') . " {$pluginName}",
wp_nonce_url(
self_admin_url("update.php?action=install-plugin&plugin={$plugin}"),
"install-plugin_{$plugin}"
)
)
);
}
return $button;
}
function wcmpAdminNoticeWoocommerce()
{
wcmpRenderAdminNoticeHtml(
__('Woocommerce plugin is required for Pagar.me module to work.', 'woo-pagarme-payments'),
wcmpGetPluginButton('woocommerce/woocommerce.php', 'WooCommerce')
);
}
function wcmpAdminNoticePermalink()
{
wcmpRenderAdminNoticeHtml(
__(
'Permalink structure in Wordpress Settings must be different from “<b>Plain</b>”. ' .
'Please correct this setting to be able to transact with Pagar.me.',
'woo-pagarme-payments'
),
array(
wcmpSingleButtonArray(
'Permalink Settings',
self_admin_url('options-permalink.php')
)
)
);
}
function wcmpAdminNoticeCheckoutFields()
{
if (!function_exists('WC')) {
return;
}
WC()->session = new WC_Session_Handler;
$billingFields = WC()->checkout->get_checkout_fields()['billing'];
$missingFields = [];
$requiredFields = [
'billing_cpf',
'billing_cnpj',
'billing_document',
'billing_first_name',
'billing_last_name'
];
if (!(new Config())->getAllowNoAddress()) {
$requiredFields[] = 'billing_address_1';
$requiredFields[] = 'billing_address_2';
$requiredFields[] = 'billing_country';
$requiredFields[] = 'billing_city';
$requiredFields[] = 'billing_state';
$requiredFields[] = 'billing_postcode';
}
foreach ($requiredFields as $field) {
if (!array_key_exists($field, $billingFields)) {
$missingFields[] = $field;
}
}
if (hasAnyBillingDocument($missingFields)) {
$missingFields = array_slice($missingFields, 2);
}
if (empty($missingFields)) {
return;
}
$message = __('The following checkout fields are required, but were not found:', 'woo-pagarme-payments');
$message .= '</p><ul>';
foreach ($missingFields as $field) {
$message .= "<li>{$field}</li>";
}
$message .= '</ul><p>';
$message .= sprintf(
__("Please, make sure to include them for Pagar.me plugin to work. If you are customizing the "
. "checkout, the address fields must have the 'name' attribute exactly as listed above. %sRead "
. "documentation »%s", "woo-pagarme-payments"),
sprintf(
'<a href="%s" target="_blank" rel="noopener">',
PAGARME_REQUIREMENTS_URL
),
'</a>'
);
$message .= '</p>';
wcmpRenderAdminNoticeHtml($message);
}
function wcmpLoadNotice($name)
{
add_action('admin_notices', "wcmp{$name}");
}
function wcmpLoadInstances()
{
require_once __DIR__ . '/vendor/autoload.php';
Woocommerce\Pagarme\Core::instance();
(new Woocommerce\Pagarme\DB\Migration\Migrator)->execute();
do_action('wcmp_init');
}
/**
* @return void
* @uses wcmpAdminNoticeWoocommerce()
* @uses wcmpAdminNoticePermalink()
* @uses wcmpAdminNoticeCheckoutFields()
*/
function wcmpPluginsLoadedCheck()
{
$woocommerce = class_exists('WooCommerce');
add_action('in_plugin_update_message-' . WCMP_PLUGIN_BASE, function ($pluginData) {
versionUpdateWarning(WCMP_VERSION, $pluginData['new_version']);
});
if (!$woocommerce) {
wcmpLoadNotice('AdminNoticeWoocommerce');
}
if ($woocommerce) {
wcmpLoadInstances();
}
if (get_option('permalink_structure') === '') {
wcmpLoadNotice('AdminNoticePermalink');
}
wcmpLoadNotice('AdminNoticeCheckoutFields');
}
add_action('plugins_loaded', 'wcmpPluginsLoadedCheck', 0);
add_action('before_woocommerce_init', 'checkCompatibilityWithFeatures', 0);
add_action('woocommerce_blocks_loaded', 'addWoocommerceSupportedBlocks');
function hasAnyBillingDocument($missingFields)
{
$hasCpf = in_array('billing_cpf', $missingFields);
$hasCnpj = in_array('billing_cnpj', $missingFields);
$hasDocument = in_array('billing_document', $missingFields);
return ($hasCpf && (!$hasCnpj || !$hasDocument))
|| ($hasCnpj && (!$hasCpf || !$hasDocument))
|| ($hasDocument && (!$hasCpf || !$hasCnpj));
}
function checkCompatibilityWithFeatures()
{
$compatibilization = new FeatureCompatibilization();
$compatibilization->callCompatibilization();
}
function addWoocommerceSupportedBlocks()
{
$compatibilization = new FeatureCompatibilization();
$compatibilization->addSupportedBlocks();
}
function versionUpdateWarning($currentVersion, $newVersion)
{
$currentVersionMajorPart = explode('.', $currentVersion)[0];
$newVersionMajorPart = explode('.', $newVersion)[0];
if ($currentVersionMajorPart >= $newVersionMajorPart) {
return;
}
?>
<hr class="pagarme-major-update-warning-separator"/>
<div class="pagarme-major-update-warning">
<p></p>
<div>
<div class="pagarme-major-update-title">
<?= __('We recommend backup before upgrading!', 'woo-pagarme-payments'); ?>
</div>
<div class="pagarme-major-update-message">
<?php
printf(
esc_html__(
'This new release contains crucial architecture and functionality updates. ' .
'We highly recommend you %1$sbackup your site before upgrading%2$s. ' .
'It is highly recommended to perform and validate the update first in the staging ' .
'environment before performing the update in production.',
'woo-pagarme-payments'
),
'<a href="https://woocommerce.com/pt-br/posts/how-to-easily-backup-and-restore-woocommerce/">',
'</a>'
);
?>
</div>
</div>
</div>
<?php
}
function wcmpOnActivation()
{
if (!class_exists('WooCommerce')) {
return;
}
add_option(WCMP_OPTION_ACTIVATE, true);
$upgradePath = ABSPATH . 'wp-admin/includes/upgrade.php';
wcmpCreateCoreConfigurationTable($upgradePath);
wcmpCreateCoreCustomerTable($upgradePath);
wcmpCreateCoreChargeTable($upgradePath);
wcmpCreateCoreOrderTable($upgradePath);
wcmpCreateCoreSavedCardTable($upgradePath);
wcmpCreateCoreTransactionTable($upgradePath);
wcmpCreateCoreHubInstallToken($upgradePath);
register_uninstall_hook(__FILE__, 'wcmpOnUninstall');
}
function wcmpCreateCoreConfigurationTable($upgradePath)
{
global $wpdb;
require_once $upgradePath;
$charset = $wpdb->get_charset_collate();
$tableName = $wpdb->prefix . 'pagarme_module_core_configuration';
$query = "CREATE TABLE IF NOT EXISTS {$tableName}
(
id int unsigned auto_increment comment 'ID' primary key,
data text not null comment 'data',
store_id varchar(50) not null comment 'Store id'
) comment 'Configuration Table' {$charset};";
dbDelta($query);
}
function wcmpCreateCoreCustomerTable($upgradePath)
{
global $wpdb;
require_once $upgradePath;
$charset = $wpdb->get_charset_collate();
$tableName = $wpdb->prefix . 'pagarme_module_core_customer';
$query = "CREATE TABLE IF NOT EXISTS {$tableName}
(
id int unsigned auto_increment comment 'ID' primary key,
code varchar(100) not null comment 'platform customer id',
pagarme_id varchar(20) not null comment 'format: cus_xxxxxxxxxxxxxxxx'
) comment 'Customer Table' {$charset};";
dbDelta($query);
}
function wcmpCreateCoreChargeTable($upgradePath)
{
global $wpdb;
require_once $upgradePath;
$charset = $wpdb->get_charset_collate();
$tableName = $wpdb->prefix . 'pagarme_module_core_charge';
$query = "CREATE TABLE IF NOT EXISTS {$tableName}
(
id int unsigned auto_increment comment 'ID' primary key,
pagarme_id varchar(19) not null comment 'format: ch_xxxxxxxxxxxxxxxx',
order_id varchar(19) not null comment 'format: or_xxxxxxxxxxxxxxxx',
code varchar(100) not null comment 'Code',
amount int unsigned not null comment 'amount',
paid_amount int unsigned not null comment 'Paid Amount',
canceled_amount int unsigned not null comment 'Canceled Amount',
refunded_amount int unsigned not null comment 'Refunded Amount',
status varchar(30) not null comment 'Status',
metadata text null comment 'Charge metadata',
customer_id varchar(50) null comment 'Charge customer id'
) comment 'Charge Table' {$charset};";
dbDelta($query);
}
function wcmpCreateCoreOrderTable($upgradePath)
{
global $wpdb;
require_once $upgradePath;
$charset = $wpdb->get_charset_collate();
$tableName = $wpdb->prefix . 'pagarme_module_core_order';
$query = "CREATE TABLE IF NOT EXISTS {$tableName}
(
id int unsigned auto_increment comment 'ID' primary key,
pagarme_id varchar(19) not null comment 'format: or_xxxxxxxxxxxxxxxx',
code varchar(100) not null comment 'Code',
status varchar(30) not null comment 'Status'
) comment 'Order Table' {$charset};";
dbDelta($query);
}
function wcmpCreateCoreTransactionTable($upgradePath)
{
global $wpdb;
require_once $upgradePath;
$charset = $wpdb->get_charset_collate();
$tableName = $wpdb->prefix . 'pagarme_module_core_transaction';
$query = "CREATE TABLE IF NOT EXISTS {$tableName}
(
id int unsigned auto_increment comment 'ID' primary key,
pagarme_id varchar(21) not null comment 'format: tran_xxxxxxxxxxxxxxxx',
charge_id varchar(19) not null comment 'format: ch_xxxxxxxxxxxxxxxx',
amount int unsigned not null comment 'amount',
paid_amount int unsigned not null comment 'paid amount',
acquirer_tid text null,
acquirer_nsu text null,
acquirer_auth_code text null,
acquirer_name text not null comment 'Type',
acquirer_message text not null comment 'Type',
type varchar(30) not null comment 'Type',
status varchar(30) not null comment 'Status',
created_at datetime not null comment 'Created At',
boleto_url text null comment 'Boleto url',
card_data text null comment 'Card data',
transaction_data text null comment 'Transaction Data'
) comment 'Transaction Table' {$charset};";
dbDelta($query);
}
function wcmpCreateCoreSavedCardTable($upgradePath)
{
global $wpdb;
require_once $upgradePath;
$charset = $wpdb->get_charset_collate();
$tableName = $wpdb->prefix . 'pagarme_module_core_saved_card';
$query = "CREATE TABLE IF NOT EXISTS {$tableName}
(
id int unsigned auto_increment comment 'ID' primary key,
pagarme_id varchar(21) not null comment 'format: card_xxxxxxxxxxxxxxxx',
owner_id varchar(21) not null comment 'format: cus_xxxxxxxxxxxxxxxx',
first_six_digits varchar(6) not null comment 'card first six digits',
last_four_digits varchar(4) not null comment 'card last four digits',
brand varchar(30) not null comment 'card brand',
owner_name varchar(50) null comment 'Card owner name',
created_at datetime not null comment 'Card createdAt'
) comment 'Saved Card Table' {$charset};";
dbDelta($query);
}
function wcmpCreateCoreHubInstallToken($upgradePath)
{
global $wpdb;
require_once $upgradePath;
$charset = $wpdb->get_charset_collate();
$tableName = $wpdb->prefix . 'pagarme_module_core_hub_install_token';
$query = "CREATE TABLE IF NOT EXISTS {$tableName}
(
id int unsigned auto_increment comment 'ID' primary key,
token varchar(255) not null comment 'hub install token',
used tinyint not null comment 'ensures token was used or not',
created_at_timestamp int not null comment 'Token Created timestap',
expire_at_timestamp int not null comment 'Token Expiration timestamp'
) comment 'Hub Install Token Table' {$charset};";
dbDelta($query);
}
function wcmpOnDeactivation()
{
// @todo
}
function wcmpOnUninstall()
{
// @todo
}
register_activation_hook(__FILE__, 'wcmpOnActivation');
register_deactivation_hook(__FILE__, 'wcmpOnDeactivation');