| 1 |
<?php |
| 2 |
/** |
| 3 |
* SureDonation Payment Markup Class file. |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
* @since 0.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SureDonation\Inc\Fields; |
| 10 |
|
| 11 |
use SureDonation\Inc\Helper; |
| 12 |
use SureDonation\Inc\Payments\Offline\Offline_Helper; |
| 13 |
use SureDonation\Inc\Payments\Payment_Helper; |
| 14 |
use SureDonation\Inc\Payments\Stripe\Stripe_Helper; |
| 15 |
use SureDonation\Inc\Post_Types\Donation_Form; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; // Exit if accessed directly. |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* SureDonation Payment Markup Class. |
| 23 |
* |
| 24 |
* @since 0.0.1 |
| 25 |
*/ |
| 26 |
class Payment_Markup extends Base { |
| 27 |
/** |
| 28 |
* Payment amount. |
| 29 |
* |
| 30 |
* @var float |
| 31 |
* @since 0.0.1 |
| 32 |
*/ |
| 33 |
protected $amount; |
| 34 |
|
| 35 |
/** |
| 36 |
* Payment currency. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
* @since 0.0.1 |
| 40 |
*/ |
| 41 |
protected $currency; |
| 42 |
|
| 43 |
/** |
| 44 |
* Stripe publishable key. |
| 45 |
* |
| 46 |
* @var string |
| 47 |
* @since 0.0.1 |
| 48 |
*/ |
| 49 |
protected $stripe_publishable_key; |
| 50 |
|
| 51 |
/** |
| 52 |
* Whether Stripe is connected. |
| 53 |
* |
| 54 |
* @var bool |
| 55 |
* @since 0.0.1 |
| 56 |
*/ |
| 57 |
protected $stripe_connected; |
| 58 |
|
| 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 |
/** |
| 68 |
* Payment mode (live or test). |
| 69 |
* |
| 70 |
* @var string |
| 71 |
* @since 0.0.1 |
| 72 |
*/ |
| 73 |
protected $payment_mode; |
| 74 |
|
| 75 |
/** |
| 76 |
* Payment type. |
| 77 |
* |
| 78 |
* @var string |
| 79 |
* @since 0.0.1 |
| 80 |
*/ |
| 81 |
protected $payment_type; |
| 82 |
|
| 83 |
/** |
| 84 |
* Subscription plans. |
| 85 |
* |
| 86 |
* @var array<string, mixed> |
| 87 |
* @since 0.0.1 |
| 88 |
*/ |
| 89 |
protected $subscription_plan; |
| 90 |
|
| 91 |
/** |
| 92 |
* Amount type. |
| 93 |
* |
| 94 |
* @var string |
| 95 |
* @since 0.0.1 |
| 96 |
*/ |
| 97 |
protected $amount_type; |
| 98 |
|
| 99 |
/** |
| 100 |
* Fixed amount. |
| 101 |
* |
| 102 |
* @var float |
| 103 |
* @since 0.0.1 |
| 104 |
*/ |
| 105 |
protected $fixed_amount; |
| 106 |
|
| 107 |
/** |
| 108 |
* Minimum amount. |
| 109 |
* |
| 110 |
* @var float |
| 111 |
* @since 0.0.1 |
| 112 |
*/ |
| 113 |
protected $minimum_amount; |
| 114 |
|
| 115 |
/** |
| 116 |
* Customer name field slug. |
| 117 |
* |
| 118 |
* @var string |
| 119 |
* @since 0.0.1 |
| 120 |
*/ |
| 121 |
protected $customer_name_field; |
| 122 |
|
| 123 |
/** |
| 124 |
* Customer email field slug. |
| 125 |
* |
| 126 |
* @var string |
| 127 |
* @since 0.0.1 |
| 128 |
*/ |
| 129 |
protected $customer_email_field; |
| 130 |
|
| 131 |
/** |
| 132 |
* Variable amount field slug. |
| 133 |
* |
| 134 |
* @var string |
| 135 |
* @since 0.0.1 |
| 136 |
*/ |
| 137 |
protected $variable_amount_field; |
| 138 |
|
| 139 |
/** |
| 140 |
* Payment methods enabled for this block. |
| 141 |
* |
| 142 |
* @var array<string> |
| 143 |
* @since 1.0.0 |
| 144 |
*/ |
| 145 |
protected $payment_methods = []; |
| 146 |
|
| 147 |
/** |
| 148 |
* Aria-describedby attribute value. |
| 149 |
* |
| 150 |
* @var string |
| 151 |
* @since 0.0.1 |
| 152 |
*/ |
| 153 |
protected $aria_described_by = ''; |
| 154 |
|
| 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 |
/** |
| 201 |
* Constructor for the Payment Markup class. |
| 202 |
* |
| 203 |
* @param array<mixed> $attributes Block attributes. |
| 204 |
* @since 0.0.1 |
| 205 |
*/ |
| 206 |
public function __construct( $attributes ) { |
| 207 |
// Get payment settings from Stripe Helper. |
| 208 |
$this->stripe_connected = Stripe_Helper::is_stripe_connected(); |
| 209 |
$this->payment_mode = Payment_Helper::get_payment_mode(); |
| 210 |
|
| 211 |
$this->slug = 'payment'; |
| 212 |
$this->set_properties( $attributes ); |
| 213 |
$this->set_unique_slug(); |
| 214 |
$this->set_markup_properties(); |
| 215 |
|
| 216 |
// Build aria-describedby attribute. |
| 217 |
$this->aria_described_by = $this->get_aria_describedby(); |
| 218 |
|
| 219 |
// Set payment-specific properties. |
| 220 |
$this->amount = $attributes['amount'] ?? 10; |
| 221 |
$this->currency = $attributes['currency'] ?? 'USD'; |
| 222 |
|
| 223 |
// Use currency from settings if not specified in block. |
| 224 |
if ( empty( $this->currency ) || 'USD' === $this->currency ) { |
| 225 |
$this->currency = Payment_Helper::get_currency(); |
| 226 |
} |
| 227 |
|
| 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 ); |
| 231 |
|
| 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. |
| 252 |
$configured_type = $attributes['paymentType'] ?? 'one-time'; |
| 253 |
$this->payment_type = Payment_Helper::effective_payment_type( $configured_type ); |
| 254 |
$this->subscription_plan = $attributes['subscriptionPlan'] ?? []; |
| 255 |
$this->amount_type = $attributes['amountType'] ?? 'fixed'; |
| 256 |
$this->fixed_amount = $attributes['fixedAmount'] ?? 10; |
| 257 |
$this->minimum_amount = $attributes['minimumAmount'] ?? 0; |
| 258 |
|
| 259 |
// Set customer field mappings. |
| 260 |
$this->customer_name_field = $attributes['customerNameField'] ?? ''; |
| 261 |
$this->customer_email_field = $attributes['customerEmailField'] ?? ''; |
| 262 |
|
| 263 |
// Set variable amount field mapping. |
| 264 |
$this->variable_amount_field = $attributes['variableAmountField'] ?? ''; |
| 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 |
|
| 319 |
// Parse payment methods from block attributes (with backward compat fallback). |
| 320 |
$block_payment_methods = $attributes['paymentMethods'] ?? null; |
| 321 |
if ( ! is_array( $block_payment_methods ) || empty( $block_payment_methods ) ) { |
| 322 |
$gateway = $attributes['gateway'] ?? 'stripe'; |
| 323 |
$block_payment_methods = [ is_string( $gateway ) ? $gateway : 'stripe' ]; |
| 324 |
} |
| 325 |
|
| 326 |
// Filter to only globally-available gateways. |
| 327 |
$available = []; |
| 328 |
foreach ( $block_payment_methods as $method ) { |
| 329 |
if ( 'stripe' === $method && $this->stripe_connected && ! empty( $this->stripe_publishable_key ) && ! $this->stripe_card_blocked ) { |
| 330 |
$available[] = 'stripe'; |
| 331 |
} elseif ( 'offline' === $method && Offline_Helper::is_offline_enabled() ) { |
| 332 |
$available[] = 'offline'; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Filter the list of available payment methods for this block. |
| 338 |
* |
| 339 |
* Allows extensions (e.g., PayPal) to add themselves when connected. |
| 340 |
* |
| 341 |
* @param array<string> $available Currently available method IDs. |
| 342 |
* @param array<string> $block_payment_methods Methods selected in the block. |
| 343 |
* @param array<string,mixed> $attributes Block attributes. |
| 344 |
* @since 1.0.0 |
| 345 |
*/ |
| 346 |
$available = apply_filters( 'suredonation_available_payment_methods', $available, $block_payment_methods, $attributes ); |
| 347 |
|
| 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; |
| 358 |
|
| 359 |
$this->payment_methods = ! empty( $available ) ? $available : $fallback; |
| 360 |
|
| 361 |
// BACKWARD COMPATIBILITY: Migrate customer fields from subscriptionPlan. |
| 362 |
if ( empty( $this->customer_name_field ) && ! empty( $this->subscription_plan['customer_name'] ) ) { |
| 363 |
$this->customer_name_field = $this->subscription_plan['customer_name']; |
| 364 |
} |
| 365 |
|
| 366 |
if ( empty( $this->customer_email_field ) && ! empty( $this->subscription_plan['customer_email'] ) ) { |
| 367 |
$this->customer_email_field = $this->subscription_plan['customer_email']; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Render the payment field markup. |
| 373 |
* |
| 374 |
* @return string |
| 375 |
* @since 0.0.1 |
| 376 |
*/ |
| 377 |
public function markup() { |
| 378 |
$has_stripe = in_array( 'stripe', $this->payment_methods, true ); |
| 379 |
|
| 380 |
// Determine which gateways are actually connected/available for this form. |
| 381 |
// get_registered_payment_methods() returns only enabled methods (Stripe when |
| 382 |
// connected, Offline when enabled, plus any added by extensions such as |
| 383 |
// PayPal), so it is the single gateway-agnostic source of truth. |
| 384 |
$methods = $this->get_registered_payment_methods(); |
| 385 |
|
| 386 |
// No payment gateway is connected/available. Show a clear message instead of |
| 387 |
// returning an empty string, which previously left donors with a silently |
| 388 |
// broken form and no way to donate. See issue #219. |
| 389 |
if ( empty( $methods ) ) { |
| 390 |
return $this->render_gateway_unavailable_notice(); |
| 391 |
} |
| 392 |
|
| 393 |
// Validate payment field requirements. |
| 394 |
if ( ! $this->validate_payment_requirements() ) { |
| 395 |
return ''; |
| 396 |
} |
| 397 |
|
| 398 |
$field_classes = $this->get_field_classes(); |
| 399 |
$payment_methods_csv = implode( ',', $this->payment_methods ); |
| 400 |
$default_gateway = $this->payment_methods[0]; |
| 401 |
|
| 402 |
ob_start(); |
| 403 |
?> |
| 404 |
<div data-block-id="<?php echo esc_attr( $this->block_id ); ?>" |
| 405 |
data-form-id="<?php echo esc_attr( $this->form_id ); ?>" |
| 406 |
class="<?php echo esc_attr( $field_classes ); ?>" |
| 407 |
data-payment-methods="<?php echo esc_attr( $payment_methods_csv ); ?>" |
| 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 |
?> |
| 415 |
<?php if ( $has_stripe && ! empty( $this->stripe_publishable_key ) ) { ?> |
| 416 |
data-stripe-key="<?php echo esc_attr( $this->stripe_publishable_key ); ?>" |
| 417 |
<?php } ?> |
| 418 |
data-currency="<?php echo esc_attr( strtolower( $this->currency ) ); ?>" |
| 419 |
data-currency-symbol="<?php echo esc_attr( Payment_Helper::get_currency_symbol( $this->currency ) ); ?>" |
| 420 |
data-payment-mode="<?php echo esc_attr( $this->payment_mode ); ?>" |
| 421 |
data-amount-type="<?php echo esc_attr( $this->amount_type ); ?>" |
| 422 |
data-fixed-amount="<?php echo esc_attr( (string) $this->fixed_amount ); ?>" |
| 423 |
data-payment-type="<?php echo esc_attr( $this->payment_type ); ?>" |
| 424 |
data-customer-name-field="<?php echo esc_attr( $this->customer_name_field ); ?>" |
| 425 |
data-customer-email-field="<?php echo esc_attr( $this->customer_email_field ); ?>" |
| 426 |
data-nonce="<?php echo esc_attr( wp_create_nonce( 'suredonation_donation_form' ) ); ?>" |
| 427 |
<?php if ( 'variable' === $this->amount_type ) { ?> |
| 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() ); ?>" |
| 432 |
<?php } ?> |
| 433 |
<?php if ( $this->minimum_amount > 0 ) { ?> |
| 434 |
data-minimum-amount="<?php echo esc_attr( (string) $this->minimum_amount ); ?>" |
| 435 |
<?php } ?> |
| 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 ) ) { ?> |
| 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' ) ); ?>" |
| 450 |
data-subscription-interval="<?php echo esc_attr( isset( $this->subscription_plan['interval'] ) ? Helper::get_string_value( $this->subscription_plan['interval'] ) : 'month' ); ?>" |
| 451 |
data-subscription-billing-cycles="<?php echo esc_attr( isset( $this->subscription_plan['billingCycles'] ) ? Helper::get_string_value( $this->subscription_plan['billingCycles'] ) : '0' ); ?>" |
| 452 |
<?php } ?> |
| 453 |
> |
| 454 |
<?php echo wp_kses_post( $this->label_markup ); ?> |
| 455 |
<?php echo wp_kses_post( $this->help_markup ); ?> |
| 456 |
<div class="sd-payment-field-wrapper"> |
| 457 |
<?php |
| 458 |
// Amount display — uses wp_kses with data attributes allowed |
| 459 |
// because wp_kses_post strips data-message-format, data-currency-symbol etc. |
| 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 |
} |
| 468 |
|
| 469 |
// Test mode notice. |
| 470 |
if ( 'test' === $this->payment_mode && $has_stripe ) { |
| 471 |
echo wp_kses_post( $this->get_test_mode_notice() ); |
| 472 |
} |
| 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 |
|
| 482 |
// Payment methods accordion ( $methods computed above ). |
| 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. |
| 484 |
?> |
| 485 |
|
| 486 |
<!-- Payment error display (hidden via CSS; shown by JS on error). --> |
| 487 |
<div class="sd-payment-error"></div> |
| 488 |
</div> |
| 489 |
</div> |
| 490 |
<?php |
| 491 |
$output = ob_get_clean(); |
| 492 |
return false !== $output ? $output : ''; |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* Get registered payment methods for display. |
| 497 |
* |
| 498 |
* @return array<mixed> Array of payment method configurations. |
| 499 |
* @since 1.0.0 |
| 500 |
*/ |
| 501 |
private function get_registered_payment_methods() { |
| 502 |
$available_methods = [ |
| 503 |
'stripe' => [ |
| 504 |
'id' => 'stripe', |
| 505 |
'label' => __( 'Credit / Debit Card', 'suredonation' ), |
| 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, |
| 512 |
'container_class' => 'sd-payment-element', |
| 513 |
'container_id' => 'sd-payment-element-' . $this->block_id, |
| 514 |
], |
| 515 |
'offline' => [ |
| 516 |
'id' => 'offline', |
| 517 |
'label' => __( 'Offline Donation', 'suredonation' ), |
| 518 |
'enabled' => Offline_Helper::is_offline_enabled(), |
| 519 |
'container_class' => 'sd-offline-instructions', |
| 520 |
'content' => Offline_Helper::get_offline_instructions( $this->get_campaign_name() ), |
| 521 |
], |
| 522 |
]; |
| 523 |
|
| 524 |
$methods = []; |
| 525 |
foreach ( $this->payment_methods as $method_id ) { |
| 526 |
if ( isset( $available_methods[ $method_id ] ) && $available_methods[ $method_id ]['enabled'] ) { |
| 527 |
$methods[ $method_id ] = $available_methods[ $method_id ]; |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* Filter the registered payment methods and their display config. |
| 533 |
* |
| 534 |
* Allows extensions to add payment methods (e.g., PayPal) with their |
| 535 |
* label, container class, and enabled state for frontend rendering. |
| 536 |
* |
| 537 |
* @param array<string, array<string,mixed>> $methods Registered methods keyed by ID. |
| 538 |
* @param array<string> $payment_methods Methods selected in this block. |
| 539 |
* @param string $block_id The block ID. |
| 540 |
* @since 1.0.0 |
| 541 |
*/ |
| 542 |
return apply_filters( 'suredonation_registered_payment_methods', $methods, $this->payment_methods, $this->block_id ); |
| 543 |
} |
| 544 |
|
| 545 |
/** |
| 546 |
* Get the campaign name for this form. |
| 547 |
* |
| 548 |
* @return string Campaign name or empty string. |
| 549 |
* @since 1.0.0 |
| 550 |
*/ |
| 551 |
private function get_campaign_name() { |
| 552 |
if ( empty( $this->form_id ) ) { |
| 553 |
return ''; |
| 554 |
} |
| 555 |
|
| 556 |
$campaign_id = Donation_Form::get_form_campaign_id( absint( $this->form_id ) ); |
| 557 |
|
| 558 |
if ( empty( $campaign_id ) ) { |
| 559 |
return ''; |
| 560 |
} |
| 561 |
|
| 562 |
return get_the_title( $campaign_id ); |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* Render payment methods as accordion. |
| 567 |
* Each payment method is an accordion item with header and collapsible content. |
| 568 |
* |
| 569 |
* @param array<mixed> $methods Array of payment methods. |
| 570 |
* @return string Payment methods accordion markup. |
| 571 |
* @since 1.0.0 |
| 572 |
*/ |
| 573 |
private function render_payment_methods_accordion( $methods ) { |
| 574 |
if ( empty( $methods ) || ! is_array( $methods ) ) { |
| 575 |
return ''; |
| 576 |
} |
| 577 |
|
| 578 |
$is_single_method = count( $methods ) === 1; |
| 579 |
$is_first = true; |
| 580 |
|
| 581 |
ob_start(); |
| 582 |
?> |
| 583 |
<div class="sd-payment-methods-accordion <?php echo esc_attr( $is_single_method ? 'sd-single-payment-method' : '' ); ?>"> |
| 584 |
<?php foreach ( $methods as $method ) { ?> |
| 585 |
<div |
| 586 |
class="sd-accordion-item <?php echo esc_attr( $is_first ? 'sd-payment-active' : '' ); ?>" |
| 587 |
data-method="<?php echo esc_attr( $method['id'] ); ?>" |
| 588 |
> |
| 589 |
<?php if ( $is_single_method ) { ?> |
| 590 |
<?php // Single method: the header is a static label, not an accordion toggle, so omit the interactive button semantics (keyboard/screen-reader users shouldn't hit an inert "button"). ?> |
| 591 |
<div class="sd-accordion-header"> |
| 592 |
<?php } else { ?> |
| 593 |
<div |
| 594 |
class="sd-accordion-header" |
| 595 |
role="button" |
| 596 |
tabindex="0" |
| 597 |
aria-expanded="<?php echo esc_attr( $is_first ? 'true' : 'false' ); ?>" |
| 598 |
aria-controls="sd-accordion-content-<?php echo esc_attr( $method['id'] ); ?>-<?php echo esc_attr( $this->block_id ); ?>" |
| 599 |
> |
| 600 |
<?php } ?> |
| 601 |
<div class="sd-payment-input-wrapper"> |
| 602 |
<input |
| 603 |
type="radio" |
| 604 |
name="sd-gateway-choice-<?php echo esc_attr( $this->block_id ); ?>" |
| 605 |
value="<?php echo esc_attr( $method['id'] ); ?>" |
| 606 |
class="sd-payment-method-radio" |
| 607 |
data-method="<?php echo esc_attr( $method['id'] ); ?>" |
| 608 |
<?php checked( $is_first ); ?> |
| 609 |
aria-label="<?php echo esc_attr( $method['label'] ); ?>" |
| 610 |
/> |
| 611 |
<span class="sd-accordion-title sd-block-label"> |
| 612 |
<?php echo esc_html( $method['label'] ); ?> |
| 613 |
</span> |
| 614 |
</div> |
| 615 |
</div> |
| 616 |
<div |
| 617 |
id="sd-accordion-content-<?php echo esc_attr( $method['id'] ); ?>-<?php echo esc_attr( $this->block_id ); ?>" |
| 618 |
class="sd-accordion-content<?php echo Offline_Helper::is_blank_instructions( $method['content'] ?? '' ) ? ' sd-accordion-content-empty' : ''; ?>" |
| 619 |
role="region" |
| 620 |
> |
| 621 |
<div |
| 622 |
class="sd-payment-method-content" |
| 623 |
data-method="<?php echo esc_attr( $method['id'] ); ?>" |
| 624 |
> |
| 625 |
<div |
| 626 |
<?php if ( ! empty( $method['container_id'] ) ) { ?> |
| 627 |
id="<?php echo esc_attr( $method['container_id'] ); ?>" |
| 628 |
<?php } ?> |
| 629 |
class="<?php echo esc_attr( $method['container_class'] ); ?>" |
| 630 |
> |
| 631 |
<?php |
| 632 |
if ( ! empty( $method['content'] ) ) { |
| 633 |
echo wp_kses_post( $method['content'] ); |
| 634 |
} |
| 635 |
?> |
| 636 |
</div> |
| 637 |
</div> |
| 638 |
</div> |
| 639 |
</div> |
| 640 |
<?php $is_first = false; ?> |
| 641 |
<?php } ?> |
| 642 |
</div> |
| 643 |
<?php |
| 644 |
$template = ob_get_clean(); |
| 645 |
|
| 646 |
return is_string( $template ) ? $template : ''; |
| 647 |
} |
| 648 |
|
| 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 |
/** |
| 780 |
* Render amount display section. |
| 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. |
| 789 |
* @return string Amount display HTML. |
| 790 |
* @since 0.0.1 |
| 791 |
*/ |
| 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 |
|
| 798 |
ob_start(); |
| 799 |
|
| 800 |
if ( 'fixed' === $amount_type ) { |
| 801 |
?> |
| 802 |
<!-- Fixed Payment Amount Display. --> |
| 803 |
<div class="sd-payment-amount sd-label"> |
| 804 |
<span class="sd-payment-value"> |
| 805 |
<?php |
| 806 |
if ( 'subscription' === $payment_type && ! empty( $this->subscription_plan ) ) { |
| 807 |
$interval = isset( $this->subscription_plan['interval'] ) ? Helper::get_string_value( $this->subscription_plan['interval'] ) : 'month'; |
| 808 |
$billing_cycles = isset( $this->subscription_plan['billingCycles'] ) ? Helper::get_string_value( $this->subscription_plan['billingCycles'] ) : '0'; |
| 809 |
$interval_label = $this->get_interval_label( $interval ); |
| 810 |
|
| 811 |
// Build subscription text. |
| 812 |
if ( 'ongoing' === $billing_cycles ) { |
| 813 |
echo esc_html( |
| 814 |
sprintf( |
| 815 |
/* translators: 1: Amount with currency, 2: Interval (day/week/month/quarter/year) */ |
| 816 |
__( '%1$s per %2$s (until cancelled)', 'suredonation' ), |
| 817 |
$this->format_currency( $fixed_amount, $this->currency ), |
| 818 |
$interval_label |
| 819 |
) |
| 820 |
); |
| 821 |
} elseif ( (int) $billing_cycles > 0 ) { |
| 822 |
echo esc_html( |
| 823 |
sprintf( |
| 824 |
/* translators: 1: Amount with currency, 2: Interval (day/week/month/quarter/year), 3: Number of billing cycles */ |
| 825 |
__( '%1$s per %2$s (%3$s payments)', 'suredonation' ), |
| 826 |
$this->format_currency( $fixed_amount, $this->currency ), |
| 827 |
$interval_label, |
| 828 |
$billing_cycles |
| 829 |
) |
| 830 |
); |
| 831 |
} else { |
| 832 |
echo esc_html( |
| 833 |
sprintf( |
| 834 |
/* translators: 1: Amount with currency, 2: Interval (day/week/month/quarter/year) */ |
| 835 |
__( '%1$s per %2$s', 'suredonation' ), |
| 836 |
$this->format_currency( $fixed_amount, $this->currency ), |
| 837 |
$interval_label |
| 838 |
) |
| 839 |
); |
| 840 |
} |
| 841 |
} else { |
| 842 |
echo esc_html( $this->format_currency( $fixed_amount, $this->currency ) ); |
| 843 |
} |
| 844 |
?> |
| 845 |
</span> |
| 846 |
</div> |
| 847 |
<?php |
| 848 |
} else { |
| 849 |
?> |
| 850 |
<!-- Variable Payment Amount Display. --> |
| 851 |
<div class="sd-variable-amount-display sd-label"> |
| 852 |
<div class="sd-payment-amount-wrapper"> |
| 853 |
<?php |
| 854 |
// Generate message format for variable amounts. |
| 855 |
$message_format = '{amount}'; |
| 856 |
if ( 'subscription' === $payment_type && ! empty( $this->subscription_plan ) ) { |
| 857 |
$interval = isset( $this->subscription_plan['interval'] ) ? Helper::get_string_value( $this->subscription_plan['interval'] ) : 'month'; |
| 858 |
$billing_cycles = isset( $this->subscription_plan['billingCycles'] ) ? Helper::get_string_value( $this->subscription_plan['billingCycles'] ) : '0'; |
| 859 |
$interval_label = $this->get_interval_label( $interval ); |
| 860 |
|
| 861 |
// Build message format based on billing cycles. |
| 862 |
if ( 'ongoing' === $billing_cycles ) { |
| 863 |
/* translators: 1: Amount with currency placeholder, 2: Interval (day/week/month/quarter/year) */ |
| 864 |
$message_format = sprintf( __( '{amount} per %s (until cancelled)', 'suredonation' ), $interval_label ); |
| 865 |
} elseif ( (int) $billing_cycles > 0 ) { |
| 866 |
/* translators: 1: Amount with currency placeholder, 2: Interval (day/week/month/quarter/year), 3: Number of billing cycles */ |
| 867 |
$message_format = sprintf( __( '{amount} per %1$s (%2$s payments)', 'suredonation' ), $interval_label, $billing_cycles ); |
| 868 |
} else { |
| 869 |
/* translators: 1: Amount with currency placeholder, 2: Interval (day/week/month/quarter/year) */ |
| 870 |
$message_format = sprintf( __( '{amount} per %s', 'suredonation' ), $interval_label ); |
| 871 |
} |
| 872 |
} |
| 873 |
?> |
| 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 ); ?>"> |
| 875 |
<?php esc_html_e( 'Complete the form to view the amount.', 'suredonation' ); ?> |
| 876 |
</span> |
| 877 |
</div> |
| 878 |
<?php if ( $minimum_amount > 0 ) { ?> |
| 879 |
<span class="sd-help"> |
| 880 |
<?php |
| 881 |
echo esc_html( |
| 882 |
sprintf( |
| 883 |
/* translators: %s: Minimum amount with currency */ |
| 884 |
__( 'Minimum amount: %s', 'suredonation' ), |
| 885 |
$this->format_currency( $minimum_amount, $this->currency ) |
| 886 |
) |
| 887 |
); |
| 888 |
?> |
| 889 |
</span> |
| 890 |
<?php } ?> |
| 891 |
</div> |
| 892 |
<?php |
| 893 |
} |
| 894 |
|
| 895 |
return (string) ob_get_clean(); |
| 896 |
} |
| 897 |
|
| 898 |
/** |
| 899 |
* Validate payment field requirements. |
| 900 |
* |
| 901 |
* @return bool True if validation passes, false otherwise. |
| 902 |
* @since 0.0.1 |
| 903 |
*/ |
| 904 |
private function validate_payment_requirements() { |
| 905 |
// Check customer email field requirement (highest priority). |
| 906 |
if ( empty( $this->customer_email_field ) ) { |
| 907 |
return false; |
| 908 |
} |
| 909 |
|
| 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() ) { |
| 915 |
if ( empty( $this->customer_name_field ) ) { |
| 916 |
return false; |
| 917 |
} |
| 918 |
|
| 919 |
if ( empty( $this->subscription_plan ) || empty( $this->subscription_plan['name'] ) ) { |
| 920 |
return false; |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
return true; |
| 925 |
} |
| 926 |
|
| 927 |
/** |
| 928 |
* Format currency for display. |
| 929 |
* |
| 930 |
* @param float $amount Amount to format. |
| 931 |
* @param string $currency Currency code. |
| 932 |
* @return string |
| 933 |
* @since 0.0.1 |
| 934 |
*/ |
| 935 |
private function format_currency( $amount, $currency ) { |
| 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 ); |
| 939 |
} |
| 940 |
|
| 941 |
/** |
| 942 |
* Get the human-readable label for a payment interval slug. |
| 943 |
* |
| 944 |
* @param string $interval_slug The slug (e.g., 'day', 'week', 'month', 'quarter', 'yearly'). |
| 945 |
* @return string The translated interval label, or the slug itself if not found. |
| 946 |
* @since 0.0.1 |
| 947 |
*/ |
| 948 |
private function get_interval_label( $interval_slug ) { |
| 949 |
$interval_labels = [ |
| 950 |
'day' => __( 'day', 'suredonation' ), |
| 951 |
'week' => __( 'week', 'suredonation' ), |
| 952 |
'month' => __( 'month', 'suredonation' ), |
| 953 |
'quarter' => __( 'quarter', 'suredonation' ), |
| 954 |
'yearly' => __( 'year', 'suredonation' ), |
| 955 |
]; |
| 956 |
|
| 957 |
return $interval_labels[ $interval_slug ] ?? $interval_slug; |
| 958 |
} |
| 959 |
|
| 960 |
/** |
| 961 |
* Render test mode notice for admin users. |
| 962 |
* Only shows if user has manage_options capability. |
| 963 |
* |
| 964 |
* @return string Test mode notice markup or empty string. |
| 965 |
* @since 0.0.1 |
| 966 |
*/ |
| 967 |
private function get_test_mode_notice() { |
| 968 |
// Only show to users with manage_options capability. |
| 969 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 970 |
return ''; |
| 971 |
} |
| 972 |
|
| 973 |
$this->prevent_page_cache(); |
| 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 |
|
| 981 |
ob_start(); |
| 982 |
?> |
| 983 |
<div class="sd-test-mode-notice"> |
| 984 |
<strong><?php esc_html_e( 'Test mode is enabled:', 'suredonation' ); ?></strong> |
| 985 |
<a href="<?php echo esc_url( $settings_url ); ?>" target="_blank" rel="noopener noreferrer"> |
| 986 |
<?php esc_html_e( 'Click here to enable live mode and accept payment', 'suredonation' ); ?> |
| 987 |
</a> |
| 988 |
</div> |
| 989 |
<?php |
| 990 |
$output = ob_get_clean(); |
| 991 |
return false !== $output ? $output : ''; |
| 992 |
} |
| 993 |
|
| 994 |
/** |
| 995 |
* Render the notice shown when no payment gateway is connected/available. |
| 996 |
* |
| 997 |
* Replaces the previous behavior of rendering nothing, which left donors with |
| 998 |
* a silently broken form. The `data-payment-available="0"` marker lets the |
| 999 |
* frontend script skip gateway init and hide the otherwise-inert submit |
| 1000 |
* button. See issue #219. |
| 1001 |
* |
| 1002 |
* @return string Notice markup. |
| 1003 |
* @since 1.1.1 |
| 1004 |
*/ |
| 1005 |
private function render_gateway_unavailable_notice() { |
| 1006 |
$field_classes = $this->get_field_classes( [ 'sd-payment-unavailable' ] ); |
| 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 |
|
| 1068 |
ob_start(); |
| 1069 |
?> |
| 1070 |
<div |
| 1071 |
data-block-id="<?php echo esc_attr( $this->block_id ); ?>" |
| 1072 |
data-form-id="<?php echo esc_attr( $this->form_id ); ?>" |
| 1073 |
data-payment-available="0" |
| 1074 |
class="<?php echo esc_attr( $field_classes ); ?>" |
| 1075 |
> |
| 1076 |
<?php echo wp_kses_post( $this->label_markup ); ?> |
| 1077 |
<div class="sd-payment-field-wrapper"> |
| 1078 |
<div class="sd-payment-notice" role="status"> |
| 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 ) : '' ); ?> |
| 1081 |
</div> |
| 1082 |
</div> |
| 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> |
| 1231 |
<?php |
| 1232 |
$output = ob_get_clean(); |
| 1233 |
return false !== $output ? $output : ''; |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|