Changeset 3842474
- Timestamp:
- 12/03/2026 01:02:15 PM (less than one hour ago)
- Location:
- sanval-payments/trunk
- Files:
-
- 4 edited
-
includes/Api/ThreeDSHandler.php (modified) (7 diffs)
-
includes/Core.php (modified) (2 diffs)
-
readme.txt (modified) (3 diffs)
-
sanval-payments.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sanval-payments/trunk/includes/Api/ThreeDSHandler.php
r3666618 r3842474 60 60 $client = $gateway->get_api_client(); 61 61 62 // Verify hash against the raw (un-sanitized, unslashed) data —these62 // Verify hash against the raw (un-sanitized, unslashed) data - these 63 63 // are the exact bytes the bank signed. 64 64 if ( ! $client->verify_callback_hash( $raw ) ) { … … 71 71 } 72 72 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 73 83 $md_status = (int) ( $post['mdStatus'] ?? 0 ); 74 84 75 85 /* 76 86 * 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 completed87 * 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 80 90 */ 81 91 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 91 109 $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 ); 93 111 return; 94 112 } … … 100 118 101 119 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 111 144 $this->capture( 'declined', $post, $gateway, $order_id ); 112 $this->fail( $order, esc_html( $err ));145 $this->fail( $order, $customer_err, ! $is_replay ); 113 146 return; 114 147 } … … 125 158 $order->update_meta_data( '_sanval_proc_code', sanitize_text_field( (string) ( $post['ProcReturnCode'] ?? '' ) ) ); 126 159 $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' ); 127 163 $order->save(); 128 164 … … 139 175 ); 140 176 141 142 177 WC()->cart->empty_cart(); 143 178 … … 146 181 } 147 182 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() ) { 152 264 $order->update_status( 153 265 'failed', … … 157 269 } 158 270 159 wc_add_notice( $message, 'error' );160 161 271 $checkout_url = function_exists( 'wc_get_checkout_url' ) 162 272 ? wc_get_checkout_url() 163 273 : home_url( '/checkout' ); 164 274 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 ) ); 166 283 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 } 167 310 } 168 311 -
sanval-payments/trunk/includes/Core.php
r3666618 r3842474 28 28 add_action( 'template_redirect', [ $this, 'handle_endpoint' ] ); 29 29 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 30 36 // Render the 3DS redirect form. 31 37 add_action( 'woocommerce_receipt_sanval_nestpay', [ $this, 'render_3ds_redirect' ] ); … … 36 42 // WooCommerce Blocks (block checkout) integration. 37 43 add_action( 'woocommerce_blocks_loaded', [ $this, 'register_blocks_support' ] ); 38 44 39 45 // Bank-required transaction details on the confirmation page and email. 40 46 new Support\PaymentReceipt(); -
sanval-payments/trunk/readme.txt
r3666618 r3842474 5 5 Tested up to: 7.1 6 6 Requires PHP: 8.1 7 Stable tag: 1.0. 97 Stable tag: 1.0.10 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 128 128 == Changelog == 129 129 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 130 136 = 1.0.9 = 131 137 * 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. … … 174 180 == Upgrade Notice == 175 181 182 = 1.0.10 = 183 Customers now see why a 3D Secure payment failed instead of being returned to a blank checkout. Recommended for all users. 184 176 185 = 1.0.9 = 177 186 Adds the bank transaction details some acquirers require on payment confirmations. -
sanval-payments/trunk/sanval-payments.php
r3666618 r3842474 5 5 * Plugin URI: https://sanval.dev/ 6 6 * 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. 97 * Version: 1.0.10 8 8 * Author: Sanval Payments 9 9 * Author URI: https://sanval.dev … … 19 19 */ 20 20 defined( 'ABSPATH' ) || exit; 21 define( 'SANVAL_VERSION', '1.0. 9' );21 define( 'SANVAL_VERSION', '1.0.10' ); 22 22 define( 'SANVAL_FILE', __FILE__ ); 23 23 define( 'SANVAL_PATH', plugin_dir_path( __FILE__ ) );
Note:
See TracChangeset
for help on using the changeset viewer.