Plugin Directory

Changeset 2783812


Ignore:
Timestamp:
09/13/2022 08:14:48 AM (4 years ago)
Author:
app360my
Message:

v1.3.4 create transaction on app360 whenever order changed to 'completed'

Location:
app360/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • app360/trunk/app360.php

    r2773243 r2783812  
    1111 * Plugin URI: https://www.app360.my/
    1212 * Description: App360 CRM allows the integration between WooCommerce and App360
    13  * Version: 1.3.2
     13 * Version: 1.3.4
    1414 * Author: App360
    1515 * License: GPLv2 or later
  • app360/trunk/readme.txt

    r2773243 r2783812  
    44Requires at least: 5.6
    55Tested up to: 5.8
    6 Stable tag: 1.3.2
     6Stable tag: 1.3.4
    77License: GPLv2 or later
    88
     
    3838
    3939== Changelog ==
     40
     41= 1.3.4 =
     42*Release Date - 13 September 2022*
     43
     44* feature  | create transaction on app360 whenever order changed to 'completed'
     45
     46= 1.3.3 =
     47*Release Date - 09 September 2022*
     48
     49* bugifx  | fix bug change order status 'processing' to 'completed'
    4050
    4151= 1.3.2 =
  • app360/trunk/transaction.php

    r2773243 r2783812  
    262262                $url = $app360_api_domain.'/client/spend?';
    263263                $url .= 'amount='.wc_format_decimal($order->get_total(), 2);
     264                $url .= '&source_type=e';
    264265                $url .= '&staff_id=0';
    265266                $url .= '&outlet_id=1';
     
    346347add_action( 'woocommerce_order_status_changed', 'app360_order_status_changed', 99, 3 );
    347348function app360_order_status_changed( $order_id, $old_status, $new_status ){
    348     $order = wc_get_order( $order_id );
    349     if( ($new_status == "completed") && !get_post_meta($order_id, 'app360_updated', true) && $order->get_payment_method() != 'app360' ) {
    350         if($order){
    351             $order_data = $order->get_data();
    352             $user_id = $order->get_customer_id();
    353             $app360_user_id = get_user_meta( $user_id, 'app360_userid', true );
    354 
    355             if($app360_user_id){
    356                 $app360_api_domain = get_option('app360_api_domain');
    357                 $app360_api = get_option('app360_api');
    358                 if( $app360_api_domain && $app360_api ){
    359                     $order_details = 'Order %23'.$order->id.' details:%0a';
    360                     foreach ( $order->get_items() as $item_id => $item ) {
    361                         $name = str_replace('&', 'and', $item->get_name());
    362                         $quantity = $item->get_quantity();
    363                         $order_details .= $name.' x'.$quantity.'%0a';
    364                     }
    365                     $order_details .= '%0aTotal($): '.$order->order_total;
    366                     $url = $app360_api_domain.'/client/spend?';
    367                     $url .= 'amount='.wc_format_decimal($order->get_total(), 2);
    368                     $url .= '&staff_id=0';
    369                     $url .= '&outlet_id=1';
    370                     $url .= '&user_id='.$app360_user_id;
    371                     $url .= '&reference='.$order_data['id'];
    372                     $url .= '&additional_text='.$order_details;
    373                     if($order->get_payment_method() != 'app360'){
    374                         $url .= '&mock_spend=1';
    375                     }
    376                     $headers = array();
    377                     $headers['Content-type'] = 'application/json';
    378                     $headers['apikey'] = $app360_api;
    379                     $response = wp_remote_post($url, ['headers'=> $headers]);
    380                     $result = is_array($response) && isset($response['body']) ? json_decode($response['body']) : null;
    381                     if($result){
    382                         if(isset($result->code)){
    383                             if($result->code == 'TR9'){
    384                                 if($order->get_payment_method() != 'app360'){
    385                                     $order->set_status($old_status);
    386                                     $order->save();
    387                                 }
    388                                 else{
    389                                     wp_delete_post($order_id,true);
    390                                 }
    391                                 throw new Exception( __( 'Insufficient balance.', 'woocommerce' ), 110 );
    392                             }
    393                             elseif($result->code != '000' && $result->code != 000){
    394                                 if($order->get_payment_method() != 'app360'){
    395                                     $order->set_status($old_status);
    396                                     $order->save();
    397                                 }
    398                                 else{
    399                                     wp_delete_post($order_id,true);
    400                                 }
    401                                 throw new Exception( __( 'Payment Gateway service down.', 'woocommerce' ), 110 );
    402                             }
    403                         }
    404                         $transaction = isset($result->transaction) ? $result->transaction : 0;
    405                         if(!$transaction){
    406                             if($order->get_payment_method() != 'app360'){
    407                                 $order->set_status($old_status);
    408                                 $order->save();
    409                             }
    410                             else{
    411                                 wp_delete_post($order_id,true);
    412                             }
    413                             throw new Exception( __( 'Payment Gateway service down.', 'woocommerce' ), 110 );
    414                         }
    415                         $app360_transaction_id = $transaction->id;
    416                         update_post_meta( $order_id, 'app360_transaction_id', $app360_transaction_id );
    417                         update_post_meta( $order_id, 'app360_updated', true );
    418                         $applied_coupons = $order->get_coupon_codes();
    419                         if($applied_coupons){
    420                             foreach($applied_coupons as $coupon){
    421                                 $args = array(
    422                                     's'  => $coupon,
    423                                     'post_type'   => 'shop_coupon'
    424                                 );
    425                                 $query = new WP_Query( $args );
    426                                 $coupon_posts = $query->posts;
    427 
    428                                 if($coupon_posts){
    429                                     $reward_id = get_post_meta($coupon_posts[0]->ID, 'voucher_id', true) ? get_post_meta($coupon_posts[0]->ID, 'voucher_id', true) : 0;
    430                                     if($reward_id && $reward_id != 0){
    431                                         $url = $app360_api_domain.'/client/voucher/use?';
    432                                         $url .= 'user_id='.$app360_user_id;
    433                                         $url .= '&reward_id='.$reward_id;
    434                                         $headers = array();
    435                                         $headers['Content-type'] = 'application/json';
    436                                         $headers['apikey'] = $app360_api;
    437                                         $response = wp_remote_get($url, ['headers'=> $headers]);
    438 
    439                                         $result = is_array($response) && isset($response['body']) ? json_decode($response['body']) : null;
    440                                     }
    441                                 }
    442                             }
    443                         }
    444                     }
    445                     else{
    446                         if($order->get_payment_method() != 'app360'){
    447                             $order->set_status($old_status);
    448                             $order->save();
    449                         }
    450                         else{
    451                             wp_delete_post($order_id,true);
    452                         }
    453                         throw new Exception( __( 'Payment Gateway service down.', 'woocommerce' ), 110 );
    454                     }
    455                 }
    456                 else{
    457                     if($order->get_payment_method() != 'app360'){
    458                         $order->set_status($old_status);
    459                         $order->save();
    460                     }
    461                     else{
    462                         wp_delete_post($order_id,true);
    463                     }
    464                     throw new Exception( __( 'API not working.', 'woocommerce' ), 110 );
    465                 }
    466             }
    467             elseif(!is_user_logged_in()){ // if user is not logged in
    468                 // do nothing
    469             }
    470             else{
    471                 if($order->get_payment_method() != 'app360'){
    472                     $order->set_status($old_status);
    473                     $order->save();
    474                 }
    475                 else{
    476                     wp_delete_post($order_id,true);
    477                 }
    478                 throw new Exception( __( 'User ID not found.', 'woocommerce' ), 110 );
    479             }
    480         }
    481     }
    482     if($new_status == 'processing'){
    483         $order = wc_get_order( $order_id );
    484         $order->set_status('completed');
    485         $order->save();
    486     }
     349    if (strtolower($new_status) === 'completed') {
     350        $order = wc_get_order( $order_id );
     351        $order_data = $order->get_data();
     352
     353        if ($order_data['payment_method'] === 'app360') {
     354            //skip app360
     355            return;
     356        }
     357
     358        $app360_transaction_id = $order->get_meta('app360_transaction_id');
     359        $app360_updated = $order->get_meta('app360_updated');
     360
     361        if ($app360_updated === true || $app360_transaction_id) {
     362            //processed
     363            return;
     364        }
     365
     366        $app360_api_domain = get_option('app360_api_domain');
     367        $app360_api = get_option('app360_api');
     368        if( $app360_api_domain && $app360_api ){
     369            $user_id = $order->get_customer_id();
     370            $app360_user_id = get_user_meta( $user_id, 'app360_userid', true );
     371
     372            $order_details = 'Order %23'.$order->id.' details:%0a';
     373            foreach ( $order->get_items() as $item_id => $item ) {
     374                $name = str_replace('&', 'and', $item->get_name());
     375                $quantity = $item->get_quantity();
     376                $order_details .= $name.' x'.$quantity.'%0a';
     377            }
     378            $order_details .= '%0aTotal($): '.$order->order_total;
     379            $url = $app360_api_domain.'/client/spend?';
     380            $url .= 'amount='.wc_format_decimal($order->get_total(), 2);
     381            $url .= '&source_type=e';
     382            $url .= '&staff_id=0';
     383            $url .= '&outlet_id=1';
     384            $url .= '&user_id='.$app360_user_id;
     385            $url .= '&reference='.$order_data['id'];
     386            $url .= '&additional_text='.$order_details;
     387            $url .= '&mock_spend=1';
     388            $headers = array();
     389            $headers['Content-type'] = 'application/json';
     390            $headers['apikey'] = $app360_api;
     391            $response = wp_remote_post($url, ['headers'=> $headers]);
     392            $result = is_array($response) && isset($response['body']) ? json_decode($response['body']) : null;
     393            if($result){
     394                if(isset($result->code)){
     395                    if($result->code != '000' && $result->code != 000){
     396                        if (isset($result->message)) {
     397                            throw new Exception( __( $result->message, 'woocommerce' ), 110 );
     398                        } else {
     399                            throw new Exception( __( 'Payment Gateway service down.', 'woocommerce' ), 110 );
     400                        }
     401                    }
     402                }
     403                $transaction = isset($result->transaction) ? $result->transaction : 0;
     404                if(!$transaction){
     405                    throw new Exception( __( 'Payment Gateway service down.', 'woocommerce' ), 110 );
     406                }
     407                $app360_transaction_id = $transaction->id;
     408                update_post_meta( $order_id, 'app360_transaction_id', $app360_transaction_id );
     409                update_post_meta( $order_id, 'app360_updated', true );
     410            }
     411        }
     412    }
     413//    if($new_status == 'processing'){
     414//        $order = wc_get_order( $order_id );
     415//        $order->set_status('completed');
     416//        $order->save();
     417//    }
    487418}
    488419
     
    512443                    $url = $app360_api_domain.'/client/spend?';
    513444                    $url .= 'amount='.wc_format_decimal($order->get_total(), 2);
     445                    $url .= '&source_type=e';
    514446                    $url .= '&staff_id=0';
    515447                    $url .= '&outlet_id=1';
Note: See TracChangeset for help on using the changeset viewer.