-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwoocommerce-mundipagg.php
220 lines (183 loc) · 5.8 KB
/
woocommerce-mundipagg.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
<?php
/**
* Plugin Name: Claudio Sanches - MundiPagg for WooCommerce
* Plugin URI: https://github.com/claudiosanches/woocommerce-mundipagg
* Description: MundiPagg gateway for WooCommerce
* Author: Claudio Sanches
* Author URI: https://claudiosanches.com/
* Version: 2.2.0
* License: GPLv2 or later
* Text Domain: woocommerce-mundipagg
* Domain Path: /languages/
*
* @package WooCommerce_MundiPagg
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WC_Mundipagg' ) ) :
/**
* Plugin's main class.
*/
class WC_Mundipagg {
/**
* Plugin version.
*
* @var string
*/
const VERSION = '2.2.0';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* Initialize the plugin actions.
*/
private function __construct() {
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
add_action( 'init', array( __CLASS__, 'add_return_endpoint' ), 0 );
if ( class_exists( 'SoapClient' ) && class_exists( 'WC_Payment_Gateway' ) && class_exists( 'Extra_Checkout_Fields_For_Brazil' ) ) {
$this->includes();
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway' ) );
add_action( 'parse_request', array( $this, 'handle_return_requests' ), 0 );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
} else {
add_action( 'admin_notices', array( $this, 'missing_dependencies_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;
}
/**
* Get templates path.
*
* @return string
*/
public static function get_templates_path() {
return plugin_dir_path( __FILE__ ) . 'templates/';
}
/**
* Load the plugin text domain for translation.
*/
public function load_plugin_textdomain() {
$locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-mundipagg' );
load_textdomain( 'woocommerce-mundipagg', trailingslashit( WP_LANG_DIR ) . 'woocommerce-mundipagg/woocommerce-mundipagg-' . $locale . '.mo' );
load_plugin_textdomain( 'woocommerce-mundipagg', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Add the gateway to WooCommerce.
*
* @param array $methods WooCommerce payment methods.
*
* @return array Payment methods with MundiPagg.
*/
public function add_gateway( $methods ) {
$methods[] = 'WC_Mundipagg_Banking_Ticket_Gateway';
$methods[] = 'WC_Mundipagg_Credit_Card_Gateway';
return $methods;
}
/**
* Includes.
*/
private function includes() {
include_once 'includes/class-wc-mundipagg-api.php';
include_once 'includes/class-wc-mundipagg-banking-ticket-gateway.php';
include_once 'includes/class-wc-mundipagg-credit-card-gateway.php';
}
/**
* Missing dependencies notice.
*
* @return string
*/
public function missing_dependencies_notice() {
if ( ! class_exists( 'SoapClient' ) ) {
include_once 'includes/admin/views/html-notice-missing-soap-client.php';
}
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
include_once 'includes/admin/views/html-notice-missing-soap-client.php';
}
if ( ! class_exists( 'Extra_Checkout_Fields_For_Brazil' ) ) {
include_once 'includes/admin/views/html-notice-missing-soap-client.php';
}
}
/**
* Created the return endpoint.
*/
public static function add_return_endpoint() {
add_rewrite_endpoint( 'wc-mundipagg-return', EP_ROOT );
}
/**
* Plugin activate method.
*/
public static function activate() {
self::add_return_endpoint();
add_option( 'wc_mundipagg_version_on_install', WC_Mundipagg::VERSION );
flush_rewrite_rules();
}
/**
* Plugin deactivate method.
*/
public static function deactivate() {
flush_rewrite_rules();
}
/**
* Handle with return requests.
*/
public function handle_return_requests() {
global $wp;
// wc-mundipagg-return endpoint requests
if ( isset( $wp->query_vars['wc-mundipagg-return'] ) || isset( $_GET['wc-mundipagg-return'] ) ) {
ob_start();
if ( isset( $_POST['xmlStatusNotification'] ) ) {
try {
$data = html_entity_decode( urldecode( $_POST['xmlStatusNotification'] ) );
$xml = @new SimpleXMLElement( $data, LIBXML_NOCDATA );
// Banking ticket.
if ( isset( $xml->BoletoTransaction ) ) {
WC_Mundipagg_API::notification_handler( $xml, 'banking-ticket' );
}
// Credit card.
if ( isset( $xml->CreditCardTransaction ) ) {
WC_Mundipagg_API::notification_handler( $xml, 'credit-card' );
}
} catch ( Exception $e ) {
wp_die( __( 'Invalid return data', 'woocommerce-mundipagg' ), __( 'Invalid return data', 'woocommerce-mundipagg' ), array( 'response' => 400 ) );
}
}
ob_end_clean();
die( '1' );
}
}
/**
* Action links.
*
* @param array $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=wc_mundipagg_banking_ticket_gateway' ) ) . '">' . __( 'Banking Ticket Settings', 'woocommerce-mundipagg' ) . '</a>';
$plugin_links[] = '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=wc_mundipagg_credit_card_gateway' ) ) . '">' . __( 'Credit Card Settings', 'woocommerce-mundipagg' ) . '</a>';
return array_merge( $plugin_links, $links );
}
}
/**
* Plugin activation and deactivation methods.
*/
register_activation_hook( __FILE__, array( 'WC_Mundipagg', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'WC_Mundipagg', 'deactivate' ) );
add_action( 'plugins_loaded', array( 'WC_Mundipagg', 'get_instance' ) );
endif;