Skip to content

Commit

Permalink
Reorder instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
maukoese committed May 27, 2020
1 parent a015a12 commit 182a003
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 43 deletions.
60 changes: 27 additions & 33 deletions inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,24 @@ function wc_mpesa_post_id_by_meta_key_and_value($key, $value)
add_action('init', 'wc_mpesa_rewrite_add_rewrites');
function wc_mpesa_rewrite_add_rewrites()
{
add_rewrite_rule('wcpesa/([^/]*)/?', 'index.php?wcpesa=$matches[1]', 'top');
add_rewrite_rule('lipwa/([^/]*)/?', 'index.php?lipwa=$matches[1]', 'top');
}

add_filter('query_vars', 'wc_mpesa_rewrite_add_var');
function wc_mpesa_rewrite_add_var($vars)
{
$vars[] = 'wcpesa';
$vars[] = 'lipwa';
return $vars;
}

add_action('template_redirect', 'wc_mpesa_process_ipn');
function wc_mpesa_process_ipn()
{
if (get_query_var('wcpesa')) {
if (get_query_var('lipwa')) {
header("Access-Control-Allow-Origin: *");
header("Content-Type: Application/json");

$action = get_query_var('wcpesa', 'something_ominous');
$action = get_query_var('lipwa', 'something_ominous');

switch ($action) {
case "validate":
Expand Down Expand Up @@ -327,7 +327,6 @@ function wc_mpesa_process_ipn()
$resultCode = $response['Body']['stkCallback']['ResultCode'];
$resultDesc = $response['Body']['stkCallback']['ResultDesc'];
$merchantRequestID = $response['Body']['stkCallback']['MerchantRequestID'];
$checkoutRequestID = $response['Body']['stkCallback']['CheckoutRequestID'];

$post = wc_mpesa_post_id_by_meta_key_and_value('_request_id', $merchantRequestID);
wp_update_post(['post_content' => file_get_contents('php://input'), 'ID' => $post]);
Expand All @@ -341,22 +340,16 @@ function wc_mpesa_process_ipn()
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$customer = "{$first_name} {$last_name}";
} else {
$customer = "MPesa Customer";
}

if (isset($response['Body']['stkCallback']['CallbackMetadata'])) {
$amount = $response['Body']['stkCallback']['CallbackMetadata']['Item'][0]['Value'];
$mpesaReceiptNumber = $response['Body']['stkCallback']['CallbackMetadata']['Item'][1]['Value'];
$balance = $response['Body']['stkCallback']['CallbackMetadata']['Item'][2]['Value'];
$transactionDate = $response['Body']['stkCallback']['CallbackMetadata']['Item'][3]['Value'];
$phone = $response['Body']['stkCallback']['CallbackMetadata']['Item'][4]['Value'];

$after_ipn_paid = round($before_ipn_paid) + round($amount);
$ipn_balance = $after_ipn_paid - $amount_due;
if (isset($response['Body']['stkCallback']['CallbackMetadata'])) {
$amount = $response['Body']['stkCallback']['CallbackMetadata']['Item'][0]['Value'];
$mpesaReceiptNumber = $response['Body']['stkCallback']['CallbackMetadata']['Item'][1]['Value'];
$balance = $response['Body']['stkCallback']['CallbackMetadata']['Item'][2]['Value'];
$transactionDate = $response['Body']['stkCallback']['CallbackMetadata']['Item'][3]['Value'];
$phone = $response['Body']['stkCallback']['CallbackMetadata']['Item'][4]['Value'];

if (wc_get_order($order_id)) {
$order = new WC_Order($order_id);
$after_ipn_paid = round($before_ipn_paid) + round($amount);
$ipn_balance = $after_ipn_paid - $amount_due;

if ($ipn_balance == 0) {
update_post_meta($post, '_order_status', 'complete');
Expand All @@ -375,24 +368,25 @@ function wc_mpesa_process_ipn()
$order->add_order_note(__("MPesa Payment from {$phone} Incomplete"));
update_post_meta($post, '_order_status', 'on-hold');
}
}

update_post_meta($post, '_paid', $after_ipn_paid);
update_post_meta($post, '_amount', $amount_due);
update_post_meta($post, '_balance', $ipn_balance);
update_post_meta($post, '_phone', $phone);
update_post_meta($post, '_customer', $customer);
update_post_meta($post, '_order_id', $order_id);
update_post_meta($post, '_receipt', $mpesaReceiptNumber);
} else {
if (wc_get_order($order_id)) {
$order = new WC_Order($order_id);
$order->update_status('on-hold');
update_post_meta($post, '_paid', $after_ipn_paid);
update_post_meta($post, '_amount', $amount_due);
update_post_meta($post, '_balance', $ipn_balance);
update_post_meta($post, '_phone', $phone);
update_post_meta($post, '_customer', $customer);
update_post_meta($post, '_order_id', $order_id);
update_post_meta($post, '_receipt', $mpesaReceiptNumber);
} else {
$order->update_status('cancelled');
$order->add_order_note(__("MPesa Error {$resultCode}: {$resultDesc}"));
}
}

exit(wp_send_json(Osen\Mpesa\STK::reconcile()));
exit(wp_send_json(Osen\Mpesa\STK::reconcile()));
} else {
exit(wp_send_json(Osen\Mpesa\STK::reconcile(function () {
return false;
})));
}
break;

case "status":
Expand Down
4 changes: 2 additions & 2 deletions inc/wc_mpesa_gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ public function init_form_fields()
),
'enable_c2b' => array(
'title' => __('Manual Payments', 'woocommerce'),
'label' => __('Enable C2B API', 'woocommerce'),
'label' => __('Enable C2B API(Offline Payments)', 'woocommerce'),
'type' => 'checkbox',
'description' => '<small>' . (($this->get_option('idtype') == 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>Once enabled, ' : '') . '<a class="button button-secondary" href="' . home_url('wcpesa/register/') . '">Click here to register confirmation & validation URLs</a></small>',
'description' => '<small>' . (($this->get_option('idtype') == 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('wcpesa/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>',
'default' => 'no',
),
// 'enable_b2c' => array(
Expand Down
11 changes: 7 additions & 4 deletions osen-wc-mpesa.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
/**
* @package Mpesa For WooCommerce
* @author Osen Concepts < [email protected] >
* @version 1.20.56
* @version 1.20.57
*
* Plugin Name: MPesa For WooCommerce
* Plugin URI: https://wc-mpesa.osen.co.ke/
* Description: This plugin extends WordPress and WooCommerce functionality to integrate <cite>Mpesa</cite> for making and receiving online payments.
* Author: Osen Concepts Kenya < [email protected] >
* Version: 1.20.56
* Version: 1.20.57
* Author URI: https://osen.co.ke/
*
* Requires at least: 4.6
* Tested up to: 5.4
*
* WC requires at least: 3.5.0
* WC tested up to: 4.0
* WC tested up to: 4.1
*
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
Expand Down Expand Up @@ -140,8 +140,8 @@ function mpesa_row_meta($links, $file)
/**
* Initialize all our custom post types
*/
Osen\Post\Types\C2B::init();
//Osen\Post\Types\B2C::init();
Osen\Post\Types\C2B::init();

/**
* Initialize our admin menus
Expand Down Expand Up @@ -221,6 +221,9 @@ function mpesa_row_meta($links, $file)
require_once $filename;
}

/**
* Auto-updates
*/
require __DIR__ . '/updates/plugin-update-checker.php';
$myUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
'https://raw.githubusercontent.com/osenco/osen-wc-mpesa/master/updates.json',
Expand Down
8 changes: 4 additions & 4 deletions updates.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "Mpesa for Woocommerce",
"version": "1.20.56",
"download_url": "https://github.com/osenco/osen-wc-mpesa/archive/v1.20.56.zip",
"version": "1.20.57",
"download_url": "https://github.com/osenco/osen-wc-mpesa/archive/v1.20.57.zip",

"homepage": "https://wc-mpesa.osen.co.ke/",
"requires": "4.5",
"tested": "5.4",
"last_updated": "2020-05-24 16:17:00",
"last_updated": "2020-05-27 14:17:00",
"upgrade_notice": "Autoupdates from here on",

"author": "Osen Concepts",
Expand All @@ -15,7 +15,7 @@
"sections": {
"description": "<p>This plugin seeks to provide a simple plug-n-play implementation for integrating M-Pesa Payments into online stores built with WooCommerce and WordPress.</p>",
"installation": "Download the latest release from Github, and extract the zip file in your plugins directory of your WordPress instalation",
"changelog": "<ul><li>Auto updates enabled</li></ul>",
"changelog": "<ul><li>Auto updates enabled</li><li>Reorder account number and paybill in C2B instructions</li><li>Mark order as cancelled if STK push times out or is cancelled</li></ul>",
"going_live": "<p>While we have made all efforts to ensure this plugin works out of the box - with minimum configuration required - the service provider requires that the user go through a certain ardous process to migrate from sandbox(test) environment to production.</p><p>We have made a <a href='https://wc-mpesa.osen.co.ke/going-live'>tutorial here</a> to walk you through the process. We however have a team ready on call to assist you in this are, at a fiat fee of KSh 4000 one-off, should you find it difficult.</p>"
},

Expand Down

0 comments on commit 182a003

Please sign in to comment.