'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'; } }