| @@ -22,8 +22,18 @@ | ||
| 22 | 22 | class Handler extends Controller{ |
| 23 | 23 | use TraitsHelper; |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | + * Status a Stripe Checkout Session order waits in until it is confirmed. | |
| 27 | + */ | |
| 28 | + const STRIPE_PENDING_STATUS = 'unpaid'; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Status a confirmed Checkout Session order is stored with. See confirm_stripe_session(). | |
| 32 | + */ | |
| 33 | + const STRIPE_CONFIRMED_STATUS = 'paid'; | |
| 34 | + | |
| 35 | + /** | |
| 26 | 36 | * PayPal button |
| 27 | 37 | * |
| 28 | 38 | * @since 0.0.1 |
| 29 | 39 | */ |
| @@ -145,15 +155,15 @@ | ||
| 145 | 155 | } |
| 146 | 156 | |
| 147 | 157 | /** |
| 148 | 158 | * Payment create in db |
| 149 | - * | |
| 159 | + * | |
| 150 | 160 | * @since 0.0.1 |
| 151 | 161 | */ |
| 152 | 162 | public static function payment_create( $data ) { |
| 153 | 163 | global $wpdb; |
| 154 | 164 | $table = "{$wpdb->prefix}better_payment"; |
| 155 | - $wpdb->insert( $table, $data ); | |
| 165 | + $wpdb->insert( $table, self::normalize_pending_stripe_status( $data ) ); | |
| 156 | 166 | if ( $wpdb->insert_id ) { |
| 157 | 167 | return $wpdb->insert_id; |
| 158 | 168 | } else { |
| 159 | 169 | return false; |
| @@ -160,8 +170,59 @@ | ||
| 160 | 170 | } |
| 161 | 171 | } |
| 162 | 172 | |
| 163 | 173 | /** |
| 174 | + * A Stripe Checkout Session order is always created pending. | |
| 175 | + * | |
| 176 | + * Every Stripe surface stores the session's `payment_status` at creation. For a session with | |
| 177 | + * nothing to charge right now — a subscription trial, a 100%-off promotion code, setup mode — | |
| 178 | + * Stripe already reports `no_payment_required` before anyone has completed the checkout. Stored | |
| 179 | + * as is, the order could never be confirmed (confirmation claims it from `unpaid`), so the trial | |
| 180 | + * or free donation never reached the thank-you page, its receipts or the campaign's donors. | |
| 181 | + * Only a Checkout Session row (`obj_id` `cs_…`) is touched: a renewal PaymentIntent row is | |
| 182 | + * written after its charge and keeps its status. | |
| 183 | + * | |
| 184 | + * @since 2.4.0 | |
| 185 | + * | |
| 186 | + * @param mixed $data Row data for payment_create(). | |
| 187 | + * @return mixed | |
| 188 | + */ | |
| 189 | + public static function normalize_pending_stripe_status( $data ) { | |
| 190 | + if ( ! is_array( $data ) || ! isset( $data['status'], $data['obj_id'] ) || ! is_string( $data['obj_id'] ) ) { | |
| 191 | + return $data; | |
| 192 | + } | |
| 193 | + | |
| 194 | + if ( 'no_payment_required' === $data['status'] && 0 === strpos( $data['obj_id'], 'cs_' ) ) { | |
| 195 | + $data['status'] = self::STRIPE_PENDING_STATUS; | |
| 196 | + } | |
| 197 | + | |
| 198 | + return $data; | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 202 | + * A new, unguessable order id: `{gateway}_{24 hex chars}`. | |
| 203 | + * | |
| 204 | + * The order id is the only key the public return pages and the PayPal status poll look | |
| 205 | + * an order up by. uniqid() is the current time in microseconds, so ids created around a | |
| 206 | + * known moment could be enumerated to read other visitors' order status and receipts. | |
| 207 | + * 96 random bits cannot be. Fits the 50-char `order_id` column with room to spare. | |
| 208 | + * | |
| 209 | + * @since 2.3.4 | |
| 210 | + * | |
| 211 | + * @param string $gateway 'paypal' | 'stripe' | 'paystack'. | |
| 212 | + * @return string | |
| 213 | + */ | |
| 214 | + public static function new_order_id( $gateway ) { | |
| 215 | + try { | |
| 216 | + $random = bin2hex( random_bytes( 12 ) ); | |
| 217 | + } catch ( \Exception $e ) { | |
| 218 | + $random = strtolower( wp_generate_password( 24, false ) ); | |
| 219 | + } | |
| 220 | + | |
| 221 | + return sanitize_key( $gateway ) . '_' . $random; | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 164 | 225 | * Mamage response |
| 165 | 226 | * |
| 166 | 227 | * @since 0.0.1 |
| 167 | 228 | */ |
| @@ -183,23 +244,9 @@ | ||
| 183 | 244 | } elseif ( !empty( $_REQUEST['better_payment_paypal_id'] ) ) { |
| 184 | 245 | $order_id = sanitize_text_field( $_REQUEST['better_payment_paypal_id'] ); |
| 185 | 246 | } |
| 186 | 247 | |
| 187 | - // Fetch transaction data from database | |
| 188 | - $transaction_data = self::get_failed_transaction_data( $order_id ); | |
| 189 | - | |
| 190 | - self::failed_message_template( $settings, $transaction_data ); | |
| 191 | - $redirection_url_error = ! empty( $settings['better_payment_form_error_page_url']['url'] ) ? esc_url( $settings['better_payment_form_error_page_url']['url'] ) : ''; | |
| 192 | - | |
| 193 | - if( $redirection_url_error ){ | |
| 194 | - ?> | |
| 195 | - <script> | |
| 196 | - setTimeout(function(){ | |
| 197 | - window.location.replace("<?php echo esc_url( $redirection_url_error ); ?>"); | |
| 198 | - }, 2000); | |
| 199 | - </script> | |
| 200 | - <?php | |
| 201 | - } | |
| 248 | + self::render_failed_response( $settings, $order_id ); | |
| 202 | 249 | |
| 203 | 250 | return $status; |
| 204 | 251 | } |
| 205 | 252 | |
| @@ -210,12 +257,72 @@ | ||
| 210 | 257 | } elseif ( !empty( $_REQUEST[ 'better_payment_paystack_status' ] ) && $_REQUEST[ 'better_payment_paystack_status' ] == 'success' ) { |
| 211 | 258 | $status = self::paystack_payment_success( $settings ); |
| 212 | 259 | } |
| 213 | 260 | |
| 214 | - if ( $status ) { | |
| 261 | + if ( is_wp_error( $status ) ) { | |
| 262 | + // A verification that confirmed no payment never renders the success page or | |
| 263 | + // redirects to the success URL (a WP_Error is truthy, and used to do both). | |
| 264 | + if ( 'bp_stripe_payment_processing' === $status->get_error_code() ) { | |
| 265 | + self::stripe_processing_message_template(); | |
| 266 | + | |
| 267 | + // The return URL keeps its query args, so reloading it verifies the order again. | |
| 268 | + return $status; | |
| 269 | + } | |
| 270 | + | |
| 271 | + if ( 'bp_stripe_payment_incomplete' === $status->get_error_code() ) { | |
| 272 | + $stripe_order_id = ! empty( $_REQUEST['better_payment_stripe_id'] ) && is_scalar( $_REQUEST['better_payment_stripe_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['better_payment_stripe_id'] ) ) : ''; | |
| 273 | + | |
| 274 | + self::render_failed_response( $settings, $stripe_order_id ); | |
| 275 | + | |
| 276 | + // Like a cancelled checkout: the form renders again under the notice. | |
| 277 | + $status = false; | |
| 278 | + } | |
| 279 | + } elseif ( ! empty( $status['pending'] ) ) { | |
| 280 | + // Use home_url() instead of HTTP_HOST to avoid open-redirect via forged Host headers. | |
| 281 | + $return_url = home_url( $_SERVER['REQUEST_URI'] ); | |
| 282 | + // PayPal uses rm=2 (POST redirect), so item_number arrives in $_POST and is absent from the URL. | |
| 283 | + // Add it explicitly so the poll-complete redirect (a plain GET) still carries item_number. | |
| 284 | + if ( ! empty( $status['order_id'] ) && strpos( $return_url, 'item_number=' ) === false ) { | |
| 285 | + $return_url = add_query_arg( 'item_number', $status['order_id'], $return_url ); | |
| 286 | + } | |
| 287 | + ?> | |
| 288 | + <section class="bp-thank_page"> | |
| 289 | + <div class="bp-thank_page-wrapper bp-paypal-pending" | |
| 290 | + data-order-id="<?php echo esc_attr( $status['order_id'] ); ?>" | |
| 291 | + data-return-url="<?php echo esc_url( $return_url ); ?>"> | |
| 292 | + | |
| 293 | + <div class="bp-thank_page-logo"> | |
| 294 | + <span class="bp-paypal-pending__spinner-wrap"> | |
| 295 | + <span class="bp-paypal-pending__ring"></span> | |
| 296 | + <img class="bp-paypal-pending__logo" | |
| 297 | + src="<?php echo esc_url( BETTER_PAYMENT_ASSETS . '/img/paypal-2.svg' ); ?>" | |
| 298 | + alt="PayPal"> | |
| 299 | + </span> | |
| 300 | + </div> | |
| 301 | + | |
| 302 | + <div class="bp-thank_page-text bp-paypal-pending__body"> | |
| 303 | + <h2 class="bp-font_ibm bp-page_header bp-paypal-pending__heading"> | |
| 304 | + <?php esc_html_e( 'Verifying your payment…', 'better-payment' ); ?> | |
| 305 | + </h2> | |
| 306 | + <p class="bp-font_ibm bp-payment_info bp-paypal-pending__sub"> | |
| 307 | + <?php esc_html_e( 'Please do not close this page. This usually takes a few seconds.', 'better-payment' ); ?> | |
| 308 | + </p> | |
| 309 | + <div class="bp-paypal-pending__dots"> | |
| 310 | + <span></span><span></span><span></span> | |
| 311 | + </div> | |
| 312 | + </div> | |
| 313 | + | |
| 314 | + <div class="bp-paypal-pending__bar"> | |
| 315 | + <div class="bp-paypal-pending__bar-inner"></div> | |
| 316 | + </div> | |
| 317 | + | |
| 318 | + </div> | |
| 319 | + </section> | |
| 320 | + <?php | |
| 321 | + } elseif ( $status ) { | |
| 215 | 322 | self::success_message_template( $settings, $status ); |
| 216 | 323 | $redirection_url_success = ! empty( $settings['better_payment_form_success_page_url']['url'] ) ? esc_url( $settings['better_payment_form_success_page_url']['url'] ) : ''; |
| 217 | - | |
| 324 | + | |
| 218 | 325 | if( $redirection_url_success ){ |
| 219 | 326 | ?> |
| 220 | 327 | <script> |
| 221 | 328 | setTimeout(function(){ |
| @@ -221,9 +328,9 @@ | ||
| 221 | 328 | setTimeout(function(){ |
| 222 | 329 | window.location.replace("<?php echo esc_url( $redirection_url_success ); ?>"); |
| 223 | 330 | }, 2000); |
| 224 | 331 | </script> |
| 225 | - <?php | |
| 332 | + <?php | |
| 226 | 333 | } |
| 227 | 334 | } |
| 228 | 335 | self::remove_arg(); |
| 229 | 336 | |
| @@ -231,298 +338,1075 @@ | ||
| 231 | 338 | } |
| 232 | 339 | |
| 233 | 340 | /** |
| 234 | 341 | * Paypal payment success |
| 235 | - * | |
| 342 | + * | |
| 343 | + * The DB write happens exclusively in handle_paypal_ipn() after PayPal verifies | |
| 344 | + * the IPN post-back. This method is read-only: it never writes to the DB. | |
| 345 | + * | |
| 346 | + * Display data priority: | |
| 347 | + * 1. DB row (IPN already fired and confirmed) — authoritative. | |
| 348 | + * 2. PayPal return-URL params ($_REQUEST) — for display only when IPN hasn't | |
| 349 | + * fired yet (local dev, race condition). Safe because no DB write occurs here. | |
| 350 | + * | |
| 236 | 351 | * @since 0.0.1 |
| 237 | 352 | */ |
| 238 | 353 | public static function paypal_payment_success( $settings = [] ) { |
| 239 | - $data = $_REQUEST; | |
| 240 | - $frontend_data = [ | |
| 241 | - 'amount' => ! empty( $data['payment_gross'] ) ? floatval( $data['payment_gross'] ) : 0, | |
| 242 | - 'email' => ! empty( $data['payer_email'] ) ? sanitize_email( $data['payer_email'] ) : '', | |
| 243 | - 'transaction_id' => ! empty( $data[ 'txn_id' ] ) ? sanitize_text_field( $data[ 'txn_id' ] ) : '', | |
| 244 | - 'currency' => ! empty( $data['mc_currency'] ) ? sanitize_text_field( $data['mc_currency'] ) : __( 'USD', 'better-payment' ), | |
| 245 | - 'method' => 'paypal', | |
| 246 | - ]; | |
| 247 | - if ( !empty( $data[ 'payment_status' ] ) && !empty( $data[ 'payer_id' ] ) && !empty( $data[ 'payer_status' ] ) ) { | |
| 248 | - global $wpdb; | |
| 249 | - $table = "{$wpdb->prefix}better_payment"; | |
| 250 | - $results = $wpdb->get_row( | |
| 251 | - $wpdb->prepare( "SELECT id,form_fields_info,referer FROM $table WHERE order_id=%s and status is NULL limit 1", sanitize_text_field( $_REQUEST[ 'item_number' ] ) ) | |
| 354 | + $item_number = ! empty( $_REQUEST['item_number'] ) ? sanitize_text_field( $_REQUEST['item_number'] ) : ''; | |
| 355 | + | |
| 356 | + if ( empty( $item_number ) ) { | |
| 357 | + return false; | |
| 358 | + } | |
| 359 | + | |
| 360 | + global $wpdb; | |
| 361 | + $table = "{$wpdb->prefix}better_payment"; | |
| 362 | + $row = $wpdb->get_row( | |
| 363 | + $wpdb->prepare( | |
| 364 | + "SELECT transaction_id, status, email, amount, currency FROM $table WHERE order_id = %s LIMIT 1", | |
| 365 | + $item_number | |
| 366 | + ) | |
| 367 | + ); | |
| 368 | + | |
| 369 | + // No matching order — reject (prevents fake success display with forged item_number). | |
| 370 | + if ( empty( $row ) ) { | |
| 371 | + return false; | |
| 372 | + } | |
| 373 | + | |
| 374 | + // IPN confirmed — return verified DB data. The payer's e-mail and the PayPal transaction | |
| 375 | + // id only for an unguessable order id: before 2.4.0 ids were `paypal_` . uniqid(), which is | |
| 376 | + // time-based and can be enumerated, so anyone could read them off the return page. | |
| 377 | + if ( $row->status === 'Completed' ) { | |
| 378 | + $unguessable = self::paypal_order_id_is_unguessable( $item_number ); | |
| 379 | + | |
| 380 | + return [ | |
| 381 | + 'amount' => floatval( $row->amount ), | |
| 382 | + 'email' => $unguessable ? sanitize_email( $row->email ) : '', | |
| 383 | + 'transaction_id' => $unguessable ? sanitize_text_field( $row->transaction_id ) : '', | |
| 384 | + 'currency' => sanitize_text_field( $row->currency ), | |
| 385 | + 'method' => 'paypal', | |
| 386 | + ]; | |
| 387 | + } | |
| 388 | + | |
| 389 | + // Order exists but IPN hasn't arrived yet — signal pending so JS can poll. | |
| 390 | + return [ 'pending' => true, 'order_id' => $item_number ]; | |
| 391 | + } | |
| 392 | + | |
| 393 | + /** | |
| 394 | + * Whether a PayPal order id came from new_order_id() (24 random characters), not the | |
| 395 | + * enumerable `paypal_` . uniqid() of releases before 2.4.0. | |
| 396 | + * | |
| 397 | + * @since 2.4.1 | |
| 398 | + * | |
| 399 | + * @param mixed $order_id The order id. | |
| 400 | + * @return bool | |
| 401 | + */ | |
| 402 | + public static function paypal_order_id_is_unguessable( $order_id ) { | |
| 403 | + return is_string( $order_id ) && 1 === preg_match( '/^paypal_[0-9a-z]{24}$/', $order_id ); | |
| 404 | + } | |
| 405 | + | |
| 406 | + /** | |
| 407 | + * Whether an order id of any gateway came from new_order_id() (`{gateway}_` + 24 random | |
| 408 | + * characters), not the enumerable `{gateway}_` . uniqid() of releases before 2.4.0. | |
| 409 | + * | |
| 410 | + * @since 2.4.1 | |
| 411 | + * | |
| 412 | + * @param mixed $order_id The order id. | |
| 413 | + * @return bool | |
| 414 | + */ | |
| 415 | + public static function order_id_is_unguessable( $order_id ) { | |
| 416 | + return is_string( $order_id ) && 1 === preg_match( '/^[a-z0-9-]+_[0-9a-z]{24}$/', $order_id ); | |
| 417 | + } | |
| 418 | + | |
| 419 | + /** | |
| 420 | + * AJAX: check whether a PayPal order has been confirmed by IPN. | |
| 421 | + * | |
| 422 | + * Called by the frontend polling loop after the user lands on the return URL | |
| 423 | + * before the IPN fires. Returns {status:'pending'} or {status:'completed'}. | |
| 424 | + * | |
| 425 | + * Lives on Handler (not Actions) so it can be registered unconditionally — | |
| 426 | + * PayPal payments flow through the block/campaign path without Elementor. | |
| 427 | + */ | |
| 428 | + public static function check_paypal_status() { | |
| 429 | + check_ajax_referer( 'better-payment', 'security' ); | |
| 430 | + | |
| 431 | + $order_id = ! empty( $_POST['order_id'] ) ? sanitize_text_field( $_POST['order_id'] ) : ''; | |
| 432 | + | |
| 433 | + if ( empty( $order_id ) ) { | |
| 434 | + wp_send_json_error( [ 'message' => 'Missing order_id' ] ); | |
| 435 | + } | |
| 436 | + | |
| 437 | + global $wpdb; | |
| 438 | + $table = "{$wpdb->prefix}better_payment"; | |
| 439 | + $row = $wpdb->get_row( | |
| 440 | + $wpdb->prepare( | |
| 441 | + "SELECT status FROM $table WHERE order_id = %s LIMIT 1", | |
| 442 | + $order_id | |
| 443 | + ) | |
| 444 | + ); | |
| 445 | + | |
| 446 | + if ( empty( $row ) ) { | |
| 447 | + wp_send_json_error( [ 'message' => 'Order not found' ] ); | |
| 448 | + } | |
| 449 | + | |
| 450 | + if ( $row->status === 'Completed' ) { | |
| 451 | + wp_send_json_success( [ 'status' => 'completed' ] ); | |
| 452 | + } | |
| 453 | + | |
| 454 | + wp_send_json_success( [ 'status' => 'pending' ] ); | |
| 455 | + } | |
| 456 | + | |
| 457 | + /** | |
| 458 | + * PayPal IPN listener. | |
| 459 | + * | |
| 460 | + * PayPal POSTs the raw IPN body here server-to-server. We echo it back with | |
| 461 | + * cmd=_notify-validate; only a VERIFIED response from PayPal's servers reaches | |
| 462 | + * apply_paypal_ipn(), the only writer. The browser return URL | |
| 463 | + * (paypal_payment_success) never writes. | |
| 464 | + * | |
| 465 | + * @since 2.1.3 | |
| 466 | + */ | |
| 467 | + public static function handle_paypal_ipn() { | |
| 468 | + $raw_post = file_get_contents( 'php://input' ); | |
| 469 | + | |
| 470 | + if ( empty( $raw_post ) ) { | |
| 471 | + status_header( 400 ); | |
| 472 | + exit; | |
| 473 | + } | |
| 474 | + | |
| 475 | + $data = []; | |
| 476 | + wp_parse_str( $raw_post, $data ); | |
| 477 | + | |
| 478 | + // PayPal includes test_ipn=1 for sandbox notifications. | |
| 479 | + $verify_url = ! empty( $data['test_ipn'] ) | |
| 480 | + ? 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr' | |
| 481 | + : 'https://ipnpb.paypal.com/cgi-bin/webscr'; | |
| 482 | + | |
| 483 | + $response = wp_remote_post( | |
| 484 | + $verify_url, | |
| 485 | + [ | |
| 486 | + 'body' => 'cmd=_notify-validate&' . $raw_post, | |
| 487 | + 'timeout' => 30, | |
| 488 | + 'sslverify' => true, | |
| 489 | + 'headers' => [ 'Content-Type' => 'application/x-www-form-urlencoded' ], | |
| 490 | + ] | |
| 491 | + ); | |
| 492 | + | |
| 493 | + // Always respond 200 to PayPal to prevent retries, but only act on VERIFIED. | |
| 494 | + if ( is_wp_error( $response ) || wp_remote_retrieve_body( $response ) !== 'VERIFIED' ) { | |
| 495 | + status_header( 200 ); | |
| 496 | + exit; | |
| 497 | + } | |
| 498 | + | |
| 499 | + self::apply_paypal_ipn( $data ); | |
| 500 | + | |
| 501 | + status_header( 200 ); | |
| 502 | + exit; | |
| 503 | + } | |
| 504 | + | |
| 505 | + /** | |
| 506 | + * Whether a notification's PayPal environment matches the order's. | |
| 507 | + * | |
| 508 | + * Orders since 2.4.1 record `paypal_mode` (`live` | `sandbox`) when created, and must match | |
| 509 | + * exactly. An older order has no record, so a live notification is accepted (a real payment | |
| 510 | + * must never be stranded) and a sandbox one only while the site's PayPal setting is not live. | |
| 511 | + * | |
| 512 | + * @since 2.4.1 | |
| 513 | + * | |
| 514 | + * @param mixed $form_info The order's `form_fields_info`, unserialized. | |
| 515 | + * @param string $ipn_mode `live` or `sandbox` (from the notification's `test_ipn`). | |
| 516 | + * @return bool | |
| 517 | + */ | |
| 518 | + public static function paypal_ipn_mode_matches( $form_info, $ipn_mode ) { | |
| 519 | + $order_mode = is_array( $form_info ) && isset( $form_info['paypal_mode'] ) ? $form_info['paypal_mode'] : ''; | |
| 520 | + | |
| 521 | + if ( 'live' === $order_mode || 'sandbox' === $order_mode ) { | |
| 522 | + return $order_mode === $ipn_mode; | |
| 523 | + } | |
| 524 | + | |
| 525 | + if ( 'live' === $ipn_mode ) { | |
| 526 | + return true; | |
| 527 | + } | |
| 528 | + | |
| 529 | + return 'yes' !== DB::get_settings( 'better_payment_settings_payment_paypal_live_mode' ); | |
| 530 | + } | |
| 531 | + | |
| 532 | + /** | |
| 533 | + * Apply a PayPal IPN to the order it names. | |
| 534 | + * | |
| 535 | + * Call ONLY with a notification handle_paypal_ipn() posted back and PayPal answered VERIFIED — | |
| 536 | + * nothing here authenticates the data. Changes nothing unless the notification is a | |
| 537 | + * `Completed` payment for a pending order (`status IS NULL`) paid to the merchant's address, | |
| 538 | + * for at least the order's amount, in its currency, under a transaction id no order holds. | |
| 539 | + * Then the row is claimed with one update (`WHERE id = … AND status IS NULL`): PayPal redelivers | |
| 540 | + * a notification it saw no 200 for and can deliver one twice at once, and only the delivery that | |
| 541 | + * wins the claim sends the receipts. | |
| 542 | + * | |
| 543 | + * @since 2.4.0 | |
| 544 | + * | |
| 545 | + * @param array $data The notification's fields. | |
| 546 | + * @return string 'confirmed' | 'ignored' | 'unmatched' | 'refused' | 'duplicate' | |
| 547 | + */ | |
| 548 | + public static function apply_paypal_ipn( array $data ) { | |
| 549 | + $data = array_map( 'sanitize_text_field', $data ); | |
| 550 | + | |
| 551 | + if ( empty( $data['payment_status'] ) || empty( $data['item_number'] ) || $data['payment_status'] !== 'Completed' ) { | |
| 552 | + return 'ignored'; | |
| 553 | + } | |
| 554 | + | |
| 555 | + global $wpdb; | |
| 556 | + $table = "{$wpdb->prefix}better_payment"; | |
| 557 | + $results = $wpdb->get_row( | |
| 558 | + $wpdb->prepare( | |
| 559 | + "SELECT id, amount, currency, form_fields_info, referer FROM $table WHERE order_id = %s AND status IS NULL LIMIT 1", | |
| 560 | + $data['item_number'] | |
| 561 | + ) | |
| 562 | + ); | |
| 563 | + | |
| 564 | + if ( empty( $results->id ) ) { | |
| 565 | + return 'unmatched'; | |
| 566 | + } | |
| 567 | + | |
| 568 | + // Validate receiver_email matches the merchant's configured PayPal email. | |
| 569 | + $form_info = maybe_unserialize( $results->form_fields_info ); | |
| 570 | + $expected_email = ! empty( $form_info['paypal_business_email'] ) ? strtolower( $form_info['paypal_business_email'] ) : ''; | |
| 571 | + $receiver = ! empty( $data['receiver_email'] ) ? strtolower( $data['receiver_email'] ) : ( ! empty( $data['business'] ) ? strtolower( $data['business'] ) : '' ); | |
| 572 | + | |
| 573 | + if ( $expected_email && $receiver && $expected_email !== $receiver ) { | |
| 574 | + return 'refused'; | |
| 575 | + } | |
| 576 | + | |
| 577 | + // A sandbox notification is VERIFIED by PayPal's sandbox, which pays in play money to an | |
| 578 | + // account anyone can create under any address. It must never confirm a live order. | |
| 579 | + $ipn_mode = ! empty( $data['test_ipn'] ) ? 'sandbox' : 'live'; | |
| 580 | + | |
| 581 | + if ( ! self::paypal_ipn_mode_matches( $form_info, $ipn_mode ) ) { | |
| 582 | + return 'refused'; | |
| 583 | + } | |
| 584 | + | |
| 585 | + // Validate the paid amount is not less than the expected order amount. | |
| 586 | + $paid_amount = ! empty( $data['mc_gross'] ) ? floatval( $data['mc_gross'] ) : 0; | |
| 587 | + if ( $paid_amount < floatval( $results->amount ) ) { | |
| 588 | + return 'refused'; | |
| 589 | + } | |
| 590 | + | |
| 591 | + // Validate currency matches. | |
| 592 | + $paid_currency = ! empty( $data['mc_currency'] ) ? strtoupper( $data['mc_currency'] ) : ''; | |
| 593 | + if ( $paid_currency && $results->currency && strtoupper( $results->currency ) !== $paid_currency ) { | |
| 594 | + return 'refused'; | |
| 595 | + } | |
| 596 | + | |
| 597 | + $txn_id = ! empty( $data['txn_id'] ) ? $data['txn_id'] : ''; | |
| 598 | + | |
| 599 | + // Prevent txn_id replay: reject if this transaction ID was already recorded. | |
| 600 | + if ( $txn_id ) { | |
| 601 | + $existing = $wpdb->get_var( | |
| 602 | + $wpdb->prepare( | |
| 603 | + "SELECT id FROM $table WHERE transaction_id = %s LIMIT 1", | |
| 604 | + $txn_id | |
| 605 | + ) | |
| 252 | 606 | ); |
| 253 | - | |
| 254 | - if ( !empty( $results->id ) ) { | |
| 255 | - $updated = $wpdb->update( | |
| 256 | - $table, | |
| 257 | - array( | |
| 258 | - 'transaction_id' => sanitize_text_field( $data[ 'txn_id' ] ), | |
| 259 | - 'status' => sanitize_text_field( $data[ 'payment_status' ] ), | |
| 260 | - 'email' => sanitize_email( $data[ 'payer_email' ] ), | |
| 261 | - 'customer_info' => maybe_serialize( $data ), | |
| 262 | - ), | |
| 263 | - array( 'ID' => $results->id ) | |
| 264 | - ); | |
| 265 | - | |
| 266 | - if ( false !== $updated ) { | |
| 267 | - //Send email notification | |
| 268 | - if ( | |
| 269 | - ( isset($settings[ 'better_payment_form_email_enable' ]) && $settings[ 'better_payment_form_email_enable' ] == 'yes' ) | |
| 270 | - || ( $results->referer === 'elementor-form' ) | |
| 271 | - ) { | |
| 272 | - $is_elementor_form = ! empty( $results->referer ) && $results->referer === 'elementor-form' ? 1 : 0; | |
| 273 | - self::better_email_notification(sanitize_text_field( $data[ 'txn_id' ] ), sanitize_email( $data[ 'payer_email' ] ), $settings, 'PayPal', $results->form_fields_info, $is_elementor_form); | |
| 274 | - } | |
| 607 | + if ( $existing ) { | |
| 608 | + return 'duplicate'; | |
| 609 | + } | |
| 610 | + } | |
| 275 | 611 | |
| 276 | - do_action( 'better_payment/payment/success', sanitize_text_field( $_REQUEST['item_number'] ?? '' ), (int) $results->id, 'paypal' ); | |
| 277 | - do_action( 'better_payment/payment_confirmed', (int) $results->id ); | |
| 612 | + $payer_email = ! empty( $data['payer_email'] ) ? sanitize_email( $data['payer_email'] ) : ''; | |
| 278 | 613 | |
| 279 | - return $frontend_data; | |
| 280 | - } | |
| 281 | - } | |
| 282 | - }else if($data['better_payment_paypal_status'] === 'success'){ | |
| 283 | - return ( isset($data[ 'txn_id' ]) && !empty( sanitize_text_field( $data[ 'txn_id' ] ) ) ) ? $frontend_data : __('Payment under processing!', 'better-payment'); | |
| 614 | + $claimed = $wpdb->update( | |
| 615 | + $table, | |
| 616 | + [ | |
| 617 | + 'transaction_id' => $txn_id, | |
| 618 | + 'status' => $data['payment_status'], | |
| 619 | + 'email' => $payer_email, | |
| 620 | + 'customer_info' => maybe_serialize( $data ), | |
| 621 | + ], | |
| 622 | + [ | |
| 623 | + 'id' => (int) $results->id, | |
| 624 | + 'status' => null, | |
| 625 | + ] | |
| 626 | + ); | |
| 627 | + | |
| 628 | + if ( 1 !== (int) $claimed ) { | |
| 629 | + // A concurrent delivery of this notification confirmed the order first. | |
| 630 | + return 'duplicate'; | |
| 284 | 631 | } |
| 285 | - return false; | |
| 632 | + | |
| 633 | + $is_elementor_form = ! empty( $results->referer ) && $results->referer === 'elementor-form' ? 1 : 0; | |
| 634 | + | |
| 635 | + // Rebuild the e-mail settings persisted at payment-create time. IPN runs | |
| 636 | + // server-to-server with no live widget context, so without this the body | |
| 637 | + // would collapse to the bare transaction id (every section flag defaults | |
| 638 | + // to 0). Legacy / pending rows created before this fix fall back to []. | |
| 639 | + $email_settings = ! empty( $form_info['email_settings'] ) && is_array( $form_info['email_settings'] ) | |
| 640 | + ? $form_info['email_settings'] | |
| 641 | + : []; | |
| 642 | + | |
| 643 | + // Mirror the Stripe / Paystack gate: honour the form's "Send Email" toggle, | |
| 644 | + // and always e-mail for Elementor Pro Form submissions. | |
| 645 | + if ( | |
| 646 | + ( isset( $email_settings['better_payment_form_email_enable'] ) && 'yes' === $email_settings['better_payment_form_email_enable'] ) | |
| 647 | + || $is_elementor_form | |
| 648 | + ) { | |
| 649 | + self::better_email_notification( $txn_id, $payer_email, $email_settings, 'PayPal', $results->form_fields_info, $is_elementor_form ); | |
| 650 | + } | |
| 651 | + | |
| 652 | + return 'confirmed'; | |
| 286 | 653 | } |
| 287 | 654 | |
| 288 | 655 | /** |
| 289 | - * Stripe payment success | |
| 290 | - * | |
| 656 | + * Stripe payment success — the return page. | |
| 657 | + * | |
| 658 | + * Reads the transaction id from the request, EXCEPT when the caller | |
| 659 | + * supplies `better_payment_stripe_id` in $settings. A caller that has | |
| 660 | + * already authorized the id against server-side state (the WooCommerce | |
| 661 | + * return handler matches it to the order's own `_bp_payment_id`) must | |
| 662 | + * pass it explicitly: a caller-side guard that reads $_GET cannot bind | |
| 663 | + * this lookup, because PHP's default request_order=GP lets a POST body | |
| 664 | + * overwrite the query string and the two would then see different ids. | |
| 665 | + * | |
| 666 | + * A pending order is verified against Stripe and confirmed through | |
| 667 | + * confirm_stripe_session(). An order an earlier return (a reload, a second | |
| 668 | + * tab) already confirmed renders the success page again without asking | |
| 669 | + * Stripe or re-running anything. See stripe_settled_return(). | |
| 670 | + * | |
| 291 | 671 | * @since 0.0.1 |
| 672 | + * | |
| 673 | + * @param array $settings Form settings; `better_payment_stripe_secret_key` is required to verify. | |
| 674 | + * @return array|false|\WP_Error Success data, false for no order to show, or why nothing was confirmed. | |
| 292 | 675 | */ |
| 293 | 676 | public static function stripe_payment_success( $settings = [] ) { |
| 294 | 677 | |
| 295 | 678 | $data = $_REQUEST; |
| 296 | 679 | |
| 297 | - if ( !empty( $data[ 'better_payment_stripe_id' ] ) ) { | |
| 298 | - global $wpdb; | |
| 299 | - $table = "{$wpdb->prefix}better_payment"; | |
| 300 | - $results = $wpdb->get_row( | |
| 301 | - $wpdb->prepare( "SELECT id, obj_id, amount, transaction_id, currency, form_fields_info, referer FROM $table WHERE order_id=%s and status = 'unpaid' limit 1", sanitize_text_field( $data[ 'better_payment_stripe_id' ] ) ) | |
| 302 | - ); | |
| 303 | - | |
| 304 | - if ( !empty( $results->obj_id ) && !empty( $settings[ 'better_payment_stripe_secret_key' ] ) ) { | |
| 305 | - $header = array( | |
| 680 | + if ( ! empty( $settings[ 'better_payment_stripe_id' ] ) ) { | |
| 681 | + $data[ 'better_payment_stripe_id' ] = $settings[ 'better_payment_stripe_id' ]; | |
| 682 | + } | |
| 683 | + | |
| 684 | + if ( empty( $data['better_payment_stripe_id'] ) || ! is_scalar( $data['better_payment_stripe_id'] ) ) { | |
| 685 | + return false; | |
| 686 | + } | |
| 687 | + | |
| 688 | + $order_id = sanitize_text_field( (string) $data['better_payment_stripe_id'] ); | |
| 689 | + $row = self::get_stripe_row( 'order_id', $order_id ); | |
| 690 | + | |
| 691 | + if ( empty( $row->id ) ) { | |
| 692 | + return false; | |
| 693 | + } | |
| 694 | + | |
| 695 | + if ( self::STRIPE_PENDING_STATUS !== $row->status ) { | |
| 696 | + return self::stripe_settled_return( $row, $order_id ); | |
| 697 | + } | |
| 698 | + | |
| 699 | + if ( empty( $row->obj_id ) || empty( $settings[ 'better_payment_stripe_secret_key' ] ) ) { | |
| 700 | + return false; | |
| 701 | + } | |
| 702 | + | |
| 703 | + // Subscription and split-payment orders are Pro's: Pro (or, for an older Pro, Lite's | |
| 704 | + // transitional copy) adds the subscription expansion and the recurring / split flags. | |
| 705 | + $verification = self::stripe_verification_for( $row ); | |
| 706 | + | |
| 707 | + $response = wp_safe_remote_post( | |
| 708 | + 'https://api.stripe.com/v1/checkout/sessions/' . rawurlencode( (string) $row->obj_id ), | |
| 709 | + array( | |
| 710 | + 'method' => 'GET', | |
| 711 | + 'headers' => array( | |
| 306 | 712 | 'Authorization' => 'Basic ' . base64_encode( sanitize_text_field( $settings[ 'better_payment_stripe_secret_key' ] ) . ':' ), |
| 307 | 713 | 'Stripe-Version' => '2019-05-16', |
| 308 | - ); | |
| 714 | + ), | |
| 715 | + 'timeout' => 70, | |
| 716 | + 'body' => array( | |
| 717 | + 'expand' => $verification['expand'], | |
| 718 | + ), | |
| 719 | + ) | |
| 720 | + ); | |
| 309 | 721 | |
| 310 | - $request = [ | |
| 311 | - 'expand' => [ | |
| 312 | - 'subscription.latest_invoice.payment_intent', | |
| 313 | - 'payment_intent' | |
| 314 | - ] | |
| 315 | - ]; | |
| 316 | - $form_fields_info = maybe_unserialize($results->form_fields_info); | |
| 317 | - $is_payment_recurring = ! empty( $form_fields_info['mode'] ) && 'subscription' === $form_fields_info['mode']; | |
| 318 | - $is_payment_split_payment = ( $is_payment_recurring ) && ( ! empty( $form_fields_info['is_payment_split_payment'] ) && 1 === intval( $form_fields_info['is_payment_split_payment'] ) ); | |
| 319 | - | |
| 320 | - $action_data = [ | |
| 321 | - 'form_fields_info' => $form_fields_info, | |
| 322 | - 'is_payment_recurring' => $is_payment_recurring, | |
| 323 | - 'is_payment_split_payment' => $is_payment_split_payment, | |
| 324 | - 'checkout_session_id' => $results->obj_id, | |
| 325 | - ]; | |
| 722 | + if ( is_wp_error( $response ) || empty( $response[ 'body' ] ) ) { | |
| 723 | + return new \WP_Error( 'stripe_error', __( 'There was a problem connecting to the Stripe API endpoint.', 'better-payment' ) ); | |
| 724 | + } | |
| 326 | 725 | |
| 327 | - $response = wp_safe_remote_post( | |
| 328 | - 'https://api.stripe.com/v1/checkout/sessions/' . $results->obj_id, | |
| 329 | - array( | |
| 330 | - 'method' => 'GET', | |
| 331 | - 'headers' => $header, | |
| 332 | - 'timeout' => 70, | |
| 333 | - 'body' => $request | |
| 334 | - ) | |
| 335 | - ); | |
| 726 | + $session = json_decode( $response[ 'body' ] ); | |
| 336 | 727 | |
| 337 | - if ( is_wp_error( $response ) || empty( $response[ 'body' ] ) ) { | |
| 338 | - return new \WP_Error( 'stripe_error', __( 'There was a problem connecting to the Stripe API endpoint.', 'better-payment' ) ); | |
| 339 | - } | |
| 728 | + if ( ! is_object( $session ) || isset( $session->error ) ) { | |
| 729 | + return new \WP_Error( 'stripe_error', __( 'There was a problem connecting to the Stripe API endpoint.', 'better-payment' ) ); | |
| 730 | + } | |
| 340 | 731 | |
| 341 | - $response = json_decode( $response[ 'body' ] ); | |
| 342 | - $transaction_id = ! empty( $results->transaction_id ) ? sanitize_text_field( $results->transaction_id ) : ''; | |
| 343 | - | |
| 344 | - if ( $is_payment_recurring && ! empty( $response->subscription->id ) ) { | |
| 345 | - $transaction_id = sanitize_text_field( $response->subscription->id ); | |
| 346 | - $subscription_obj = $response->subscription; | |
| 347 | - $form_fields_info['subscription_id'] = sanitize_text_field( $transaction_id ); | |
| 348 | - $form_fields_info['subscription_customer_id'] = ! empty( $response->customer ) ? sanitize_text_field( $response->customer ) : ''; | |
| 349 | - $form_fields_info['subscription_plan_id'] = ! empty( $subscription_obj->items->data[0]->plan->id ) ? sanitize_text_field( $subscription_obj->items->data[0]->plan->id ) : ''; | |
| 350 | - $form_fields_info['subscription_interval'] = ! empty( $subscription_obj->items->data[0]->plan->interval_count ) ? intval( $subscription_obj->items->data[0]->plan->interval_count ) : ''; | |
| 351 | - $form_fields_info['subscription_interval'] .= ! empty( $subscription_obj->items->data[0]->plan->interval ) ? ' ' . sanitize_text_field( $subscription_obj->items->data[0]->plan->interval ) : $form_fields_info['subscription_interval']; | |
| 352 | - $form_fields_info['subscription_current_period_start'] = ! empty( $subscription_obj->current_period_start ) ? intval( $subscription_obj->current_period_start ) : 0; | |
| 353 | - $form_fields_info['subscription_current_period_end'] = ! empty( $subscription_obj->current_period_end ) ? intval( $subscription_obj->current_period_end ) : 0; | |
| 354 | - $form_fields_info['subscription_status'] = sanitize_text_field( $response->status ); | |
| 355 | - $form_fields_info['subscription_created_date'] = sanitize_text_field( $subscription_obj->created ); | |
| 356 | - } | |
| 732 | + return self::confirm_stripe_session( $row, $session, $settings, $verification ); | |
| 733 | + } | |
| 357 | 734 | |
| 358 | - if ( isset( $response->error->message ) ) { | |
| 359 | - return new \WP_Error( 'stripe_error', __( 'There was a problem connecting to the Stripe API endpoint.', 'better-payment' ) ); | |
| 360 | - } | |
| 735 | + /** | |
| 736 | + * Confirm a pending Stripe order from its verified Checkout Session. | |
| 737 | + * | |
| 738 | + * The one place a Stripe payment is confirmed: the return page. In order: | |
| 739 | + * | |
| 740 | + * 1. The session must be this row's (when it carries an id), must not be a setup-mode session | |
| 741 | + * (the WooCommerce free-trial checkout, which OrderHandler confirms by its SetupIntent), and | |
| 742 | + * must be paid (stripe_session_is_paid()). Nothing below runs for a session that is not: an | |
| 743 | + * abandoned checkout, or a completed one whose delayed payment has not cleared (a later | |
| 744 | + * return confirms it once it has). | |
| 745 | + * 2. Pro's verified-session step, the customer e-mail and the charged amount are computed. The | |
| 746 | + * amount is `amount_total / 100` whenever Stripe reports one — 0 for a 100%-off promotion | |
| 747 | + * code or a trial — and the form's amount is kept as `original_amount` when they differ. | |
| 748 | + * Pro's step must stay free of side effects: a request that loses the claim has asked it too. | |
| 749 | + * 3. The row is claimed with ONE update, `WHERE id = … AND status = 'unpaid'`, which writes the | |
| 750 | + * confirmed status, amount, e-mail, session and Pro's details together. However many returns | |
| 751 | + * race for a session (a reload, a second tab), exactly one update matches. | |
| 752 | + * 4. Only that winner fires `better_payment/stripe_payment/success` and | |
| 753 | + * `better_payment/payment_confirmed` and sends the receipts. A caller that lost the claim to a | |
| 754 | + * confirmation gets the same success data back, so the visitor still sees the thank-you page. | |
| 755 | + * | |
| 756 | + * The confirmed status is always `paid`, including for a session Stripe reports as | |
| 757 | + * `no_payment_required`: campaign totals and donor counts, Pro's Donors Wall and the WooCommerce | |
| 758 | + * bridge count `paid`, and a $0 donation is still a donation. Stripe's own value stays in | |
| 759 | + * `customer_info`. | |
| 760 | + * | |
| 761 | + * @since 2.4.0 | |
| 762 | + * | |
| 763 | + * @param object|null $row Pending row from get_stripe_row() (null is refused). | |
| 764 | + * @param mixed $session Decoded Checkout Session, with $verification['expand'] expanded where Pro needs it (anything but an object is refused). | |
| 765 | + * @param array|null $settings Form settings for the receipts; null or [] for none. | |
| 766 | + * @param array|null $verification stripe_verification_for( $row ); computed when null. | |
| 767 | + * @return array|\WP_Error Success data for the thank-you page, or why nothing was confirmed. | |
| 768 | + */ | |
| 769 | + public static function confirm_stripe_session( $row, $session, $settings = null, $verification = null ) { | |
| 770 | + global $wpdb; | |
| 361 | 771 | |
| 362 | - $customer_email = $is_payment_recurring && ( ! empty( $response->customer_email ) ) ? sanitize_email( $response->customer_email ) : ''; | |
| 363 | - $customer_email_optional = ''; | |
| 364 | - if ( | |
| 365 | - isset( $response->payment_intent ) && | |
| 366 | - isset( $response->payment_intent->charges ) && | |
| 367 | - isset( $response->payment_intent->charges->data ) && | |
| 368 | - ! empty( $response->payment_intent->charges->data ) | |
| 369 | - ) { | |
| 370 | - $charge = current( $response->payment_intent->charges->data ); | |
| 371 | - | |
| 372 | - if ( | |
| 373 | - isset( $charge->billing_details ) && | |
| 374 | - isset( $charge->billing_details->email ) && | |
| 375 | - ! empty( $charge->billing_details->email ) | |
| 376 | - ) { | |
| 377 | - $customer_email_optional = sanitize_email( $charge->billing_details->email ); | |
| 378 | - } | |
| 379 | - } | |
| 772 | + $table = "{$wpdb->prefix}better_payment"; | |
| 773 | + $settings = is_array( $settings ) ? $settings : []; | |
| 380 | 774 | |
| 381 | - $form_fields_info['is_coupon_applied'] = 0; | |
| 382 | - $exact_paid_amount = ! empty( $response->amount_total ) ? floatval( $response->amount_total ) : $results->amount * 100; | |
| 383 | - $exact_paid_amount = $exact_paid_amount / 100; | |
| 384 | - $form_fields_info['exact_paid_amount'] = $exact_paid_amount; | |
| 385 | - if( (int)$exact_paid_amount != (int)$results->amount ) { | |
| 386 | - $form_fields_info['paid_amount_diff'] = $results->amount - $exact_paid_amount; | |
| 387 | - $form_fields_info['is_coupon_applied'] = 1; | |
| 388 | - } | |
| 389 | - | |
| 390 | - $updated = $wpdb->update( | |
| 391 | - $table, | |
| 392 | - array( | |
| 393 | - 'amount' => $exact_paid_amount, | |
| 394 | - 'status' => sanitize_text_field( $response->payment_status ), | |
| 395 | - 'email' => $is_payment_recurring ? $customer_email : $customer_email_optional, | |
| 396 | - 'customer_info' => maybe_serialize( $response ), | |
| 397 | - 'form_fields_info' => maybe_serialize( $form_fields_info ), | |
| 398 | - 'transaction_id' => $transaction_id, | |
| 399 | - ), | |
| 400 | - array( 'ID' => $results->id ) | |
| 401 | - ); | |
| 775 | + if ( ! is_object( $row ) || empty( $row->id ) || ! is_object( $session ) ) { | |
| 776 | + return new \WP_Error( 'bp_stripe_confirm_invalid', __( 'There is no Stripe payment to confirm.', 'better-payment' ) ); | |
| 777 | + } | |
| 402 | 778 | |
| 403 | - do_action('better_payment/stripe_payment/success', $action_data); | |
| 404 | - do_action( 'better_payment/payment_confirmed', (int) $results->id ); | |
| 405 | - | |
| 406 | - $frontend_data = [ | |
| 407 | - 'amount' => $exact_paid_amount, | |
| 408 | - 'email' => $is_payment_recurring ? $customer_email : $customer_email_optional, | |
| 409 | - 'transaction_id' => $transaction_id, | |
| 410 | - 'currency' => ! empty( $results->currency ) ? $results->currency : __( 'USD', 'better-payment' ), | |
| 411 | - 'method' => 'stripe', | |
| 412 | - 'is_payment_recurring' => $is_payment_recurring, | |
| 413 | - 'is_payment_split_payment' => $is_payment_split_payment, | |
| 414 | - 'is_payment_mixed' => ! empty( $form_fields_info['is_payment_mixed'] ) ? intval( $form_fields_info['is_payment_mixed'] ) : 0, | |
| 415 | - 'mixed_payment_selected_type' => ! empty( $form_fields_info['mixed_payment_selected_type'] ) ? sanitize_text_field( $form_fields_info['mixed_payment_selected_type'] ) : '', | |
| 416 | - ]; | |
| 779 | + if ( isset( $session->id ) && ( empty( $row->obj_id ) || (string) $session->id !== (string) $row->obj_id ) ) { | |
| 780 | + return new \WP_Error( 'bp_stripe_session_mismatch', __( 'Stripe did not confirm a payment for this checkout.', 'better-payment' ) ); | |
| 781 | + } | |
| 417 | 782 | |
| 418 | - if ( $is_payment_split_payment ) { | |
| 419 | - $total_amount = ! empty( $form_fields_info['split_payment_total_amount'] ) | |
| 420 | - ? sanitize_text_field( $form_fields_info['split_payment_total_amount'] ) | |
| 421 | - : 0; | |
| 422 | - $total_installment = ! empty( $form_fields_info['split_payment_installment_iteration'] ) | |
| 423 | - ? sanitize_text_field( $form_fields_info['split_payment_installment_iteration'] ) | |
| 424 | - : 1; | |
| 783 | + if ( isset( $session->mode ) && 'setup' === $session->mode ) { | |
| 784 | + return new \WP_Error( 'bp_stripe_setup_session', __( 'This checkout saves a payment method and is confirmed by its order.', 'better-payment' ) ); | |
| 785 | + } | |
| 425 | 786 | |
| 426 | - // $frontend_data['amount'] = floatval( $total_amount / $total_installment ); | |
| 427 | - $frontend_data['total_amount'] = $total_amount; | |
| 428 | - } | |
| 787 | + $stored_fields = isset( $row->form_fields_info ) ? maybe_unserialize( $row->form_fields_info ) : []; | |
| 788 | + $form_fields_info = is_array( $stored_fields ) ? $stored_fields : []; | |
| 429 | 789 | |
| 430 | - if ( false !== $updated ) { | |
| 431 | - //Send email notification | |
| 432 | - $better_customer_email = $is_payment_recurring ? $customer_email : $customer_email_optional; | |
| 433 | - | |
| 434 | - if ( | |
| 435 | - (isset($settings[ 'better_payment_form_email_enable' ]) && $settings[ 'better_payment_form_email_enable' ] == 'yes' ) | |
| 436 | - || ( $results->referer === 'elementor-form' ) | |
| 437 | - ) { | |
| 438 | - $is_elementor_form = ! empty( $results->referer ) && $results->referer === 'elementor-form' ? 1 : 0; | |
| 439 | - self::better_email_notification($transaction_id, $better_customer_email, $settings, 'Stripe', $results->form_fields_info, $is_elementor_form); | |
| 440 | - } | |
| 790 | + if ( ! is_array( $verification ) || ! isset( $verification['expand'] ) ) { | |
| 791 | + $verification = self::stripe_verification_for( $row ); | |
| 792 | + } | |
| 441 | 793 | |
| 442 | - return $frontend_data; | |
| 443 | - } | |
| 794 | + // Stripe returns the session whether or not anyone paid for it. Nothing below — | |
| 795 | + // Pro's verified-session step, the row update, the confirmation hooks, the e-mails, | |
| 796 | + // the success page — may run for a session that is not complete and paid. The row | |
| 797 | + // stays 'unpaid', so a later return can still confirm it. | |
| 798 | + if ( ! self::stripe_session_is_paid( $session ) ) { | |
| 799 | + return self::stripe_unpaid_session_error( $session ); | |
| 800 | + } | |
| 801 | + | |
| 802 | + $action_data = [ | |
| 803 | + 'form_fields_info' => $form_fields_info, | |
| 804 | + 'is_payment_recurring' => $verification['is_payment_recurring'], | |
| 805 | + 'is_payment_split_payment' => $verification['is_payment_split_payment'], | |
| 806 | + 'checkout_session_id' => $row->obj_id, | |
| 807 | + ]; | |
| 808 | + | |
| 809 | + // A subscription's id, customer e-mail and details, and the thank-you page's | |
| 810 | + // recurring / split flags, are Pro's to add. | |
| 811 | + $verified = PaymentEntitlement::stripe_verified_session( | |
| 812 | + [ | |
| 813 | + 'form_fields_info' => $form_fields_info, | |
| 814 | + 'transaction_id' => ! empty( $row->transaction_id ) ? sanitize_text_field( $row->transaction_id ) : '', | |
| 815 | + 'email' => self::stripe_session_customer_email( $session ), | |
| 816 | + 'frontend_data' => [], | |
| 817 | + ], | |
| 818 | + $session, | |
| 819 | + $verification | |
| 820 | + ); | |
| 821 | + | |
| 822 | + $confirmed_fields = is_array( $verified['form_fields_info'] ) ? $verified['form_fields_info'] : $form_fields_info; | |
| 823 | + $transaction_id = $verified['transaction_id']; | |
| 824 | + $customer_email = $verified['email']; | |
| 825 | + $amounts = self::stripe_paid_amounts( $session, isset( $row->amount ) ? $row->amount : 0, $confirmed_fields ); | |
| 826 | + $paid_amount = $amounts['amount']; | |
| 827 | + $confirmed_fields = $amounts['form_fields_info']; | |
| 828 | + | |
| 829 | + $claimed = $wpdb->update( | |
| 830 | + $table, | |
| 831 | + array( | |
| 832 | + 'amount' => $paid_amount, | |
| 833 | + 'status' => self::STRIPE_CONFIRMED_STATUS, | |
| 834 | + 'email' => $customer_email, | |
| 835 | + 'customer_info' => maybe_serialize( $session ), | |
| 836 | + 'form_fields_info' => maybe_serialize( $confirmed_fields ), | |
| 837 | + 'transaction_id' => $transaction_id, | |
| 838 | + ), | |
| 839 | + array( | |
| 840 | + 'id' => (int) $row->id, | |
| 841 | + 'status' => self::STRIPE_PENDING_STATUS, | |
| 842 | + ) | |
| 843 | + ); | |
| 844 | + | |
| 845 | + if ( 1 !== (int) $claimed ) { | |
| 846 | + // Another return (a reload, a second tab) got here first (or the write failed). | |
| 847 | + // Whatever it recorded is the outcome; nothing is fired or sent twice. | |
| 848 | + $current = self::get_stripe_row( 'id', (int) $row->id ); | |
| 849 | + | |
| 850 | + if ( ! empty( $current->id ) && self::STRIPE_CONFIRMED_STATUS === $current->status ) { | |
| 851 | + return self::stripe_frontend_data( $current, $verified['frontend_data'] ); | |
| 444 | 852 | } |
| 853 | + | |
| 854 | + return new \WP_Error( 'bp_stripe_confirm_failed', __( 'The payment could not be recorded. Please reload this page.', 'better-payment' ) ); | |
| 445 | 855 | } |
| 446 | - return false; | |
| 856 | + | |
| 857 | + do_action('better_payment/stripe_payment/success', $action_data); | |
| 858 | + do_action( 'better_payment/payment_confirmed', (int) $row->id ); | |
| 859 | + | |
| 860 | + $frontend_data = [ | |
| 861 | + 'amount' => $paid_amount, | |
| 862 | + 'email' => $customer_email, | |
| 863 | + 'transaction_id' => $transaction_id, | |
| 864 | + 'currency' => ! empty( $row->currency ) ? $row->currency : __( 'USD', 'better-payment' ), | |
| 865 | + 'method' => 'stripe', | |
| 866 | + ] + $verified['frontend_data']; | |
| 867 | + | |
| 868 | + //Send email notification | |
| 869 | + if ( | |
| 870 | + ( isset( $settings[ 'better_payment_form_email_enable' ] ) && 'yes' === $settings[ 'better_payment_form_email_enable' ] ) | |
| 871 | + || ( isset( $row->referer ) && 'elementor-form' === $row->referer ) | |
| 872 | + ) { | |
| 873 | + $is_elementor_form = isset( $row->referer ) && 'elementor-form' === $row->referer ? 1 : 0; | |
| 874 | + | |
| 875 | + self::better_email_notification( $transaction_id, $customer_email, $settings, 'Stripe', $row->form_fields_info, $is_elementor_form ); | |
| 876 | + } | |
| 877 | + | |
| 878 | + return $frontend_data; | |
| 447 | 879 | } |
| 448 | - | |
| 880 | + | |
| 449 | 881 | /** |
| 882 | + * A Stripe transaction row, looked up by `id`, `order_id` or `obj_id` (the Checkout Session id). | |
| 883 | + * | |
| 884 | + * @since 2.4.0 | |
| 885 | + * | |
| 886 | + * @param string $field 'id' | 'order_id' | 'obj_id'. | |
| 887 | + * @param mixed $value Value to match; anything but a non-empty scalar matches nothing. | |
| 888 | + * @return object|null | |
| 889 | + */ | |
| 890 | + public static function get_stripe_row( $field, $value ) { | |
| 891 | + global $wpdb; | |
| 892 | + | |
| 893 | + if ( ! in_array( $field, [ 'id', 'order_id', 'obj_id' ], true ) || ! is_scalar( $value ) || '' === (string) $value ) { | |
| 894 | + return null; | |
| 895 | + } | |
| 896 | + | |
| 897 | + $table = "{$wpdb->prefix}better_payment"; | |
| 898 | + $format = 'id' === $field ? '%d' : '%s'; | |
| 899 | + | |
| 900 | + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table from $wpdb->prefix, column and format from the whitelist above. | |
| 901 | + $row = $wpdb->get_row( $wpdb->prepare( "SELECT id, order_id, obj_id, amount, transaction_id, currency, email, status, source, referer, form_fields_info, payment_date FROM $table WHERE $field = $format LIMIT 1", $value ) ); | |
| 902 | + | |
| 903 | + return is_object( $row ) ? $row : null; | |
| 904 | + } | |
| 905 | + | |
| 906 | + /** | |
| 907 | + * How a row's Checkout Session is retrieved and whether it is a subscription — Pro's answer. | |
| 908 | + * | |
| 909 | + * @since 2.4.0 | |
| 910 | + * | |
| 911 | + * @param object|null $row Transaction row; null reads as a row with no stored fields. | |
| 912 | + * @return array `expand`, `is_payment_recurring`, `is_payment_split_payment`. | |
| 913 | + */ | |
| 914 | + public static function stripe_verification_for( $row ) { | |
| 915 | + $form_fields_info = is_object( $row ) && isset( $row->form_fields_info ) ? maybe_unserialize( $row->form_fields_info ) : []; | |
| 916 | + | |
| 917 | + return PaymentEntitlement::stripe_verification_args( | |
| 918 | + [ | |
| 919 | + 'expand' => [ 'payment_intent' ], | |
| 920 | + 'is_payment_recurring' => false, | |
| 921 | + 'is_payment_split_payment' => false, | |
| 922 | + ], | |
| 923 | + is_array( $form_fields_info ) ? $form_fields_info : [] | |
| 924 | + ); | |
| 925 | + } | |
| 926 | + | |
| 927 | + /** | |
| 928 | + * What a verified Checkout Session charged, and what the transaction's fields record about it. | |
| 929 | + * | |
| 930 | + * The charged amount is `amount_total / 100` whenever Stripe reports a numeric total — 0 | |
| 931 | + * included: a 100%-off promotion code or a trial charges nothing, and a row claiming otherwise | |
| 932 | + * would add money nobody paid to a campaign's total. It used to fall back to the form's amount | |
| 933 | + * whenever the total was 0. Only a session with no total at all keeps the row's amount. | |
| 934 | + * | |
| 935 | + * `exact_paid_amount` is always recorded. When the charge differs from the amount the order was | |
| 936 | + * created for, that amount is kept as `original_amount`; `is_coupon_applied` and | |
| 937 | + * `paid_amount_diff` (read by the transaction details and the receipt) are set only when a | |
| 938 | + * discount explains the difference — a trial is not a coupon. | |
| 939 | + * | |
| 940 | + * @since 2.4.0 | |
| 941 | + * | |
| 942 | + * @param mixed $session Decoded Checkout Session. | |
| 943 | + * @param mixed $row_amount The row's amount (what the order was created for). | |
| 944 | + * @param array $form_fields_info The fields to record into. | |
| 945 | + * @return array `amount` (float) and `form_fields_info`. | |
| 946 | + */ | |
| 947 | + public static function stripe_paid_amounts( $session, $row_amount, array $form_fields_info ) { | |
| 948 | + $original = is_numeric( $row_amount ) ? (float) $row_amount : 0.0; | |
| 949 | + $total = is_object( $session ) && isset( $session->amount_total ) && is_numeric( $session->amount_total ) ? (float) $session->amount_total : null; | |
| 950 | + $paid = null === $total ? $original : $total / 100; | |
| 951 | + | |
| 952 | + $form_fields_info['is_coupon_applied'] = 0; | |
| 953 | + $form_fields_info['exact_paid_amount'] = $paid; | |
| 954 | + unset( $form_fields_info['original_amount'], $form_fields_info['paid_amount_diff'] ); | |
| 955 | + | |
| 956 | + if ( (int) round( $paid * 100 ) !== (int) round( $original * 100 ) ) { | |
| 957 | + $form_fields_info['original_amount'] = $original; | |
| 958 | + | |
| 959 | + $discounted = ( is_object( $session ) && isset( $session->total_details->amount_discount ) && is_numeric( $session->total_details->amount_discount ) && (float) $session->total_details->amount_discount > 0 ) | |
| 960 | + || ( isset( $form_fields_info['stripe_coupon_code'] ) && is_scalar( $form_fields_info['stripe_coupon_code'] ) && '' !== trim( (string) $form_fields_info['stripe_coupon_code'] ) ); | |
| 961 | + | |
| 962 | + if ( $discounted ) { | |
| 963 | + $form_fields_info['paid_amount_diff'] = $original - $paid; | |
| 964 | + $form_fields_info['is_coupon_applied'] = 1; | |
| 965 | + } | |
| 966 | + } | |
| 967 | + | |
| 968 | + return [ | |
| 969 | + 'amount' => $paid, | |
| 970 | + 'form_fields_info' => $form_fields_info, | |
| 971 | + ]; | |
| 972 | + } | |
| 973 | + | |
| 974 | + /** | |
| 975 | + * The customer's e-mail on a Checkout Session: the charge's billing e-mail when the PaymentIntent | |
| 976 | + * is expanded, else the session's `customer_details` / `customer_email`. | |
| 977 | + * | |
| 978 | + * @since 2.4.0 | |
| 979 | + * | |
| 980 | + * @param mixed $session Decoded Checkout Session. | |
| 981 | + * @return string | |
| 982 | + */ | |
| 983 | + public static function stripe_session_customer_email( $session ) { | |
| 984 | + if ( ! is_object( $session ) ) { | |
| 985 | + return ''; | |
| 986 | + } | |
| 987 | + | |
| 988 | + if ( isset( $session->payment_intent->charges->data ) && is_array( $session->payment_intent->charges->data ) && ! empty( $session->payment_intent->charges->data ) ) { | |
| 989 | + $charge = current( $session->payment_intent->charges->data ); | |
| 990 | + | |
| 991 | + if ( isset( $charge->billing_details->email ) && is_string( $charge->billing_details->email ) && '' !== $charge->billing_details->email ) { | |
| 992 | + return sanitize_email( $charge->billing_details->email ); | |
| 993 | + } | |
| 994 | + } | |
| 995 | + | |
| 996 | + if ( isset( $session->customer_details->email ) && is_string( $session->customer_details->email ) && '' !== $session->customer_details->email ) { | |
| 997 | + return sanitize_email( $session->customer_details->email ); | |
| 998 | + } | |
| 999 | + | |
| 1000 | + if ( isset( $session->customer_email ) && is_string( $session->customer_email ) ) { | |
| 1001 | + return sanitize_email( $session->customer_email ); | |
| 1002 | + } | |
| 1003 | + | |
| 1004 | + return ''; | |
| 1005 | + } | |
| 1006 | + | |
| 1007 | + /** | |
| 1008 | + * Whether an order id is one Handler::new_order_id() minted for Stripe. | |
| 1009 | + * | |
| 1010 | + * A return for an order that is already confirmed shows its receipt from the row, by id alone. | |
| 1011 | + * Orders from before 2.3.4 carry a time-based id that could be enumerated, so only an | |
| 1012 | + * unguessable one gets that page. | |
| 1013 | + * | |
| 1014 | + * @since 2.4.0 | |
| 1015 | + * | |
| 1016 | + * @param mixed $order_id Order id. | |
| 1017 | + * @return bool | |
| 1018 | + */ | |
| 1019 | + public static function stripe_order_id_is_unguessable( $order_id ) { | |
| 1020 | + return is_string( $order_id ) && 1 === preg_match( '/^stripe_[0-9a-f]{24}$/', $order_id ); | |
| 1021 | + } | |
| 1022 | + | |
| 1023 | + /** | |
| 1024 | + * The return page for an order that is no longer pending. | |
| 1025 | + * | |
| 1026 | + * - Confirmed (`paid`): the success data, rebuilt from the row — no Stripe call, no hook, no | |
| 1027 | + * e-mail — with Pro's thank-you page flags. Only for an unguessable order id. | |
| 1028 | + * - Anything else (refunded, an older release's statuses): nothing, as before. | |
| 1029 | + * | |
| 1030 | + * @param object $row The row. | |
| 1031 | + * @param string $order_id The order id the return names. | |
| 1032 | + * @return array|false | |
| 1033 | + */ | |
| 1034 | + private static function stripe_settled_return( $row, $order_id ) { | |
| 1035 | + if ( self::STRIPE_CONFIRMED_STATUS !== $row->status || ! self::stripe_order_id_is_unguessable( $order_id ) ) { | |
| 1036 | + return false; | |
| 1037 | + } | |
| 1038 | + | |
| 1039 | + $form_fields_info = isset( $row->form_fields_info ) ? maybe_unserialize( $row->form_fields_info ) : []; | |
| 1040 | + | |
| 1041 | + // Only the thank-you page flags are used, and Pro derives them from the stored fields. | |
| 1042 | + $details = PaymentEntitlement::stripe_verified_session( | |
| 1043 | + [ | |
| 1044 | + 'form_fields_info' => is_array( $form_fields_info ) ? $form_fields_info : [], | |
| 1045 | + 'transaction_id' => (string) $row->transaction_id, | |
| 1046 | + 'email' => (string) $row->email, | |
| 1047 | + 'frontend_data' => [], | |
| 1048 | + ], | |
| 1049 | + new \stdClass(), | |
| 1050 | + self::stripe_verification_for( $row ) | |
| 1051 | + ); | |
| 1052 | + | |
| 1053 | + return self::stripe_frontend_data( $row, $details['frontend_data'] ); | |
| 1054 | + } | |
| 1055 | + | |
| 1056 | + /** | |
| 1057 | + * Success data for a confirmed Stripe row. | |
| 1058 | + * | |
| 1059 | + * @param object $row Confirmed row. | |
| 1060 | + * @param array $extra Pro's thank-you page flags. | |
| 1061 | + * @return array | |
| 1062 | + */ | |
| 1063 | + private static function stripe_frontend_data( $row, array $extra = [] ) { | |
| 1064 | + return [ | |
| 1065 | + 'amount' => (float) $row->amount, | |
| 1066 | + 'email' => sanitize_email( (string) $row->email ), | |
| 1067 | + 'transaction_id' => sanitize_text_field( (string) $row->transaction_id ), | |
| 1068 | + 'currency' => ! empty( $row->currency ) ? sanitize_text_field( $row->currency ) : __( 'USD', 'better-payment' ), | |
| 1069 | + 'method' => 'stripe', | |
| 1070 | + ] + $extra; | |
| 1071 | + } | |
| 1072 | + | |
| 1073 | + /** | |
| 1074 | + * Whether a retrieved Stripe Checkout Session has really been paid for. | |
| 1075 | + * | |
| 1076 | + * Retrieving a session proves only that it exists. A visitor can come back to the return | |
| 1077 | + * URL from a checkout they abandoned (`status` is `open`, or `expired`), and a completed | |
| 1078 | + * checkout paid with a delayed method (bank debits, vouchers) is `complete` with | |
| 1079 | + * `payment_status: unpaid` until the money arrives — sometimes days later, sometimes never. | |
| 1080 | + * Both used to confirm the order: the row, `better_payment/payment_confirmed`, Pro's | |
| 1081 | + * payment types, the receipts and the success page. | |
| 1082 | + * | |
| 1083 | + * Paid means `payment_status` is `paid` (the funds are there), or `no_payment_required` | |
| 1084 | + * (a 100%-off promotion code, a subscription trial) on a `complete` session: Stripe also | |
| 1085 | + * reports `no_payment_required` for such a session before it is completed. A present | |
| 1086 | + * `status` other than `complete` is never paid. A missing `status` is tolerated only for | |
| 1087 | + * `paid` — requests pin `Stripe-Version: 2019-05-16`, and `paid` is only ever reported for | |
| 1088 | + * a completed checkout — so a response without the field cannot stall every payment. | |
| 1089 | + * Anything else missing or unrecognised is unpaid. | |
| 1090 | + * | |
| 1091 | + * @since 2.4.0 | |
| 1092 | + * | |
| 1093 | + * @param mixed $session Decoded Checkout Session. | |
| 1094 | + * @return bool | |
| 1095 | + */ | |
| 1096 | + public static function stripe_session_is_paid( $session ) { | |
| 1097 | + if ( ! is_object( $session ) ) { | |
| 1098 | + return false; | |
| 1099 | + } | |
| 1100 | + | |
| 1101 | + $status = isset( $session->status ) && is_string( $session->status ) ? $session->status : ''; | |
| 1102 | + $payment_status = isset( $session->payment_status ) && is_string( $session->payment_status ) ? $session->payment_status : ''; | |
| 1103 | + | |
| 1104 | + if ( 'paid' === $payment_status ) { | |
| 1105 | + return '' === $status || 'complete' === $status; | |
| 1106 | + } | |
| 1107 | + | |
| 1108 | + return 'no_payment_required' === $payment_status && 'complete' === $status; | |
| 1109 | + } | |
| 1110 | + | |
| 1111 | + /** | |
| 1112 | + * Why a Stripe return did not confirm its order. | |
| 1113 | + * | |
| 1114 | + * `bp_stripe_payment_processing`: the checkout was completed but Stripe has not been paid | |
| 1115 | + * yet (a delayed payment method). `bp_stripe_payment_incomplete`: anything else — the | |
| 1116 | + * checkout was never completed. The message is shown in WooCommerce order notes. | |
| 1117 | + * | |
| 1118 | + * @since 2.4.0 | |
| 1119 | + * | |
| 1120 | + * @param mixed $session Decoded Checkout Session. | |
| 1121 | + * @return \WP_Error | |
| 1122 | + */ | |
| 1123 | + public static function stripe_unpaid_session_error( $session ) { | |
| 1124 | + $status = is_object( $session ) && isset( $session->status ) && is_string( $session->status ) ? $session->status : ''; | |
| 1125 | + $payment_status = is_object( $session ) && isset( $session->payment_status ) && is_string( $session->payment_status ) ? $session->payment_status : ''; | |
| 1126 | + $data = [ | |
| 1127 | + 'status' => sanitize_text_field( $status ), | |
| 1128 | + 'payment_status' => sanitize_text_field( $payment_status ), | |
| 1129 | + ]; | |
| 1130 | + | |
| 1131 | + if ( 'complete' === $status && 'unpaid' === $payment_status ) { | |
| 1132 | + return new \WP_Error( 'bp_stripe_payment_processing', __( 'Stripe has not confirmed this payment yet. It is confirmed once the payment clears.', 'better-payment' ), $data ); | |
| 1133 | + } | |
| 1134 | + | |
| 1135 | + return new \WP_Error( 'bp_stripe_payment_incomplete', __( 'Stripe did not confirm a payment for this checkout.', 'better-payment' ), $data ); | |
| 1136 | + } | |
| 1137 | + | |
| 1138 | + /** | |
| 1139 | + * The return page for a completed Stripe checkout whose payment has not cleared yet. | |
| 1140 | + * | |
| 1141 | + * Deliberately neither the success page (nothing has been paid) nor the failure page (the | |
| 1142 | + * payment may still succeed). | |
| 1143 | + * | |
| 1144 | + * @since 2.4.0 | |
| 1145 | + * | |
| 1146 | + * @return void | |
| 1147 | + */ | |
| 1148 | + private static function stripe_processing_message_template() { | |
| 1149 | + ?> | |
| 1150 | + <section class="bp-thank_page"> | |
| 1151 | + <div class="bp-thank_page-wrapper bp-stripe-processing"> | |
| 1152 | + <div class="bp-thank_page-text"> | |
| 1153 | + <h2 class="bp-font_ibm bp-page_header"><?php esc_html_e( 'Your payment is processing', 'better-payment' ); ?></h2> | |
| 1154 | + <p class="bp-font_ibm bp-payment_info"><?php esc_html_e( 'Stripe has not confirmed your payment yet. Some payment methods take a few days to clear. You do not need to pay again; reload this page later to see your confirmation.', 'better-payment' ); ?></p> | |
| 1155 | + </div> | |
| 1156 | + </div> | |
| 1157 | + </section> | |
| 1158 | + <?php | |
| 1159 | + } | |
| 1160 | + | |
| 1161 | + /** | |
| 1162 | + * The failed-payment page, with the optional redirect to the form's error page. | |
| 1163 | + * | |
| 1164 | + * @param array $settings Form settings. | |
| 1165 | + * @param string $order_id The order the return is for. | |
| 1166 | + * @return void | |
| 1167 | + */ | |
| 1168 | + private static function render_failed_response( $settings, $order_id ) { | |
| 1169 | + self::failed_message_template( $settings, self::get_failed_transaction_data( $order_id ) ); | |
| 1170 | + | |
| 1171 | + $redirection_url_error = ! empty( $settings['better_payment_form_error_page_url']['url'] ) ? esc_url( $settings['better_payment_form_error_page_url']['url'] ) : ''; | |
| 1172 | + | |
| 1173 | + if ( $redirection_url_error ) { | |
| 1174 | + ?> | |
| 1175 | + <script> | |
| 1176 | + setTimeout(function(){ | |
| 1177 | + window.location.replace("<?php echo esc_url( $redirection_url_error ); ?>"); | |
| 1178 | + }, 2000); | |
| 1179 | + </script> | |
| 1180 | + <?php | |
| 1181 | + } | |
| 1182 | + } | |
| 1183 | + | |
| 1184 | + /** | |
| 450 | 1185 | * Paystack payment success |
| 451 | - * | |
| 1186 | + * | |
| 1187 | + * Verifies a pending order against Paystack, then claims it with one update | |
| 1188 | + * (`WHERE id = … AND status = 'unpaid'`). Only the request that wins the claim fires | |
| 1189 | + * `better_payment/payment_confirmed`, sends the receipts and fires | |
| 1190 | + * `better_payment/payment/success`, so a reload, a double-click or two tabs returning at once | |
| 1191 | + * confirm the order exactly once. A request that loses the claim to a confirmation, and a later | |
| 1192 | + * return for an order that is already confirmed, render the success page from the row — only | |
| 1193 | + * when the return's `reference` is the one that order holds. | |
| 1194 | + * | |
| 452 | 1195 | * @since 0.0.1 |
| 453 | 1196 | */ |
| 454 | 1197 | public static function paystack_payment_success( $settings = [] ) { |
| 455 | 1198 | $data = $_REQUEST; |
| 456 | 1199 | |
| 457 | - if ( ! empty( $data[ 'better_payment_paystack_id' ] ) ) { | |
| 458 | - global $wpdb; | |
| 459 | - $table = "{$wpdb->prefix}better_payment"; | |
| 460 | - $results = $wpdb->get_row( | |
| 461 | - $wpdb->prepare( "SELECT id,obj_id,transaction_id,form_fields_info,referer FROM $table WHERE order_id=%s and status = 'unpaid' limit 1", sanitize_text_field( $data[ 'better_payment_paystack_id' ] ) ) | |
| 1200 | + if ( empty( $data[ 'better_payment_paystack_id' ] ) || ! is_scalar( $data[ 'better_payment_paystack_id' ] ) ) { | |
| 1201 | + return false; | |
| 1202 | + } | |
| 1203 | + | |
| 1204 | + if ( empty( $data['reference'] ) || ! is_scalar( $data['reference'] ) ) { | |
| 1205 | + return false; | |
| 1206 | + } | |
| 1207 | + | |
| 1208 | + global $wpdb; | |
| 1209 | + $table = "{$wpdb->prefix}better_payment"; | |
| 1210 | + $order_id = sanitize_text_field( (string) $data[ 'better_payment_paystack_id' ] ); | |
| 1211 | + $reference = sanitize_text_field( (string) $data['reference'] ); | |
| 1212 | + $results = $wpdb->get_row( | |
| 1213 | + $wpdb->prepare( "SELECT id,obj_id,transaction_id,amount,currency,email,status,form_fields_info,referer FROM $table WHERE order_id=%s limit 1", $order_id ) | |
| 1214 | + ); | |
| 1215 | + | |
| 1216 | + if ( empty( $results->id ) ) { | |
| 1217 | + return false; | |
| 1218 | + } | |
| 1219 | + | |
| 1220 | + if ( 'unpaid' !== $results->status ) { | |
| 1221 | + return self::paystack_settled_return( $results, $reference ); | |
| 1222 | + } | |
| 1223 | + | |
| 1224 | + $header_info = array( | |
| 1225 | + 'Authorization' => 'Bearer ' . sanitize_text_field( isset( $settings[ 'better_payment_paystack_secret_key' ] ) ? $settings[ 'better_payment_paystack_secret_key' ] : '' ), | |
| 1226 | + "Cache-Control: no-cache", | |
| 1227 | + ); | |
| 1228 | + | |
| 1229 | + $response = wp_safe_remote_get( | |
| 1230 | + 'https://api.paystack.co/transaction/verify/' . rawurlencode( $reference ), | |
| 1231 | + array( | |
| 1232 | + 'headers' => $header_info, | |
| 1233 | + 'timeout' => 70, | |
| 1234 | + ) | |
| 1235 | + ); | |
| 1236 | + | |
| 1237 | + $response = json_decode(wp_remote_retrieve_body($response)); | |
| 1238 | + | |
| 1239 | + if ( empty( $response->status ) ) { | |
| 1240 | + return false; | |
| 1241 | + } | |
| 1242 | + | |
| 1243 | + $verified = isset( $response->data ) ? $response->data : null; | |
| 1244 | + | |
| 1245 | + if ( ! self::paystack_verification_matches( $results, $verified ) | |
| 1246 | + || self::paystack_reference_claimed_elsewhere( $verified->reference, (int) $results->id ) ) { | |
| 1247 | + return false; | |
| 1248 | + } | |
| 1249 | + | |
| 1250 | + $claimed = $wpdb->update( | |
| 1251 | + $table, | |
| 1252 | + array( | |
| 1253 | + 'status' => ! empty( $response->data->status ) ? sanitize_text_field( $response->data->status ) : '', | |
| 1254 | + 'amount' => ! empty( $response->data->amount ) ? floatval( ( $response->data->amount ) / 100 ) : 0, | |
| 1255 | + 'obj_id' => ! empty( $response->data->id ) ? sanitize_text_field( $response->data->id ) : '', | |
| 1256 | + 'transaction_id' => ! empty( $response->data->reference ) ? sanitize_text_field( $response->data->reference ) : '', | |
| 1257 | + 'customer_info' => ! empty( $response->data->customer ) ? maybe_serialize( $response->data->customer ) : '', | |
| 1258 | + 'email' => ! empty( $response->data->customer->email ) ? sanitize_email( $response->data->customer->email ) : '', | |
| 1259 | + ), | |
| 1260 | + array( | |
| 1261 | + 'id' => (int) $results->id, | |
| 1262 | + 'status' => 'unpaid', | |
| 1263 | + ) | |
| 1264 | + ); | |
| 1265 | + | |
| 1266 | + if ( 1 !== (int) $claimed ) { | |
| 1267 | + // Another return confirmed this order first (or the write failed): nothing is fired or sent twice. | |
| 1268 | + $current = $wpdb->get_row( | |
| 1269 | + $wpdb->prepare( "SELECT id,transaction_id,amount,currency,email,status FROM $table WHERE id=%d limit 1", (int) $results->id ) | |
| 462 | 1270 | ); |
| 463 | - | |
| 464 | - $header_info = array( | |
| 465 | - 'Authorization' => 'Bearer ' . sanitize_text_field( $settings[ 'better_payment_paystack_secret_key' ] ), | |
| 466 | - "Cache-Control: no-cache", | |
| 467 | - ); | |
| 468 | - | |
| 469 | - $response = wp_safe_remote_get( | |
| 470 | - 'https://api.paystack.co/transaction/verify/'. sanitize_text_field( $data[ 'reference' ] ), | |
| 471 | - array( | |
| 472 | - 'headers' => $header_info, | |
| 473 | - 'timeout' => 70, | |
| 474 | - ) | |
| 475 | - ); | |
| 476 | 1271 | |
| 477 | - $response = json_decode(wp_remote_retrieve_body($response)); | |
| 478 | - | |
| 479 | - if ( ! empty( $response->status ) ) { | |
| 480 | - $updated = $wpdb->update( | |
| 481 | - $table, | |
| 482 | - array( | |
| 483 | - 'status' => ! empty( $response->data->status ) ? sanitize_text_field( $response->data->status ) : '', | |
| 484 | - 'amount' => ! empty( $response->data->amount ) ? floatval( ( $response->data->amount ) / 100 ) : 0, | |
| 485 | - 'obj_id' => ! empty( $response->data->id ) ? sanitize_text_field( $response->data->id ) : '', | |
| 486 | - 'transaction_id' => ! empty( $response->data->reference ) ? sanitize_text_field( $response->data->reference ) : '', | |
| 487 | - 'customer_info' => ! empty( $response->data->customer ) ? maybe_serialize( $response->data->customer ) : '', | |
| 488 | - 'email' => ! empty( $response->data->customer->email ) ? sanitize_email( $response->data->customer->email ) : '', | |
| 489 | - ), | |
| 490 | - array( 'ID' => $results->id ) | |
| 491 | - ); | |
| 492 | - do_action( 'better_payment/payment_confirmed', (int) $results->id ); | |
| 493 | - } | |
| 1272 | + return ! empty( $current->id ) ? self::paystack_settled_return( $current, $reference ) : false; | |
| 1273 | + } | |
| 494 | 1274 | |
| 495 | - $better_customer_email = ! empty( $response->data->customer->email ) ? maybe_serialize( $response->data->customer->email ) : ''; | |
| 496 | - $transaction_id = ! empty( $response->data->reference ) ? sanitize_text_field( $response->data->reference ) : ''; | |
| 1275 | + do_action( 'better_payment/payment_confirmed', (int) $results->id ); | |
| 497 | 1276 | |
| 498 | - $frontend_data = [ | |
| 499 | - 'amount' => ! empty( $response->data->amount ) ? floatval( ( $response->data->amount ) / 100 ) : 0, | |
| 500 | - 'email' => ! empty( $better_customer_email ) ? $better_customer_email : '', | |
| 501 | - 'transaction_id' => $transaction_id, | |
| 502 | - 'currency' => ! empty( $response->data->currency ) ? sanitize_text_field( $response->data->currency ) : __( 'USD', 'better-payment' ), | |
| 503 | - 'method' => 'paystack', | |
| 504 | - ]; | |
| 1277 | + $better_customer_email = ! empty( $response->data->customer->email ) ? maybe_serialize( $response->data->customer->email ) : ''; | |
| 1278 | + $transaction_id = ! empty( $response->data->reference ) ? sanitize_text_field( $response->data->reference ) : ''; | |
| 505 | 1279 | |
| 506 | - if ( ! empty( $updated ) && $better_customer_email ) { | |
| 507 | - //Send email notification | |
| 508 | - if ( | |
| 509 | - (isset($settings[ 'better_payment_form_email_enable' ]) && $settings[ 'better_payment_form_email_enable' ] == 'yes' ) | |
| 510 | - || ( $results->referer === 'elementor-form' ) | |
| 511 | - ) { | |
| 512 | - $is_elementor_form = ! empty( $results->referer ) && $results->referer === 'elementor-form' ? 1 : 0; | |
| 513 | - self::better_email_notification($transaction_id, $better_customer_email, $settings, 'Paystack', $results->form_fields_info, $is_elementor_form); | |
| 514 | - } | |
| 1280 | + $frontend_data = [ | |
| 1281 | + 'amount' => ! empty( $response->data->amount ) ? floatval( ( $response->data->amount ) / 100 ) : 0, | |
| 1282 | + 'email' => ! empty( $better_customer_email ) ? $better_customer_email : '', | |
| 1283 | + 'transaction_id' => $transaction_id, | |
| 1284 | + 'currency' => ! empty( $response->data->currency ) ? sanitize_text_field( $response->data->currency ) : __( 'USD', 'better-payment' ), | |
| 1285 | + 'method' => 'paystack', | |
| 1286 | + ]; | |
| 515 | 1287 | |
| 516 | - do_action( 'better_payment/payment/success', sanitize_text_field( $data['better_payment_paystack_id'] ?? '' ), (int) $results->id, 'paystack' ); | |
| 1288 | + //Send email notification | |
| 1289 | + if ( | |
| 1290 | + $better_customer_email | |
| 1291 | + && ( | |
| 1292 | + ( isset( $settings[ 'better_payment_form_email_enable' ] ) && $settings[ 'better_payment_form_email_enable' ] == 'yes' ) | |
| 1293 | + || ( $results->referer === 'elementor-form' ) | |
| 1294 | + ) | |
| 1295 | + ) { | |
| 1296 | + $is_elementor_form = ! empty( $results->referer ) && $results->referer === 'elementor-form' ? 1 : 0; | |
| 1297 | + self::better_email_notification($transaction_id, $better_customer_email, $settings, 'Paystack', $results->form_fields_info, $is_elementor_form); | |
| 1298 | + } | |
| 517 | 1299 | |
| 518 | - return $frontend_data; | |
| 519 | - } | |
| 1300 | + do_action( 'better_payment/payment/success', $order_id, (int) $results->id, 'paystack' ); | |
| 1301 | + | |
| 1302 | + return $frontend_data; | |
| 1303 | + } | |
| 1304 | + | |
| 1305 | + /** | |
| 1306 | + * The return page for a Paystack order that is no longer pending. | |
| 1307 | + * | |
| 1308 | + * A confirmed order (`success`) shows its success page again, from the row, when the return | |
| 1309 | + * carries the reference the order holds — the order id alone is not enough to see a receipt. | |
| 1310 | + * Anything else shows nothing, as before. | |
| 1311 | + * | |
| 1312 | + * @param object $row The row. | |
| 1313 | + * @param string $reference The return's `reference`. | |
| 1314 | + * @return array|false | |
| 1315 | + */ | |
| 1316 | + private static function paystack_settled_return( $row, $reference ) { | |
| 1317 | + $stored = isset( $row->transaction_id ) ? (string) $row->transaction_id : ''; | |
| 1318 | + | |
| 1319 | + if ( ! isset( $row->status ) || 'success' !== $row->status || '' === $stored || ! hash_equals( $stored, (string) $reference ) ) { | |
| 1320 | + return false; | |
| 520 | 1321 | } |
| 521 | - return false; | |
| 1322 | + | |
| 1323 | + return [ | |
| 1324 | + 'amount' => (float) $row->amount, | |
| 1325 | + 'email' => sanitize_email( (string) $row->email ), | |
| 1326 | + 'transaction_id' => sanitize_text_field( $stored ), | |
| 1327 | + 'currency' => ! empty( $row->currency ) ? sanitize_text_field( $row->currency ) : __( 'USD', 'better-payment' ), | |
| 1328 | + 'method' => 'paystack', | |
| 1329 | + ]; | |
| 522 | 1330 | } |
| 523 | 1331 | |
| 524 | 1332 | /** |
| 1333 | + * Whether a verified Paystack transaction really pays for the pending order. | |
| 1334 | + * | |
| 1335 | + * The verify call looks up whatever `reference` the return URL carries, and the return | |
| 1336 | + * URL is the visitor's to edit. Without this, a cheap paid transaction could be replayed | |
| 1337 | + * against an expensive pending order and mark it paid. Checked, in order: | |
| 1338 | + * - the transaction itself succeeded. Paystack's top-level `status: true` only says the | |
| 1339 | + * reference resolved — it is just as true for an abandoned or failed checkout, which is | |
| 1340 | + * how a visitor who paid nothing used to reach the success page; | |
| 1341 | + * - the reference is the one Paystack issued for this order at initialize (rows created | |
| 1342 | + * before references were stored have none, and fall through to the amount check — | |
| 1343 | + * paystack_reference_claimed_elsewhere() keeps that fallback from being replayable); | |
| 1344 | + * - the amount paid covers the amount the order was created for — which | |
| 1345 | + * PaymentRequestGuard has already held to what the form allows; | |
| 1346 | + * - the currency is the order's. | |
| 1347 | + * | |
| 1348 | + * @param object $row Pending row: transaction_id, amount, currency. | |
| 1349 | + * @param mixed $verified `data` object from Paystack's /transaction/verify response. | |
| 1350 | + * @return bool | |
| 1351 | + */ | |
| 1352 | + private static function paystack_verification_matches( $row, $verified ) { | |
| 1353 | + if ( ! is_object( $verified ) ) { | |
| 1354 | + return false; | |
| 1355 | + } | |
| 1356 | + | |
| 1357 | + if ( ! isset( $verified->status ) || 'success' !== $verified->status ) { | |
| 1358 | + return false; | |
| 1359 | + } | |
| 1360 | + | |
| 1361 | + $expected_reference = isset( $row->transaction_id ) ? (string) $row->transaction_id : ''; | |
| 1362 | + $verified_reference = isset( $verified->reference ) ? (string) $verified->reference : ''; | |
| 1363 | + | |
| 1364 | + if ( '' !== $expected_reference && $verified_reference !== $expected_reference ) { | |
| 1365 | + return false; | |
| 1366 | + } | |
| 1367 | + | |
| 1368 | + $paid = isset( $verified->amount ) && is_numeric( $verified->amount ) ? floatval( $verified->amount ) / 100 : 0.0; | |
| 1369 | + | |
| 1370 | + if ( $paid + 0.005 < floatval( isset( $row->amount ) ? $row->amount : 0 ) ) { | |
| 1371 | + return false; | |
| 1372 | + } | |
| 1373 | + | |
| 1374 | + $paid_currency = isset( $verified->currency ) ? strtoupper( (string) $verified->currency ) : ''; | |
| 1375 | + $order_currency = isset( $row->currency ) ? strtoupper( (string) $row->currency ) : ''; | |
| 1376 | + | |
| 1377 | + return '' === $paid_currency || '' === $order_currency || $paid_currency === $order_currency; | |
| 1378 | + } | |
| 1379 | + | |
| 1380 | + /** | |
| 1381 | + * Whether a Paystack reference already belongs to a different order. | |
| 1382 | + * | |
| 1383 | + * Every order created since references were stored holds its own reference from | |
| 1384 | + * initialize, so this only ever decides the legacy fallback — an order with no stored | |
| 1385 | + * reference. Without it, one genuine payment could be replayed to mark every such | |
| 1386 | + * order paid. A reference no order has claimed can still settle exactly one of them. | |
| 1387 | + * | |
| 1388 | + * @param mixed $reference Verified reference. | |
| 1389 | + * @param int $row_id The order being verified. | |
| 1390 | + * @return bool True when the reference must not settle this order. | |
| 1391 | + */ | |
| 1392 | + private static function paystack_reference_claimed_elsewhere( $reference, $row_id ) { | |
| 1393 | + global $wpdb; | |
| 1394 | + | |
| 1395 | + $reference = is_scalar( $reference ) ? sanitize_text_field( (string) $reference ) : ''; | |
| 1396 | + | |
| 1397 | + if ( '' === $reference ) { | |
| 1398 | + return true; | |
| 1399 | + } | |
| 1400 | + | |
| 1401 | + $table = "{$wpdb->prefix}better_payment"; | |
| 1402 | + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix. | |
| 1403 | + $other = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $table WHERE transaction_id = %s AND id <> %d LIMIT 1", $reference, (int) $row_id ) ); | |
| 1404 | + | |
| 1405 | + return ! empty( $other ); | |
| 1406 | + } | |
| 1407 | + | |
| 1408 | + /** | |
| 525 | 1409 | * Success message template |
| 526 | 1410 | * |
| 527 | 1411 | * @since 0.0.1 |
| 528 | 1412 | */ |
| @@ -587,34 +1471,11 @@ | ||
| 587 | 1471 | $better_payment_form_success_message_currency_text = !empty( $settings['better_payment_form_success_message_currency_text'] ) |
| 588 | 1472 | ? esc_html( $settings['better_payment_form_success_message_currency_text'] ) |
| 589 | 1473 | : esc_html__( 'Currency', 'better-payment' ); |
| 590 | 1474 | $better_payment_form_success_message_pay_method_text = !empty( $settings['better_payment_form_success_message_pay_method_text'] ) ? esc_html( $settings['better_payment_form_success_message_pay_method_text'] ) : esc_html__( 'Payment Method', 'better-payment' ); |
| 591 | - $better_payment_form_success_message_pay_type_text = !empty( $settings['better_payment_form_success_message_pay_type_text'] ) | |
| 592 | - ? esc_html( $settings['better_payment_form_success_message_pay_type_text'] ) | |
| 593 | - : esc_html__( 'Payment Type', 'better-payment' ); | |
| 594 | - $better_payment_form_payment_type = !empty( $settings['better_payment_form_payment_type'] ) | |
| 595 | - ? esc_html( $settings['better_payment_form_payment_type'] ) | |
| 596 | - : ''; | |
| 597 | - if ( ! empty( $better_payment_form_payment_type ) ) { | |
| 598 | - if ( 'one-time' === $better_payment_form_payment_type ) { | |
| 599 | - $better_payment_form_payment_type_value = esc_html__( 'One Time Payment', 'better-payment' ); | |
| 600 | - } elseif ( 'recurring' === $better_payment_form_payment_type ) { | |
| 601 | - $better_payment_form_payment_type_value = esc_html__( 'Recurring Payment', 'better-payment' ); | |
| 602 | - } elseif ( 'mixed' === $better_payment_form_payment_type ) { | |
| 603 | - $mixed_payment_selected_type = ! empty( $tr_id['mixed_payment_selected_type'] ) ? sanitize_text_field( $tr_id['mixed_payment_selected_type'] ) : ''; | |
| 604 | - $is_mixed_recurring = 'recurring' === $mixed_payment_selected_type; | |
| 605 | - | |
| 606 | - if ( empty( $mixed_payment_selected_type ) ) { | |
| 607 | - $is_mixed_recurring = ! empty( $tr_id['is_payment_recurring'] ) && empty( $tr_id['is_payment_split_payment'] ); | |
| 608 | - } | |
| 609 | - | |
| 610 | - $better_payment_form_payment_type_value = $is_mixed_recurring | |
| 611 | - ? esc_html__( 'Recurring Payment', 'better-payment' ) | |
| 612 | - : esc_html__( 'One Time Payment', 'better-payment' ); | |
| 613 | - } else { | |
| 614 | - $better_payment_form_payment_type_value = esc_html__( 'Split Payment', 'better-payment' ); | |
| 615 | - } | |
| 616 | - } | |
| 1475 | + // Rows only Pro's payment types add: Payment Type, and a split payment's "Paid Amount … out of …". | |
| 1476 | + $pro_rows_after_payment_method = PaymentEntitlement::success_rows( 'after_payment_method', $settings, $tr_id, [ 'currency_symbol' => $bp__currency_symbol ] ); | |
| 1477 | + $pro_rows_after_merchant_details = PaymentEntitlement::success_rows( 'after_merchant_details', $settings, $tr_id, [ 'currency_symbol' => $bp__currency_symbol ] ); | |
| 617 | 1478 | $better_payment_form_success_message_merchant_details_text = !empty( $settings['better_payment_form_success_message_merchant_details_text'] ) |
| 618 | 1479 | ? esc_html( $settings['better_payment_form_success_message_merchant_details_text'] ) |
| 619 | 1480 | : esc_html__( 'Merchant Details', 'better-payment' ); |
| 620 | 1481 | $better_payment_form_success_message_merchant_details_value = isset( $settings['better_payment_form_payment_source'] ) && $settings['better_payment_form_payment_source'] === 'manual' && ! empty( $settings['better_payment_form_title'] ) |
| @@ -619,11 +1480,8 @@ | ||
| 619 | 1480 | : esc_html__( 'Merchant Details', 'better-payment' ); |
| 620 | 1481 | $better_payment_form_success_message_merchant_details_value = isset( $settings['better_payment_form_payment_source'] ) && $settings['better_payment_form_payment_source'] === 'manual' && ! empty( $settings['better_payment_form_title'] ) |
| 621 | 1482 | ? esc_html($settings['better_payment_form_title']) |
| 622 | 1483 | : esc_html( get_bloginfo( 'name' ) ); |
| 623 | - $better_payment_form_success_message_paid_amount_text = !empty( $settings['better_payment_form_success_message_paid_amount_text'] ) | |
| 624 | - ? esc_html( $settings['better_payment_form_success_message_paid_amount_text'] ) | |
| 625 | - : esc_html__( 'Paid Amount', 'better-payment' ); | |
| 626 | 1484 | $better_payment_form_success_message_purchase_details_text = !empty( $settings['better_payment_form_success_message_purchase_details_text'] ) |
| 627 | 1485 | ? esc_html( $settings['better_payment_form_success_message_purchase_details_text'] ) |
| 628 | 1486 | : esc_html__( 'Purchase Details', 'better-payment' ); |
| 629 | 1487 | $better_payment_form_success_message_print_text = !empty( $settings['better_payment_form_success_message_print_text'] ) |
| @@ -734,18 +1592,16 @@ | ||
| 734 | 1592 | <img src="<?php echo esc_url( $payment_method_logo ); ?>" alt="<?php echo esc_attr( $tr_id['method'] ); ?>" class="bp-payment_method-logo"> |
| 735 | 1593 | <?php endif; ?> |
| 736 | 1594 | </div> |
| 737 | 1595 | </li> |
| 738 | - <?php if( !empty( $better_payment_form_payment_type ) ): ?> | |
| 1596 | + <?php foreach ( $pro_rows_after_payment_method as $pro_row ) : ?> | |
| 739 | 1597 | <li class="bp-flex bp-font_ibm bp-page_info-list_item"> |
| 740 | 1598 | <span class="bp-info_content"> |
| 741 | - <?php | |
| 742 | - echo wp_kses_post( $better_payment_form_success_message_pay_type_text ); | |
| 743 | - ?> | |
| 1599 | + <?php echo wp_kses_post( $pro_row['label'] ); ?> | |
| 744 | 1600 | </span> |
| 745 | - <span class="bp-info_content bp-bold"><?php echo wp_kses_post( $better_payment_form_payment_type_value ); ?></span> | |
| 1601 | + <span class="bp-info_content bp-bold"><?php echo wp_kses_post( $pro_row['value'] ); ?></span> | |
| 746 | 1602 | </li> |
| 747 | - <?php endif; ?> | |
| 1603 | + <?php endforeach; ?> | |
| 748 | 1604 | <li class="bp-flex bp-font_ibm bp-page_info-list_item"> |
| 749 | 1605 | <span class="bp-info_content"> |
| 750 | 1606 | <?php |
| 751 | 1607 | echo wp_kses_post( $better_payment_form_success_message_merchant_details_text ); |
| @@ -754,20 +1610,16 @@ | ||
| 754 | 1610 | <span class="bp-info_content bp-bold"> |
| 755 | 1611 | <?php echo wp_kses_post( $better_payment_form_success_message_merchant_details_value ); ?> |
| 756 | 1612 | </span> |
| 757 | 1613 | </li> |
| 758 | - <?php if( isset( $tr_id['is_payment_split_payment'] ) && $tr_id['is_payment_split_payment'] ): ?> | |
| 1614 | + <?php foreach ( $pro_rows_after_merchant_details as $pro_row ) : ?> | |
| 759 | 1615 | <li class="bp-flex bp-font_ibm bp-page_info-list_item"> |
| 760 | 1616 | <span class="bp-info_content"> |
| 761 | - <?php | |
| 762 | - echo wp_kses_post( $better_payment_form_success_message_paid_amount_text ); | |
| 763 | - ?> | |
| 1617 | + <?php echo wp_kses_post( $pro_row['label'] ); ?> | |
| 764 | 1618 | </span> |
| 765 | - <span class="bp-info_content bp-bold"> | |
| 766 | - <?php echo $bp__currency_symbol . esc_html($tr_id['amount']); ?> out of <?php echo $bp__currency_symbol . esc_html($tr_id['total_amount']); ?> | |
| 767 | - </span> | |
| 1619 | + <span class="bp-info_content bp-bold"><?php echo wp_kses_post( $pro_row['value'] ); ?></span> | |
| 768 | 1620 | </li> |
| 769 | - <?php endif; ?> | |
| 1621 | + <?php endforeach; ?> | |
| 770 | 1622 | <?php |
| 771 | 1623 | $has_woo_products = !empty($settings['better_payment_form_woocommerce_product_id']) || !empty($settings['better_payment_form_woocommerce_product_ids']); |
| 772 | 1624 | $has_fluentcart_products = !empty($settings['better_payment_form_fluentcart_product_id']) || !empty($settings['better_payment_form_fluentcart_product_ids']); |
| 773 | 1625 | |
| @@ -888,9 +1740,8 @@ | ||
| 888 | 1740 | $image_url = $settings[ 'better_payment_form_error_message_icon' ][ 'value' ]; |
| 889 | 1741 | $default_icon = 0; |
| 890 | 1742 | } |
| 891 | 1743 | } |
| 892 | - // dd($settings[ 'better_payment_form_error_message_icon' ]); | |
| 893 | 1744 | ?> |
| 894 | 1745 | <section class="payment-failed-screen-section"> |
| 895 | 1746 | <div class="bp-thank_page-wrapper"> |
| 896 | 1747 | <div class="bp-thank_page-logo"> |
| @@ -1010,8 +1861,9 @@ | ||
| 1010 | 1861 | params.delete('better_payment_paystack_status'); |
| 1011 | 1862 | params.delete('better_payment_widget_id'); |
| 1012 | 1863 | params.delete('better_payment_stripe_id'); |
| 1013 | 1864 | params.delete('better_payment_paystack_id'); |
| 1865 | + params.delete('item_number'); | |
| 1014 | 1866 | window.history.replaceState({}, '', `${location.pathname}?${params}`); |
| 1015 | 1867 | } |
| 1016 | 1868 | </script> |
| 1017 | 1869 | <?php |
| @@ -1042,9 +1894,11 @@ | ||
| 1042 | 1894 | return [ |
| 1043 | 1895 | 'amount' => floatval( $result->amount ), |
| 1044 | 1896 | 'currency' => sanitize_text_field( $result->currency ), |
| 1045 | 1897 | 'currency_symbol' => $helper_obj->get_currency_symbol( $result->currency ), |
| 1046 | - 'transaction_id' => !empty( $result->transaction_id ) ? sanitize_text_field( $result->transaction_id ) : 'N/A', | |
| 1898 | + // The order id is request data, and ids before 2.4.0 were `{gateway}_` . uniqid() — | |
| 1899 | + // enumerable — so the gateway's transaction id is shown only for an unguessable id. | |
| 1900 | + 'transaction_id' => ! empty( $result->transaction_id ) && self::order_id_is_unguessable( $order_id ) ? sanitize_text_field( $result->transaction_id ) : 'N/A', | |
| 1047 | 1901 | 'method' => sanitize_text_field( $result->source ), |
| 1048 | 1902 | ]; |
| 1049 | 1903 | } |
| 1050 | 1904 | |
| @@ -1051,10 +1905,47 @@ | ||
| 1051 | 1905 | return false; |
| 1052 | 1906 | } |
| 1053 | 1907 | |
| 1054 | 1908 | /** |
| 1909 | + * Whitelist the e-mail related settings that better_email_notification() reads. | |
| 1910 | + * | |
| 1911 | + * PayPal confirms payments asynchronously through the IPN listener | |
| 1912 | + * (handle_paypal_ipn), where the live Elementor widget / block settings are NOT | |
| 1913 | + * available. Stripe and Paystack build their e-mail synchronously on the return | |
| 1914 | + * page, so they still have the full settings array in hand. To reach parity we | |
| 1915 | + * persist this subset into the transaction's form_fields_info at payment-create | |
| 1916 | + * time, and the IPN handler rebuilds the same $settings to produce an identical | |
| 1917 | + * e-mail body (heading, From / To / Transaction Summary / footer sections, | |
| 1918 | + * subjects, headers) and to honour the "Send Email" on/off toggle. | |
| 1919 | + * | |
| 1920 | + * @param mixed $settings Full widget / block / form settings (guarded: non-array input returns []). | |
| 1921 | + * | |
| 1922 | + * @return array Whitelisted e-mail settings (safe to serialize into form_fields_info). | |
| 1923 | + * @since 2.2.2 | |
| 1924 | + */ | |
| 1925 | + public static function extract_email_settings( $settings = [] ) { | |
| 1926 | + $email_settings = []; | |
| 1927 | + | |
| 1928 | + if ( ! is_array( $settings ) ) { | |
| 1929 | + return $email_settings; | |
| 1930 | + } | |
| 1931 | + | |
| 1932 | + foreach ( $settings as $key => $value ) { | |
| 1933 | + if ( | |
| 1934 | + 0 === strpos( $key, 'better_payment_email_' ) | |
| 1935 | + || 0 === strpos( $key, 'better_payment_form_email_' ) | |
| 1936 | + || 'better_payment_form_title' === $key | |
| 1937 | + ) { | |
| 1938 | + $email_settings[ $key ] = $value; | |
| 1939 | + } | |
| 1940 | + } | |
| 1941 | + | |
| 1942 | + return $email_settings; | |
| 1943 | + } | |
| 1944 | + | |
| 1945 | + /** | |
| 1055 | 1946 | * Email template |
| 1056 | - * | |
| 1947 | + * | |
| 1057 | 1948 | * @since 0.0.1 |
| 1058 | 1949 | */ |
| 1059 | 1950 | public static function better_email_notification($transaction_id, $customer_email, $settings, $referrer='Stripe', $form_fields_info='', $is_elementor_form = 0){ |
| 1060 | 1951 | //Send Email: customer and admin |
| @@ -1325,43 +2216,30 @@ | ||
| 1325 | 2216 | $currency_symbol = $helper_obj->get_currency_symbol( esc_html($currency) ); |
| 1326 | 2217 | $payment_date = wp_date(get_option('date_format').' '.get_option('time_format'), strtotime($transaction_obj->payment_date)); |
| 1327 | 2218 | } |
| 1328 | 2219 | |
| 1329 | - $is_empty_email_body = empty($email_body); | |
| 1330 | - | |
| 1331 | - $email_body = do_shortcode( $email_body ); | |
| 1332 | - $bp_form_fields_html_content = $email_body; | |
| 1333 | - | |
| 1334 | - $bp_all_fields_shortcode = 'bp-all-fields'; | |
| 1335 | 2220 | $referer_content_page_link = '#'; |
| 1336 | - | |
| 1337 | - $field_text = __('Field', 'better-payment'); | |
| 1338 | - $value_text = __('Entry', 'better-payment'); | |
| 1339 | 2221 | |
| 1340 | 2222 | $form_fields_info_arr = maybe_unserialize($form_fields_info); |
| 2223 | + $form_fields_info_arr = is_array( $form_fields_info_arr ) ? $form_fields_info_arr : []; | |
| 1341 | 2224 | |
| 1342 | 2225 | $email_logo_url = ! empty( $args['email_logo_url'] ) ? $args['email_logo_url'] : ''; |
| 1343 | 2226 | $email_content_greeting = ! empty( $args['email_content_greeting'] ) ? 1 : 0; |
| 1344 | 2227 | $email_form_name = ! empty( $args['email_form_name'] ) ? $args['email_form_name'] : 'N/A'; |
| 1345 | 2228 | |
| 1346 | - $bp_form_fields_html_header = '<table style="margin-bottom: 20px; font-size: 14px; border-collapse: collapse; width: 100%;">'; | |
| 2229 | + $form_field_rows = []; | |
| 1347 | 2230 | |
| 1348 | - $bp_form_fields_html_footer = "</table>"; | |
| 1349 | - | |
| 1350 | - $content = ''. $line_break; | |
| 1351 | - | |
| 1352 | 2231 | foreach ( $form_fields_info_arr as $key => $field ) { |
| 1353 | - if ( $key === 'is_payment_split_payment' ) { | |
| 1354 | - $is_payment_split_payment = 1; | |
| 1355 | - } | |
| 1356 | - //Hide few fields | |
| 2232 | + //Hide few fields: storage keys, not fields the visitor filled in (the From section hides the same ones) | |
| 1357 | 2233 | if( |
| 1358 | 2234 | $key === 'referer_page_id' || $key === 'referer_widget_id' || $key === 'source' || $key === 'el_form_fields' |
| 1359 | - || $key === 'is_woo_layout' | |
| 1360 | - || $key === 'is_payment_split_payment' | |
| 1361 | - || $key === 'split_payment_total_amount' | |
| 1362 | - || $key === 'split_payment_total_amount_price_id' | |
| 1363 | - || $key === 'split_payment_installment_price_id' | |
| 2235 | + || $key === 'amount_quantity' | |
| 2236 | + || $key === 'woo_product_id' | |
| 2237 | + || $key === 'woo_product_ids' | |
| 2238 | + || $key === 'fluentcart_product_id' | |
| 2239 | + || $key === 'fluentcart_product_ids' | |
| 2240 | + || $key === 'detailed_product_info' | |
| 2241 | + || $key === 'email_settings' | |
| 1364 | 2242 | ) { |
| 1365 | 2243 | if($key === 'referer_page_id'){ |
| 1366 | 2244 | $referer_content_page_link = !empty($field) ? get_permalink( $field ) : $referer_content_page_link; |
| 1367 | 2245 | } |
| @@ -1368,40 +2246,42 @@ | ||
| 1368 | 2246 | |
| 1369 | 2247 | continue; |
| 1370 | 2248 | } |
| 1371 | 2249 | |
| 2250 | + $label_key = $key; | |
| 2251 | + | |
| 1372 | 2252 | if($key === 'primary_first_name'){ |
| 1373 | - $key = 'first_name'; | |
| 2253 | + $label_key = 'first_name'; | |
| 1374 | 2254 | $customer_first_name = $field; |
| 1375 | 2255 | } |
| 1376 | 2256 | if($key === 'primary_last_name'){ |
| 1377 | - $key = 'last_name'; | |
| 2257 | + $label_key = 'last_name'; | |
| 1378 | 2258 | $customer_last_name = $field; |
| 1379 | 2259 | } |
| 1380 | 2260 | |
| 1381 | 2261 | if($key === 'primary_email'){ |
| 1382 | - $key = 'email'; | |
| 2262 | + $label_key = 'email'; | |
| 1383 | 2263 | } |
| 1384 | 2264 | |
| 1385 | 2265 | if($key === 'primary_payment_amount'){ |
| 1386 | - $key = 'payment_amount'; | |
| 2266 | + $label_key = 'payment_amount'; | |
| 1387 | 2267 | } |
| 1388 | - | |
| 1389 | - $key_formatted = self::better_title_case($key); | |
| 2268 | + | |
| 2269 | + $key_formatted = self::better_title_case($label_key); | |
| 1390 | 2270 | if($key_formatted === 'Amount'){ |
| 1391 | 2271 | $key_formatted = __('Paid', 'better-payment'); |
| 1392 | - | |
| 1393 | - if ( ! empty( $is_payment_split_payment ) ) { | |
| 1394 | - $key_formatted = __('Product Price: ', 'better-payment'); | |
| 1395 | - } | |
| 1396 | 2272 | } |
| 1397 | 2273 | |
| 1398 | - $content .= "<tr> | |
| 1399 | - <td style='padding: 7px; border: 1px solid #666;'> <strong>$key_formatted</strong> </td> | |
| 1400 | - <td style='padding: 7px; border: 1px solid #666;'> $field </td> | |
| 1401 | - </tr>"; | |
| 2274 | + $form_field_rows[] = [ | |
| 2275 | + 'key' => $key, | |
| 2276 | + 'label' => $key_formatted, | |
| 2277 | + 'value' => $field, | |
| 2278 | + ]; | |
| 1402 | 2279 | } |
| 1403 | 2280 | |
| 2281 | + // A Pro payment's own fields (split payments, layout 6) are hidden or relabelled by Pro. | |
| 2282 | + $all_fields_rows = PaymentEntitlement::email_rows( $form_field_rows, 'all_fields', $form_fields_info_arr ); | |
| 2283 | + | |
| 1404 | 2284 | if($customer_first_name) { |
| 1405 | 2285 | $customer_name = $customer_first_name . ' '; |
| 1406 | 2286 | } |
| 1407 | 2287 | if($customer_last_name) { |
| @@ -1502,25 +2382,17 @@ | ||
| 1502 | 2382 | 'email_content_from_section_show' => intval( $args['email_content_from_section_show'] ), |
| 1503 | 2383 | 'email_content_to_section_show' => intval( $args['email_content_to_section_show'] ), |
| 1504 | 2384 | 'email_content_transaction_summary_show' => intval( $args['email_content_transaction_summary_show'] ), |
| 1505 | 2385 | 'email_content_footer_text_show' => intval( $args['email_content_footer_text_show'] ), |
| 1506 | - 'email_content_customer' => wp_kses_post( $args['email_content_customer'] ), | |
| 1507 | - 'email_content_admin' => wp_kses_post( $args['email_content_admin'] ), | |
| 2386 | + // The form's messages, with `[bp-all-fields]` replaced where the author placed it. | |
| 2387 | + 'email_content_customer' => self::render_email_message( $args['email_content_customer'] ?? '', $all_fields_rows, 'plain' === ( $args['email_content_type_customer'] ?? 'html' ) ), | |
| 2388 | + 'email_content_admin' => self::render_email_message( $args['email_content_admin'] ?? '', $all_fields_rows, 'plain' === ( $args['email_content_type'] ?? 'html' ) ), | |
| 1508 | 2389 | 'is_elementor_form' => intval( $args['is_elementor_form'] ), |
| 1509 | 2390 | 'email_content_type' => $args['email_content_type'] ?? 'html', |
| 1510 | 2391 | 'email_content_type_customer' => $args['email_content_type_customer'] ?? 'html', |
| 1511 | 2392 | ]; |
| 1512 | 2393 | |
| 1513 | - $bp_form_fields_html_body = $content; | |
| 1514 | - $bp_form_fields_html_content = $bp_form_fields_html_header . $bp_form_fields_html_body . $bp_form_fields_html_footer ; | |
| 1515 | - | |
| 1516 | - //Replace shortcode with form fields info | |
| 1517 | - $email_body = str_replace( "[$bp_all_fields_shortcode]", $bp_form_fields_html_content, $email_body ); | |
| 1518 | - | |
| 1519 | - // Email V2 | |
| 1520 | - // General content | |
| 1521 | - $form_name = | |
| 1522 | - | |
| 2394 | + // Email V2: the template is the whole body. | |
| 1523 | 2395 | ob_start(); |
| 1524 | 2396 | |
| 1525 | 2397 | include BETTER_PAYMENT_ADMIN_VIEWS_PATH . "/template-email-notification.php"; |
| 1526 | 2398 | |
| @@ -1528,8 +2400,76 @@ | ||
| 1528 | 2400 | ob_end_clean(); |
| 1529 | 2401 | |
| 1530 | 2402 | return $email_body; |
| 1531 | 2403 | } |
| 2404 | + | |
| 2405 | + /** | |
| 2406 | + * A form's e-mail message, with `[bp-all-fields]` replaced by the submitted fields. | |
| 2407 | + * | |
| 2408 | + * The template prints the message; the fields used to be built into a body the template's | |
| 2409 | + * output then overwrote, so the token reached every e-mail as literal text. The message is | |
| 2410 | + * the form's own setting and is kses-filtered as it always was. The fields are the | |
| 2411 | + * visitor's, so they are escaped here and inserted after the filter — never passed through | |
| 2412 | + * it or do_shortcode(), where a submitted value could add markup or run a shortcode. | |
| 2413 | + * | |
| 2414 | + * @since 2.4.0 | |
| 2415 | + * | |
| 2416 | + * @param mixed $message The form's message setting. | |
| 2417 | + * @param array[] $rows `label` and `value` of each field to list, in order. | |
| 2418 | + * @param bool $is_plain Whether the e-mail is sent as plain text. | |
| 2419 | + * @return string | |
| 2420 | + */ | |
| 2421 | + public static function render_email_message( $message, array $rows, $is_plain = false ) { | |
| 2422 | + $message = wp_kses_post( is_scalar( $message ) ? (string) $message : '' ); | |
| 2423 | + | |
| 2424 | + if ( false === strpos( $message, '[bp-all-fields]' ) ) { | |
| 2425 | + return $message; | |
| 2426 | + } | |
| 2427 | + | |
| 2428 | + return str_replace( '[bp-all-fields]', self::email_all_fields_markup( $rows, $is_plain ), $message ); | |
| 2429 | + } | |
| 2430 | + | |
| 2431 | + /** | |
| 2432 | + * The `[bp-all-fields]` block: a two-column table, or `Label: value` lines in a plain-text e-mail. | |
| 2433 | + * | |
| 2434 | + * @param array[] $rows `label` and `value` of each field. | |
| 2435 | + * @param bool $is_plain Plain text instead of HTML. | |
| 2436 | + * @return string | |
| 2437 | + */ | |
| 2438 | + private static function email_all_fields_markup( array $rows, $is_plain ) { | |
| 2439 | + $lines = []; | |
| 2440 | + $cells = ''; | |
| 2441 | + | |
| 2442 | + foreach ( $rows as $row ) { | |
| 2443 | + if ( ! isset( $row['label'] ) || ! is_scalar( $row['label'] ) || ! array_key_exists( 'value', $row ) ) { | |
| 2444 | + continue; | |
| 2445 | + } | |
| 2446 | + | |
| 2447 | + // A nested value (stored settings, a product list) is not something a visitor typed. | |
| 2448 | + if ( ! is_scalar( $row['value'] ) && null !== $row['value'] ) { | |
| 2449 | + continue; | |
| 2450 | + } | |
| 2451 | + | |
| 2452 | + $label = (string) $row['label']; | |
| 2453 | + $value = (string) $row['value']; | |
| 2454 | + | |
| 2455 | + if ( $is_plain ) { | |
| 2456 | + $lines[] = rtrim( wp_strip_all_tags( $label ), ': ' ) . ': ' . wp_strip_all_tags( $value ); | |
| 2457 | + continue; | |
| 2458 | + } | |
| 2459 | + | |
| 2460 | + $cells .= '<tr>' | |
| 2461 | + . '<td style="padding: 7px; border: 1px solid #666;"><strong>' . esc_html( $label ) . '</strong></td>' | |
| 2462 | + . '<td style="padding: 7px; border: 1px solid #666;">' . esc_html( $value ) . '</td>' | |
| 2463 | + . '</tr>'; | |
| 2464 | + } | |
| 2465 | + | |
| 2466 | + if ( $is_plain ) { | |
| 2467 | + return implode( "\n", $lines ); | |
| 2468 | + } | |
| 2469 | + | |
| 2470 | + return '<table class="bp-all-fields" style="margin-bottom: 20px; font-size: 14px; border-collapse: collapse; width: 100%;">' . $cells . '</table>'; | |
| 2471 | + } | |
| 1532 | 2472 | |
| 1533 | 2473 | /** |
| 1534 | 2474 | * String helper method |
| 1535 | 2475 | * helps to convert string to title case |