forked from albrtinoaugusto/wc-mpesa-payment-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc-mpesa-payment-gateway.php
461 lines (381 loc) · 17.9 KB
/
wc-mpesa-payment-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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
/*
Plugin Name: Payment Gateway - Mpesa for WooCommerce
Plugin URI: https://wordpress.org/plugins/wc-m-pesa-payment-gateway/
Description: Receive payments directly to your store through the Vodacom Mozambique M-Pesa.
Version: 1.2.2
WC requires at least: 4.0.0
WC tested up to: 5.2.1
Author: karson <[email protected]>
Author URI: http://karsonadam.com
Copyright: © 2019 karson <[email protected]>.
License: GNU General Public License v2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
$wc_mpesa_db_version = "1.2.2";
add_action('plugins_loaded', 'wc_mpesa_init', 0);
add_action('plugins_loaded', 'wc_mpesa_update_check');
register_activation_hook(__FILE__, 'wc_mpesa_install');
/**
* TODO: Remove custom table functions
*
* @return void
*/
function wc_mpesa_install()
{
global $wc_mpesa_db_version;
global $wpdb;
$table_name = $wpdb->prefix . "wc_mpesa_transactions";
if (!get_option('wc_mpesa_version', $wc_mpesa_db_version)) {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$wpdb->query("DROP TABLE IF EXISTS $table_name");
update_option('wc_mpesa_version', $wc_mpesa_db_version);
}
}
function wc_mpesa_update_check()
{
global $wc_mpesa_db_version;
if ($wc_mpesa_db_version != get_option('wc_mpesa_version')) {
wc_mpesa_install();
}
}
function wc_mpesa_init()
{
require 'vendor/autoload.php';
if (!class_exists('WC_Payment_Gateway')) return;
/**
* Localisation
*/
load_plugin_textdomain('wc-mpesa-payment-gateway', false, dirname(plugin_basename(__FILE__)) . '/languages');
/**
* Gateway class
*/
class WC_Gateway_MPESA extends WC_Payment_Gateway
{
public function __construct()
{
$this->id = 'wc-mpesa-payment-gateway';
$this->icon = apply_filters('wc-mpesa_icon', plugins_url('assets/img/m-pesa-logo.png', __FILE__));
$this->has_fields = false;
$this->method_title = __('Mpesa for WooCommerce', 'wc-mpesa-payment-gateway');
$this->method_description = __('Accept Mpesa Payments for WooCommerce', 'wc-mpesa-payment-gateway');
// Load the settings.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Define user set variables
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->api_key = $this->get_option('api_key');
$this->public_key = $this->get_option('public_key');
$this->service_provider = $this->get_option('service_provider');
$this->test = $this->get_option('test');
// Actions
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('woocommerce_receipt_' . $this->id, array($this, 'payment_form_html'));
add_action('wp_enqueue_scripts', array($this, 'payment_scripts'));
add_action('woocommerce_api_process_action', array($this, 'process_action'));
/**
* Set a minimum order amount for checkout
*/
add_action('woocommerce_checkout_process', 'wc_minimum_order_amount');
add_action('woocommerce_before_cart', 'wc_minimum_order_amount');
}
/**
* Create form fields for the payment gateway
*
* @return void
*/
public function init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable', 'wc-mpesa-payment-gateway'),
'type' => 'checkbox',
'label' => __('Enable Mpesa payment gateway', 'wc-mpesa-payment-gateway'),
'default' => 'no'
),
'title' => array(
'title' => __('Title', 'wc-mpesa-payment-gateway'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout', 'wc-mpesa-payment-gateway'),
'default' => __('Mpesa for WooCommerce', 'wc-mpesa-payment-gateway'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Customer Message', 'wc-mpesa-payment-gateway'),
'type' => 'textarea',
'default' => __('Pay via mpesa', 'wc-mpesa-payment-gateway')
),
'api_key' => array(
'title' => __('API Key', 'wc-mpesa-payment-gateway'),
'type' => 'password',
'default' => __('', 'wc-mpesa-payment-gateway')
),
'public_key' => array(
'title' => __('Public Key', 'wc-mpesa-payment-gateway'),
'type' => 'textarea',
'default' => __('', 'wc-mpesa-payment-gateway')
),
'service_provider' => array(
'title' => __('Service Provider Code', 'wc-mpesa-payment-gateway'),
'type' => 'text',
'description' => __('Use 171717 for testing', 'wc-mpesa-payment-gateway'),
'default' => 171717
),
'test' => array(
'title' => __('Test Mode', 'wc-mpesa-payment-gateway'),
'type' => 'checkbox',
'label' => __('Enable Test Environment', 'wc-mpesa-payment-gateway'),
'default' => 'yes',
),
);
}
public function payment_fields()
{
session_start();
// ok, let's display some description before the payment form
if ($this->description) {
// you can instructions for test mode, I mean test card numbers etc.
if ('yes' == $this->test) {
$this->description .= __('<br/> TEST MODE ENABLED.', 'wc-mpesa-payment-gateway');
$this->description = trim($this->description);
}
// display the description with <p> tags etc.
echo wpautop(wp_kses_post($this->description));
}
if(isset($_SESSION['wc_mpesa_number'])){
$number = $this->wc_mpesa_validate_number($_SESSION['wc_mpesa_number']);
}else{
$number = '';
}
// I will echo() the form, but you can close PHP tags and print it directly in HTML
echo '<fieldset id="wc-' . esc_attr($this->id) . '-cc-form" class="wc-credit-card-form wc-payment-form" style="background:transparent;">';
//Use unique IDs, because other gateways could already use
echo '<div class="form-row form-row-wide"><label>' . esc_html__('Mpesa number', 'wc-mpesa-payment-gateway') . '<span class="required">*</span></label>
<input name="wc_mpesa_number" type="tel" value="' . esc_attr($number) . '" placeholder="' . esc_attr__('ex: 84 123 4567', 'wc-mpesa-payment-gateway') . '">
</div>';
echo '<div class="clear"></div></fieldset>';
}
public function validate_fields()
{
//validate currency
if ('MZN' != get_woocommerce_currency()) {
wc_add_notice(__('Currency not supported!', 'wc-mpesa-payment-gateway'), 'error');
return false;
}
//validate phone
$number = $this->wc_mpesa_validate_number($_POST['wc_mpesa_number']);
if (!$number) {
wc_add_notice(__('Phone number is required!', 'wc-mpesa-payment-gateway'), 'error');
return false;
}
//save phone to use on payment screen and new transactions
session_start();
$_SESSION['wc_mpesa_number'] = $number;
return true;
}
public function wc_mpesa_validate_number($number){
$number = filter_var($number, FILTER_VALIDATE_INT);
//validade mpesa numbers to only accept 84 and 85 prefix ex: 84 8283607
if (!isset($number) || strlen($number) != 9 || !preg_match('/^8[4|5][0-9]{7}$/', $number)) {
wc_add_notice(__('Phone number is incorrect!', 'wc-mpesa-payment-gateway'), 'error');
return false;
}
return $number;
}
function payment_scripts()
{
if (!is_checkout_pay_page()) {
return;
}
if ('no' == $this->enabled) {
return;
}
// Load only on specified pages
wp_enqueue_script('payment', plugin_dir_url(__FILE__) . '/assets/js/main.js', array(), false, true);
wp_localize_script('payment', 'payment_text', [
'status' => [
'intro' => [
'title' => __('Payment Information', 'wc-mpesa-payment-gateway'),
'description' => __('<ul><li>Check your details before pressing the button below.</li><li>Your phone number MUST be registered with MPesa (and Active) for this to work.</li><li>You will receive a pop-up on the phone requesting payment confirmation.</li><li>Enter your service PIN (MPesa) to continue.</li><li>You will receive a confirmation message shortly thereafter</li></ul>', 'wc-mpesa-payment-gateway'),
],
'requested' => [
'title' => __('Payment request sent!', 'wc-mpesa-payment-gateway'),
'description' => __('Check your mobile phone and enter your PIN code to confirm payment ...', 'wc-mpesa-payment-gateway')
],
'received' => [
'title' => __('Payment received!', 'wc-mpesa-payment-gateway'),
'description' => __('Your payment has been received and your order will be processed soon.', 'wc-mpesa-payment-gateway')
],
'timeout' => [
'title' => __('Payment timeout exceeded!', 'wc-mpesa-payment-gateway'),
'description' => __('Use your browser\'s back button and try again.', 'wc-mpesa-payment-gateway')
],
'failed' => [
'title' => __('Payment failed!', 'wc-mpesa-payment-gateway'),
'description' => __('Try again or use your browser\'s back button to change the number.', 'wc-mpesa-payment-gateway')
],
],
'buttons' => [
'pay' => __('Pay', 'wc-mpesa-payment-gateway'),
]
]);
wp_enqueue_style('style', plugin_dir_url(__FILE__) . '/assets/css/style.css', false, false, 'all');
}
function payment_form_html($order_id)
{
// modify post object here
$order = new WC_Order($order_id);
$return_url = $this->get_return_url($order);
$data = json_encode(['order_id' => $order_id, 'return_url'=> $return_url]);
require plugin_dir_path(__FILE__) . '/templates/payment.php';
}
/**
* Process the order payment status
*
* @param int $order_id
* @return array
*/
public function process_payment($order_id)
{
$order = new WC_Order($order_id);
$checkout_url = $order->get_checkout_payment_url(true);
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $checkout_url
);
}
function process_action()
{
session_start();
if (isset($_SESSION['wc_mpesa_number'])) {
$number = $this->wc_mpesa_validate_number($_SESSION['wc_mpesa_number']);
} else {
$number = false;
}
$response = [];
//Initialize API
$mpesa = new \Karson\MpesaPhpSdk\Mpesa();
$mpesa->setApiKey($this->api_key);
$mpesa->setPublicKey($this->public_key);
if ('yes' != $this->test) {
$mpesa->setEnv('live');
}
//Update code to use wp_send_json status instead custom status to reduce redundancy
$order = new WC_Order(filter_input(INPUT_POST, 'order_id', FILTER_VALIDATE_INT));
$order_id = $order->get_id();
if ($order_id && $number != false) {
$amount = $order->get_total();
$reference_id = $this->generate_reference_id($order_id);
$number = "258${number}";
try {
$result = $mpesa->c2b($order_id, $number, $amount, $reference_id, $this->service_provider);
} catch (\Exception $e) {
$response['status'] = 'failed';
if (WP_DEBUG) {
$response['error_message'] = $e->getMessage();
$response['raw'] = $result->response;
$response['request'] = [
'order_id' => $order_id,
'phone' => $phone,
'amount' => $amount,
'reference_id' => $reference_id,
'service_provider' => $this->service_provider,
];
}
return wp_send_json_error($response);
}
if ('yes' == $this->test) {
$response['raw'] = $result->response;
}
if ($result->response->output_ResponseCode == 'INS-0') {
// Mark as paid
$order->payment_complete();
// Reduce stock levels
$order->reduce_order_stock();
// some notes to customer (replace true with false to make it private)
$order->add_order_note('Your order is paid! Thank you!', true);
// Remove cart
WC()->cart->empty_cart();
$response['status'] = 'success';
} else {
// Mark as Failed
$response['status'] = 'failed';
switch ($result->response->output_ResponseCode) {
//show detailed error message
case 'INS-13':
$error_message = __('Invalid Shortcode Used!', 'wc-mpesa-payment-gateway');
break;
case 'INS-16':
$error_message = __('Unable to handle the request due to a temporary overloading!', 'wc-mpesa-payment-gateway');
break;
case 'INS-996':
$error_message = __('Customer Account Status Not Active!', 'wc-mpesa-payment-gateway');
break;
case 'INS-2001':
$error_message = __('Initiator authentication error!', 'wc-mpesa-payment-gateway');
break;
case 'INS-2006':
$error_message = __('Insufficient balance!', 'wc-mpesa-payment-gateway');
break;
default:
break;
}
//Detect API key error
if ($result->response->output_error) {
if (strpos($result->response->output_error, 'not authorized')) {
$error_message = __('API or Public key is not authorized!', 'wc-mpesa-payment-gateway');
} else if ($result->response->output_error = 'Bad API Key') {
$error_message = __('Bad API Key!', 'wc-mpesa-payment-gateway');
}
}
$response['error_message'] = $error_message;
$order->update_status('failed', __('Payment failed', 'wc-mpesa-payment-gateway'));
}
}
wp_send_json($response);
}
function generate_reference_id($order_id)
{
//generate uniq reference_id
return substr($order_id . bin2hex(random_bytes(5)), 0, 10);
}
function wc_minimum_order_amount()
{
// Set this variable to specify a minimum order value
$minimum = 1;
if (WC()->cart->total < $minimum) {
if (is_cart()) {
wc_print_notice(
sprintf(
'Your current order total is %s — you must have an order with a minimum of %s to place your order ',
wc_price(WC()->cart->total),
wc_price($minimum)
),
'error'
);
} else {
wc_add_notice(
sprintf(
'Your current order total is %s — you must have an order with a minimum of %s to place your order',
wc_price(WC()->cart->total),
wc_price($minimum)
),
'error'
);
}
}
}
} //END WC_Gateway_MPESA
/**
* Add the Gateway to WooCommerce
**/
function woocommerce_add_gateway_mpesa_gateway($methods)
{
$methods[] = 'WC_Gateway_MPESA';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'woocommerce_add_gateway_mpesa_gateway');
}