- Core\GoalTargetPolicy; atomic close in the capture path; friendly 'goal reached' message - In-flight checkouts still capture; new orders for a closed goal are refused - Rewards menu overlay: card hugs content, text scales to the window, resize-aware, scroll only as a last resort Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
141 lines
5.3 KiB
PHP
141 lines
5.3 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\PayPal;
|
|
|
|
use StreamGoals\Core\FeeCalculator;
|
|
use StreamGoals\Core\Money;
|
|
use StreamGoals\Core\ProfanityFilter;
|
|
|
|
/**
|
|
* Pure validation + total computation for a new donation order. The server is the
|
|
* only authority on the total: client-supplied totals/fees are never read.
|
|
*/
|
|
final class OrderValidator {
|
|
|
|
public const NAME_MAX = 100;
|
|
|
|
/**
|
|
* @param array $input Raw client params (goal_id, amount, display_name, anonymous, message, cover_fees).
|
|
* @param array $goal sg_goals row as array (id, currency, status, starts_at, ends_at).
|
|
* @param array $settings Settings::all()-shaped array.
|
|
* @return array{goal_id:int,currency:string,base_minor:int,fee_minor:int,total_minor:int,donor_name:string,is_anonymous:bool,message:string,cover_fees:bool,moderation:string}
|
|
* @throws ValidationException
|
|
*/
|
|
public static function validate( array $input, array $goal, array $settings, FeeCalculator $fees, int $nowTs ): array {
|
|
$q = self::quote( $input, $goal, $settings, $fees, $nowTs );
|
|
$base = $q['base_minor'];
|
|
$currency = $q['currency'];
|
|
$fee = $q['fee_minor'];
|
|
$cover = $q['cover_fees'];
|
|
|
|
$message = self::clean( $input['message'] ?? '' );
|
|
$maxLen = max( 0, min( 500, (int) ( $settings['message_max_length'] ?? 200 ) ) );
|
|
if ( mb_strlen( $message ) > $maxLen ) {
|
|
throw new ValidationException( 'message_too_long', "Message must be at most {$maxLen} characters." );
|
|
}
|
|
|
|
$name = self::clean( $input['display_name'] ?? '' );
|
|
$wantsAnon = filter_var( $input['anonymous'] ?? false, FILTER_VALIDATE_BOOLEAN );
|
|
$policy = (string) ( $settings['anonymity_policy'] ?? 'allow' );
|
|
if ( mb_strlen( $name ) > self::NAME_MAX ) {
|
|
throw new ValidationException( 'name_too_long', 'Display name is too long.' );
|
|
}
|
|
switch ( $policy ) {
|
|
case 'force_anonymous':
|
|
$anon = true;
|
|
break;
|
|
case 'require_name':
|
|
if ( '' === $name ) {
|
|
throw new ValidationException( 'name_required', 'Please enter a display name.' );
|
|
}
|
|
$anon = false;
|
|
break;
|
|
default:
|
|
$anon = $wantsAnon;
|
|
}
|
|
if ( $anon ) {
|
|
$name = ''; // never persist the name of an anonymous donor
|
|
}
|
|
|
|
// Mask profanity before anything is stored (same-length masking, so length checks above still hold).
|
|
$filter = new ProfanityFilter( $settings );
|
|
$name = $filter->filter( $name );
|
|
$message = $filter->filter( $message );
|
|
|
|
return array(
|
|
'goal_id' => (int) $goal['id'],
|
|
'currency' => $currency,
|
|
'base_minor' => $base,
|
|
'fee_minor' => $fee,
|
|
'total_minor' => $base + $fee,
|
|
'donor_name' => $name,
|
|
'is_anonymous' => $anon,
|
|
'message' => $message,
|
|
'cover_fees' => $cover,
|
|
'moderation' => ( ! empty( $settings['moderate_messages'] ) && '' !== $message ) ? 'pending' : 'approved',
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Goal-open check, amount parsing/minimum and fee cover only (used by the live fee preview).
|
|
*
|
|
* @return array{currency:string,base_minor:int,fee_minor:int,total_minor:int,cover_fees:bool}
|
|
* @throws ValidationException
|
|
*/
|
|
public static function quote( array $input, array $goal, array $settings, FeeCalculator $fees, int $nowTs ): array {
|
|
if ( 'complete' === ( $goal['status'] ?? '' ) ) {
|
|
throw new ValidationException( 'goal_inactive', 'This goal is closed and no longer accepting donations. Thank you for your support!', 409 );
|
|
}
|
|
if ( ! self::goalIsOpen( $goal, $nowTs ) ) {
|
|
throw new ValidationException( 'goal_inactive', 'This goal is not accepting donations.', 409 );
|
|
}
|
|
$currency = strtoupper( (string) $goal['currency'] );
|
|
$rawAmount = is_scalar( $input['amount'] ?? null ) ? trim( (string) $input['amount'] ) : '';
|
|
try {
|
|
$base = Money::fromDecimalString( $rawAmount, $currency )->minor;
|
|
} catch ( \InvalidArgumentException $e ) {
|
|
throw new ValidationException( 'invalid_amount', 'Please enter a valid amount.' );
|
|
}
|
|
$min = \StreamGoals\Core\Settings::effectiveMin( (int) ( $settings['min_donation_minor'] ?? 0 ) );
|
|
if ( $base <= 0 || $base < $min ) {
|
|
throw new ValidationException( 'amount_too_small', 'The minimum donation is ' . ( new Money( $min, $currency ) )->toDecimalString() . ' ' . $currency . '.' );
|
|
}
|
|
if ( $base > 99999999 ) { // sanity cap (~$999,999.99)
|
|
throw new ValidationException( 'amount_too_large', 'The amount is too large.' );
|
|
}
|
|
$cover = filter_var( $input['cover_fees'] ?? false, FILTER_VALIDATE_BOOLEAN );
|
|
$fee = $cover ? $fees->feeFor( $base ) : 0;
|
|
return array(
|
|
'currency' => $currency,
|
|
'base_minor' => $base,
|
|
'fee_minor' => $fee,
|
|
'total_minor' => $base + $fee,
|
|
'cover_fees' => $cover,
|
|
);
|
|
}
|
|
|
|
/** Active status and inside the optional [starts_at, ends_at) window (UTC datetimes). */
|
|
public static function goalIsOpen( array $goal, int $nowTs ): bool {
|
|
if ( 'active' !== ( $goal['status'] ?? '' ) ) {
|
|
return false;
|
|
}
|
|
if ( ! empty( $goal['starts_at'] ) && strtotime( $goal['starts_at'] . ' UTC' ) > $nowTs ) {
|
|
return false;
|
|
}
|
|
if ( ! empty( $goal['ends_at'] ) && strtotime( $goal['ends_at'] . ' UTC' ) <= $nowTs ) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private static function clean( mixed $v ): string {
|
|
if ( ! is_scalar( $v ) ) {
|
|
return '';
|
|
}
|
|
$s = strip_tags( (string) $v );
|
|
$s = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', $s ) ?? '';
|
|
return trim( $s );
|
|
}
|
|
}
|