Plugin Directory

Changeset 3202472


Ignore:
Timestamp:
12/04/2024 03:02:00 PM (16 months ago)
Author:
webimpian
Message:

Added validation for subscription billing intervals to only allow every week and every month subscriptions

Location:
bayarcash-wc
Files:
340 added
3 edited

Legend:

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

    r3202439 r3202472  
    1313 * Plugin Name:         Bayarcash WC
    1414 * Plugin URI:          https://bayarcash.com/
    15  * Version:             4.2.8
     15 * Version:             4.2.9
    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/Gateway/DirectDebitGateway.php

    r3202131 r3202472  
    22namespace Bayarcash\WooCommerce;
    33
     4use DateTime;
    45use Exception;
    56use JetBrains\PhpStorm\NoReturn;
     7use WC_Abstract_Order;
    68use WC_Order;
    79use WC_Order_Refund;
     
    285287        );
    286288    }
    287 
    288289    public function process_bayarcash(): void {
    289290        $this->log('Starting Direct Debit Bayarcash process');
     
    293294            $order = $this->get_and_validate_order();
    294295            $settings = $this->get_payment_settings($this->id);
     296
     297            // Validate subscription period and interval
     298            $this->validate_subscription_period($order);
     299            $this->validate_subscription_interval($order);
    295300
    296301            // Initialize the Bayarcash SDK
     
    310315            $this->log('Validation Exception: ' . $error_message);
    311316
    312             // Extract detailed error messages
    313317            $errors = $e->errors;
    314318            if (is_array($errors) && !empty($errors)) {
     
    319323
    320324            $this->log('Detailed Validation Errors: ' . $detailed_error);
    321 
    322             // Display user-friendly message
    323325            wc_add_notice(__('We encountered an issue while processing your Direct Debit enrollment: ', 'bayarcash-wc') . $detailed_error, 'error');
    324 
    325326            wp_redirect(wc_get_checkout_url());
    326327            exit;
    327328        } catch (Exception $e) {
    328329            $this->log('General Exception: ' . $e->getMessage());
    329             wc_add_notice(__('An unexpected error occurred. Please try again or contact support.', 'bayarcash-wc'), 'error');
     330
     331            if (str_contains($e->getMessage(), 'Only weekly and monthly subscriptions')) {
     332                wc_add_notice(__('Only weekly and monthly subscriptions are supported.', 'bayarcash-wc'), 'error');
     333            } else if (str_contains($e->getMessage(), 'Current billing schedule:')) {
     334                // Pass through the full error message with the billing schedule details
     335                wc_add_notice($e->getMessage(), 'error');
     336            } else {
     337                wc_add_notice(__('Unable to process your order. Please try again later.', 'bayarcash-wc'), 'error');
     338            }
     339
    330340            wp_redirect(wc_get_checkout_url());
    331341            exit;
     
    333343    }
    334344
     345    /**
     346     * @throws Exception
     347     */
     348    private function validate_subscription_period(\WC_Abstract_Order $order): void {
     349        foreach ($order->get_items() as $item) {
     350            $product = $item->get_product();
     351            if ($this->is_subscription_product($product)) {
     352                $period = \WC_Subscriptions_Product::get_period($product);
     353                if (!in_array($period, ['week', 'month'])) {
     354                    throw new Exception(__('Only weekly and monthly subscriptions are supported.', 'bayarcash-wc'));
     355                }
     356            }
     357        }
     358    }
     359
     360    /**
     361     * @throws Exception
     362     */
     363    private function validate_subscription_interval(\WC_Abstract_Order $order): void {
     364        foreach ($order->get_items() as $item) {
     365            $product = $item->get_product();
     366            if ($this->is_subscription_product($product)) {
     367                $interval = \WC_Subscriptions_Product::get_interval($product);
     368                $period_text = \WC_Subscriptions_Product::get_period($product);
     369
     370                $this->log(sprintf('Validating subscription interval: %d %s', $interval, $period_text));
     371
     372                if ((int)$interval !== 1) {
     373                    $period_text = \WC_Subscriptions_Product::get_period($product);
     374                    throw new Exception(sprintf(
     375                        __('Only subscriptions billed every week or every month are supported. Current billing schedule: every %d %s(s)', 'bayarcash-wc'),
     376                        $interval,
     377                        $period_text
     378                    ));
     379                }
     380            }
     381        }
     382    }
    335383    /**
    336384     * @throws Exception
     
    368416    }
    369417
    370     private function prepare_enrollment_args(\WC_Abstract_Order $order, array $settings): array
     418    private function prepare_enrollment_args( WC_Abstract_Order $order, array $settings): array
    371419    {
    372420        $order_no = $order->get_id();
     
    383431
    384432        $data_dec_arr = explode(',', $this->data_dec->data);
     433
     434        // Get expiry date from subscription product
     435        $expiry_date = $this->get_subscription_expiry_date($order);
    385436
    386437        $args = [
     
    401452        ];
    402453
     454        // Add expiry_date if it exists
     455        if ($expiry_date) {
     456            $args['expiry_date'] = $expiry_date->format('Y-m-d');
     457        }
     458
    403459        $this->initialize_bayarcash_sdk($settings);
    404460
     
    408464    }
    409465
    410     private function get_subscription_period(\WC_Abstract_Order $order): string
     466    /**
     467     * Get the subscription expiry date based on product settings
     468     *
     469     * @param WC_Abstract_Order $order
     470     *
     471     * @return ?DateTime
     472     */
     473    private function get_subscription_expiry_date( WC_Abstract_Order $order ): ?DateTime
     474    {
     475        foreach ($order->get_items() as $item) {
     476            $product = $item->get_product();
     477
     478            if (!$this->is_subscription_product($product)) {
     479                continue;
     480            }
     481
     482            // Get subscription length and period
     483            $length = \WC_Subscriptions_Product::get_length($product);
     484            $period = \WC_Subscriptions_Product::get_period($product);
     485
     486            // If no length specified or invalid period, subscription doesn't expire
     487            if (empty($length) || !in_array($period, ['week', 'month'])) {
     488                return null;
     489            }
     490
     491            // Create DateTime object for current time
     492            $expiry_date = new DateTime();
     493
     494            // Add subscription length based on period
     495            switch ($period) {
     496                case 'week':
     497                    $expiry_date->modify("+{$length} weeks");
     498                    break;
     499                case 'month':
     500                    $expiry_date->modify("+{$length} months");
     501                    break;
     502            }
     503
     504            return $expiry_date;
     505        }
     506
     507        return null;
     508    }
     509
     510    private function get_subscription_period( WC_Abstract_Order $order): string
    411511    {
    412512        if (!$this->has_subscriptions()) {
  • bayarcash-wc/trunk/readme.txt

    r3202439 r3202472  
    55Tested up to: 6.7
    66Requires PHP: 7.4
    7 Stable tag: 4.2.8
     7Stable tag: 4.2.9
    88License: GPLv3
    99License URI: https://www.gnu.org/licenses/gpl-3.0.txt
     
    8989== Changelog ==
    9090
     91= 4.2.9 =
     92* Added validation for subscription billing intervals to only allow every week and every month subscriptions
     93
    9194= 4.2.8 =
    9295* Added error handling for non-MYR currency transactions
Note: See TracChangeset for help on using the changeset viewer.