Skip to content

Commit

Permalink
september release
Browse files Browse the repository at this point in the history
  • Loading branch information
maukoese committed Sep 8, 2020
1 parent b053873 commit dc9d7cd
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 349 deletions.
49 changes: 28 additions & 21 deletions inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
* @since 0.18.01
*/

use Osen\Woocommerce\Mpesa\STK;
use Osen\Woocommerce\Mpesa\C2B;

function wc_mpesa_post_id_by_meta_key_and_value($key, $value)
{
global $wpdb;
Expand All @@ -33,7 +36,7 @@ function wc_mpesa_process_order($order)

add_action('woocommerce_thankyou_mpesa', function ($order_id) {
$mpesa = get_option('woocommerce_mpesa_settings');
$idtype = Osen\Woocommerce\Mpesa\C2B::$type;
$idtype = C2B::$type;
$url = home_url('?pesaipn&order=');

if (wc_get_order($order_id)) {
Expand Down Expand Up @@ -88,6 +91,18 @@ function wc_mpesa_process_order($order)
<input type="hidden" id="current_order" value="<?php echo $order_id; ?>">
<input type="hidden" id="payment_method" value="<?php echo $order->get_payment_method(); ?>">
<p class="saving" id="mpesa_receipt">Confirming receipt, please wait</p>
<table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
<tbody>
<tr class="woocommerce-table__line-item order_item">
<td class="woocommerce-table__product-name product-name">
<form action="<?php echo home_url("lipwa/request"); ?>" method="POST" id="renitiate-form">
<input type="hidden" name="order" value="<?php echo $order_id; ?>">
<button id="renitiate-button" class="button alt" type="submit"><?php echo $mpesa['resend'] ?? 'Resend STK Push'; ?></button>
</form>
</td>
</tr>
</tbody>
</table>
</section>

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

</td>
</tr>
<tr class="woocommerce-table__line-item order_item">
<td class="woocommerce-table__product-name product-name">
<form action="<?php echo home_url("lipwa/request"); ?>" mhod="POST" id="renitiate-form">
<input type="hidden" name="order" value="<?php echo $order_id; ?>">
<button id="renitiate-button" class="button alt" type="submit"><?php echo $mpesa['resend'] ?? 'Resend STK Push'; ?></button>
</form>
</td>
</tr>
</tbody>
</table>
</section>
Expand Down Expand Up @@ -234,13 +241,13 @@ function wc_mpesa_process_ipn()
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();

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

wp_send_json($result);
break;
case "validate":
exit(wp_send_json(
Osen\Woocommerce\Mpesa\STK::validate()
STK::validate()
));
break;

Expand Down Expand Up @@ -309,7 +316,7 @@ function wc_mpesa_process_ipn()
update_post_meta($post, '_order_status', 'complete');

$headers = 'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>' . "\r\n";
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);
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);
} elseif ($ipn_balance < 0) {
$currency = get_woocommerce_currency();
$order->update_status((isset($mpesa['completion']) ? $mpesa['completion'] : 'completed'), __("{$phone} has overpayed by {$currency} {$ipn_balance}. Receipt Number {$mpesaReceiptNumber}"));
Expand All @@ -329,11 +336,11 @@ function wc_mpesa_process_ipn()
update_post_meta($post, '_order_id', $order_id);
update_post_meta($post, '_receipt', $mpesaReceiptNumber);

exit(wp_send_json(Osen\Woocommerce\Mpesa\STK::confirm()));
exit(wp_send_json(STK::confirm()));
break;

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

$headers[] = 'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>' . "\r\n";
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);
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);
} elseif ($ipn_balance < 0) {
$currency = get_woocommerce_currency();
$order->update_status((isset($mpesa['completion']) ? $mpesa['completion'] : 'completed'), __("{$phone} has overpayed by {$currency} {$ipn_balance}. Receipt Number {$mpesaReceiptNumber}"));
Expand All @@ -420,17 +427,17 @@ function wc_mpesa_process_ipn()
$order->add_order_note(__("MPesa Error {$resultCode}: {$resultDesc}"));
}

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

case "status":
$transaction = $_POST['transaction'];
exit(wp_send_json(Osen\Woocommerce\Mpesa\STK::status($transaction)));
exit(wp_send_json(STK::status($transaction)));
break;

case "result":
Expand Down Expand Up @@ -466,7 +473,7 @@ function wc_mpesa_process_ipn()
$ReferenceData = $result['ReferenceData'];
$ReferenceItem = $ReferenceData['ReferenceItem'];
$Occasion = $ReferenceItem[0]['Value'];
exit(wp_send_json(Osen\Woocommerce\Mpesa\STK::validate()));
exit(wp_send_json(STK::validate()));
break;

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

exit(wp_send_json(Osen\Woocommerce\Mpesa\STK::timeout()));
exit(wp_send_json(STK::timeout()));
break;
default:
exit(wp_send_json(Osen\Woocommerce\Mpesa\C2B::register()));
exit(wp_send_json(C2B::register()));
}
}

Expand Down
45 changes: 23 additions & 22 deletions inc/wc_mpesa_gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* @package MPesa For WooCommerce
* @subpackage WooCommerce Functions
* @subpackage WooCommerce Mpesa Gateway
* @author Osen Concepts < [email protected] >
* @since 0.18.01
*/
Expand Down Expand Up @@ -47,26 +47,26 @@ class WC_MPESA_Gateway extends WC_Payment_Gateway
* Constructor for the gateway.
*/
public function __construct()
{
{
$this->id = 'mpesa';
$this->icon = apply_filters('woocommerce_mpesa_icon', plugins_url('mpesa.png', __FILE__));
$this->method_title = __('Lipa Na MPesa', 'woocommerce');
$this->method_title = __('Lipa Na MPesa', 'woocommerce');

// Load settings
$this->init_form_fields();
$this->init_settings();
$this->init_settings();

$env = $this->get_option('env', 'sandbox');
$b2c_settings = ($this->get_option('enable_b2c', 'no') == 'yes')
? '<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>'
: '';
? '<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>'
: '';
$test_cred = ($env == 'sandbox')
? '<li>You can <a href="https://developer.safaricom.co.ke/test_credentials" target="_blank" >get sandbox test credentials here</a>.</li>'
: '';
? '<li>You can <a href="https://developer.safaricom.co.ke/test_credentials" target="_blank" >get sandbox test credentials here</a>.</li>'
: '';
$color = isset($_GET['reg-state']) ? $_GET['reg-state'] : 'black';
$register = isset($_GET['mpesa-urls-registered']) ? "<div style='color: {$color}'>{$_GET['mpesa-urls-registered']}</div>" : '';
$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>');

$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>');
$this->has_fields = false;

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

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

Expand Down Expand Up @@ -248,7 +248,7 @@ public function init_form_fields()
'title' => __('Manual Payments', 'woocommerce'),
'label' => __('Enable C2B API(Offline Payments)', 'woocommerce'),
'type' => 'checkbox',
'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>',
'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>',
'default' => 'no',
),
// 'enable_b2c' => array(
Expand All @@ -260,10 +260,10 @@ public function init_form_fields()
// ),
'debug' => array(
'title' => __('Debug Mode', 'woocommerce'),
'label' => __('Check to enable debug mode and show request body', 'woocommerce').$this->get_option('debug'),
'label' => __('Check to enable debug mode and show request body', 'woocommerce'),
'type' => 'checkbox',
'default' => 'no',
'description' => '<small>' .__('Show Request Body(to send to Daraja on request)', 'woocommerce').'<small>'
'description' => '<small>' . __('Show Request Body(to send to Daraja team on request)', 'woocommerce') . '<small>'
)
);
}
Expand Down Expand Up @@ -335,17 +335,18 @@ public function process_payment($order_id)
$total = $order->get_total();
$phone = $order->get_billing_phone();
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$c2b = get_option('woocommerce_mpesa_settings');
$last_name = $order->get_billing_last_name();

$c2b = get_option('woocommerce_mpesa_settings');

if (($c2b['debug'] ?? 'no') == 'yes') {
$result = STK::request($phone, $total, $order_id, get_bloginfo('name') . ' Purchase', 'WCMPesa', true);
WC()->session->set('mpesa_request', json_encode($result['requested']));
$message = json_encode($result['requested']);
WC()->session->set('mpesa_request', $message);
} else {
$result = STK::request($phone, $total, $order_id, get_bloginfo('name') . ' Purchase', 'WCMPesa');
}
}

if ($result) {
$request_id = $result['MerchantRequestID'];

Expand Down
16 changes: 8 additions & 8 deletions inc/woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function wcmpesa_new_order_column($columns)

/**
* Render custom column content within edit.php table on event post types.
*
*
* @access public
* @param string $column The name of the column being acted upon
* @return void
Expand All @@ -30,11 +30,11 @@ function shop_order_payments_table_column_content($column_id, $post_id)
case 'mpesa':
$statuses = array(
"processing" => "This Order Is Processing",
"on-hold" => "This Order Is On Hold",
"complete" => "This Order Is Complete",
"cancelled" => "This Order Is Cancelled",
"refunded" => "This Order Is Refunded",
"failed" => "This Order Failed"
"on-hold" => "This Order Is On Hold",
"complete" => "This Order Is Complete",
"cancelled" => "This Order Is Cancelled",
"refunded" => "This Order Is Refunded",
"failed" => "This Order Failed",
);

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>';
Expand Down Expand Up @@ -78,7 +78,7 @@ function woocommerce_emails_attach_downloadables($attachments, $status, $order)
echo '
<section class="woocommerce-order-details">
<p>Mpesa request body</p>
<code>'.WC()->session->get('mpesa_request').'</code>
<code>' . WC()->session->get('mpesa_request') . '</code>
</section>';
}
});
});
Loading

0 comments on commit dc9d7cd

Please sign in to comment.