-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
315 lines (257 loc) · 10.8 KB
/
index.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
<?php
/**
* @package endpay
*/
/*
* Plugin Name: EndPay
* Plugin URI: https://endpay.cl
* Description: Payment solutions.
* Author: EndPay
* Author URI: http://endpay.cl
* Version: 1.0.2
* License: BSD-3
* Text Domain: endpay
/*
* This action hook registers our PHP class as a WooCommerce payment gateway
*/
add_filter( 'woocommerce_payment_gateways', 'add_gateway_class' );
function add_gateway_class( $gateways ) {
$gateways[] = 'WC_EndPay_Gateway'; // your class name is here
return $gateways;
}
/**
* Reemplaza el ícono
*/
function endpay_replace_icon( $icon_html, $id ) {
if($id === 'endpay'){
return '<img src="' . plugins_url( 'assets/webpay.png', __FILE__ ) . '" width="200" >';
}
return $icon_html;
}
add_filter( 'woocommerce_gateway_icon', 'endpay_replace_icon', 10, 2 );
/*
* The class itself, please note that it is inside plugins_loaded action hook
*/
add_action( 'plugins_loaded', 'init_gateway_class' );
function init_gateway_class() {
class WC_EndPay_Gateway extends WC_Payment_Gateway {
/**
* Class constructor, more about it in Step 3
*/
public function __construct() {
$this->id = 'endpay'; // payment gateway plugin ID
$this->icon = apply_filters( 'woocommerce_gateway_icon', 'endpay_replace_icon', 10, 2); // URL of the icon that will be displayed on checkout page near your gateway name
$this->has_fields = true; // in case you need a custom credit card form
$this->method_title = 'Endpay Gateway';
$this->method_description = 'Payment Solutions'; // will be displayed on the options page
$this->isProd = true;
$this->host = $this->isProd ? 'https://api.endpay.cl/1.0' : 'http://localhost:3000/api/1.0';
// gateways can support subscriptions, refunds, saved payment methods,
// but in this tutorial we begin with simple payments
$this->supports = array(
'products'
);
// Method with all the options fields
$this->init_form_fields();
// Load the settings.
$this->title = $this->get_option( 'title', 'Webpay' );
$this->init_settings();
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions' );
$this->enabled = $this->get_option( 'enabled' );
$this->testmode = 'yes' === $this->get_option( 'testmode' );
$this->commerceId = $this->testmode ? $this->get_option( 'test_commerce_id' ) : $this->get_option( 'commerce_id' );
$this->apiKey = $this->testmode ? $this->get_option( 'test_apiKey' ) : $this->get_option( 'api_key' );
// This action hook saves the settings
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
// We need custom JavaScript to obtain a token
add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
// You can also register a webhook here
add_action( 'woocommerce_api_wc_gateway_' . $this->id, array( $this, 'webhook' ) );
if (!$this->is_valid_for_use()) {
$this->enabled = false;
}
}
/**
* Plugin options, we deal with it in Step 3 too
*/
public function init_form_fields(){
$this->form_fields = array(
'enabled' => array(
'title' => 'Enable/Disable',
'label' => 'Enable EndPay Gateway',
'type' => 'checkbox',
'description' => '',
'default' => 'no'
),
/*
'title' => array(
'title' => 'Title',
'type' => 'text',
'description' => '',
'default' => 'Webpay',
'desc_tip' => false,
'hidden' => false
),
'description' => array(
'title' => 'Description',
'type' => 'textarea',
'description' => 'This controls the description which the user sees during checkout.',
'default' => __( 'Pay with your credit card via our super-cool payment gateway.', 'woocommerce'),
),
'instructions' => array(
'title' => __( 'Instructions', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'Instructions that will be added to the thank you page.', 'woocommerce' ),
'default' => __( 'Pay with cash upon deliversy.', 'woocommerce' ),
'desc_tip' => true,
),
'testmode' => array(
'title' => 'Test mode',
'label' => 'Enable Test Mode',
'type' => 'checkbox',
'description' => 'Place the payment gateway in test mode using test API keys.',
'default' => 'yes',
'desc_tip' => true,
),
'test_commerce_id' => array(
'title' => 'Test Commerce ID',
'type' => 'text'
),
'test_apiKey' => array(
'title' => 'Test API Key',
'type' => 'password',
),
*/
'commerce_id' => array(
'title' => 'Código de comercio',
'type' => 'text',
'description' => 'Indica tu código de comercio para el ambiente de producción. Este se te entregará al registrarte en <a href="https://endpay.cl" target="_blank">https://endpay.cl</a>',
'placeholder' => 'Ej: 11234'
),
'api_key' => array(
'title' => 'API Key',
'type' => 'text',
'description' => 'Esta llave privada te la entregará EndPay en la configuración de tu cuenta.',
'placeholder' => 'Ej: XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX'
)
);
}
/**
* You will need it if you want your custom credit card form, Step 4 is about it
*/
public function payment_fields() {
}
/*
* Custom CSS and JS, in most cases required only when you decided to go with a custom credit card form
*/
public function payment_scripts() {
}
/*
* Fields validation, more in Step 5
*/
public function validate_fields() {
}
/*
* We're processing the payments here, everything about it is in Step 5
*/
public function process_payment( $order_id ) {
global $woocommerce;
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
// we need it to get any order detailes
$order = wc_get_order( $order_id );
$amount = (int) number_format($order->get_total(), 0, ',', '');
$notify_url = add_query_arg('wc-api', 'wc_gateway_' . $this->id, home_url('/'));
/*
* Array with parameters for API interaction
*/
$data = array(
'subject' => 'Pago woocommerce',
'amount' => $amount,
'transaction_id' => $order_id,
'return_url' => $current_url,
'cancel_url' => $current_url,
'notify_url' => $notify_url
);
$args = array(
'headers' => array(
'X-Commerce-Id' => $this->commerceId,
'X-Api-Key' => $this->apiKey
),
'body' => $data,
);
/*
* Your API interaction could be built with wp_remote_post()
*/
// TODO use prod
$response = wp_remote_post( $this->host . '/payments/create', $args );
if( is_wp_error( $response ) ) {
wc_add_notice( 'Connection error.', 'error' );
return;
}
$body = json_decode( $response['body'], true );
if(isset($body['error'])){
return wc_add_notice( 'Error.', 'error' );
}
// Redirect to the thank you page
error_log( print_r($body, TRUE) );
return array(
'result' => 'success',
'redirect' => $body['url']
);
/*
} else {
wc_add_notice( 'Please try again.', 'error' );
return;
}*/
}
/**
* Comprueba configuración de moneda (Peso Chileno).
**/
public static function is_valid_for_use()
{
return in_array(get_woocommerce_currency(), ['CLP']);
}
/*
* In case you need a webhook
*/
public function webhook() {
global $woocommerce;
$paymentId = $_POST['id'];
$args = array(
'headers' => array(
'X-Commerce-Id' => $this->commerceId,
'X-Api-Key' => $this->apiKey
)
);
/*
* Get data Payment with wp_remote_get()
*/
$response = wp_remote_get( $this->host . '/payments/read/' . $paymentId, $args );
if( is_wp_error( $response ) ) {
wc_add_notice( 'Connection error.', 'error' );
return;
}
$body = json_decode( $response['body'], true );
error_log(print_r($body, TRUE));
if(isset($body['error'])){
error_log(print_r($body, TRUE));
return wp_send_json($body);
}
if($body['status'] != 'done'){
return;
}
$order_id = $body['transaction_id'];
$order = wc_get_order( $order_id );
// we received the payment
$order->payment_complete($body['id']);
$order->reduce_order_stock();
// some notes to customer (replace true with false to make it private)
$order->add_order_note(sprintf('Pago verificado con código único de verificación endpay #%s', $body['id']));
// Empty cart
$woocommerce->cart->empty_cart();
return wp_send_json(true);
}
}
}