Add PayPal client, donate flow, webhook; wire rules engine with per-goal lock

Replace the static Engine call (which silently failed) with Engine::default()
and a MySQL named lock. Add engine->overlay integration test.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 08:47:00 -08:00
1 parent 65082fb2f5
commit 589e84f28b
21 files changed
+1659

No files matched your search

+131
View File
@@ -0,0 +1,131 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\PayPal;
use StreamGoals\Core\FeeCalculator;
use StreamGoals\Core\Money;
/**
* 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
}
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 ( ! 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 = (int) ( $settings['min_donation_minor'] ?? 0 );
if ( $base <= 0 || $base < $min ) {
throw new ValidationException( 'amount_too_small', 'The amount is below the minimum donation.' );
}
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 );
}
}