Plugin Directory

Changeset 3842474


Ignore:
Timestamp:
12/03/2026 01:02:15 PM (less than one hour ago)
Author:
sanvaldev
Message:

Release 1.0.10 - checkout error message after failed 3DS, duplicate callback guard

Location:
sanval-payments/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sanval-payments/trunk/includes/Api/ThreeDSHandler.php

    r3666618 r3842474  
    6060        $client = $gateway->get_api_client();
    6161
    62         // Verify hash against the raw (un-sanitized, unslashed) data — these
     62        // Verify hash against the raw (un-sanitized, unslashed) data - these
    6363        // are the exact bytes the bank signed.
    6464        if ( ! $client->verify_callback_hash( $raw ) ) {
    … …  
    7171        }
    7272
     73        // Several NestPay deployments post the same terminal result twice: once
     74        // server-to-server (the payload carries callbackCall=true) and once as
     75        // the browser redirect to failUrl. Without a guard both pass
     76        // verification, both add an order note and both call update_status(),
     77        // which can emit a second Failed order email when the two requests
     78        // overlap. Acknowledge the replay instead.
     79        $bank_oid  = (string) ( $post['oid'] ?? '' );
     80        $is_replay = '' !== $bank_oid
     81            && (string) $order->get_meta( '_sanval_failed_oid' ) === $bank_oid;
     82
    7383        $md_status = (int) ( $post['mdStatus'] ?? 0 );
    7484
    7585        /*
    7686         * NestPay mdStatus codes:
    77          *  1        – Full authentication (3DS v1 / v2 frictionless or challenge passed)
    78          *  2, 3, 4  – Attempted authentication (card/issuer not enrolled; liability shift)
    79          *  0, 5–9   – Authentication failed / not completed
     87         *  1        - Full authentication (3DS v1 / v2 frictionless or challenge passed)
     88         *  2, 3, 4  - Attempted authentication (card/issuer not enrolled; liability shift)
     89         *  0, 5-9   - Authentication failed / not completed
    8090         */
    8191        if ( ! in_array( $md_status, [ 1, 2, 3, 4 ], true ) ) {
    82             $error_msg = $post['mdErrorMsg'] ?? $post['ErrMsg'] ?? __( 'Authentication failed.', 'sanval-payments' );
    83             $order->add_order_note(
    84                 sprintf(
    85                     /* translators: 1: mdStatus code, 2: bank error message */
    86                     __( 'Sanval Payments: 3DS failed. mdStatus=%1$s. Bank message: %2$s', 'sanval-payments' ),
    87                     $md_status,
    88                     esc_html( $error_msg )
    89                 )
    90             );
     92            // The bank's own wording goes into the order note, where the
     93            // merchant and support can read it in full.
     94            $bank_msg = (string) ( $post['mdErrorMsg'] ?? $post['ErrMsg'] ?? '' );
     95
     96            if ( ! $is_replay ) {
     97                $order->add_order_note(
     98                    sprintf(
     99                        /* translators: 1: mdStatus code, 2: bank error message */
     100                        __( 'Sanval Payments: 3DS failed. mdStatus=%1$s. Bank message: %2$s', 'sanval-payments' ),
     101                        $md_status,
     102                        esc_html( '' !== $bank_msg ? $bank_msg : __( 'none', 'sanval-payments' ) )
     103                    )
     104                );
     105                $order->update_meta_data( '_sanval_failed_oid', $bank_oid );
     106                $order->save();
     107            }
     108
    91109            $this->capture( 'md_failed', $post, $gateway, $order_id );
    92             $this->fail( $order, esc_html( $error_msg ) );
     110            $this->fail( $order, $this->customer_message( $md_status, $bank_msg ), ! $is_replay );
    93111            return;
    94112        }
    … …  
    100118
    101119        if ( $proc_return !== '00' || $response !== 'Approved' ) {
    102             $err = $post['ErrMsg'] ?? __( 'Payment was declined by the bank.', 'sanval-payments' );
    103             $order->add_order_note(
    104                 sprintf(
    105                     /* translators: 1: bank ProcReturnCode, 2: bank error message */
    106                     __( 'Sanval Payments: declined. ProcReturnCode=%1$s. Message: %2$s', 'sanval-payments' ),
    107                     esc_html( $proc_return ),
    108                     esc_html( $err )
    109                 )
    110             );
     120            $bank_err = (string) ( $post['ErrMsg'] ?? '' );
     121
     122            if ( ! $is_replay ) {
     123                $order->add_order_note(
     124                    sprintf(
     125                        /* translators: 1: bank ProcReturnCode, 2: bank error message */
     126                        __( 'Sanval Payments: declined. ProcReturnCode=%1$s. Message: %2$s', 'sanval-payments' ),
     127                        esc_html( $proc_return ),
     128                        esc_html( '' !== $bank_err ? $bank_err : __( 'none', 'sanval-payments' ) )
     129                    )
     130                );
     131                $order->update_meta_data( '_sanval_failed_oid', $bank_oid );
     132                $order->save();
     133            }
     134
     135            // Decline reasons from NestPay are usually short and useful
     136            // ("Not sufficient funds"), so they are shown when they are safe
     137            // to display; otherwise the customer gets the generic wording.
     138            $customer_err = $this->safe_bank_text( $bank_err );
     139
     140            if ( '' === $customer_err ) {
     141                $customer_err = __( 'Your payment was declined by the bank. Please try again or use a different card.', 'sanval-payments' );
     142            }
     143
    111144            $this->capture( 'declined', $post, $gateway, $order_id );
    112             $this->fail( $order, esc_html( $err ) );
     145            $this->fail( $order, $customer_err, ! $is_replay );
    113146            return;
    114147        }
    … …  
    125158        $order->update_meta_data( '_sanval_proc_code', sanitize_text_field( (string) ( $post['ProcReturnCode'] ?? '' ) ) );
    126159        $order->update_meta_data( '_sanval_trx_date',  sanitize_text_field( (string) ( $post['EXTRA_TRXDATE'] ?? '' ) ) );
     160        // A later retry on the same order must not be mistaken for a replay of
     161        // this attempt, so the failure marker is cleared once a payment lands.
     162        $order->delete_meta_data( '_sanval_failed_oid' );
    127163        $order->save();
    128164
    … …  
    139175        );
    140176
    141 
    142177        WC()->cart->empty_cart();
    143178
    … …  
    146181    }
    147182
    148     // ─────────────────────────────────────────────────────────────────────────
    149 
    150     private function fail( ?\WC_Order $order, string $message ): void {
    151         if ( $order ) {
     183    // -------------------------------------------------------------------------
     184
     185    /**
     186     * Build the message the customer sees after a failed authentication.
     187     *
     188     * NestPay's mdErrorMsg is written for integrators, not shoppers: for a
     189     * declined challenge Banca Intesa returns the full ACS URL including the
     190     * one-time token, which is meaningless to the customer and would end up
     191     * in their address bar. The customer gets a readable, translatable
     192     * sentence keyed on mdStatus; the bank's own text is kept in the order
     193     * note, and appended here only when it is short and contains no URL.
     194     */
     195    private function customer_message( int $md_status, string $bank_msg ): string {
     196        switch ( $md_status ) {
     197            case 0:
     198                $message = __( 'Your bank did not authenticate this card. Please try again or use a different card.', 'sanval-payments' );
     199                break;
     200
     201            case 5:
     202                $message = __( '3D Secure authentication is temporarily unavailable for this card. Please try again later or use a different card.', 'sanval-payments' );
     203                break;
     204
     205            case 6:
     206                $message = __( '3D Secure authentication could not be completed. Please try again or use a different card.', 'sanval-payments' );
     207                break;
     208
     209            case 7:
     210            case 8:
     211            case 9:
     212                $message = __( 'A technical error occurred during authentication. Please try again in a few minutes.', 'sanval-payments' );
     213                break;
     214
     215            default:
     216                $message = __( 'Payment could not be authenticated. Please try again or use a different card.', 'sanval-payments' );
     217        }
     218
     219        $extra = $this->safe_bank_text( $bank_msg );
     220
     221        if ( '' !== $extra ) {
     222            $message = sprintf(
     223                /* translators: 1: plain-language reason, 2: short message from the bank */
     224                __( '%1$s (%2$s)', 'sanval-payments' ),
     225                $message,
     226                $extra
     227            );
     228        }
     229
     230        /**
     231         * Filter the checkout error shown after a failed 3DS authentication.
     232         *
     233         * @param string $message   Message shown to the customer.
     234         * @param int    $md_status NestPay mdStatus code.
     235         * @param string $bank_msg  Unmodified text returned by the bank.
     236         */
     237        return (string) apply_filters( 'sanval_payments_customer_error_message', $message, $md_status, $bank_msg );
     238    }
     239
     240    /**
     241     * Return the bank's text only when it is safe to put in front of a
     242     * shopper: no markup, no link, and short enough to read at a glance.
     243     */
     244    private function safe_bank_text( string $text ): string {
     245        $text = trim( wp_strip_all_tags( $text ) );
     246
     247        if ( '' === $text ) {
     248            return '';
     249        }
     250
     251        if ( preg_match( '#https?://|www\.#i', $text ) ) {
     252            return '';
     253        }
     254
     255        if ( function_exists( 'mb_strlen' ) ? mb_strlen( $text ) > 120 : strlen( $text ) > 120 ) {
     256            return '';
     257        }
     258
     259        return $text;
     260    }
     261
     262    private function fail( ?\WC_Order $order, string $message, bool $allow_status_change = true ): void {
     263        if ( $order && $allow_status_change && $order->needs_payment() ) {
    152264            $order->update_status(
    153265                'failed',
    … …  
    157269        }
    158270
    159         wc_add_notice( $message, 'error' );
    160 
    161271        $checkout_url = function_exists( 'wc_get_checkout_url' )
    162272            ? wc_get_checkout_url()
    163273            : home_url( '/checkout' );
    164274
    165         wp_safe_redirect( $checkout_url );
     275        // The bank posts this result from its own domain, so the WooCommerce
     276        // session cookie (SameSite=Lax) is not sent and wc_add_notice() would
     277        // write into a session the customer never sees. Hand the message over
     278        // in a short-lived transient keyed by a one-time token instead.
     279        $token = wp_generate_password( 20, false );
     280        set_transient( 'sanval_notice_' . $token, $message, 5 * MINUTE_IN_SECONDS );
     281
     282        wp_safe_redirect( add_query_arg( 'sanval_notice', $token, $checkout_url ) );
    166283        exit;
     284    }
     285
     286    /**
     287     * Re-create the failure notice in the customer's own session once they
     288     * land back on checkout. Works for the classic and the block checkout.
     289     */
     290    public static function maybe_restore_notice(): void {
     291        if ( is_admin() || ! function_exists( 'is_checkout' ) || ! is_checkout() ) {
     292            return;
     293        }
     294
     295        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
     296        $token = isset( $_GET['sanval_notice'] )
     297            ? sanitize_key( wp_unslash( $_GET['sanval_notice'] ) )
     298            : '';
     299
     300        if ( '' === $token ) {
     301            return;
     302        }
     303
     304        $message = get_transient( 'sanval_notice_' . $token );
     305        delete_transient( 'sanval_notice_' . $token );
     306
     307        if ( $message && function_exists( 'wc_add_notice' ) ) {
     308            wc_add_notice( (string) $message, 'error' );
     309        }
    167310    }
    168311
  • sanval-payments/trunk/includes/Core.php

    r3666618 r3842474  
    2828        add_action( 'template_redirect', [ $this, 'handle_endpoint' ] );
    2929
     30        // Restore the bank's failure message in the customer's own session
     31        // after the cross-site redirect back to checkout. See
     32        // ThreeDSHandler::fail() for why the message cannot be carried in the
     33        // session directly.
     34        add_action( 'template_redirect', [ '\Sanval\Payments\Api\ThreeDSHandler', 'maybe_restore_notice' ], 5 );
     35
    3036        // Render the 3DS redirect form.
    3137        add_action( 'woocommerce_receipt_sanval_nestpay', [ $this, 'render_3ds_redirect' ] );
    … …  
    3642        // WooCommerce Blocks (block checkout) integration.
    3743        add_action( 'woocommerce_blocks_loaded', [ $this, 'register_blocks_support' ] );
    38        
     44
    3945        // Bank-required transaction details on the confirmation page and email.
    4046        new Support\PaymentReceipt();
  • sanval-payments/trunk/readme.txt

    r3666618 r3842474  
    55Tested up to: 7.1
    66Requires PHP: 8.1
    7 Stable tag: 1.0.9
     7Stable tag: 1.0.10
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    128128== Changelog ==
    129129
     130= 1.0.10 =
     131* Fixed: after a failed 3D Secure authentication the customer was returned to the checkout with no explanation at all. The reason is now shown on the checkout page. The bank posts its result from its own domain, so the message is carried back through a one-time token rather than the session cookie, which browsers do not send on a cross-site POST.
     132* Improved: the checkout message is now a plain-language, translatable sentence based on the bank's mdStatus code. Some banks return a raw diagnostic string here, including the full 3D Secure ACS URL with a one-time token; that is no longer put in front of the shopper. The bank's original wording is still recorded in full in the order notes.
     133* Added: a guard against duplicate result callbacks. Some NestPay deployments post the same terminal result twice, once server-to-server and once as the browser redirect. The order note, the status change and the resulting email are now emitted once.
     134* Added: a `sanval_payments_customer_error_message` filter for merchants who want to reword the checkout failure message.
     135
    130136= 1.0.9 =
    131137* Added an option to show the bank transaction details on the order confirmation page and in the customer email. Some acquirers require this before a store may go live, including Banca Intesa Serbia. Enabled by default; can be turned off in the gateway settings.
    … …  
    174180== Upgrade Notice ==
    175181
     182= 1.0.10 =
     183Customers now see why a 3D Secure payment failed instead of being returned to a blank checkout. Recommended for all users.
     184
    176185= 1.0.9 =
    177186Adds the bank transaction details some acquirers require on payment confirmations.
  • sanval-payments/trunk/sanval-payments.php

    r3666618 r3842474  
    55 * Plugin URI:  https://sanval.dev/
    66 * Description: Professional payment gateway for WooCommerce. Supports the Payten/NestPay network across Turkey, Serbia, and CEE — İşbank, Akbank, Ziraat, Banca Intesa, AIK, NLB Komercijalna, and more.
    7  * Version:     1.0.9
     7 * Version:     1.0.10
    88 * Author:      Sanval Payments
    99 * Author URI:  https://sanval.dev
    … …  
    1919 */
    2020defined( 'ABSPATH' ) || exit;
    21 define( 'SANVAL_VERSION', '1.0.9' );
     21define( 'SANVAL_VERSION', '1.0.10' );
    2222define( 'SANVAL_FILE', __FILE__ );
    2323define( 'SANVAL_PATH', plugin_dir_path( __FILE__ ) );
Note: See TracChangeset for help on using the changeset viewer.