-
Notifications
You must be signed in to change notification settings - Fork 26
/
woocommerce-pagseguro-oficial.php
291 lines (251 loc) · 9.47 KB
/
woocommerce-pagseguro-oficial.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
<?php
/**
* Plugin Name: WooCommerce PagSeguro Oficial
* Plugin URI: http://github.com/pagseguro/woocomerce
* Description: Gateway de pagamento PagSeguro para WooCommerce.
* Author: PagSeguro Internet LTDA.
* Author URI: https://pagseguro.uol.com.br/v2/guia-de-integracao/downloads.html#!Modulos
* Version: 2.0.1
* License: GPLv2 or later
* Text Domain: woocommerce-pagseguro-oficial
* Domain Path: languages/
*
* @package WooCommerce_PagSeguro_Oficial
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! defined( 'PS_PLUGIN_DIR' ) ) {
define( 'PS_PLUGIN_DIR', __FILE__ );
}
if ( ! class_exists( 'WC_PagSeguro' )) :
class WC_PagSeguro {
/**
* Plugin version.
*
* @var string
*/
const VERSION = '2.0.1';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
public function __construct()
{
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Checks with WooCommerce is installed.
if ( class_exists( 'WC_Payment_Gateway' ) ) {
$this->requires();
WC_PagSeguro_Shortcodes::init();
WC_PagSeguro_Status::init();
// Filters
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
// Actions
add_action( 'init', 'notification_listener');
add_action( 'init', array( $this, 'ajax_listener'));
} else {
add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
}
}
/**
* Return an instance of this class.
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Load the plugin text domain for translation.
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'woocommerce-pagseguro-oficial', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Add the gateway to WooCommerce.
*
* @param array $methods WooCommerce payment methods.
*
* @return array Payment methods with PagSeguro.
*/
public function add_gateway( $methods ) {
$methods[] = 'WC_PagSeguro_Gateway';
return $methods;
}
/**
* Requires.
*/
private function requires() {
require_once 'library/vendor/autoload.php';
require_once 'classes/exceptions/class-wc-pagseguro-exception.php';
require_once 'classes/class-wc-pagseguro-api.php';
require_once 'classes/class-wc-pagseguro-gateway.php';
require_once 'classes/class-wc-pagseguro-model.php';
require_once 'classes/class-wc-pagseguro-shortcodes.php';
require_once 'classes/class-wc-pagseguro-admin.php';
require_once 'classes/class-wc-pagseguro-status.php';
require_once 'classes/class-wc-pagseguro-payload.php';
require_once 'classes/class-wc-pagseguro-direct-payment.php';
require_once 'classes/admin/class-wc-pagseguro-methods.php';
require_once 'classes/admin/class-wc-pagseguro-conciliation.php';
require_once 'classes/admin/class-wc-pagseguro-cancel.php';
}
/**
* Ajax listener
*/
public function ajax_listener()
{
$requests = $this->filter_requests();
if (! is_null($requests['is_ajax']) && $requests['is_ajax'] && $requests['action'] == 'conciliation') {
$conciliation = new WC_PagSeguro_Conciliation;
$conciliation->init(get_option('woocommerce_pagseguro_settings'));
}
if (! is_null($requests['is_ajax']) && $requests['is_ajax'] && $requests['action'] == 'cancel') {
$cancel = new WC_PagSeguro_Cancel();
$cancel->init(get_option('woocommerce_pagseguro_settings'));
}
}
/**
* Get requests from http post|get
*
* @return array
*/
public function filter_requests()
{
return [
'is_ajax' => (!isset($_REQUEST['ajax'])) ? null: filter_var($_REQUEST['ajax'], FILTER_SANITIZE_STRING),
'action' => (!isset($_REQUEST['action'])) ? null : filter_var($_REQUEST['action'], FILTER_SANITIZE_STRING)
];
}
/**
* Action links.
*
* @return array
*/
public function plugin_action_links($links) {
$plugin_links = array();
$plugin_links[] = '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=pagseguro' ) ) . '">' . __( 'Settings', 'woocommerce-pagseguro-oficial' ) . '</a>';
return array_merge( $plugin_links, $links );
}
/**
* WooCommerce fallback notice.
*
* @return string
*/
public function woocommerce_missing_notice() {
$html = '<div class="error"><p>';
$html .= __( 'WooCommerce PagSeguro depends on the WooCommerce plugin to work!', 'woocommerce-pagseguro-oficial' );
$html .= '</p></div>';
echo $html;
}
}
add_action( 'plugins_loaded', array( 'WC_PagSeguro', 'get_instance' ) );
/**
* Insert Modal Markup in the top markup page
**/
function template_modals() {
include 'template/admin/modals.php';
};
add_action('admin_head', 'template_modals');
/**
* Include dataTables styles
*/
add_action('admin_enqueue_scripts', function(){
wp_enqueue_style(
'dataTable-styles',
plugins_url('/assets/css/jquery.dataTables.css', PS_PLUGIN_DIR), '', 1, 'screen'
);
});
/**
* Include ajax object
*/
add_action( 'wp_enqueue_scripts', function(){
wp_enqueue_script( 'ajax-script', plugins_url( '/assets/js/direct-payment.js', PS_PLUGIN_DIR ), array('jquery') );
wp_localize_script( 'ajax-script', 'ajax_object',
array( 'ajax_url' => plugins_url( '/classes/class-wc-pagseguro-ajax.php' , PS_PLUGIN_DIR ) ));
});
/**
* Include bootstrap to front-end
*/
add_action('wp_enqueue_scripts', function(){
wp_enqueue_script(
'bootstrap-script',
plugins_url( '/assets/js/vendor/bootstrap.min.js', PS_PLUGIN_DIR ), array('jquery')
);
wp_enqueue_style(
'bootstrap-styles',
plugins_url('/assets/css/vendor/bootstrap.min.css', PS_PLUGIN_DIR)
);
});
/**
* Direct payment styles
*/
add_action('wp_enqueue_scripts', function (){
wp_enqueue_style(
'font-awesome-styles',
plugins_url('/assets/css/vendor/font-awesome.min.css', PS_PLUGIN_DIR)
);
wp_enqueue_style(
'direct-payment-styles',
plugins_url('/assets/css/direct-payment.css', PS_PLUGIN_DIR)
);
});
/**
* Intercept woocommerce checkout process
*/
add_action('woocommerce_checkout_process', 'ps_validade_checkout_proccess');
function ps_validade_checkout_proccess()
{
if ($_POST['payment_method']) {
try {
$billing_address_1 = explode(', ', $_POST['billing_address_1']);
if (!isset($billing_address_1[1])) {
throw new Exception('[PAGSEGURO]: Invalid address');
};
} catch (Exception $exception) {
wc_add_notice(__('Endereço com formato inválido. Exemplo: Rua São João, 11'), 'error');
}
try {
if (!isset($_POST['billing_address_2']) || !$_POST['billing_address_2']) {
throw new Exception('[PAGSEGURO]: Invalid address');
}
} catch (Exception $exception) {
wc_add_notice(__('Por favor, preencha o bairro.'), 'error');
}
try {
$phone = strlen(filter_var($_POST['billing_phone'], FILTER_SANITIZE_NUMBER_INT));
if ($phone < 9 || $phone > 11) {
throw new Exception('[PAGSEGURO]: Invalid phone');
}
} catch (Exception $exception) {
wc_add_notice(__('Telefone inválido. Preencha DDD + NÚMERO'), 'error');
}
}
}
/**
* Call actions for notification
*/
function notification_listener()
{
if(isset($_REQUEST['notificationurl']) && $_REQUEST['notificationurl'] == "true" && isset($_POST)) {
$notification = new WC_PagSeguro_Gateway();
$notification->process_nofitication();
}
}
/**
* Setup activation hook
*/
register_activation_hook( __FILE__, function (){
require_once 'classes/class-wc-pagseguro-setup.php';
require_once 'classes/class-wc-pagseguro-pages.php';
WC_PagSeguro_Setup::plugin_activated();
});
endif;