Skip to content

Commit

Permalink
Resolved SMS sending, and configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
maukoese committed Sep 9, 2019
1 parent 89e97f0 commit c62d89a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 28 deletions.
28 changes: 27 additions & 1 deletion inc/ipn.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ function at_process_ipn()

$headers = 'From: ' . get_bloginfo('name') . ' <' . get_bloginifo('admin_email') . '>' . "\r\n";
wp_mail($order["billing_address"], 'Your Mpesa payment', 'We acknowledge receipt of your payment via M-PESA of KSh. ' . $amount . ' on ' . $transactionDate . 'with receipt Number ' . $mpesaReceiptNumber . '.', $headers);

$total = round($order->get_total());
$reference = 'ORDER#' . $order_id;

$username = at_option('username');
$apiKey = at_option('key');
$AT = new AfricasTalking\SDK\AfricasTalking($username, $apiKey);
$sms = $AT->sms();

$recipients = $order->get_billing_phone();
$message = "We acknowledge receipt of your payment via M-PESA of KSh. {$amount} on {$transactionDate} for {$reference} with receipt number {$mpesaReceiptNumber}";
$from = at_option('shortcode');

try {
// Thats it, hit send and we'll take care of the rest
$result = $sms->send([
'to' => $recipients,
'message' => $message,
'from' => at_option('shortcode'),
]);
} catch (Exception $e) {
$result = [
'status' => 'error',
'data' => $e->getMessage(),
];
}
} elseif ($ipn_balance < 0) {
$currency = get_woocommerce_currency();
$order->payment_complete();
Expand All @@ -123,7 +149,7 @@ function at_process_ipn()
update_post_meta($post, '_order_id', $order_id);
update_post_meta($post, '_receipt', $mpesaReceiptNumber);

exit(wp_send_json(Osen\Mpesa\STK::confirm()));
exit(wp_send_json(['tests']));
break;

default:
Expand Down
9 changes: 4 additions & 5 deletions inc/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ function at_apis_render_page()
$sms = $AT->sms();
$recipients = strip_tags(trim($_POST['sms_phone']));
$message = strip_tags(trim($_POST['sms_message']));
$from = "AT2FA";

$phones = array();
if (strpos(',', $recipients) !== false) {
Expand All @@ -134,11 +133,11 @@ function at_apis_render_page()
$result = $sms->send([
'to' => $phones,
'message' => $message,
'from' => $from,
'from' => at_option('shortcode'),
]);

echo '<div class="notice notice-'.$result['status'].' is-dismissible">
<p>'.json_encode($result['data']).'.</p>
<p>'.$result['data']->SMSMessageData->Message.'.</p>
</div>';
} catch (Exception $e) {
echo '<div class="notice notice-error is-dismissible">
Expand All @@ -157,7 +156,7 @@ function at_apis_render_page()
</th>
<td>
<input class="regular-text" type="tel" id="at_shortcode" name="sms_phone" value="+254"/>
<br><small>Separate multiple phone numbers with comma</small>
<br><small>Separate multiple phone numbers with commas</small>
</td>
</tr>
<tr valign="top">
Expand Down Expand Up @@ -238,7 +237,7 @@ function at_apis_render_page()
</th>
<td>
<input class="regular-text" type="tel" id="at_shortcode" name="b2c_phone" value="+254"/>
<br><small>Separate multiple phone numbers with comma</small>
<br><small>Separate multiple phone numbers with commas</small>
</td>
</tr>
<tr valign="top">
Expand Down
2 changes: 1 addition & 1 deletion inc/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function at_settings_init() {

add_settings_field(
'shortcode',
__( 'AT Shortcode', 'africastalking_options' ),
__( 'SMS Sender ID', 'africastalking_options' ),
'at_fields_at_mpesa_shortcode_cb',
'africastalking_options',
'at_section_mpesa',
Expand Down
36 changes: 18 additions & 18 deletions inc/woocommerce.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
require_once plugin_dir_path(__DIR__) . 'vendor/autoload.php';

add_action('woocommerce_order_status_pending_to_processing_notification', 'iconic_processing_notification', 10, 1);
function iconic_processing_notification($order_id)
add_action('woocommerce_order_status_pending_to_processing_notificaton', 'at_processing_notificaton', 10, 1);
function at_processing_notificaton($order_id)
{

$order = wc_get_order($order_id);
Expand All @@ -18,15 +18,15 @@ function iconic_processing_notification($order_id)
$sms = $AT->sms();

$recipients = $order->get_billing_phone();
$message = "Hi {$first_name} {$last_name}, your order {$reference} of KSh {$total} has been received and being processed.";
$from = "myShortCode";
$message = "Hi {$first_name} {$last_name}, your order {$reference} of KSh {$total} has been received and is being processed.";
$from = at_option('shortcode');

try {
// Thats it, hit send and we'll take care of the rest
$result = $sms->send([
'to' => $recipients,
'message' => $message,
'from' => $from,
'from' => at_option('shortcode'),
]);
} catch (Exception $e) {
$result = [
Expand All @@ -38,8 +38,8 @@ function iconic_processing_notification($order_id)
return $result;
}

add_action('woocommerce_thankyou_africastalking', 'wc_ati_add_content_thankyou_africastalking');
function wc_ati_add_content_thankyou_africastalking($order_id)
add_action('woocommerce_thankyou_africastalking', 'wc_at_add_content_thankyou_africastalking');
function wc_at_add_content_thankyou_africastalking($order_id)
{
$order = wc_get_order($order_id);

Expand Down Expand Up @@ -73,16 +73,16 @@ function wc_ati_add_content_thankyou_africastalking($order_id)
}

.saving span {
animation: blink 1.4s linear infinite;
animation-fill-mode: both;
animaton: blink 1.4s linear infinite;
animaton-fill-mode: both;
}

.saving span:nth-child(2) {
animation-delay: .2s;
animaton-delay: .2s;
}

.saving span:nth-child(3) {
animation-delay: .4s;
animaton-delay: .4s;
}
</style>
<section class="woocommerce-order-details africastalking">
Expand All @@ -92,26 +92,26 @@ function wc_ati_add_content_thankyou_africastalking($order_id)
</section><?php
}

add_action('wp_footer', 'ati_ajax_polling');
function ati_ajax_polling()
add_action('wp_footer', 'at_ajax_polling');
function at_ajax_polling()
{?>
<script id="atipn_atichecker">
var atichecker = setInterval(() => {
<script id="atpn_atchecker">
var atchecker = setInterval(() => {
if (document.getElementById("payment_method") !== null && document.getElementById("payment_method").value !== 'africastalking') {
clearInterval(atichecker);
clearInterval(atchecker);
}

jQuery(function($) {
var order = $("#current_order").val();
if (order !== undefined || order !== '') {
$.get('<?php echo home_url('?atipncheck&order='); ?>' + order, [], function(data) {
$.get('<?php echo home_url('?atpncheck&order='); ?>' + order, [], function(data) {
if (data.receipt == '' || data.receipt == 'N/A') {
$("#africastalking_receipt").html('Confirming payment <span>.</span><span>.</span><span>.</span><span>.</span><span>.</span><span>.</span>');
} else {
$(".woocommerce-order-overview").append('<li class="woocommerce-order-overview__payment-method method">Receipt number: <strong>' + data.receipt + '</strong></li>');
$(".woocommerce-table--order-details > tfoot").find('tr:last-child').prev().after('<tr><th scope="row">Receipt number:</th><td>' + data.receipt +'</td></tr>');
$("#africastalking_receipt").html('Payment confirmed. Receipt number: <b>' + data.receipt + '</b>');
clearInterval(atichecker);
clearInterval(atchecker);
return false;
}
})
Expand Down
27 changes: 24 additions & 3 deletions osen-wc-africas-talking.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function __construct()
{
// global ID
$this->id = "africastalking";
$this->method_title = __("Africa's Talking C2B", 'woocommerce');
$this->method_title = __("Africa's Talking", 'woocommerce');
$this->method_description = ($this->get_option('enabled') == 'yes')
? 'Receive payments through Africa\'s Talking.'
: __('<p>Log into your <a href="https://account.africastalking.com" target="_blank">Africas Talking Account</a> and copy your API key here.:</p>
Expand Down Expand Up @@ -196,9 +196,9 @@ public function init_form_fields()
'default' => __('Africa\'s Talking C2B', 'woocommerce'),
),
'shortcode' => array(
'title' => __('AT Shortcode', 'woocommerce'),
'title' => __('SMS Sender ID', 'woocommerce'),
'type' => 'text',
'desc_tip' => __('This is the Till number provided by Africas Talking when you signed up for an account.', 'woocommerce'),
'desc_tip' => __('This is your Africa\'s Talking Sender ID.', 'woocommerce'),
'default' => 'AT2FA',
),
'username' => array(
Expand Down Expand Up @@ -328,6 +328,27 @@ public function process_payment($order_id)

$this->instructions .= '<p>Awaiting Africa\'s Talking confirmation of payment from ' . $phone . ' for request ' . $request_id . '. Check your phone for the STK Prompt.</p>';

$order = wc_get_order($order_id);

$sms = $AT->sms();

$message = "Hi {$first_name} {$last_name}, your order {$reference} of KSh {$total} has been received and being processed.";
$from = at_option('shortcode');

try {
// Thats it, hit send and we'll take care of the rest
$result = $sms->send([
'to' => $phone,
'message' => $message,
'from' => at_option('shortcode'),
]);
} catch (Exception $e) {
$result = [
'status' => 'error',
'data' => $e->getMessage(),
];
}

// Return thankyou redirect
return array(
'result' => 'success',
Expand Down

0 comments on commit c62d89a

Please sign in to comment.