forked from michelpl/woocommerce-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoo-pagarme-payments.php
290 lines (230 loc) · 8.99 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
<?php
/*
* Plugin Name: Pagar.me module for Woocommerce
* Version: 2.0.15
* Author: Pagar.me
* Author URI: https://pagar.me
* Text Domain: woo-pagarme-payments
* Domain Path: /languages
* License: GPL2
* Description: Enable Pagar.me Gateway for WooCommerce
* WC requires at least: 3.9.0
* WC tested up to: 5.4.0
*/
if (!function_exists('add_action')) {
exit(0);
}
require_once dirname(__FILE__) . '/constants.php';
function wcmp_render_admin_notice_html($message, $type = 'error')
{
?>
<div class="<?php echo esc_html($type); ?> notice is-dismissible">
<p>
<strong><?php esc_html_e('Pagar.me module for Woocommerce', 'woo-pagarme-payments'); ?>: </strong>
<?php echo /*phpcs:ignore*/ esc_attr($message); ?>
</p>
</div>
<?php
}
if (version_compare(PHP_VERSION, '7.1', '<')) {
function wcmp_admin_notice_php_version()
{
wcmp_render_admin_notice_html(
__('Your PHP version is not supported. Required >= 7.1.', 'woo-pagarme-payments')
);
}
_wcmp_load_notice('admin_notice_php_version');
return;
}
function wcmp_admin_notice_error()
{
wcmp_render_admin_notice_html(
__('WooCoomerce plugin is required.', 'woo-pagarme-payments')
);
}
function wcmp_admin_notice_error_wecffb()
{
wcmp_render_admin_notice_html(
__(
'WooCoomerce Extra Checkout Fields For Brazil plugin is required.',
'woo-pagarme-payments'
)
);
}
function _wcmp_load_notice($name)
{
add_action('admin_notices', "wcmp_{$name}");
}
function _wcmp_load_instances()
{
require_once 'vendor/autoload.php';
Woocommerce\Pagarme\Core::instance();
(new Woocommerce\Pagarme\DB\Migration\Migrator)->execute();
do_action('wcmp_init');
}
function wcmp_plugins_loaded_check()
{
$woocommerce = class_exists('WooCommerce');
$checkout_fields = class_exists('Extra_Checkout_Fields_For_Brazil');
if ($woocommerce && $checkout_fields) {
_wcmp_load_instances();
return;
}
if (!$woocommerce) {
_wcmp_load_notice('admin_notice_error');
}
if (!$checkout_fields) {
_wcmp_load_notice('admin_notice_error_wecffb');
}
}
add_action('plugins_loaded', 'wcmp_plugins_loaded_check', 0);
function wcmp_on_activation()
{
if (!class_exists('WooCommerce')) {
return;
}
add_option(WCMP_OPTION_ACTIVATE, true);
wcmp_create_core_configuration_table();
wcmp_create_core_customer_table();
wcmp_create_core_charge_table();
wcmp_create_core_order_table();
wcmp_create_core_saved_card_table();
wcmp_create_core_transaction_table();
wcmp_create_core_hub_install_token();
register_uninstall_hook(__FILE__, 'wcmp_on_uninstall');
}
function wcmp_create_core_configuration_table()
{
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'pagarme_module_core_configuration';
$query = "CREATE TABLE IF NOT EXISTS {$table_name}
(
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 wcmp_create_core_customer_table()
{
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'pagarme_module_core_customer';
$query = "CREATE TABLE IF NOT EXISTS {$table_name}
(
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 wcmp_create_core_charge_table()
{
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'pagarme_module_core_charge';
$query = "CREATE TABLE IF NOT EXISTS {$table_name}
(
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 wcmp_create_core_order_table()
{
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'pagarme_module_core_order';
$query = "CREATE TABLE IF NOT EXISTS {$table_name}
(
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 wcmp_create_core_transaction_table()
{
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'pagarme_module_core_transaction';
$query = "CREATE TABLE IF NOT EXISTS {$table_name}
(
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 wcmp_create_core_saved_card_table()
{
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'pagarme_module_core_saved_card';
$query = "CREATE TABLE IF NOT EXISTS {$table_name}
(
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 wcmp_create_core_hub_install_token()
{
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'pagarme_module_core_hub_install_token';
$query = "CREATE TABLE IF NOT EXISTS {$table_name}
(
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 wcmp_on_deactivation()
{
}
function wcmp_on_uninstall()
{
}
register_activation_hook(__FILE__, 'wcmp_on_activation');
register_deactivation_hook(__FILE__, 'wcmp_on_deactivation');