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:
1 parent
65082fb2f5
commit
589e84f28b
21 files changed
+1659
No files matched your search
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Rest;
|
||||
|
||||
use StreamGoals\Core\Settings;
|
||||
use StreamGoals\PayPal\Client;
|
||||
use StreamGoals\PayPal\DonationStore;
|
||||
use StreamGoals\PayPal\WebhookEvent;
|
||||
use StreamGoals\PayPal\WpCache;
|
||||
|
||||
/** PayPal webhook receiver. Signature is verified through PayPal's API before anything is trusted. */
|
||||
final class WebhookController {
|
||||
|
||||
public static function register(): void {
|
||||
add_action( 'rest_api_init', array( self::class, 'routes' ) );
|
||||
}
|
||||
|
||||
public static function routes(): void {
|
||||
register_rest_route(
|
||||
'streamgoals/v1',
|
||||
'/webhook',
|
||||
array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array( self::class, 'handle' ),
|
||||
'permission_callback' => '__return_true', // authenticated by PayPal signature
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function handle( \WP_REST_Request $req ) {
|
||||
$webhookId = (string) Settings::get( 'paypal_webhook_id', '' );
|
||||
if ( '' === $webhookId ) {
|
||||
return new \WP_Error( 'sg_webhook_unconfigured', 'Webhook is not configured.', array( 'status' => 503 ) );
|
||||
}
|
||||
$raw = $req->get_body();
|
||||
if ( ! Client::fromSettings()->verifyWebhook( $req->get_headers(), $raw, $webhookId ) ) {
|
||||
return new \WP_Error( 'sg_webhook_signature', 'Invalid signature.', array( 'status' => 401 ) );
|
||||
}
|
||||
$data = json_decode( $raw, true );
|
||||
if ( ! is_array( $data ) ) {
|
||||
return new \WP_Error( 'sg_webhook_body', 'Bad payload.', array( 'status' => 400 ) );
|
||||
}
|
||||
$ev = WebhookEvent::fromArray( $data );
|
||||
$cache = new WpCache();
|
||||
$seen = 'sg_wh_' . md5( $ev->eventId );
|
||||
if ( '' !== $ev->eventId && false !== $cache->get( $seen ) ) {
|
||||
return rest_ensure_response( array( 'result' => 'duplicate' ) );
|
||||
}
|
||||
|
||||
$result = self::process( $ev );
|
||||
if ( '' !== $ev->eventId ) {
|
||||
$cache->set( $seen, 1, DAY_IN_SECONDS );
|
||||
}
|
||||
return rest_ensure_response( array( 'result' => $result ) );
|
||||
}
|
||||
|
||||
private static function process( WebhookEvent $ev ): string {
|
||||
switch ( $ev->type ) {
|
||||
case 'PAYMENT.CAPTURE.COMPLETED':
|
||||
return self::completed( $ev );
|
||||
case 'PAYMENT.CAPTURE.REFUNDED':
|
||||
return self::reversed( $ev, 'refunded' );
|
||||
case 'PAYMENT.CAPTURE.REVERSED':
|
||||
return self::reversed( $ev, 'reversed' );
|
||||
}
|
||||
return 'ignored';
|
||||
}
|
||||
|
||||
private static function locate( WebhookEvent $ev ): ?object {
|
||||
$row = null;
|
||||
if ( ctype_digit( $ev->customId ) && '' !== $ev->customId ) {
|
||||
$row = DonationStore::find( (int) $ev->customId );
|
||||
}
|
||||
if ( ! $row && '' !== $ev->captureId ) {
|
||||
$row = DonationStore::find_by_capture( $ev->captureId );
|
||||
}
|
||||
if ( ! $row && '' !== $ev->orderId ) {
|
||||
$row = DonationStore::find_by_order( $ev->orderId );
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
|
||||
private static function completed( WebhookEvent $ev ): string {
|
||||
$row = self::locate( $ev );
|
||||
if ( ! $row || '' === $ev->captureId ) {
|
||||
return 'ignored';
|
||||
}
|
||||
if ( 'pending' !== $row->status ) {
|
||||
return 'duplicate'; // already completed via the capture endpoint (or refunded)
|
||||
}
|
||||
if ( ! $ev->amount || $ev->amount->minor !== (int) $row->total_minor || $ev->amount->currency !== $row->currency ) {
|
||||
error_log( 'StreamGoals: webhook amount mismatch for donation ' . $row->id ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
|
||||
return 'ignored';
|
||||
}
|
||||
if ( DonationStore::mark_completed( (int) $row->id, $ev->captureId ) ) {
|
||||
DonationStore::notify_applied( (int) $row->id );
|
||||
return 'completed';
|
||||
}
|
||||
return 'duplicate';
|
||||
}
|
||||
|
||||
private static function reversed( WebhookEvent $ev, string $status ): string {
|
||||
$row = self::locate( $ev );
|
||||
if ( ! $row ) {
|
||||
return 'ignored';
|
||||
}
|
||||
if ( 'completed' !== $row->status ) {
|
||||
return 'duplicate';
|
||||
}
|
||||
// A partial refund does not undo the whole donation; only full refunds/reversals do.
|
||||
if ( 'refunded' === $status && $ev->amount && $ev->amount->minor < (int) $row->total_minor ) {
|
||||
return 'partial_refund_ignored';
|
||||
}
|
||||
if ( DonationStore::mark_reversed( (int) $row->id, $status ) ) {
|
||||
DonationStore::notify_reversed( (int) $row->id );
|
||||
return $status;
|
||||
}
|
||||
return 'duplicate';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user