forked from MarceloPedra/conekta-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconekta_card_gateway.php
executable file
·335 lines (290 loc) · 12.2 KB
/
conekta_card_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
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
<?php
if (!class_exists('Conekta')) {
require_once("lib/conekta-php/lib/Conekta.php");
}
/*
* Title : Conekta Payment extension for WooCommerce
* Author : Cristina Randall
* Url : https://wordpress.org/plugins/conekta-woocommerce
*/
class WC_Conekta_Card_Gateway extends WC_Conekta_Plugin
{
protected $GATEWAY_NAME = "WC_Conekta_Card_Gateway";
protected $is_sandbox = true;
protected $order = null;
protected $transaction_id = null;
protected $conekta_order_id = null;
protected $transaction_error_message = null;
protected $currencies = array('MXN', 'USD');
public function __construct() {
$this->id = 'conektacard';
$this->method_title = __('Conekta Card', 'conektacard');
$this->has_fields = true;
$this->ckpg_init_form_fields();
$this->init_settings();
$this->title = $this->settings['title'];
$this->description = '';
$this->icon = $this->settings['alternate_imageurl'] ?
$this->settings['alternate_imageurl'] :
WP_PLUGIN_URL . "/" . plugin_basename( dirname(__FILE__))
. '/images/credits.png';
$this->use_sandbox_api = strcmp($this->settings['debug'], 'yes') == 0;
$this->enable_meses = strcmp($this->settings['meses'], 'yes') == 0;
$this->test_api_key = $this->settings['test_api_key'];
$this->live_api_key = $this->settings['live_api_key'];
$this->test_publishable_key = $this->settings['test_publishable_key'];
$this->live_publishable_key = $this->settings['live_publishable_key'];
$this->publishable_key = $this->use_sandbox_api ?
$this->test_publishable_key :
$this->live_publishable_key;
$this->secret_key = $this->use_sandbox_api ?
$this->test_api_key :
$this->live_api_key;
$this->lang_options = parent::ckpg_set_locale_options()->
ckpg_get_lang_options();
add_action('wp_enqueue_scripts', array($this, 'ckpg_payment_fields'));
add_action(
'woocommerce_update_options_payment_gateways_'.$this->id,
array($this, 'process_admin_options')
);
add_action('admin_notices', array(&$this, 'ckpg_perform_ssl_check'));
if (!$this->ckpg_validate_currency()) {
$this->enabled = false;
}
if(empty($this->secret_key)) {
$this->enabled = false;
}
}
/**
* Checks to see if SSL is configured and if plugin is configured in production mode
* Forces use of SSL if not in testing
*/
public function ckpg_perform_ssl_check()
{
///
if (!$this->use_sandbox_api
&& get_option('woocommerce_force_ssl_checkout') == 'no'
&& $this->enabled == 'yes') {
echo '<div class="error"><p>'
.sprintf(
__('%s sandbox testing is disabled and can performe live transactions'
.' but the <a href="%s">force SSL option</a> is disabled; your checkout'
.' is not secure! Please enable SSL and ensure your server has a valid SSL'
.' certificate.', 'woothemes'),
$this->GATEWAY_NAME, admin_url('admin.php?page=settings')
)
.'</p></div>';
}
}
public function ckpg_init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'type' => 'checkbox',
'title' => __('Enable/Disable', 'woothemes'),
'label' => __('Enable Credit Card Payment', 'woothemes'),
'default' => 'yes'
),
'meses' => array(
'type' => 'checkbox',
'title' => __('Meses sin Intereses', 'woothemes'),
'label' => __('Enable Meses sin Intereses', 'woothemes'),
'default' => 'no'
),
'debug' => array(
'type' => 'checkbox',
'title' => __('Testing', 'woothemes'),
'label' => __('Turn on testing', 'woothemes'),
'default' => 'no'
),
'title' => array(
'type' => 'text',
'title' => __('Title', 'woothemes'),
'description' => __('This controls the title which the user sees during checkout.', 'woothemes'),
'default' => __('Pago con Tarjeta de Crédito o Débito', 'woothemes')
),
'test_api_key' => array(
'type' => 'password',
'title' => __('Conekta API Test Private key', 'woothemes'),
'default' => __('', 'woothemes')
),
'test_publishable_key' => array(
'type' => 'text',
'title' => __('Conekta API Test Public key', 'woothemes'),
'default' => __('', 'woothemes')
),
'live_api_key' => array(
'type' => 'password',
'title' => __('Conekta API Live Private key', 'woothemes'),
'default' => __('', 'woothemes')
),
'live_publishable_key' => array(
'type' => 'text',
'title' => __('Conekta API Live Public key', 'woothemes'),
'default' => __('', 'woothemes')
),
'alternate_imageurl' => array(
'type' => 'text',
'title' => __('Alternate Image to display on checkout, use fullly qualified url, served via https', 'woothemes'),
'default' => __('', 'woothemes')
),
);
}
public function admin_options() {
include_once('templates/admin.php');
}
public function payment_fields() {
include_once('templates/payment.php');
}
public function ckpg_payment_fields() {
if (!is_checkout()) {
return;
}
wp_enqueue_script('conekta_js', 'https://conektaapi.s3.amazonaws.com/v0.3.2/js/conekta.js', '', '', true);
wp_enqueue_script('tokenize', WP_PLUGIN_URL."/".plugin_basename(dirname(__FILE__)).'/assets/js/tokenize.js', '', '1.0', true); //check import convention
//PCI
$params = array(
'public_key' => $this->publishable_key
);
wp_localize_script('tokenize', 'wc_conekta_params', $params);
}
protected function ckpg_send_to_conekta()
{
global $woocommerce;
include_once('conekta_gateway_helper.php');
\Conekta\Conekta::setApiKey($this->secret_key);
\Conekta\Conekta::setApiVersion('2.0.0');
\Conekta\Conekta::setPlugin($this->name);
\Conekta\Conekta::setPluginVersion($this->version);
\Conekta\Conekta::setLocale('es');
//ALL $data VAR ASSIGNATION IS FREE OF VALIDATION
$data = ckpg_get_request_data($this->order);
$amount = (int) $data['amount'];
$items = $this->order->get_items();
$taxes = $this->order->get_taxes();
$line_items = ckpg_build_line_items($items, parent::ckpg_get_version());
$discount_lines = ckpg_build_discount_lines($data);
$shipping_lines = ckpg_build_shipping_lines($data);
$shipping_contact = ckpg_build_shipping_contact($data);
$tax_lines = ckpg_build_tax_lines($taxes);
$customer_info = ckpg_build_customer_info($data);
$order_metadata = ckpg_build_order_metadata($data);
$order_details = array(
'currency' => $data['currency'],
'line_items' => $line_items,
'customer_info' => $customer_info,
'shipping_lines' => $shipping_lines,
'discount_lines' => $discount_lines,
'tax_lines' => $tax_lines
);
if (!empty($shipping_contact)) {
$order_details = array_merge($order_details, array('shipping_contact' => $shipping_contact));
}
if (!empty($order_metadata)) {
$order_details = array_merge($order_details, array('metadata' => $order_metadata));
}
$order_details = ckpg_check_balance($order_details, $amount);
try {
$conekta_order_id = get_post_meta($this->order->get_id(), 'conekta-order-id', true);
if (!empty($conekta_order_id)) {
$order = \Conekta\Order::find($conekta_order_id);
$order->update($order_details);
} else {
$order = \Conekta\Order::create($order_details);
}
//ORDER ID IS GENERATED BY RESPONSE
update_post_meta($this->order->get_id(), 'conekta-order-id', $order->id);
$charge_details = array(
'payment_method' => array(
'type' => 'card',
'token_id' => $data['token']
),
'amount' => $amount
);
$monthly_installments = $data['monthly_installments'];
if ($monthly_installments > 1) {
$charge_details['payment_method']['monthly_installments'] = $monthly_installments;
}
$charge = $order->createCharge($charge_details);
$this->transaction_id = $charge->id;
if ($data['monthly_installments'] > 1) {
update_post_meta( $this->order->get_id(), 'meses-sin-intereses', $data['monthly_installments']);
}
update_post_meta( $this->order->get_id(), 'transaction_id', $this->transaction_id);
return true;
} catch(\Conekta\Handler $e) {
$description = $e->getMessage();
global $wp_version;
if (version_compare($wp_version, '4.1', '>=')) {
wc_add_notice(__('Error: ', 'woothemes') . $description , $notice_type = 'error');
} else {
error_log('Gateway Error:' . $description . "\n");
$woocommerce->add_error(__('Error: ', 'woothemes') . $description);
}
return false;
}
}
protected function ckpg_mark_as_failed_payment()
{
$this->order->add_order_note(
sprintf(
"%s Credit Card Payment Failed : '%s'",
$this->GATEWAY_NAME,
$this->transaction_error_message
)
);
}
protected function ckpg_completeOrder()
{
global $woocommerce;
if ($this->order->get_status() == 'completed')
return;
// adjust stock levels and change order status
$this->order->payment_complete();
$woocommerce->cart->empty_cart();
$this->order->add_order_note(
sprintf(
"%s payment completed with Transaction Id of '%s'",
$this->GATEWAY_NAME,
$this->transaction_id
)
);
unset($_SESSION['order_awaiting_payment']);
}
public function process_payment($order_id)
{
global $woocommerce;
$this->order = new WC_Order($order_id);
if ($this->ckpg_send_to_conekta())
{
$this->ckpg_completeOrder();
$result = array(
'result' => 'success',
'redirect' => $this->get_return_url($this->order)
);
return $result;
}
else
{
$this->ckpg_mark_as_failed_payment();
WC()->session->reload_checkout = true;
}
}
/**
* Checks if woocommerce has enabled available currencies for plugin
*
* @access public
* @return bool
*/
public function ckpg_validate_currency() {
return in_array(get_woocommerce_currency(), $this->currencies);
}
public function ckpg_is_null_or_empty_string($string) {
return (!isset($string) || trim($string) === '');
}
}
function ckpg_conekta_card_add_gateway($methods) {
array_push($methods, 'WC_Conekta_Card_Gateway');
return $methods;
}
add_filter('woocommerce_payment_gateways', 'ckpg_conekta_card_add_gateway');