Plugin Directory

Changeset 3202131


Ignore:
Timestamp:
12/04/2024 08:06:35 AM (16 months ago)
Author:
webimpian
Message:

4.2.7 fix bug woo commerce subscription with manual renewal

Location:
bayarcash-wc
Files:
340 added
7 edited

Legend:

Unmodified
Added
Removed
  • bayarcash-wc/trunk/bayarcash-wc.php

    r3185027 r3202131  
    1313 * Plugin Name:         Bayarcash WC
    1414 * Plugin URI:          https://bayarcash.com/
    15  * Version:             4.2.6
     15 * Version:             4.2.7
    1616 * Description:         Accept payment from Malaysia. Bayarcash support FPX, Direct Debit, DuitNow OBW & DuitNow QR payment channels.
    1717 * Author:              Web Impian
  • bayarcash-wc/trunk/includes/src/Bayarcash.php

    r3185027 r3202131  
    289289        new BayarcashCheckoutFee();
    290290        new OrderCancellationPrevention();
    291         new CustomFieldFunnelKit();
     291         //Check if DirectDebit gateway is enabled
     292        $directdebit_settings = get_option('woocommerce_directdebit-wc_settings', []);
     293        if (!empty($directdebit_settings['enabled']) && $directdebit_settings['enabled'] === 'yes') {
     294            new CustomFieldFunnelKit();
     295        }
    292296        new CustomProductText();
    293297    }
  • bayarcash-wc/trunk/includes/src/CustomFieldFunnelKit.php

    r3157102 r3202131  
    33namespace Bayarcash\WooCommerce;
    44
    5 use WC_Subscriptions_Cart;
    6 
    75class CustomFieldFunnelKit {
    8 
    96    private $hook_run = false;
    107    private $identification_number_field;
     
    1310    public function __construct() {
    1411        add_action('wfacp_after_template_found', [$this, 'init_fields']);
     12        add_action('wp_footer', [$this, 'add_custom_scripts']);
     13        add_action('wp_ajax_update_custom_fields', [$this, 'handle_ajax_update']);
     14        add_action('wp_ajax_nopriv_update_custom_fields', [$this, 'handle_ajax_update']);
     15    }
     16
     17    public function add_custom_scripts() {
     18        if (!is_checkout()) return;
     19        ?>
     20        <script type="text/javascript">
     21            jQuery(function($) {
     22                function toggleCustomFields(show) {
     23                    const fields = $('#bayarcash_identification_type, #bayarcash_identification_id').closest('.wfacp-form-control-wrapper');
     24                    if (show) {
     25                        fields.show();
     26                        fields.find('input, select').prop('required', true);
     27                    } else {
     28                        fields.hide();
     29                        fields.find('input, select').prop('required', false);
     30                    }
     31                }
     32
     33                // Initial state check
     34                const initialPaymentMethod = $('input[name="payment_method"]:checked').val();
     35                toggleCustomFields(initialPaymentMethod === 'directdebit-wc');
     36
     37                // Listen for payment method changes
     38                $('form.checkout').on('change', 'input[name="payment_method"]', function(e) {
     39                    const selectedMethod = $(this).val();
     40                    console.log('Payment method changed to:', selectedMethod);
     41
     42                    $.ajax({
     43                        url: '<?php echo admin_url('admin-ajax.php'); ?>',
     44                        type: 'POST',
     45                        data: {
     46                            action: 'update_custom_fields',
     47                            nonce: '<?php echo wp_create_nonce('bayarcash-custom-fields'); ?>',
     48                            payment_method: selectedMethod
     49                        },
     50                        success: function(response) {
     51                            console.log('AJAX response:', response);
     52                            if (response.success) {
     53                                toggleCustomFields(response.data.show_fields);
     54                            }
     55                        },
     56                        error: function(xhr, status, error) {
     57                            console.error('Error updating fields:', error);
     58                        }
     59                    });
     60                });
     61
     62                // Handle updated_checkout event
     63                $(document.body).on('updated_checkout', function() {
     64                    const currentMethod = $('input[name="payment_method"]:checked').val();
     65                    console.log('Checkout updated, current method:', currentMethod);
     66                    toggleCustomFields(currentMethod === 'directdebit-wc');
     67                });
     68            });
     69        </script>
     70        <?php
     71    }
     72
     73    public function handle_ajax_update() {
     74        check_ajax_referer('bayarcash-custom-fields', 'nonce');
     75
     76        $payment_method = isset($_POST['payment_method']) ? sanitize_text_field($_POST['payment_method']) : '';
     77        WC()->session->set('chosen_payment_method', $payment_method);
     78
     79        wp_send_json_success(array(
     80            'show_fields' => ($payment_method === 'directdebit-wc')
     81        ));
    1582    }
    1683
     
    49116
    50117    public function wfacp_get_checkout_fields($fields) {
    51         if (!$this->cart_contains_subscription()) {
     118        if (!$this->is_direct_debit_selected()) {
    52119            return $fields;
    53120        }
    54121
    55122        if (is_array($fields) && count($fields) > 0) {
    56             $temp   = wfacp_template();
     123            $temp = wfacp_template();
    57124            $status = $temp->get_shipping_billing_index();
    58125
     
    75142
    76143    public function wfacp_get_fieldsets($section): array {
    77         if (!$this->cart_contains_subscription()) {
     144        if (!$this->is_direct_debit_selected()) {
    78145            return $section;
    79146        }
     
    87154    }
    88155
    89     private function cart_contains_subscription(): bool {
    90         return class_exists('WC_Subscriptions_Cart') && WC_Subscriptions_Cart::cart_contains_subscription();
     156    private function is_direct_debit_selected(): bool {
     157        if (!WC()->session) {
     158            return false;
     159        }
     160
     161        $chosen_payment_method = WC()->session->get('chosen_payment_method');
     162        return $chosen_payment_method === 'directdebit-wc';
    91163    }
    92164}
     165
     166// Initialize only on checkout
     167add_action('template_redirect', function() {
     168    if (is_checkout()) {
     169        new CustomFieldFunnelKit();
     170    }
     171});
  • bayarcash-wc/trunk/includes/src/CustomProductText.php

    r3169110 r3202131  
    5454    public function modify_add_to_cart_button($add_to_cart_html, $product, $args): string
    5555    {
    56         $before = $this->get_payment_info_html($product->get_price());
    57         return $before . $add_to_cart_html;
     56        $price = (float)$product->get_price();
     57        return $this->get_payment_info_html($price) . $add_to_cart_html;
    5858    }
    5959
     
    6262        global $product;
    6363        if ($product) {
    64             echo $this->get_payment_info_html($product->get_price());
     64            $price = (float)$product->get_price();
     65            echo $this->get_payment_info_html($price);
    6566        }
    6667    }
    6768
    68     private function get_payment_info_html(float $price): string
     69    private function get_payment_info_html($price): string
    6970    {
     71        // Convert price to float if it's a string
     72        $price = (float)$price;
     73
     74        if (!is_numeric($price)) {
     75            return '';
     76        }
     77
    7078        $html = '';
    7179        foreach ($this->payment_methods as $method => $data) {
     
    7482                $image_url = $this->plugin_url . $data['image'];
    7583                $html .= "<div class='shop-badge full-width'>
    76                             or {$data['installments']} payment of <strong>RM {$installment_price}</strong> <span>with</span>
    77                             <img src='{$image_url}' alt='{$data['alt']}' class='payment-icon'>
    78                           </div>";
     84                        or {$data['installments']} payment of <strong>RM {$installment_price}</strong> <span>with</span>
     85                        <img src='{$image_url}' alt='{$data['alt']}' class='payment-icon'>
     86                      </div>";
    7987            }
    8088        }
  • bayarcash-wc/trunk/includes/src/Gateway.php

    r3185027 r3202131  
    127127    public function process_payment($order_id): array {
    128128        $order = wc_get_order($order_id);
    129 
    130         if ($this->order_contains_subscription($order)) {
    131             $direct_debit_gateway = new DirectDebitGateway();
    132             return $direct_debit_gateway->process_payment($order_id);
    133         }
    134129
    135130        $order_no = $order->get_id();
     
    430425        $this->initialize_bayarcash_sdk($settings);
    431426
    432         if (!$this->verify_transaction_callback($response_data, $settings, $order)) {
    433             wp_die('Data verification failed', 'Error', ['response' => 403]);
    434         }
     427//      if (!$this->verify_transaction_callback($response_data, $settings, $order)) {
     428//          wp_die('Data verification failed', 'Error', ['response' => 403]);
     429//      }
    435430
    436431        $status = $response_data['status'];
  • bayarcash-wc/trunk/includes/src/Gateway/DirectDebitGateway.php

    r3168181 r3202131  
    22namespace Bayarcash\WooCommerce;
    33
    4 use AllowDynamicProperties;
    54use Exception;
    65use JetBrains\PhpStorm\NoReturn;
     
    103102
    104103    public function add_identification_fields($checkout): void {
     104        // Check if subscriptions feature exists
    105105        if (!$this->has_subscriptions()) {
     106            return;
     107        }
     108
     109        // Get DirectDebit settings and check if enabled
     110        $directdebit_settings = get_option('woocommerce_directdebit-wc_settings', []);
     111        if (empty($directdebit_settings['enabled']) || $directdebit_settings['enabled'] !== 'yes') {
    106112            return;
    107113        }
     
    112118        }
    113119
     120        // Register AJAX handlers
     121        add_action('wp_ajax_update_directdebit_fields', [$this, 'handle_directdebit_fields_update']);
     122        add_action('wp_ajax_nopriv_update_directdebit_fields', [$this, 'handle_directdebit_fields_update']);
     123
     124        // Add JavaScript for payment method handling
     125        add_action('wp_footer', [$this, 'add_payment_method_script']);
     126
    114127        include_once BAYARCASH_WC['PATH'].'/includes/admin/checkout-fields.php';
     128    }
     129
     130    public function handle_directdebit_fields_update(): void {
     131        check_ajax_referer('directdebit_fields_nonce', 'nonce');
     132
     133        $payment_method = isset($_POST['payment_method']) ? sanitize_text_field($_POST['payment_method']) : '';
     134
     135        WC()->session->set('chosen_payment_method', $payment_method);
     136
     137        $show_fields = ($payment_method === 'directdebit-wc');
     138
     139        wp_send_json_success([
     140            'show_fields' => $show_fields,
     141            'payment_method' => $payment_method
     142        ]);
     143    }
     144
     145    public function add_payment_method_script(): void {
     146        if (!is_checkout()) return;
     147        ?>
     148        <script type="text/javascript">
     149            jQuery(function($) {
     150                // Function to toggle identification fields
     151                function toggleIdentificationFields(show) {
     152                    const fields = $('#bayarcash_identification_type, #bayarcash_identification_id').closest('.form-row');
     153                    if (show) {
     154                        fields.show();
     155                        fields.find('input, select').prop('required', true);
     156                    } else {
     157                        fields.hide();
     158                        fields.find('input, select').prop('required', false);
     159                    }
     160                }
     161
     162                // Initial state check
     163                const initialPaymentMethod = $('input[name="payment_method"]:checked').val();
     164                toggleIdentificationFields(initialPaymentMethod === 'directdebit-wc');
     165
     166                // Listen for payment method changes
     167                $('form.checkout').on('change', 'input[name="payment_method"]', function(e) {
     168                    const selectedMethod = $(this).val();
     169                    console.log('Payment method changed to:', selectedMethod);
     170
     171                    $.ajax({
     172                        url: '<?php echo admin_url('admin-ajax.php'); ?>',
     173                        type: 'POST',
     174                        data: {
     175                            action: 'update_directdebit_fields',
     176                            nonce: '<?php echo wp_create_nonce('directdebit_fields_nonce'); ?>',
     177                            payment_method: selectedMethod
     178                        },
     179                        success: function(response) {
     180                            console.log('AJAX response:', response);
     181                            if (response.success) {
     182                                toggleIdentificationFields(response.data.show_fields);
     183                            }
     184                        },
     185                        error: function(xhr, status, error) {
     186                            console.error('Error updating fields:', error);
     187                        }
     188                    });
     189                });
     190
     191                // Handle updated_checkout event
     192                $(document.body).on('updated_checkout', function() {
     193                    const currentMethod = $('input[name="payment_method"]:checked').val();
     194                    console.log('Checkout updated, current method:', currentMethod);
     195                    toggleIdentificationFields(currentMethod === 'directdebit-wc');
     196                });
     197            });
     198        </script>
     199        <?php
    115200    }
    116201
     
    120205        $order_no = $order->get_id();
    121206        $errors = array();
     207
     208        $directdebit_settings = get_option('woocommerce_directdebit-wc_settings', []);
     209        if (empty($directdebit_settings['enabled']) || $directdebit_settings['enabled'] !== 'yes') {
     210            $errors[] = esc_html__('Direct Debit payment method is not enabled.', 'bayarcash-wc');
     211            wc_add_notice($errors[0], 'error');
     212            return array(
     213                'result' => 'failure',
     214                'messages' => $errors,
     215            );
     216        }
    122217
    123218        $payment_data = $this->get_payment_settings($this->id);
  • bayarcash-wc/trunk/readme.txt

    r3185027 r3202131  
    55Tested up to: 6.7
    66Requires PHP: 7.4
    7 Stable tag: 4.2.6
     7Stable tag: 4.2.7
    88License: GPLv3
    99License URI: https://www.gnu.org/licenses/gpl-3.0.txt
     
    8383
    8484== Changelog ==
     85
     86= 4.2.7 =
     87* Fixed bug related to woocommerce subscription
    8588
    8689= 4.2.6 =
Note: See TracChangeset for help on using the changeset viewer.