PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.6.1
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.6.1
1.6.1 1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
← All changes | inc/fields/payment-markup.php +563 -33 1.2.0 → 1.6.1 View file →
@@ -56,8 +56,16 @@
56 56 */
57 57 protected $stripe_connected;
58 58
59 59 /**
60 + * Whether the resolved Stripe account is known to be unable to charge cards.
61 + *
62 + * @var bool
63 + * @since 1.5.1
64 + */
65 + protected $stripe_card_blocked = false;
66 +
67 + /**
60 68 * Payment mode (live or test).
61 69 *
62 70 * @var string
63 71 * @since 0.0.1
@@ -144,8 +152,53 @@
144 152 */
145 153 protected $aria_described_by = '';
146 154
147 155 /**
156 + * The payment type as configured in the editor, before the default-choice and
157 + * Pro-availability rewrites below.
158 + *
159 + * $payment_type holds the *active* type being rendered; this holds the admin's
160 + * intent, so 'both' can be distinguished from a plain single-mode block.
161 + *
162 + * @var string
163 + * @since 1.5.1
164 + */
165 + protected $original_payment_type = 'one-time';
166 +
167 + /**
168 + * Which choice is pre-selected in 'both' mode ('one-time' or 'subscription').
169 + *
170 + * @var string
171 + * @since 1.5.1
172 + */
173 + protected $default_payment_choice = 'one-time';
174 +
175 + /**
176 + * Admin-configured label for the "make it recurring" opt-in checkbox, shown only
177 + * in 'both' mode.
178 + *
179 + * @var string
180 + * @since 1.5.1
181 + */
182 + protected $recurring_toggle_label = '';
183 +
184 + /**
185 + * One-time amount configuration. Used only in 'both' mode.
186 + *
187 + * @var array<string, mixed>
188 + * @since 1.5.1
189 + */
190 + protected $one_time_config = [];
191 +
192 + /**
193 + * Subscription amount configuration. Used only in 'both' mode.
194 + *
195 + * @var array<string, mixed>
196 + * @since 1.5.1
197 + */
198 + protected $subscription_config = [];
199 +
200 + /**
148 201 * Constructor for the Payment Markup class.
149 202 *
150 203 * @param array<mixed> $attributes Block attributes.
151 204 * @since 0.0.1
@@ -171,14 +224,34 @@
171 224 if ( empty( $this->currency ) || 'USD' === $this->currency ) {
172 225 $this->currency = Payment_Helper::get_currency();
173 226 }
174 227
175 - // Get appropriate Stripe publishable key based on mode.
176 - $this->stripe_publishable_key = Stripe_Helper::get_stripe_publishable_key();
228 + // Get the publishable key for this form's selected account (mode-aware).
229 + $stripe_account_id = Stripe_Helper::resolve_account_for_form( $this->form_id );
230 + $this->stripe_publishable_key = Stripe_Helper::get_stripe_publishable_key( '', $stripe_account_id );
177 231
178 - // Fall back to one-time if subscription is configured but pro is not active.
232 + // Stripe told us at connect (or on account.updated) that this account
233 + // cannot charge cards in this mode. Rendering the card box anyway is what
234 + // gave donors an empty "Credit / Debit Card" accordion and lost every
235 + // attempt in silence. Only a positively-known-bad state counts — an
236 + // account we have never checked stays enabled, because a stale flag
237 + // hiding a working gateway is a worse failure than the one being fixed.
238 + $this->stripe_card_blocked = Stripe_Helper::is_card_capability_blocked( $stripe_account_id );
239 +
240 + // Fall back to one-time if a recurring path is configured but Pro is not
241 + // active or too old. 'both' collapses too, which is what hides the donor's
242 + // recurring choice: the chooser only renders while the type is still 'both'.
243 + //
244 + // The collapse lives in Payment_Helper::effective_payment_type(), shared
245 + // with validate_payment_type(). Both have to reach the same answer: if the
246 + // validator disagrees with what this markup rendered, it rejects the very
247 + // request its own form invited.
248 + //
249 + // $configured_type stays the RAW attribute — the 'both' branch further down
250 + // keys on it deliberately, because a collapsed form still has 'both' stored
251 + // and validate_payment_amount() reads that stored config.
179 252 $configured_type = $attributes['paymentType'] ?? 'one-time';
180 - $this->payment_type = 'subscription' === $configured_type && ! defined( 'SUREDONATION_PRO_VER' ) ? 'one-time' : $configured_type;
253 + $this->payment_type = Payment_Helper::effective_payment_type( $configured_type );
181 254 $this->subscription_plan = $attributes['subscriptionPlan'] ?? [];
182 255 $this->amount_type = $attributes['amountType'] ?? 'fixed';
183 256 $this->fixed_amount = $attributes['fixedAmount'] ?? 10;
184 257 $this->minimum_amount = $attributes['minimumAmount'] ?? 0;
@@ -189,8 +262,61 @@
189 262
190 263 // Set variable amount field mapping.
191 264 $this->variable_amount_field = $attributes['variableAmountField'] ?? '';
192 265
266 + // Dual-mode ('both') configuration. $payment_type is rewritten below to the
267 + // active choice, so keep the admin's intent separately.
268 + $this->original_payment_type = $this->payment_type;
269 +
270 + $this->default_payment_choice = $attributes['defaultPaymentChoice'] ?? 'one-time';
271 + if ( ! in_array( $this->default_payment_choice, [ 'one-time', 'subscription' ], true ) ) {
272 + $this->default_payment_choice = 'one-time';
273 + }
274 +
275 + $this->recurring_toggle_label = $attributes['recurringToggleLabel'] ?? __( 'Make this a recurring donation', 'suredonation' );
276 +
277 + $this->one_time_config = [
278 + 'amount_type' => $attributes['oneTimeAmountType'] ?? 'fixed',
279 + 'fixed_amount' => isset( $attributes['oneTimeFixedAmount'] ) ? (float) $attributes['oneTimeFixedAmount'] : 10.0,
280 + 'minimum_amount' => isset( $attributes['oneTimeMinimumAmount'] ) ? (float) $attributes['oneTimeMinimumAmount'] : 0.0,
281 + 'variable_field' => $attributes['oneTimeVariableAmountField'] ?? '',
282 + ];
283 +
284 + $this->subscription_config = [
285 + 'amount_type' => $attributes['subscriptionAmountType'] ?? 'fixed',
286 + 'fixed_amount' => isset( $attributes['subscriptionFixedAmount'] ) ? (float) $attributes['subscriptionFixedAmount'] : 10.0,
287 + 'minimum_amount' => isset( $attributes['subscriptionMinimumAmount'] ) ? (float) $attributes['subscriptionMinimumAmount'] : 0.0,
288 + 'variable_field' => $attributes['subscriptionVariableAmountField'] ?? '',
289 + ];
290 +
291 + // In 'both' mode the initially visible amount — and the mode Stripe Elements
292 + // initialises in — is the default choice. Rewrite the scalar properties so all
293 + // the rendering below keeps working unchanged; the chooser's JS re-syncs them
294 + // client-side when the donor switches.
295 + //
296 + // Keyed on the CONFIGURED type, not the (possibly collapsed) active type: when
297 + // the recurring add-on is inactive a 'both' form collapses to one-time and the
298 + // chooser never renders, but validate_payment_amount() still reads the stored
299 + // 'both' config and checks the submitted amount against the one_time sub-config.
300 + // Seed the active scalars from that sub-config rather than the shared
301 + // attributes, so the price the donor sees matches the price validation enforces
302 + // — otherwise a form whose shared and one-time amounts diverge renders one
303 + // price and then rejects it with no admin signal.
304 + if ( 'both' === $configured_type ) {
305 + // The chooser now defaults to one-time and opts into recurring via a
306 + // checkbox, so a 'both' block always initialises in one-time regardless of
307 + // the (legacy) defaultPaymentChoice — the JS reveals the subscription panel
308 + // and re-syncs these scalars when the box is ticked. This is also what
309 + // validate_payment_amount() demands when Pro is inactive and the form has
310 + // collapsed to one-time, so the two cases converge.
311 + $this->default_payment_choice = 'one-time';
312 + $this->payment_type = 'one-time';
313 + $this->amount_type = $this->one_time_config['amount_type'];
314 + $this->fixed_amount = $this->one_time_config['fixed_amount'];
315 + $this->minimum_amount = $this->one_time_config['minimum_amount'];
316 + $this->variable_amount_field = $this->one_time_config['variable_field'];
317 + }
318 +
193 319 // Parse payment methods from block attributes (with backward compat fallback).
194 320 $block_payment_methods = $attributes['paymentMethods'] ?? null;
195 321 if ( ! is_array( $block_payment_methods ) || empty( $block_payment_methods ) ) {
196 322 $gateway = $attributes['gateway'] ?? 'stripe';
@@ -199,9 +325,9 @@
199 325
200 326 // Filter to only globally-available gateways.
201 327 $available = [];
202 328 foreach ( $block_payment_methods as $method ) {
203 - if ( 'stripe' === $method && $this->stripe_connected && ! empty( $this->stripe_publishable_key ) ) {
329 + if ( 'stripe' === $method && $this->stripe_connected && ! empty( $this->stripe_publishable_key ) && ! $this->stripe_card_blocked ) {
204 330 $available[] = 'stripe';
205 331 } elseif ( 'offline' === $method && Offline_Helper::is_offline_enabled() ) {
206 332 $available[] = 'offline';
207 333 }
@@ -218,10 +344,21 @@
218 344 * @since 1.0.0
219 345 */
220 346 $available = apply_filters( 'suredonation_available_payment_methods', $available, $block_payment_methods, $attributes );
221 347
222 - $this->payment_methods = ! empty( $available ) ? $available : $block_payment_methods;
348 + // The fallback exists so a form still advertises something when nothing
349 + // resolved as available, but it must not resurrect a gateway we
350 + // positively know cannot charge: `payment_methods` is what
351 + // `data-payment-methods` advertises to the frontend script, and listing
352 + // a gateway whose container was never rendered makes the script select
353 + // it, try to mount into nothing, and never initialise the gateway that
354 + // does work — an empty box again, by a different route.
355 + $fallback = $this->stripe_card_blocked
356 + ? array_values( array_diff( $block_payment_methods, [ 'stripe' ] ) )
357 + : $block_payment_methods;
223 358
359 + $this->payment_methods = ! empty( $available ) ? $available : $fallback;
360 +
224 361 // BACKWARD COMPATIBILITY: Migrate customer fields from subscriptionPlan.
225 362 if ( empty( $this->customer_name_field ) && ! empty( $this->subscription_plan['customer_name'] ) ) {
226 363 $this->customer_name_field = $this->subscription_plan['customer_name'];
227 364 }
@@ -268,8 +405,14 @@
268 405 data-form-id="<?php echo esc_attr( $this->form_id ); ?>"
269 406 class="<?php echo esc_attr( $field_classes ); ?>"
270 407 data-payment-methods="<?php echo esc_attr( $payment_methods_csv ); ?>"
271 408 data-gateway="<?php echo esc_attr( $default_gateway ); ?>"
409 + <?php
410 + // data-stripe-key and data-payment-mode are render-time values and only
411 + // a fallback: the form script fetches the current gateway configuration
412 + // at runtime (Payment_Helper::get_frontend_gateway_config()) because a
413 + // page cache can serve this markup long after a test/live switch.
414 + ?>
272 415 <?php if ( $has_stripe && ! empty( $this->stripe_publishable_key ) ) { ?>
273 416 data-stripe-key="<?php echo esc_attr( $this->stripe_publishable_key ); ?>"
274 417 <?php } ?>
275 418 data-currency="<?php echo esc_attr( strtolower( $this->currency ) ); ?>"
@@ -282,13 +425,28 @@
282 425 data-customer-email-field="<?php echo esc_attr( $this->customer_email_field ); ?>"
283 426 data-nonce="<?php echo esc_attr( wp_create_nonce( 'suredonation_donation_form' ) ); ?>"
284 427 <?php if ( 'variable' === $this->amount_type ) { ?>
285 428 data-variable-amount-field="<?php echo esc_attr( $this->variable_amount_field ); ?>"
429 + <?php } elseif ( '' !== $this->get_inactive_variable_field() ) { ?>
430 + <?php // In 'both' mode, populate the top-level variable-field attribute from whichever choice is the variable one so the amount is resolvable in the initial (one-time) markup; the runtime switch then rewrites it per choice from the per-choice data-* attributes. ?>
431 + data-variable-amount-field="<?php echo esc_attr( $this->get_inactive_variable_field() ); ?>"
286 432 <?php } ?>
287 433 <?php if ( $this->minimum_amount > 0 ) { ?>
288 434 data-minimum-amount="<?php echo esc_attr( (string) $this->minimum_amount ); ?>"
289 435 <?php } ?>
290 - <?php if ( 'subscription' === $this->payment_type && ! empty( $this->subscription_plan ) ) { ?>
436 + <?php if ( 'both' === $this->original_payment_type ) { ?>
437 + data-original-payment-type="both"
438 + data-default-payment-choice="<?php echo esc_attr( $this->default_payment_choice ); ?>"
439 + data-one-time-amount-type="<?php echo esc_attr( Helper::get_string_value( $this->one_time_config['amount_type'] ) ); ?>"
440 + data-one-time-fixed-amount="<?php echo esc_attr( Helper::get_string_value( $this->one_time_config['fixed_amount'] ) ); ?>"
441 + data-one-time-minimum-amount="<?php echo esc_attr( Helper::get_string_value( $this->one_time_config['minimum_amount'] ) ); ?>"
442 + data-one-time-variable-amount-field="<?php echo esc_attr( Helper::get_string_value( $this->one_time_config['variable_field'] ) ); ?>"
443 + data-subscription-amount-type="<?php echo esc_attr( Helper::get_string_value( $this->subscription_config['amount_type'] ) ); ?>"
444 + data-subscription-fixed-amount="<?php echo esc_attr( Helper::get_string_value( $this->subscription_config['fixed_amount'] ) ); ?>"
445 + data-subscription-minimum-amount="<?php echo esc_attr( Helper::get_string_value( $this->subscription_config['minimum_amount'] ) ); ?>"
446 + data-subscription-variable-amount-field="<?php echo esc_attr( Helper::get_string_value( $this->subscription_config['variable_field'] ) ); ?>"
447 + <?php } ?>
448 + <?php if ( $this->has_subscription_path() && ! empty( $this->subscription_plan ) ) { ?>
291 449 data-subscription-plan-name="<?php echo esc_attr( isset( $this->subscription_plan['name'] ) ? Helper::get_string_value( $this->subscription_plan['name'] ) : __( 'Subscription Plan', 'suredonation' ) ); ?>"
292 450 data-subscription-interval="<?php echo esc_attr( isset( $this->subscription_plan['interval'] ) ? Helper::get_string_value( $this->subscription_plan['interval'] ) : 'month' ); ?>"
293 451 data-subscription-billing-cycles="<?php echo esc_attr( isset( $this->subscription_plan['billingCycles'] ) ? Helper::get_string_value( $this->subscription_plan['billingCycles'] ) : '0' ); ?>"
294 452 <?php } ?>
@@ -298,9 +456,16 @@
298 456 <div class="sd-payment-field-wrapper">
299 457 <?php
300 458 // Amount display — uses wp_kses with data attributes allowed
301 459 // because wp_kses_post strips data-message-format, data-currency-symbol etc.
302 - echo wp_kses( $this->render_amount_display(), Helper::get_allowed_form_html() );
460 + if ( 'both' === $this->original_payment_type ) {
461 + // Donor picks the type; both amount panels are rendered and the
462 + // chooser's JS toggles which one is visible.
463 + echo wp_kses( $this->render_payment_type_chooser(), Helper::get_allowed_form_html() );
464 + echo wp_kses( $this->render_dual_amount_displays(), Helper::get_allowed_form_html() );
465 + } else {
466 + echo wp_kses( $this->render_amount_display(), Helper::get_allowed_form_html() );
467 + }
303 468
304 469 // Test mode notice.
305 470 if ( 'test' === $this->payment_mode && $has_stripe ) {
306 471 echo wp_kses_post( $this->get_test_mode_notice() );
@@ -305,8 +470,16 @@
305 470 if ( 'test' === $this->payment_mode && $has_stripe ) {
306 471 echo wp_kses_post( $this->get_test_mode_notice() );
307 472 }
308 473
474 + // Admin-only: the form renders (Offline is enabled) but no real
475 + // gateway is connected.
476 + echo wp_kses_post( $this->get_gateway_setup_notice() );
477 +
478 + // Admin-only: the form renders on its other gateways, but the
479 + // card form was dropped because Stripe cannot charge.
480 + echo wp_kses_post( $this->get_capability_notice() );
481 +
309 482 // Payment methods accordion ( $methods computed above ).
310 483 echo $this->render_payment_methods_accordion( $methods ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Method builds markup with esc_attr/esc_html on all dynamic values and wp_kses_post on content.
311 484 ?>
312 485
@@ -329,9 +502,14 @@
329 502 $available_methods = [
330 503 'stripe' => [
331 504 'id' => 'stripe',
332 505 'label' => __( 'Credit / Debit Card', 'suredonation' ),
333 - 'enabled' => $this->stripe_connected && ! empty( $this->stripe_publishable_key ),
506 + // The capability check belongs here rather than in the
507 + // `suredonation_available_payment_methods` loop above: that loop
508 + // falls back to the raw block selection when it ends up empty, so
509 + // dropping Stripe there would put it straight back on a form that
510 + // offers nothing else.
511 + 'enabled' => $this->stripe_connected && ! empty( $this->stripe_publishable_key ) && ! $this->stripe_card_blocked,
334 512 'container_class' => 'sd-payment-element',
335 513 'container_id' => 'sd-payment-element-' . $this->block_id,
336 514 ],
337 515 'offline' => [
@@ -468,23 +646,165 @@
468 646 return is_string( $template ) ? $template : '';
469 647 }
470 648
471 649 /**
650 + * Whether this block can create a subscription — either it is subscription-only,
651 + * or it is 'both' and the donor may choose recurring.
652 + *
653 + * @return bool
654 + * @since 1.5.1
655 + */
656 + private function has_subscription_path() {
657 + return 'subscription' === $this->payment_type || 'both' === $this->original_payment_type;
658 + }
659 +
660 + /**
661 + * Variable-amount field slug of the choice that is *not* active on page load.
662 + *
663 + * Returns '' outside 'both' mode, or when the inactive choice is not variable.
664 + *
665 + * @return string
666 + * @since 1.5.1
667 + */
668 + private function get_inactive_variable_field() {
669 + if ( 'both' !== $this->original_payment_type ) {
670 + return '';
671 + }
672 +
673 + $inactive = 'subscription' === $this->default_payment_choice
674 + ? $this->one_time_config
675 + : $this->subscription_config;
676 +
677 + return 'variable' === $inactive['amount_type']
678 + ? Helper::get_string_value( $inactive['variable_field'] )
679 + : '';
680 + }
681 +
682 + /**
683 + * Render the "make it recurring" toggle, shown only in 'both' mode.
684 + *
685 + * The block always initialises as one-time; ticking this checkbox opts the donor
686 + * into the recurring choice (the JS reveals the subscription amount panel and
687 + * re-points the block at that config). A single opt-in checkbox is friendlier than
688 + * a two-button chooser for a form that is one-time by default.
689 + *
690 + * @return string Chooser markup.
691 + * @since 1.5.1
692 + */
693 + private function render_payment_type_chooser() {
694 + $checkbox_id = 'sd-payment-recurring-' . $this->block_id;
695 + $one_time_panel = 'sd-payment-amount-' . $this->block_id . '-one-time';
696 + $subscription_panel = 'sd-payment-amount-' . $this->block_id . '-subscription';
697 +
698 + /**
699 + * Filter the label on the "make it recurring" opt-in checkbox. Defaults to the
700 + * admin-configured `recurringToggleLabel` block attribute.
701 + *
702 + * @since 1.5.1
703 + * @param string $label Checkbox label.
704 + * @param string $block_id The payment block id.
705 + */
706 + $label = apply_filters(
707 + 'suredonation_payment_recurring_toggle_label',
708 + $this->recurring_toggle_label,
709 + $this->block_id
710 + );
711 +
712 + // Mirror the checkbox-field markup (sd-checkbox-wrap / sd-checkbox-label /
713 + // sd-input-checkbox / sd-checkbox-text) so this opt-in inherits the exact same
714 + // styling as every other SureDonation checkbox instead of a bespoke look.
715 + ob_start();
716 + ?>
717 + <div class="sd-payment-recurring-toggle">
718 + <div class="sd-block-wrap sd-checkbox-wrap">
719 + <label class="sd-checkbox-label" for="<?php echo esc_attr( $checkbox_id ); ?>">
720 + <input
721 + class="sd-input-checkbox sd-payment-recurring-checkbox"
722 + type="checkbox"
723 + id="<?php echo esc_attr( $checkbox_id ); ?>"
724 + value="1"
725 + <?php // Without this, a soft reload restores the tick while the script re-runs from scratch. The handler reconciles the mismatch too; this stops it arising. ?>
726 + autocomplete="off"
727 + aria-controls="<?php echo esc_attr( $one_time_panel . ' ' . $subscription_panel ); ?>"
728 + />
729 + <span class="sd-checkbox-text"><?php echo esc_html( Helper::get_string_value( $label ) ); ?></span>
730 + </label>
731 + </div>
732 + </div>
733 + <?php
734 + $markup = ob_get_clean();
735 + return is_string( $markup ) ? $markup : '';
736 + }
737 +
738 + /**
739 + * Render both amount panels for 'both' mode, one per choice. Only the default
740 + * choice's panel is visible; the chooser's JS toggles them.
741 + *
742 + * @return string Amount panels markup.
743 + * @since 1.5.1
744 + */
745 + private function render_dual_amount_displays() {
746 + $panels = [
747 + 'one-time' => $this->one_time_config,
748 + 'subscription' => $this->subscription_config,
749 + ];
750 +
751 + ob_start();
752 + foreach ( $panels as $type => $config ) {
753 + $panel_id = 'sd-payment-amount-' . $this->block_id . '-' . $type;
754 + ?>
755 + <div
756 + id="<?php echo esc_attr( $panel_id ); ?>"
757 + class="sd-payment-amount-block sd-payment-amount-block--<?php echo esc_attr( $type ); ?>"
758 + data-payment-type="<?php echo esc_attr( $type ); ?>"
759 + <?php echo $type === $this->default_payment_choice ? '' : 'hidden'; ?>
760 + >
761 + <?php
762 + echo wp_kses(
763 + $this->render_amount_display(
764 + $type,
765 + Helper::get_string_value( $config['amount_type'] ),
766 + floatval( Helper::get_string_value( $config['fixed_amount'] ) ),
767 + floatval( Helper::get_string_value( $config['minimum_amount'] ) )
768 + ),
769 + Helper::get_allowed_form_html()
770 + );
771 + ?>
772 + </div>
773 + <?php
774 + }
775 + $markup = ob_get_clean();
776 + return is_string( $markup ) ? $markup : '';
777 + }
778 +
779 + /**
472 780 * Render amount display section.
473 781 *
782 + * Defaults to this block's active configuration. 'both' mode passes each choice's
783 + * configuration explicitly so the same renderer produces both panels.
784 + *
785 + * @param string|null $payment_type Payment type to render for, or null for the active one.
786 + * @param string|null $amount_type 'fixed' or 'variable', or null for the active one.
787 + * @param float|null $fixed_amount Fixed amount, or null for the active one.
788 + * @param float|null $minimum_amount Minimum amount, or null for the active one.
474 789 * @return string Amount display HTML.
475 790 * @since 0.0.1
476 791 */
477 - private function render_amount_display() {
792 + private function render_amount_display( $payment_type = null, $amount_type = null, $fixed_amount = null, $minimum_amount = null ) {
793 + $payment_type = null === $payment_type ? $this->payment_type : $payment_type;
794 + $amount_type = null === $amount_type ? $this->amount_type : $amount_type;
795 + $fixed_amount = null === $fixed_amount ? $this->fixed_amount : $fixed_amount;
796 + $minimum_amount = null === $minimum_amount ? $this->minimum_amount : $minimum_amount;
797 +
478 798 ob_start();
479 799
480 - if ( 'fixed' === $this->amount_type ) {
800 + if ( 'fixed' === $amount_type ) {
481 801 ?>
482 802 <!-- Fixed Payment Amount Display. -->
483 803 <div class="sd-payment-amount sd-label">
484 804 <span class="sd-payment-value">
485 805 <?php
486 - if ( 'subscription' === $this->payment_type && ! empty( $this->subscription_plan ) ) {
806 + if ( 'subscription' === $payment_type && ! empty( $this->subscription_plan ) ) {
487 807 $interval = isset( $this->subscription_plan['interval'] ) ? Helper::get_string_value( $this->subscription_plan['interval'] ) : 'month';
488 808 $billing_cycles = isset( $this->subscription_plan['billingCycles'] ) ? Helper::get_string_value( $this->subscription_plan['billingCycles'] ) : '0';
489 809 $interval_label = $this->get_interval_label( $interval );
490 810
@@ -493,9 +813,9 @@
493 813 echo esc_html(
494 814 sprintf(
495 815 /* translators: 1: Amount with currency, 2: Interval (day/week/month/quarter/year) */
496 816 __( '%1$s per %2$s (until cancelled)', 'suredonation' ),
497 - $this->format_currency( $this->fixed_amount, $this->currency ),
817 + $this->format_currency( $fixed_amount, $this->currency ),
498 818 $interval_label
499 819 )
500 820 );
501 821 } elseif ( (int) $billing_cycles > 0 ) {
@@ -502,9 +822,9 @@
502 822 echo esc_html(
503 823 sprintf(
504 824 /* translators: 1: Amount with currency, 2: Interval (day/week/month/quarter/year), 3: Number of billing cycles */
505 825 __( '%1$s per %2$s (%3$s payments)', 'suredonation' ),
506 - $this->format_currency( $this->fixed_amount, $this->currency ),
826 + $this->format_currency( $fixed_amount, $this->currency ),
507 827 $interval_label,
508 828 $billing_cycles
509 829 )
510 830 );
@@ -512,15 +832,15 @@
512 832 echo esc_html(
513 833 sprintf(
514 834 /* translators: 1: Amount with currency, 2: Interval (day/week/month/quarter/year) */
515 835 __( '%1$s per %2$s', 'suredonation' ),
516 - $this->format_currency( $this->fixed_amount, $this->currency ),
836 + $this->format_currency( $fixed_amount, $this->currency ),
517 837 $interval_label
518 838 )
519 839 );
520 840 }
521 841 } else {
522 - echo esc_html( $this->format_currency( $this->fixed_amount, $this->currency ) );
842 + echo esc_html( $this->format_currency( $fixed_amount, $this->currency ) );
523 843 }
524 844 ?>
525 845 </span>
526 846 </div>
@@ -532,9 +852,9 @@
532 852 <div class="sd-payment-amount-wrapper">
533 853 <?php
534 854 // Generate message format for variable amounts.
535 855 $message_format = '{amount}';
536 - if ( 'subscription' === $this->payment_type && ! empty( $this->subscription_plan ) ) {
856 + if ( 'subscription' === $payment_type && ! empty( $this->subscription_plan ) ) {
537 857 $interval = isset( $this->subscription_plan['interval'] ) ? Helper::get_string_value( $this->subscription_plan['interval'] ) : 'month';
538 858 $billing_cycles = isset( $this->subscription_plan['billingCycles'] ) ? Helper::get_string_value( $this->subscription_plan['billingCycles'] ) : '0';
539 859 $interval_label = $this->get_interval_label( $interval );
540 860
@@ -554,9 +874,9 @@
554 874 <span class="sd-payment-value" data-currency="<?php echo esc_attr( strtolower( $this->currency ) ); ?>" data-currency-symbol="<?php echo esc_attr( Payment_Helper::get_currency_symbol( $this->currency ) ); ?>" data-message-format="<?php echo esc_attr( $message_format ); ?>">
555 875 <?php esc_html_e( 'Complete the form to view the amount.', 'suredonation' ); ?>
556 876 </span>
557 877 </div>
558 - <?php if ( $this->minimum_amount > 0 ) { ?>
878 + <?php if ( $minimum_amount > 0 ) { ?>
559 879 <span class="sd-help">
560 880 <?php
561 881 echo esc_html(
562 882 sprintf(
@@ -561,9 +881,9 @@
561 881 echo esc_html(
562 882 sprintf(
563 883 /* translators: %s: Minimum amount with currency */
564 884 __( 'Minimum amount: %s', 'suredonation' ),
565 - $this->format_currency( $this->minimum_amount, $this->currency )
885 + $this->format_currency( $minimum_amount, $this->currency )
566 886 )
567 887 );
568 888 ?>
569 889 </span>
@@ -586,10 +906,13 @@
586 906 if ( empty( $this->customer_email_field ) ) {
587 907 return false;
588 908 }
589 909
590 - // Check subscription-specific requirements.
591 - if ( 'subscription' === $this->payment_type ) {
910 + // Check subscription-specific requirements. Keyed on whether a subscription
911 + // is reachable at all, not on the active type: in 'both' mode the donor can
912 + // switch to recurring even when one-time is the default, so the name field
913 + // and plan must be configured either way.
914 + if ( $this->has_subscription_path() ) {
592 915 if ( empty( $this->customer_name_field ) ) {
593 916 return false;
594 917 }
595 918
@@ -609,17 +932,11 @@
609 932 * @return string
610 933 * @since 0.0.1
611 934 */
612 935 private function format_currency( $amount, $currency ) {
613 - $symbol = Payment_Helper::get_currency_symbol( $currency );
614 -
615 - // Format based on currency.
616 - if ( in_array( $currency, [ 'JPY', 'KRW' ], true ) ) {
617 - // No decimal places for these currencies.
618 - return $symbol . number_format( $amount, 0 );
619 - }
620 -
621 - return $symbol . number_format( $amount, 2 );
936 + // Delegate to the single source of truth so decimal handling and the
937 + // currency sign position stay consistent with every other surface.
938 + return Payment_Helper::format_amount( $amount, $currency );
622 939 }
623 940
624 941 /**
625 942 * Get the human-readable label for a payment interval slug.
@@ -652,11 +969,16 @@
652 969 if ( ! current_user_can( 'manage_options' ) ) {
653 970 return '';
654 971 }
655 972
656 - // Build dynamic link to payment settings.
657 - $settings_url = admin_url( 'admin.php?page=suredonation_settings&tab=payments' );
973 + $this->prevent_page_cache();
658 974
975 + // Build dynamic link to payment settings (shared, hash-routed URL).
976 + // Tagged so the settings screen can record that this notice is what brought
977 + // the admin over. No subpage: the payments screen falls back to the Stripe
978 + // panel, which is where the arrival is read.
979 + $settings_url = Payment_Helper::get_settings_url( '', [ 'sd_notice' => 'frontend_test_mode' ] );
980 +
659 981 ob_start();
660 982 ?>
661 983 <div class="sd-test-mode-notice">
662 984 <strong><?php esc_html_e( 'Test mode is enabled:', 'suredonation' ); ?></strong>
@@ -682,8 +1004,68 @@
682 1004 */
683 1005 private function render_gateway_unavailable_notice() {
684 1006 $field_classes = $this->get_field_classes( [ 'sd-payment-unavailable' ] );
685 1007
1008 + // Both audiences are told the gateways are not configured, but only donors
1009 + // are told to contact the administrator — an admin *is* that person and
1010 + // gets the actionable button below, so the sentence would send them in a
1011 + // circle. Mirrors the capability + settings-URL pattern used by
1012 + // get_test_mode_notice().
1013 + $is_admin = current_user_can( 'manage_options' );
1014 + $configure_url = '';
1015 + $configure_text = '';
1016 +
1017 + // The two audiences get different copy here, so either variant being
1018 + // cached and replayed to the other is wrong.
1019 + if ( $is_admin ) {
1020 + $this->prevent_page_cache();
1021 + }
1022 +
1023 + // "Not configured" is wrong when Stripe is connected and simply cannot
1024 + // charge — the admin would go looking for a setup step they already
1025 + // completed, and reconnecting does not fix an account-side restriction.
1026 + // Say what is actually true instead. Donors are never told which
1027 + // gateway or why: it is the site's problem, not theirs.
1028 + $blocked_only = $this->stripe_card_blocked;
1029 +
1030 + if ( $blocked_only ) {
1031 + $notice_text = $is_admin
1032 + ? __( 'Stripe is connected but cannot accept card payments for this account.', 'suredonation' )
1033 + : __( 'Card payments are currently unavailable. Please contact the site administrator.', 'suredonation' );
1034 + } else {
1035 + $notice_text = $is_admin
1036 + ? __( 'Payment gateways are not configured.', 'suredonation' )
1037 + : __( 'Payment gateways are not configured. Please contact the site administrator.', 'suredonation' );
1038 + }
1039 +
1040 + if ( $is_admin && $blocked_only ) {
1041 + // The fix is in the gateway's own settings, where the capability
1042 + // warning and the Stripe dashboard link live.
1043 + $configure_url = Payment_Helper::get_settings_url( 'stripe', [ 'sd_notice' => 'stripe_capability' ] );
1044 + $configure_text = __( 'Review Stripe account status', 'suredonation' );
1045 + } elseif ( $is_admin ) {
1046 + // Route the admin to the right place. If a gateway is already usable
1047 + // on the site, the block simply hasn't selected it — send them to
1048 + // this form's editor to fix the payment block. Otherwise no gateway
1049 + // is set up at all — send them to global payment settings.
1050 + $edit_link = $this->form_id > 0 ? get_edit_post_link( (int) $this->form_id ) : '';
1051 +
1052 + if ( Payment_Helper::has_usable_gateway() && $edit_link ) {
1053 + // Deliberately untagged: this branch goes to the block editor, not
1054 + // the settings screen, so there is no arrival for it to record.
1055 + // Tracking it would need a hook in the editor, which is not worth
1056 + // a tracker on a public page — the reason this whole mechanism
1057 + // records arrivals rather than clicks.
1058 + $configure_url = $edit_link;
1059 + $configure_text = __( 'Edit this form’s payment settings', 'suredonation' );
1060 + } else {
1061 + // No gateway connected — send the admin straight to the gateway
1062 + // connect screen (Stripe) rather than the currency/mode page.
1063 + $configure_url = Payment_Helper::get_settings_url( 'stripe', [ 'sd_notice' => 'frontend_gateway_unavailable' ] );
1064 + $configure_text = __( 'Configure payment gateway', 'suredonation' );
1065 + }
1066 + }
1067 +
686 1068 ob_start();
687 1069 ?>
688 1070 <div
689 1071 data-block-id="<?php echo esc_attr( $this->block_id ); ?>"
@@ -693,12 +1075,160 @@
693 1075 >
694 1076 <?php echo wp_kses_post( $this->label_markup ); ?>
695 1077 <div class="sd-payment-field-wrapper">
696 1078 <div class="sd-payment-notice" role="status">
697 - <p><?php esc_html_e( 'Payment gateways are not configured. Please contact the site administrator.', 'suredonation' ); ?></p>
1079 + <p><?php echo esc_html( $notice_text ); ?></p>
1080 + <?php echo wp_kses_post( $is_admin ? $this->render_configure_link( $configure_url, $configure_text ) : '' ); ?>
698 1081 </div>
699 1082 </div>
700 1083 </div>
1084 + <?php
1085 + $output = ob_get_clean();
1086 + return false !== $output ? $output : '';
1087 + }
1088 +
1089 + /**
1090 + * Render the admin-only notice shown when the payment field renders but no
1091 + * real payment gateway is connected.
1092 + *
1093 + * Enabling Offline Donation makes the field render normally, so the
1094 + * donor-facing "gateways are not configured" notice never fires — an admin
1095 + * viewing the live form gets no signal that neither Stripe nor PayPal was
1096 + * ever connected. Offline is a manual method, not a gateway, so the prompt
1097 + * is still warranted. Donors see nothing: the form works for them.
1098 + *
1099 + * @return string Notice markup, or empty string when a gateway is connected
1100 + * or the viewer is not an administrator.
1101 + * @since 1.5.1
1102 + */
1103 + private function get_gateway_setup_notice() {
1104 + if ( ! current_user_can( 'manage_options' ) || Payment_Helper::is_any_gateway_connected() ) {
1105 + return '';
1106 + }
1107 +
1108 + $this->prevent_page_cache();
1109 +
1110 + ob_start();
1111 + ?>
1112 + <div class="sd-payment-notice sd-payment-notice--admin" role="status">
1113 + <p><?php esc_html_e( 'No payment gateway is connected, so donors can only give using the offline method.', 'suredonation' ); ?></p>
1114 + <?php
1115 + echo wp_kses_post(
1116 + $this->render_configure_link(
1117 + Payment_Helper::get_settings_url( 'stripe', [ 'sd_notice' => 'frontend_gateway_setup' ] ),
1118 + __( 'Configure payment gateway', 'suredonation' )
1119 + )
1120 + );
1121 + ?>
1122 + </div>
1123 + <?php
1124 + $output = ob_get_clean();
1125 + return false !== $output ? $output : '';
1126 + }
1127 +
1128 + /**
1129 + * Render the admin-only notice shown when the card form was dropped because
1130 + * the connected Stripe account cannot charge cards.
1131 + *
1132 + * Only fires when the field still renders, which means another gateway
1133 + * covered for it. That is the quiet case: donors can still give, so the
1134 + * donor-facing notice never runs, and an admin looking at the live form sees
1135 + * a working PayPal button with no hint that the card option they configured
1136 + * has silently gone. Leaving that unsaid is the same silence this whole
1137 + * feature exists to end, just relocated — the site would keep taking a
1138 + * fraction of the donations it expects until someone happened to notice.
1139 + *
1140 + * Donors see nothing: the form works for them, and the account state is the
1141 + * site's business.
1142 + *
1143 + * @return string Notice markup, or empty string when the card form was not
1144 + * dropped or the viewer is not an administrator.
1145 + * @since 1.5.1
1146 + */
1147 + private function get_capability_notice() {
1148 + if ( ! $this->stripe_card_blocked || ! current_user_can( 'manage_options' ) ) {
1149 + return '';
1150 + }
1151 +
1152 + $this->prevent_page_cache();
1153 +
1154 + ob_start();
1155 + ?>
1156 + <div class="sd-payment-notice sd-payment-notice--admin" role="status">
1157 + <p><?php esc_html_e( 'Stripe is connected but cannot accept card payments for this account, so the card option is hidden on this form.', 'suredonation' ); ?></p>
1158 + <?php
1159 + echo wp_kses_post(
1160 + $this->render_configure_link(
1161 + // The marker rides the query string so the settings screen
1162 + // can record that this notice is what brought the admin
1163 + // there. Tracking the arrival rather than the click keeps
1164 + // every byte of analytics off the public page the notice
1165 + // renders on.
1166 + Payment_Helper::get_settings_url( 'stripe', [ 'sd_notice' => 'stripe_capability' ] ),
1167 + __( 'Review Stripe account status', 'suredonation' )
1168 + )
1169 + );
1170 + ?>
1171 + </div>
1172 + <?php
1173 + $output = ob_get_clean();
1174 + return false !== $output ? $output : '';
1175 + }
1176 +
1177 + /**
1178 + * Keep the current response out of any full-page cache.
1179 + *
1180 + * Several notices here are chosen by a `current_user_can()` check while
1181 + * rendering a public donation page, so the HTML an admin receives differs
1182 + * from a donor's. A full-page cache in front of the site does not know that:
1183 + * if it stores an admin's copy, every later visitor is served admin-only
1184 + * text — the account's Stripe status, and a deep link into wp-admin — and if
1185 + * it stores a donor's, an admin stops being warned at all.
1186 + *
1187 + * Most caches already skip logged-in users, so this is belt to that braces;
1188 + * it is cheap, and the failure it prevents is silent. `DONOTCACHEPAGE` is the
1189 + * convention every major WordPress cache honours, and it is still read after
1190 + * a block renders. `nocache_headers()` covers proxies, guarded because
1191 + * headers are usually long gone by the time a block runs.
1192 + *
1193 + * @return void
1194 + * @since 1.5.1
1195 + */
1196 + private function prevent_page_cache() {
1197 + if ( ! defined( 'DONOTCACHEPAGE' ) ) {
1198 + // Intentionally unprefixed: this is the name the caching plugins
1199 + // read, so a prefixed one would do nothing at all.
1200 + define( 'DONOTCACHEPAGE', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
1201 + }
1202 +
1203 + if ( ! headers_sent() ) {
1204 + nocache_headers();
1205 + }
1206 + }
1207 +
1208 + /**
1209 + * Render the actionable settings link shared by the payment notices.
1210 + *
1211 + * @param string $url Target URL.
1212 + * @param string $text Link text.
1213 + * @return string Link markup, or empty string when there is nothing to link to.
1214 + * @since 1.5.1
1215 + */
1216 + private function render_configure_link( $url, $text ) {
1217 + if ( '' === $url || '' === $text ) {
1218 + return '';
1219 + }
1220 +
1221 + ob_start();
1222 + ?>
1223 + <a
1224 + class="sd-payment-notice__configure"
1225 + href="<?php echo esc_url( $url ); ?>"
1226 + target="_blank"
1227 + rel="noopener noreferrer"
1228 + >
1229 + <?php echo esc_html( $text ); ?>
1230 + </a>
701 1231 <?php
702 1232 $output = ob_get_clean();
703 1233 return false !== $output ? $output : '';
704 1234 }