Skip to content

Commit

Permalink
Instructions update for Going live.
Browse files Browse the repository at this point in the history
  • Loading branch information
maukoese authored Jul 13, 2018
1 parent da3bc0b commit 71f8396
Showing 1 changed file with 61 additions and 7 deletions.
68 changes: 61 additions & 7 deletions osen-wc-mpesa.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ 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_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 );
$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>';
$this->has_fields = false;

// Load settings
Expand Down Expand Up @@ -396,6 +396,16 @@ public function init_form_fields()
'description' => __( 'ID of WordPress user to assign authorship of payments generated by this plugin', 'woocommerce' ),
'default' => __( '1', 'woocommerce' ),
'desc_tip' => true,
),
'completion' => array(
'title' => __( 'Order Status on Payment', 'woocommerce' ),
'type' => 'select',
'options' => array(
'complete' => __( 'Mark order as complete', 'woocommerce' ),
'processing' => __( 'Mark order as processing', 'woocommerce' )
),
'description' => __( 'What status to set the order after Mpesa payment has been received', 'woocommerce' ),
'desc_tip' => true,
)
);
}
Expand Down Expand Up @@ -591,15 +601,15 @@ public function process_payment( $order_id )
if( ! $content ){
$error_message = 'Could not connect to MPesa to process payment. Please try again';
$order->update_status( 'failed', __( 'Could not connect to MPesa to process payment.', 'woocommerce' ) );
wc_add_notice( __( 'Failed! ', 'woothemes' ) . $error_message, 'error' );
wc_add_notice( __( 'Failed! ', 'woocommerce' ) . $error_message, 'error' );
return array(
'result' => 'fail',
'redirect' => ''
);
} elseif ( isset( $result->errorCode ) ) {
$error_message = 'MPesa Error '.$result->errorCode.': '.$result->errorMessage;
$order->update_status( 'failed', __( $error_message, 'woocommerce' ) );
wc_add_notice( __( 'Failed! ', 'woothemes' ) . $error_message, 'error' );
wc_add_notice( __( 'Failed! ', 'woocommerce' ) . $error_message, 'error' );
return array(
'result' => 'fail',
'redirect' => ''
Expand Down Expand Up @@ -722,6 +732,8 @@ function wc_mpesa_confirm()
}
header( "Access-Control-Allow-Origin: *" );
header( 'Content-Type:Application/json' );

$mpesa = new WC_Gateway_MPESA();
wp_send_json( $mpesa->proceed() );
}

Expand All @@ -741,6 +753,8 @@ function wc_mpesa_validate()
}
header( "Access-Control-Allow-Origin: *" );
header( 'Content-Type:Application/json' );

$mpesa = new WC_Gateway_MPESA();
wp_send_json( $mpesa->proceed() );
}

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

$after_ipn_paid = $before_ipn_paid+$amount;
$after_ipn_paid = round($before_ipn_paid)+round($amount);
$ipn_balance = $after_ipn_paid-$amount_due;

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

if ( $ipn_balance == 0 ) {
$order->payment_complete();
//$order->update_status( 'complete' );
$mpesa = new WC_Gateway_MPESA();
$order->update_status( $mpesa->get_option('completion') );
$order->add_order_note( __( "Full MPesa Payment Received From {$phone}. Receipt Number {$mpesaReceiptNumber}" ) );
update_post_meta( $post, '_order_status', 'complete' );
} elseif ( $ipn_balance < 0 ) {
Expand Down Expand Up @@ -859,8 +873,48 @@ function wc_mpesa_timeout()
}
}

add_filter( 'manage_edit-shop_order_columns', 'wcmpesa_new_order_column' );
function wcmpesa_new_order_column( $columns ) {
$columns['mpesa'] = 'Reinitiate Mpesa';
return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'wcmpesa_new_order_column' );

add_filter( 'woocommerce_email_attachments', 'woocommerce_emails_attach_downloadables', 10, 3);
function woocommerce_emails_attach_downloadables($attachments, $status, $order) {
if ( ! is_object( $order ) || ! isset( $status ) ) {
return $attachments;
}
if ( empty( $order ) ) {
return $attachments;
}
if ( ! $order->has_downloadable_item() ) {
return $attachments;
}
$allowed_statuses = array( 'customer_invoice', 'customer_completed_order' );
if ( isset( $status ) && in_array( $status, $allowed_statuses ) ) {
foreach ( $order->get_items() as $item_id => $item ) {
foreach ( $order->get_item_downloads( $item ) as $download ) {
$attachments[] = str_replace( content_url(), WP_CONTENT_DIR, $download['file'] );
}
}
}
return $attachments;
}

add_action('woocommerce_email_order_details', 'uiwc_email_order_details_products', 1, 4);
function uiwc_email_order_details_products($order, $admin, $plain, $email) {
$post = get_post_id_by_meta_key_and_value( '_order_id', $merchantRequestID );
$receipt = get_post_meta( $post, '_receipt', true );
echo __( '<strong>MPESA RECEIPT NUMBER:</strong> '.$receipt, 'uiwc' );
}

add_action( 'woocommerce_before_email_order', 'add_order_instruction_email', 10, 2 );
function add_order_instruction_email( $order, $sent_to_admin ) {

if ( ! $sent_to_admin ) {

if ( 'mpesa' == $order->payment_method ) {
echo wpautop( wptexturize( $instructions ) ) . PHP_EOL;
}
}
}

0 comments on commit 71f8396

Please sign in to comment.