Drop refund handling, finalize captures via webhook, add profanity filter
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
35f5d1acfb
commit
2e3abf33d9
11 files changed
+420
-82
No files matched your search
@@ -4,6 +4,7 @@ declare( strict_types=1 );
|
||||
namespace StreamGoals\Rest;
|
||||
|
||||
use StreamGoals\Core\Settings;
|
||||
use StreamGoals\PayPal\CaptureFinalizer;
|
||||
use StreamGoals\PayPal\Client;
|
||||
use StreamGoals\PayPal\DonationStore;
|
||||
use StreamGoals\PayPal\WebhookEvent;
|
||||
@@ -55,14 +56,10 @@ final class WebhookController {
|
||||
return rest_ensure_response( array( 'result' => $result ) );
|
||||
}
|
||||
|
||||
/** Only PAYMENT.CAPTURE.COMPLETED is acted on; every other event is acknowledged (200) so PayPal stops retrying. */
|
||||
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' );
|
||||
if ( 'PAYMENT.CAPTURE.COMPLETED' === $ev->type ) {
|
||||
return self::completed( $ev );
|
||||
}
|
||||
return 'ignored';
|
||||
}
|
||||
@@ -86,36 +83,18 @@ final class WebhookController {
|
||||
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
|
||||
$res = CaptureFinalizer::finalize(
|
||||
$row,
|
||||
'COMPLETED',
|
||||
$ev->captureId,
|
||||
$ev->amount,
|
||||
array( DonationStore::class, 'mark_completed' ),
|
||||
array( DonationStore::class, 'notify_applied' )
|
||||
);
|
||||
if ( CaptureFinalizer::PENDING === $res ) {
|
||||
error_log( 'StreamGoals: webhook capture does not match donation ' . $row->id . '; left pending' ); // 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';
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user