Skip to content

Commit 71f8396

Browse files
authored
Instructions update for Going live.
1 parent da3bc0b commit 71f8396

File tree

1 file changed

+61
-7
lines changed

1 file changed

+61
-7
lines changed

osen-wc-mpesa.php

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function __construct()
204204
$this->id = 'mpesa';
205205
$this->icon = apply_filters( 'woocommerce_mpesa_icon', plugins_url( 'mpesa.png', __FILE__ ) );
206206
$this->method_title = __( 'Lipa Na MPesa', 'woocommerce' );
207-
$this->method_description = __( '<h4 style="color: red;">IMPORTANT!</h4><li>Please <a href="https://developer.safaricom.co.ke/" target="_blank" >create an app on Daraja</a> if you haven\'t. Fill in the app\'s consumer key and secret below.</li><li>For security purposes, and for the MPesa Instant Payment Notification to work, ensure your site is running over https(SSL).</li>'.$reg_notice.$test_cred );
207+
$this->method_description = __( '<h4 style="color: red;">IMPORTANT!</h4><li>Please <a href="https://developer.safaricom.co.ke/" target="_blank" >create an app on Daraja</a> if you haven\'t. Fill in the app\'s consumer key and secret below.</li><li>For security purposes, and for the MPesa Instant Payment Notification to work, ensure your site is running over https(SSL).</li>'.$reg_notice.$test_cred ).'<li>Peter Njeru has a <a href="https://peternjeru.co.ke/safdaraja/ui/#go_live_tutorial">nice tutorial</a> here on migrating from Sandbox(test) environment, to Production(live) environment. We offer the service at a fiat fee of KSh 4000. Call <a href="tel:+254705459494">+254705459494</a> or email <a href="mailto:[email protected]">[email protected]</a> if you need help.</li>';
208208
$this->has_fields = false;
209209

210210
// Load settings
@@ -396,6 +396,16 @@ public function init_form_fields()
396396
'description' => __( 'ID of WordPress user to assign authorship of payments generated by this plugin', 'woocommerce' ),
397397
'default' => __( '1', 'woocommerce' ),
398398
'desc_tip' => true,
399+
),
400+
'completion' => array(
401+
'title' => __( 'Order Status on Payment', 'woocommerce' ),
402+
'type' => 'select',
403+
'options' => array(
404+
'complete' => __( 'Mark order as complete', 'woocommerce' ),
405+
'processing' => __( 'Mark order as processing', 'woocommerce' )
406+
),
407+
'description' => __( 'What status to set the order after Mpesa payment has been received', 'woocommerce' ),
408+
'desc_tip' => true,
399409
)
400410
);
401411
}
@@ -591,15 +601,15 @@ public function process_payment( $order_id )
591601
if( ! $content ){
592602
$error_message = 'Could not connect to MPesa to process payment. Please try again';
593603
$order->update_status( 'failed', __( 'Could not connect to MPesa to process payment.', 'woocommerce' ) );
594-
wc_add_notice( __( 'Failed! ', 'woothemes' ) . $error_message, 'error' );
604+
wc_add_notice( __( 'Failed! ', 'woocommerce' ) . $error_message, 'error' );
595605
return array(
596606
'result' => 'fail',
597607
'redirect' => ''
598608
);
599609
} elseif ( isset( $result->errorCode ) ) {
600610
$error_message = 'MPesa Error '.$result->errorCode.': '.$result->errorMessage;
601611
$order->update_status( 'failed', __( $error_message, 'woocommerce' ) );
602-
wc_add_notice( __( 'Failed! ', 'woothemes' ) . $error_message, 'error' );
612+
wc_add_notice( __( 'Failed! ', 'woocommerce' ) . $error_message, 'error' );
603613
return array(
604614
'result' => 'fail',
605615
'redirect' => ''
@@ -722,6 +732,8 @@ function wc_mpesa_confirm()
722732
}
723733
header( "Access-Control-Allow-Origin: *" );
724734
header( 'Content-Type:Application/json' );
735+
736+
$mpesa = new WC_Gateway_MPESA();
725737
wp_send_json( $mpesa->proceed() );
726738
}
727739

@@ -741,6 +753,8 @@ function wc_mpesa_validate()
741753
}
742754
header( "Access-Control-Allow-Origin: *" );
743755
header( 'Content-Type:Application/json' );
756+
757+
$mpesa = new WC_Gateway_MPESA();
744758
wp_send_json( $mpesa->proceed() );
745759
}
746760

@@ -787,15 +801,15 @@ function wc_mpesa_reconcile()
787801
$transactionDate = $response['Body']['stkCallback']['CallbackMetadata']['Item'][3]['Value'];
788802
$phone = $response['Body']['stkCallback']['CallbackMetadata']['Item'][4]['Value'];
789803

790-
$after_ipn_paid = $before_ipn_paid+$amount;
804+
$after_ipn_paid = round($before_ipn_paid)+round($amount);
791805
$ipn_balance = $after_ipn_paid-$amount_due;
792806

793807
if( wc_get_order( $order_id ) ){
794808
$order = new WC_Order( $order_id );
795809

796810
if ( $ipn_balance == 0 ) {
797-
$order->payment_complete();
798-
//$order->update_status( 'complete' );
811+
$mpesa = new WC_Gateway_MPESA();
812+
$order->update_status( $mpesa->get_option('completion') );
799813
$order->add_order_note( __( "Full MPesa Payment Received From {$phone}. Receipt Number {$mpesaReceiptNumber}" ) );
800814
update_post_meta( $post, '_order_status', 'complete' );
801815
} elseif ( $ipn_balance < 0 ) {
@@ -859,8 +873,48 @@ function wc_mpesa_timeout()
859873
}
860874
}
861875

876+
add_filter( 'manage_edit-shop_order_columns', 'wcmpesa_new_order_column' );
862877
function wcmpesa_new_order_column( $columns ) {
863878
$columns['mpesa'] = 'Reinitiate Mpesa';
864879
return $columns;
865880
}
866-
add_filter( 'manage_edit-shop_order_columns', 'wcmpesa_new_order_column' );
881+
882+
add_filter( 'woocommerce_email_attachments', 'woocommerce_emails_attach_downloadables', 10, 3);
883+
function woocommerce_emails_attach_downloadables($attachments, $status, $order) {
884+
if ( ! is_object( $order ) || ! isset( $status ) ) {
885+
return $attachments;
886+
}
887+
if ( empty( $order ) ) {
888+
return $attachments;
889+
}
890+
if ( ! $order->has_downloadable_item() ) {
891+
return $attachments;
892+
}
893+
$allowed_statuses = array( 'customer_invoice', 'customer_completed_order' );
894+
if ( isset( $status ) && in_array( $status, $allowed_statuses ) ) {
895+
foreach ( $order->get_items() as $item_id => $item ) {
896+
foreach ( $order->get_item_downloads( $item ) as $download ) {
897+
$attachments[] = str_replace( content_url(), WP_CONTENT_DIR, $download['file'] );
898+
}
899+
}
900+
}
901+
return $attachments;
902+
}
903+
904+
add_action('woocommerce_email_order_details', 'uiwc_email_order_details_products', 1, 4);
905+
function uiwc_email_order_details_products($order, $admin, $plain, $email) {
906+
$post = get_post_id_by_meta_key_and_value( '_order_id', $merchantRequestID );
907+
$receipt = get_post_meta( $post, '_receipt', true );
908+
echo __( '<strong>MPESA RECEIPT NUMBER:</strong> '.$receipt, 'uiwc' );
909+
}
910+
911+
add_action( 'woocommerce_before_email_order', 'add_order_instruction_email', 10, 2 );
912+
function add_order_instruction_email( $order, $sent_to_admin ) {
913+
914+
if ( ! $sent_to_admin ) {
915+
916+
if ( 'mpesa' == $order->payment_method ) {
917+
echo wpautop( wptexturize( $instructions ) ) . PHP_EOL;
918+
}
919+
}
920+
}

0 commit comments

Comments
 (0)