forked from greguly/cielo-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-cielo-gateway.php
235 lines (203 loc) · 6.67 KB
/
woocommerce-cielo-gateway.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
<?php
/**
* Plugin Name: Cielo WooCommerce
* Plugin URI: http://omniwp.com.br/plugins/
* Description: Adiciona a opção de pagamento pela Cielo ao WooCommerce
* Author: Gabriel Reguly, claudiosanches
* Author URI: http://omniwp.com.br
* Version: 3.1.1
* License: GPLv2 or later
* Text Domain: cielo-woocommerce
* Domain Path: /languages/
* Copyright: © 2012, 2013, 2014 omniWP
* License: GNU General Public License v2.0
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WC_Cielo' ) ) :
/**
* WooCommerce WC_Cielo main class.
*/
class WC_Cielo {
/**
* Plugin version.
*
* @var string
*/
const VERSION = '3.1.1';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* Initialize the plugin public actions.
*/
private function __construct() {
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Checks with WooCommerce and WooCommerce is installed.
if ( class_exists( 'WC_Payment_Gateway' ) ) {
$this->upgrade();
$this->includes();
// Add the gateway.
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway' ) );
add_action('template_redirect', __CLASS__.'::create_hooked_pages');
// Admin actions.
if ( is_admin() ) {
add_action( 'woocommerce_process_shop_order_meta', array( $this, 'cancel_payment' ), 999 );
}
} else {
add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
}
}
/**
*
*/
public function create_hooked_pages () {
session_start();
global $wp_query;
if ($wp_query->query['pagename'] === 'redirect_page') {
$wp_query->is_404 = false;
$gateway = new WC_Cielo_Gateway();
$gateway->redirect_page();
exit();
}
if ($wp_query->query['pagename'] === 'notification_page') {
$wp_query->is_404 = false;
header("HTTP/1.1 200 OK");
$gateway = new WC_Cielo_Gateway();
$gateway->notification_page();
exit();
}
if ($wp_query->query['pagename'] === 'change_status_page') {
$wp_query->is_404 = false;
header("HTTP/1.1 200 OK");
$gateway = new WC_Cielo_Gateway();
$gateway->change_status_page();
exit();
}
}
/**
* 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.
*
* @return void
*/
public function load_plugin_textdomain() {
$locale = apply_filters( 'plugin_locale', get_locale(), 'cielo-woocommerce' );
load_textdomain( 'cielo-woocommerce', trailingslashit( WP_LANG_DIR ) . 'cielo-woocommerce/cielo-woocommerce-' . $locale . '.mo' );
load_plugin_textdomain( 'cielo-woocommerce', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Includes.
*
* @return void
*/
private function includes() {
include_once( 'includes/class-wc-cielo-xml.php' );
include_once( 'includes/class-wc-cielo-api.php' );
include_once( 'includes/class-wc-cielo-gateway.php' );
}
/**
* Add the gateway to WooCommerce.
*
* @param array $methods WooCommerce payment methods.
*
* @return array Payment methods with Cielo.
*/
public function add_gateway( $methods ) {
$methods[] = 'WC_Cielo_Gateway';
return $methods;
}
/**
* Cancel order payment.
*
* @param int $order_id Order ID.
*
* @return void
*/
public function cancel_payment( $order_id ) {
$order = new WC_Order( $order_id );
$refunded = get_post_meta( $order_id, '_wc_cielo_transaction_refunded', true );
if ( 'refunded' == $order->status && 'yes' != $refunded && 'cielo' == $order->payment_method ) {
$diff = ( strtotime( $order->order_date ) - strtotime( current_time( 'mysql' ) ) );
$days = absint( $diff / ( 60 * 60 * 24 ) );
$prefix = __( 'Cielo', 'cielo-woocommerce' ) . ': ';
if ( 90 > $days ) {
$tid = get_post_meta( $order->id, '_wc_cielo_transaction_tid', true );
$gateway = new WC_Cielo_Gateway();
$response = $gateway->api->do_transaction_cancellation( $order, $tid, $order->id . '-' . time() );
// Already canceled.
if ( isset( $response->mensagem ) && ! empty( $response->mensagem ) ) {
$order->add_order_note( $prefix . sanitize_text_field( $response->mensagem ) );
} else {
$order->add_order_note( $prefix . __( 'Transaction canceled successfully', 'cielo-woocommerce' ) );
}
} else {
$order->add_order_note( $prefix . __( 'This transaction has been made more than 90 days and therefore it can not be canceled', 'cielo-woocommerce' ) );
}
update_post_meta( $order_id, '_wc_cielo_transaction_refunded', 'yes' );
}
}
/**
* Upgrade plugin options.
*
* @return void
*/
private function upgrade() {
if ( is_admin() ) {
$version = get_option( 'wc_cielo_version', '0' );
if ( version_compare( $version, WC_Cielo::VERSION, '<' ) ) {
$options = get_option( 'woocommerce_cielo_settings' );
$new_options = array();
// Upgrade from 2.0.x.
if ( isset( $options['mode'] ) ) {
$new_options['enabled'] = $options['enabled'];
$new_options['title'] = $options['title'];
$new_options['description'] = $options['description'];
$new_options['environment'] = $options['mode'];
$new_options['number'] = $options['numero'];
$new_options['key'] = $options['chave'];
$new_options['methods'] = $options['meios'];
$new_options['authorization'] = $options['autorizar'];
$new_options['smallest_installment'] = $options['parcela_minima'];
$new_options['interest_rate'] = $options['taxa_juros'];
$new_options['interest_rate'] = $options['debit_discount'];
$new_options['installments'] = $options['parcelas'];
$new_options['interest'] = $options['juros'];
$new_options['installment_type'] = ( '2' == $options['parcelamento'] ) ? 'client' : 'store';
$new_options['design'] = 'default';
} else {
$new_options = $options;
}
update_option( 'woocommerce_cielo_settings', $new_options );
update_option( 'wc_cielo_version', WC_Cielo::VERSION );
}
}
}
/**
* WooCommerce fallback notice.
*
* @return string
*/
public function woocommerce_missing_notice() {
echo '<div class="error"><p>' . sprintf( __( 'WooCommerce Cielo Gateway depends on the last version of the %s to work!', 'cielo-woocommerce' ), '<a href="http://wordpress.org/extend/plugins/woocommerce/">' . __( 'WooCommerce', 'cielo-woocommerce' ) . '</a>' ) . '</p></div>';
}
}
add_action( 'plugins_loaded', array( 'WC_Cielo', 'get_instance' ), 0 );
endif;