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

+123
View File
@@ -0,0 +1,123 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\PayPal;
/** $wpdb access for sg_donations / sg_goals used by the donate flow. WordPress-only. */
final class DonationStore {
private static function t( string $name ): string {
global $wpdb;
return $wpdb->prefix . 'sg_' . $name;
}
public static function goal( int $id ): ?array {
global $wpdb;
$row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . self::t( 'goals' ) . ' WHERE id = %d', $id ), ARRAY_A );
return $row ?: null;
}
/** @return array[] */
public static function active_goals(): array {
global $wpdb;
$rows = $wpdb->get_results( 'SELECT * FROM ' . self::t( 'goals' ) . " WHERE status = 'active' ORDER BY id ASC", ARRAY_A );
$now = time();
return array_values( array_filter( (array) $rows, static fn( array $g ): bool => OrderValidator::goalIsOpen( $g, $now ) ) );
}
public static function find( int $id ): ?object {
global $wpdb;
return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . self::t( 'donations' ) . ' WHERE id = %d', $id ) ) ?: null;
}
public static function find_by_order( string $orderId ): ?object {
global $wpdb;
return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . self::t( 'donations' ) . ' WHERE paypal_order_id = %s ORDER BY id DESC LIMIT 1', $orderId ) ) ?: null;
}
public static function find_by_capture( string $captureId ): ?object {
global $wpdb;
if ( '' === $captureId ) {
return null;
}
return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . self::t( 'donations' ) . ' WHERE paypal_capture_id = %s', $captureId ) ) ?: null;
}
/** @param array $v Output of OrderValidator::validate(). @return int Donation id (0 on failure). */
public static function insert_pending( array $v ): int {
global $wpdb;
$ok = $wpdb->insert(
self::t( 'donations' ),
array(
'goal_id' => $v['goal_id'],
'paypal_order_id' => '',
'base_minor' => $v['base_minor'],
'fee_minor' => $v['fee_minor'],
'total_minor' => $v['total_minor'],
'currency' => $v['currency'],
'donor_name' => $v['donor_name'],
'is_anonymous' => $v['is_anonymous'] ? 1 : 0,
'message' => $v['message'],
'status' => 'pending',
'moderation' => $v['moderation'],
'created_at' => gmdate( 'Y-m-d H:i:s' ),
),
array( '%d', '%s', '%d', '%d', '%d', '%s', '%s', '%d', '%s', '%s', '%s', '%s' )
);
return $ok ? (int) $wpdb->insert_id : 0;
}
public static function set_order_id( int $id, string $orderId ): void {
global $wpdb;
$wpdb->update( self::t( 'donations' ), array( 'paypal_order_id' => $orderId ), array( 'id' => $id ), array( '%s' ), array( '%d' ) );
}
public static function delete( int $id ): void {
global $wpdb;
$wpdb->delete( self::t( 'donations' ), array( 'id' => $id ), array( '%d' ) );
}
/** Atomic pending -> completed. True only for the single caller that wins the transition. */
public static function mark_completed( int $id, string $captureId ): bool {
global $wpdb;
$n = $wpdb->query( $wpdb->prepare( 'UPDATE ' . self::t( 'donations' ) . " SET status = 'completed', paypal_capture_id = %s WHERE id = %d AND status = 'pending'", $captureId, $id ) );
return 1 === $n;
}
/** Atomic completed -> refunded|reversed. */
public static function mark_reversed( int $id, string $status ): bool {
global $wpdb;
if ( ! in_array( $status, array( 'refunded', 'reversed' ), true ) ) {
return false;
}
$n = $wpdb->query( $wpdb->prepare( 'UPDATE ' . self::t( 'donations' ) . " SET status = %s WHERE id = %d AND status = 'completed'", $status, $id ) );
return 1 === $n;
}
/** Hand a freshly completed donation to the rules engine (if present). Never throws. */
public static function notify_applied( int $id ): void {
self::call_engine( 'applyDonation', $id );
}
public static function notify_reversed( int $id ): void {
self::call_engine( 'reverseDonation', $id );
}
private static function call_engine( string $method, int $id ): void {
$row = self::find( $id );
if ( ! $row ) {
return;
}
try {
$donation = \StreamGoals\Core\DonationRepository::toDonation( (array) $row );
\StreamGoals\Rules\Engine::withGoalLock(
$donation->goalId,
static function () use ( $method, $donation ): void {
\StreamGoals\Rules\Engine::default()->$method( $donation );
}
);
} catch ( \Throwable $e ) {
error_log( 'StreamGoals: Engine::' . $method . ' failed for donation ' . $id . ': ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
}
}
}