41 lines
1.2 KiB
PHP
41 lines
1.2 KiB
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\PayPal;
|
||
|
|
|
||
|
|
use StreamGoals\Core\Money;
|
||
|
|
|
||
|
|
/** Pure extraction of the fields we care about from a PayPal webhook event. */
|
||
|
|
final class WebhookEvent {
|
||
|
|
|
||
|
|
public function __construct(
|
||
|
|
public readonly string $eventId,
|
||
|
|
public readonly string $type,
|
||
|
|
public readonly string $captureId,
|
||
|
|
public readonly string $customId,
|
||
|
|
public readonly string $orderId,
|
||
|
|
public readonly ?Money $amount
|
||
|
|
) {}
|
||
|
|
|
||
|
|
public static function fromArray( array $event ): self {
|
||
|
|
$r = is_array( $event['resource'] ?? null ) ? $event['resource'] : array();
|
||
|
|
$type = (string) ( $event['event_type'] ?? '' );
|
||
|
|
$capId = (string) ( $r['id'] ?? '' );
|
||
|
|
$amount = null;
|
||
|
|
if ( isset( $r['amount']['value'], $r['amount']['currency_code'] ) ) {
|
||
|
|
try {
|
||
|
|
$amount = Money::fromDecimalString( (string) $r['amount']['value'], (string) $r['amount']['currency_code'] );
|
||
|
|
} catch ( \InvalidArgumentException $e ) {
|
||
|
|
$amount = null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return new self(
|
||
|
|
(string) ( $event['id'] ?? '' ),
|
||
|
|
$type,
|
||
|
|
$capId,
|
||
|
|
(string) ( $r['custom_id'] ?? '' ),
|
||
|
|
(string) ( $r['supplementary_data']['related_ids']['order_id'] ?? '' ),
|
||
|
|
$amount
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|