Skip to content

Commit dc9d7cd

Browse files
committed
september release
1 parent b053873 commit dc9d7cd

File tree

9 files changed

+363
-349
lines changed

9 files changed

+363
-349
lines changed

inc/functions.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* @since 0.18.01
88
*/
99

10+
use Osen\Woocommerce\Mpesa\STK;
11+
use Osen\Woocommerce\Mpesa\C2B;
12+
1013
function wc_mpesa_post_id_by_meta_key_and_value($key, $value)
1114
{
1215
global $wpdb;
@@ -33,7 +36,7 @@ function wc_mpesa_process_order($order)
3336

3437
add_action('woocommerce_thankyou_mpesa', function ($order_id) {
3538
$mpesa = get_option('woocommerce_mpesa_settings');
36-
$idtype = Osen\Woocommerce\Mpesa\C2B::$type;
39+
$idtype = C2B::$type;
3740
$url = home_url('?pesaipn&order=');
3841

3942
if (wc_get_order($order_id)) {
@@ -88,6 +91,18 @@ function wc_mpesa_process_order($order)
8891
<input type="hidden" id="current_order" value="<?php echo $order_id; ?>">
8992
<input type="hidden" id="payment_method" value="<?php echo $order->get_payment_method(); ?>">
9093
<p class="saving" id="mpesa_receipt">Confirming receipt, please wait</p>
94+
<table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
95+
<tbody>
96+
<tr class="woocommerce-table__line-item order_item">
97+
<td class="woocommerce-table__product-name product-name">
98+
<form action="<?php echo home_url("lipwa/request"); ?>" method="POST" id="renitiate-form">
99+
<input type="hidden" name="order" value="<?php echo $order_id; ?>">
100+
<button id="renitiate-button" class="button alt" type="submit"><?php echo $mpesa['resend'] ?? 'Resend STK Push'; ?></button>
101+
</form>
102+
</td>
103+
</tr>
104+
</tbody>
105+
</table>
91106
</section>
92107

93108
<?php if (isset($mpesa['enable_c2b']) && $mpesa['enable_c2b'] == 'yes') : ?>
@@ -121,14 +136,6 @@ function wc_mpesa_process_order($order)
121136

122137
</td>
123138
</tr>
124-
<tr class="woocommerce-table__line-item order_item">
125-
<td class="woocommerce-table__product-name product-name">
126-
<form action="<?php echo home_url("lipwa/request"); ?>" mhod="POST" id="renitiate-form">
127-
<input type="hidden" name="order" value="<?php echo $order_id; ?>">
128-
<button id="renitiate-button" class="button alt" type="submit"><?php echo $mpesa['resend'] ?? 'Resend STK Push'; ?></button>
129-
</form>
130-
</td>
131-
</tr>
132139
</tbody>
133140
</table>
134141
</section>
@@ -234,13 +241,13 @@ function wc_mpesa_process_ipn()
234241
$first_name = $order->get_billing_first_name();
235242
$last_name = $order->get_billing_last_name();
236243

237-
$result = Osen\Woocommerce\Mpesa\STK::request($phone, $total, $order_id, get_bloginfo('name') . ' Purchase', 'WCMPesa');
244+
$result = STK::request($phone, $total, $order_id, get_bloginfo('name') . ' Purchase', 'WCMPesa');
238245

239246
wp_send_json($result);
240247
break;
241248
case "validate":
242249
exit(wp_send_json(
243-
Osen\Woocommerce\Mpesa\STK::validate()
250+
STK::validate()
244251
));
245252
break;
246253

@@ -309,7 +316,7 @@ function wc_mpesa_process_ipn()
309316
update_post_meta($post, '_order_status', 'complete');
310317

311318
$headers = 'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>' . "\r\n";
312-
wp_mail($order["billing_address"], 'Your Mpesa payment', 'We acknowledge receipt of your payment via MPesa of KSh. ' . $amount . ' on ' . $transactionDate . 'with receipt Number ' . $mpesaReceiptNumber . '.', $headers);
319+
wp_mail($order->get_billing_email(), 'Your Mpesa payment', 'We acknowledge receipt of your payment via MPesa of KSh. ' . $amount . ' on ' . $transactionDate . 'with receipt Number ' . $mpesaReceiptNumber . '.', $headers);
313320
} elseif ($ipn_balance < 0) {
314321
$currency = get_woocommerce_currency();
315322
$order->update_status((isset($mpesa['completion']) ? $mpesa['completion'] : 'completed'), __("{$phone} has overpayed by {$currency} {$ipn_balance}. Receipt Number {$mpesaReceiptNumber}"));
@@ -329,11 +336,11 @@ function wc_mpesa_process_ipn()
329336
update_post_meta($post, '_order_id', $order_id);
330337
update_post_meta($post, '_receipt', $mpesaReceiptNumber);
331338

332-
exit(wp_send_json(Osen\Woocommerce\Mpesa\STK::confirm()));
339+
exit(wp_send_json(STK::confirm()));
333340
break;
334341

335342
case "register":
336-
Osen\Woocommerce\Mpesa\C2B::register(function ($response) {
343+
C2B::register(function ($response) {
337344
$status = isset($response['ResponseDescription']) ? 'success' : 'fail';
338345
if ($status == 'fail') {
339346
$message = isset($response['errorMessage']) ? $response['errorMessage'] : 'Could not register M-PESA URLs, try again later.';
@@ -396,7 +403,7 @@ function wc_mpesa_process_ipn()
396403
$order->update_status((isset($mpesa['completion']) ? $mpesa['completion'] : 'completed'), __("Full MPesa Payment Received From {$phone}. Receipt Number {$mpesaReceiptNumber}"));
397404

398405
$headers[] = 'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>' . "\r\n";
399-
wp_mail($order["billing_address"], 'Your Mpesa payment', 'We acknowledge receipt of your payment via MPesa of KSh. ' . $amount . ' on ' . $transactionDate . '. Receipt number ' . $mpesaReceiptNumber, $headers);
406+
wp_mail($order->get_billing_email(), 'Your Mpesa payment', 'We acknowledge receipt of your payment via MPesa of KSh. ' . $amount . ' on ' . $transactionDate . '. Receipt number ' . $mpesaReceiptNumber, $headers);
400407
} elseif ($ipn_balance < 0) {
401408
$currency = get_woocommerce_currency();
402409
$order->update_status((isset($mpesa['completion']) ? $mpesa['completion'] : 'completed'), __("{$phone} has overpayed by {$currency} {$ipn_balance}. Receipt Number {$mpesaReceiptNumber}"));
@@ -420,17 +427,17 @@ function wc_mpesa_process_ipn()
420427
$order->add_order_note(__("MPesa Error {$resultCode}: {$resultDesc}"));
421428
}
422429

423-
exit(wp_send_json(Osen\Woocommerce\Mpesa\STK::reconcile()));
430+
exit(wp_send_json(STK::reconcile()));
424431
} else {
425-
exit(wp_send_json(Osen\Woocommerce\Mpesa\STK::reconcile(function () {
432+
exit(wp_send_json(STK::reconcile(function () {
426433
return false;
427434
})));
428435
}
429436
break;
430437

431438
case "status":
432439
$transaction = $_POST['transaction'];
433-
exit(wp_send_json(Osen\Woocommerce\Mpesa\STK::status($transaction)));
440+
exit(wp_send_json(STK::status($transaction)));
434441
break;
435442

436443
case "result":
@@ -466,7 +473,7 @@ function wc_mpesa_process_ipn()
466473
$ReferenceData = $result['ReferenceData'];
467474
$ReferenceItem = $ReferenceData['ReferenceItem'];
468475
$Occasion = $ReferenceItem[0]['Value'];
469-
exit(wp_send_json(Osen\Woocommerce\Mpesa\STK::validate()));
476+
exit(wp_send_json(STK::validate()));
470477
break;
471478

472479
case "timeout":
@@ -493,10 +500,10 @@ function wc_mpesa_process_ipn()
493500
$order->add_order_note(__("MPesa Payment Timed Out", 'woocommerce'));
494501
}
495502

496-
exit(wp_send_json(Osen\Woocommerce\Mpesa\STK::timeout()));
503+
exit(wp_send_json(STK::timeout()));
497504
break;
498505
default:
499-
exit(wp_send_json(Osen\Woocommerce\Mpesa\C2B::register()));
506+
exit(wp_send_json(C2B::register()));
500507
}
501508
}
502509

inc/wc_mpesa_gateway.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* @package MPesa For WooCommerce
5-
* @subpackage WooCommerce Functions
5+
* @subpackage WooCommerce Mpesa Gateway
66
* @author Osen Concepts < [email protected] >
77
* @since 0.18.01
88
*/
@@ -47,26 +47,26 @@ class WC_MPESA_Gateway extends WC_Payment_Gateway
4747
* Constructor for the gateway.
4848
*/
4949
public function __construct()
50-
{
50+
{
5151
$this->id = 'mpesa';
5252
$this->icon = apply_filters('woocommerce_mpesa_icon', plugins_url('mpesa.png', __FILE__));
53-
$this->method_title = __('Lipa Na MPesa', 'woocommerce');
54-
53+
$this->method_title = __('Lipa Na MPesa', 'woocommerce');
54+
5555
// Load settings
5656
$this->init_form_fields();
57-
$this->init_settings();
58-
57+
$this->init_settings();
58+
5959
$env = $this->get_option('env', 'sandbox');
6060
$b2c_settings = ($this->get_option('enable_b2c', 'no') == 'yes')
61-
? '<strong>These settings are for Customer-2-Business payments. Click here to <a href="' . admin_url('edit.php?post_type=mpesaipn&page=wc_mpesa_b2c_preferences') . '">Setup Business-2-Customer</a>.</strong>'
62-
: '';
61+
? '<strong>These settings are for Customer-2-Business payments. Click here to <a href="' . admin_url('edit.php?post_type=mpesaipn&page=wc_mpesa_b2c_preferences') . '">Setup Business-2-Customer</a>.</strong>'
62+
: '';
6363
$test_cred = ($env == 'sandbox')
64-
? '<li>You can <a href="https://developer.safaricom.co.ke/test_credentials" target="_blank" >get sandbox test credentials here</a>.</li>'
65-
: '';
64+
? '<li>You can <a href="https://developer.safaricom.co.ke/test_credentials" target="_blank" >get sandbox test credentials here</a>.</li>'
65+
: '';
6666
$color = isset($_GET['reg-state']) ? $_GET['reg-state'] : 'black';
6767
$register = isset($_GET['mpesa-urls-registered']) ? "<div style='color: {$color}'>{$_GET['mpesa-urls-registered']}</div>" : '';
68-
69-
$this->method_description = $register . (($env == 'live') ? __('Receive payments via Safaricom M-PESA', 'woocommerce') : __('<h4 style="color: red;">IMPORTANT!</h4>' . $b2c_settings . '<li>Please <a href="https://developer.safaricom.co.ke/" target="_blank" >create an app on Daraja</a> if you haven\'t. If yoou already have a production app, fill in the app\'s consumer key and secret below.</li><li>Ensure you have access to the <a href="https://org.ke.m-pesa.com/">MPesa Web Portal</a>. You\'ll need this for when you go LIVE.</li><li>For security purposes, and for the MPesa Instant Payment Notification to work, ensure your site is running over https(SSL).</li>' . $test_cred) . '<li>We have a <a target="_blank" href="https://wc-mpesa.osen.co.ke/going-live">nice tutorial</a> here on migrating from Sandbox(test) environment, to Production(live) environment.<br> We offer the service at a fiat fee of KSh 4000. Call <a href="tel:+254204404993">+254204404993</a> or email <a href="mailto:[email protected]">[email protected]</a> if you need help.</li>');
68+
69+
$this->method_description = $register . (($env == 'live') ? __('Receive payments via Safaricom M-PESA', 'woocommerce') : __('<h4 style="color: red;">IMPORTANT!</h4>' . $b2c_settings . '<li>Please <a href="https://developer.safaricom.co.ke/" target="_blank" >create an app on Daraja</a> if you haven\'t. If yoou already have a production app, fill in the app\'s consumer key and secret below.</li><li>Ensure you have access to the <a href="https://org.ke.m-pesa.com/">MPesa Web Portal</a>. You\'ll need this for when you go LIVE.</li><li>For security purposes, and for the MPesa Instant Payment Notification to work, ensure your site is running over https(SSL).</li>' . $test_cred) . '<li>We have a <a target="_blank" href="https://wc-mpesa.osen.co.ke/going-live">nice tutorial</a> here on migrating from Sandbox(test) environment, to Production(live) environment.<br> We offer the service at a fiat fee of KSh 4000. Call <a href="tel:+254204404993">+254204404993</a> or email <a href="mailto:[email protected]">[email protected]</a> if you need help.</li>');
7070
$this->has_fields = false;
7171

7272
// Get settings
@@ -75,7 +75,7 @@ public function __construct()
7575
$this->instructions = $this->get_option('instructions');
7676
$this->enable_for_methods = $this->get_option('enable_for_methods', array());
7777
$this->enable_for_virtual = $this->get_option('enable_for_virtual', 'yes') === 'yes' ? true : false;
78-
$this->debug = $this->get_option('debug', 'no') === 'yes' ? true : false;
78+
$this->debug = $this->get_option('debug', 'no') === 'yes' ? true : false;
7979

8080
add_action('woocommerce_thankyou_' . $this->id, array($this, 'thankyou_page'));
8181

@@ -248,7 +248,7 @@ public function init_form_fields()
248248
'title' => __('Manual Payments', 'woocommerce'),
249249
'label' => __('Enable C2B API(Offline Payments)', 'woocommerce'),
250250
'type' => 'checkbox',
251-
'description' => '<small>' . (($this->get_option('idtype', 4) == 4) ? 'This requires C2B Validation, which is an optional feature that needs to be activated on M-Pesa. <br>Request for activation by sending an email to <a href="mailto:[email protected]">[email protected]</a>, or through a chat on the <a href="https://developer.safaricom.co.ke/">developer portal.</a><br>' : '') . '<a class="button button-secondary" href="' . home_url('lipwa/register/') . '">Once enabled, click here to register confirmation & validation URLs</a><p>Kindly note that if this is disabled, the user can still resend an STK push if the first one fails.</p></small>',
251+
'description' => '<small>This requires C2B Validation, which is an optional feature that needs to be activated on M-Pesa. <br>Request for activation by sending an email to <a href="mailto:[email protected]">[email protected]</a>, or through a chat on the <a href="https://developer.safaricom.co.ke/">developer portal.</a><br><br> <a class="button button-secondary" href="' . home_url('lipwa/register/') . '">Once enabled, click here to register confirmation & validation URLs</a><br><i>Kindly note that if this is disabled, the user can still resend an STK push if the first one fails.</i></small>',
252252
'default' => 'no',
253253
),
254254
// 'enable_b2c' => array(
@@ -260,10 +260,10 @@ public function init_form_fields()
260260
// ),
261261
'debug' => array(
262262
'title' => __('Debug Mode', 'woocommerce'),
263-
'label' => __('Check to enable debug mode and show request body', 'woocommerce').$this->get_option('debug'),
263+
'label' => __('Check to enable debug mode and show request body', 'woocommerce'),
264264
'type' => 'checkbox',
265265
'default' => 'no',
266-
'description' => '<small>' .__('Show Request Body(to send to Daraja on request)', 'woocommerce').'<small>'
266+
'description' => '<small>' . __('Show Request Body(to send to Daraja team on request)', 'woocommerce') . '<small>'
267267
)
268268
);
269269
}
@@ -335,17 +335,18 @@ public function process_payment($order_id)
335335
$total = $order->get_total();
336336
$phone = $order->get_billing_phone();
337337
$first_name = $order->get_billing_first_name();
338-
$last_name = $order->get_billing_last_name();
339-
340-
$c2b = get_option('woocommerce_mpesa_settings');
338+
$last_name = $order->get_billing_last_name();
339+
340+
$c2b = get_option('woocommerce_mpesa_settings');
341341

342342
if (($c2b['debug'] ?? 'no') == 'yes') {
343343
$result = STK::request($phone, $total, $order_id, get_bloginfo('name') . ' Purchase', 'WCMPesa', true);
344-
WC()->session->set('mpesa_request', json_encode($result['requested']));
344+
$message = json_encode($result['requested']);
345+
WC()->session->set('mpesa_request', $message);
345346
} else {
346347
$result = STK::request($phone, $total, $order_id, get_bloginfo('name') . ' Purchase', 'WCMPesa');
347-
}
348-
348+
}
349+
349350
if ($result) {
350351
$request_id = $result['MerchantRequestID'];
351352

inc/woocommerce.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function wcmpesa_new_order_column($columns)
1616

1717
/**
1818
* Render custom column content within edit.php table on event post types.
19-
*
19+
*
2020
* @access public
2121
* @param string $column The name of the column being acted upon
2222
* @return void
@@ -30,11 +30,11 @@ function shop_order_payments_table_column_content($column_id, $post_id)
3030
case 'mpesa':
3131
$statuses = array(
3232
"processing" => "This Order Is Processing",
33-
"on-hold" => "This Order Is On Hold",
34-
"complete" => "This Order Is Complete",
35-
"cancelled" => "This Order Is Cancelled",
36-
"refunded" => "This Order Is Refunded",
37-
"failed" => "This Order Failed"
33+
"on-hold" => "This Order Is On Hold",
34+
"complete" => "This Order Is Complete",
35+
"cancelled" => "This Order Is Cancelled",
36+
"refunded" => "This Order Is Refunded",
37+
"failed" => "This Order Failed",
3838
);
3939

4040
echo ($value = get_post_meta($post_id, '_order_status', true)) ? '<a href="' . admin_url('post.php?post=' . esc_attr(trim($order_id)) . '&action=edit">' . esc_attr($statuses[$value]) . '</a>') : '<a href="' . admin_url('post.php?post=' . esc_attr(trim($order_id)) . '&action=edit"') . '>Set Status</a>';
@@ -78,7 +78,7 @@ function woocommerce_emails_attach_downloadables($attachments, $status, $order)
7878
echo '
7979
<section class="woocommerce-order-details">
8080
<p>Mpesa request body</p>
81-
<code>'.WC()->session->get('mpesa_request').'</code>
81+
<code>' . WC()->session->get('mpesa_request') . '</code>
8282
</section>';
8383
}
84-
});
84+
});

0 commit comments

Comments
 (0)