Plugin Directory

Changeset 3220525


Ignore:
Timestamp:
01/11/2025 04:19:46 AM (15 months ago)
Author:
webimpian
Message:
  • Fixed duplicate order notes and payment status updates in callback handling
Location:
bayarcash-givewp
Files:
355 added
6 edited

Legend:

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

    r3219894 r3220525  
    1313 * Plugin Name:         Bayarcash GiveWP
    1414 * Plugin URI:          https://bayarcash.com/
    15  * Version:             4.2.0
     15 * Version:             4.2.1
    1616 * Description:         Accept online donation & QR from Malaysia. Currently, Bayarcash support FPX, Direct Debit and DuitNow payment channels.
    1717 * Author:              Web Impian
  • bayarcash-givewp/trunk/includes/src/BayarcashCallbacks.php

    r3219894 r3220525  
    118118            }
    119119
    120             give_update_payment_status($payment_id, 'pending');
    121120            give_update_meta($payment_id, 'bayarcash_transaction_id', $transaction_data['transaction_id']);
    122121            error_log('Pre-transaction data processed successfully');
  • bayarcash-givewp/trunk/includes/src/CronEvent.php

    r3165846 r3220525  
    7979            $donation_form_id = give_get_payment_form_id($payment->ID);
    8080
     81            // Handle block channel payment gateways
     82            if (strpos($payment_gateway, '_block') !== false) {
     83                $payment_gateway = $this->mapBlockGatewayToRegular($payment_gateway);
     84            }
     85
    8186            // Get the appropriate token based on the payment gateway
    8287            $bayarcash_portal_token = $this->get_portal_token($payment_gateway, $donation_form_id);
     
    96101            $this->pt->data_store()->update_payment_fpx($transaction_data);
    97102            error_log('Payment updated');
    98 
    99103        }
    100104
    101105        error_log('Bayarcash GiveWP: check_payment method completed at ' . current_time('mysql'));
     106    }
     107
     108    /**
     109     * Maps block gateway IDs to regular gateway IDs
     110     *
     111     * @param string $blockGatewayId The block gateway ID to map
     112     * @return string The mapped regular gateway ID
     113     */
     114    private function mapBlockGatewayToRegular(string $blockGatewayId): string {
     115        $gatewayMap = [
     116            'bayarcash_block' => 'bayarcash',
     117            'bayarcash_duitnow_block' => 'bayarcash_duitnow',
     118            'bayarcash_linecredit_block' => 'bayarcash_linecredit',
     119            'bayarcash_duitnowqr_block' => 'bayarcash_duitnowqr',
     120            'bayarcash_duitnowshopee_block' => 'bayarcash_duitnowshopee',
     121            'bayarcash_duitnowboost_block' => 'bayarcash_duitnowboost',
     122            'bayarcash_duitnowqris_block' => 'bayarcash_duitnowqris',
     123            'bayarcash_duitnowqriswallet_block' => 'bayarcash_duitnowqriswallet'
     124        ];
     125
     126        return $gatewayMap[$blockGatewayId] ?? 'bayarcash';
    102127    }
    103128
  • bayarcash-givewp/trunk/includes/src/DataRequest.php

    r3165846 r3220525  
    9696    {
    9797        if (defined('WP_DEBUG') && WP_DEBUG) {
    98             error_log('BayarCash Debug: ' . $message);
     98            error_log('Bayarcash Debug: ' . $message);
    9999        }
    100100    }
  • bayarcash-givewp/trunk/includes/src/DataStore.php

    r3165846 r3220525  
    44class DataStore
    55{
     6    private function note_exists($payment_id, $exchange_reference, $new_status): bool {
     7        global $wpdb;
     8
     9        // Get current payment status
     10        $current_status = get_post_meta($payment_id, '_give_payment_status', true);
     11
     12        // If current status is failed and new status is complete, allow the update
     13        if ($current_status === 'failed' && $new_status === 'complete') {
     14            return false;
     15        }
     16
     17        $query = $wpdb->prepare(
     18            "SELECT COUNT(*) FROM {$wpdb->prefix}give_comments
     19             WHERE comment_parent = %d
     20             AND comment_content LIKE %s
     21             AND comment_content LIKE %s",
     22            $payment_id,
     23            '%' . $wpdb->esc_like($exchange_reference) . '%',
     24            '%Transaction Status: ' . $wpdb->esc_like($new_status) . '%'
     25        );
     26
     27        error_log('Checking for duplicate note with query: ' . $query);
     28
     29        $count = $wpdb->get_var($query);
     30        error_log('Found notes count: ' . var_export($count, true));
     31
     32        return $count > 0;
     33    }
     34
    635    public function update_payment_fpx($transaction_data)
    736    {
     
    1544
    1645        $status_list = ['new', 'pending', 'unsuccessful', 'successful', 'cancelled'];
    17         $status_name = $status_list[ $status_number ] ?? 'unknown';
     46        $status_name = $status_list[$status_number] ?? 'unknown';
    1847
    1948        $form_url = Give()->payment_meta->get_meta($payment_id, '_give_current_url', true);
     
    2150
    2251        $payment_note_arr = static function ($records, $status = '') {
    23             return [
     52            $amount = number_format((float)$records['amount'], 2, '.', '');
     53            return implode(' | ', [
    2454                'Donation ID: '.$records['order_number'],
    2555                'Exchange Reference Number: '.$records['exchange_reference_number'],
    26                 'ID Number: '.$records['id'],
     56                'ID Number: '.$records['transaction_id'],
    2757                'Transaction Status: '.$status,
    2858                'Transaction Status Description: '.$records['status_description'],
    2959                'Donor Bank Name: '.$records['payer_bank_name'],
    30                 'Transaction Amount: '.$records['currency'].' '.$records['amount'],
     60                'Transaction Amount: '.$records['currency'].' '.$amount,
    3161                'Donor Name: '.$records['payer_name'],
    32                 'Donor Email: '.$records['payer_email'],
    33             ];
     62                'Donor Email: '.$records['payer_email']
     63            ]);
    3464        };
    3565
     66        // Map status names to payment statuses
    3667        if ($status_name === 'successful') {
    3768            $payment_status = 'complete';
     69        } elseif (in_array($status_name, ['unsuccessful', 'cancelled'], true)) {
     70            $payment_status = 'failed';
     71        } else {
     72            $payment_status = 'pending';
     73        }
     74
     75        // Create note content
     76        $note_content = $payment_note_arr($transaction_data, $payment_status);
     77
     78        // Check if this exact status update already exists
     79        if (!$this->note_exists($payment_id, $transaction_data['exchange_reference_number'], $payment_status)) {
    3880            give_update_payment_status($payment_id, $payment_status);
    39             //delete_post_meta($payment_id, 'bayarcash_fpx_transaction_exchange_no');
     81            give_insert_payment_note($payment_id, $note_content);
    4082
    41             give_insert_payment_note($payment_id, implode(' | ', $payment_note_arr($transaction_data, $payment_status)));
     83            $log_content = $payment_status === 'pending'
     84                ? 'The payment status of #' . $payment_id . ' is still not resolved yet.'
     85                : $note_content;
    4286
    4387            debug_log([
     
    4589                'form-url'    => $form_url,
    4690                'request-uri' => $request_uri,
    47                 'content'     => implode(' | ', $payment_note_arr($transaction_data, $payment_status)),
     91                'content'     => $log_content,
    4892            ]);
    4993
    50             error_log('Payment ' . $payment_id . ' updated to complete status');
    51         } elseif ($status_name === 'unsuccessful' || $status_name === 'cancelled') {
    52             $payment_status = 'failed';
    53             give_update_payment_status($payment_id, $payment_status);
    54             //delete_post_meta($payment_id, 'bayarcash_fpx_transaction_exchange_no');
    55 
    56             give_insert_payment_note($payment_id, implode(' | ', $payment_note_arr($transaction_data, $payment_status)));
    57 
    58             debug_log([
    59                 'caller'      => __METHOD__,
    60                 'form-url'    => $form_url,
    61                 'request-uri' => $request_uri,
    62                 'content'     => implode(' | ', $payment_note_arr($transaction_data, $payment_status)),
    63             ]);
    64 
    65             error_log('Payment ' . $payment_id . ' updated to failed status');
     94            error_log('Payment ' . $payment_id . ' updated to ' . $payment_status . ' status');
    6695        } else {
    67             $payment_status = 'pending';
    68             give_update_payment_status($payment_id, $payment_status);
    69             give_insert_payment_note($payment_id, implode(' | ', $payment_note_arr($transaction_data, $payment_status)));
    70             debug_log([
    71                 'caller'      => __METHOD__,
    72                 'form-url'    => $form_url,
    73                 'request-uri' => $request_uri,
    74                 'content'     => 'The payment status of #' . $payment_id . ' is still not resolved yet.',
    75             ]);
    76 
    77             error_log('The payment status of #' . $payment_id . ' is still not resolved yet.');
     96            error_log('Duplicate status update skipped for payment ' . $payment_id);
    7897        }
    7998    }
  • bayarcash-givewp/trunk/readme.txt

    r3219896 r3220525  
    55Tested up to: 6.7
    66Requires PHP: 7.4
    7 Stable tag: 4.2.0
     7Stable tag: 4.2.1
    88License: GPLv3
    99License URI: https://www.gnu.org/licenses/gpl-3.0.txt
     
    7777
    7878== Changelog ==
     79
     80= 4.2.1 =
     81* Fixed duplicate order notes and payment status updates in callback handling
    7982
    8083= 4.2.0 =
Note: See TracChangeset for help on using the changeset viewer.