From 0eaee8514ba87c3ba3324395ab70f17581ac5061 Mon Sep 17 00:00:00 2001 From: Michael Woodward Date: Fri, 16 Feb 2024 14:39:38 +0000 Subject: [PATCH] feat!: Split Notification objects from API Entities (#38) Allows long term backwards compatibility with notifications whilst not reducing the type safety of API entities BREAKING CHANGE: Any existing notification handling will result in different types for objects but their behaviour/functionality remains the same --- rector.php | 2 + src/Entities/Event.php | 16 +- src/Notifications/Entities/Address.php | 59 +++ src/Notifications/Entities/Adjustment.php | 65 +++ .../Entities/Adjustment/AdjustmentItem.php | 41 ++ src/Notifications/Entities/Business.php | 55 ++ .../Entities/Business/BusinessesContacts.php | 26 + src/Notifications/Entities/Customer.php | 52 ++ src/Notifications/Entities/DateTime.php | 36 ++ .../Entities/Discount.php} | 16 +- .../Entities/Discount/DiscountStatus.php | 21 + .../Entities/Discount/DiscountType.php | 26 + src/Notifications/Entities/Entity.php | 20 + src/Notifications/Entities/Payout.php | 29 ++ .../Entities/Payout/PayoutStatus.php | 17 + src/Notifications/Entities/Price.php | 71 +++ src/Notifications/Entities/Product.php | 54 ++ src/Notifications/Entities/Report.php | 50 ++ .../Entities/Report/ReportFilter.php | 31 ++ .../Entities/Report/ReportFilterName.php | 32 ++ .../Entities/Report/ReportFilterOperator.php | 24 + .../Entities/Report/ReportStatus.php | 28 + .../Entities/Report/ReportType.php | 28 + src/Notifications/Entities/Shared/Action.php | 32 ++ .../Entities/Shared/AddressPreview.php | 29 ++ .../Entities/Shared/AdjustmentItemTotals.php | 31 ++ .../Entities/Shared/AdjustmentProration.php | 29 ++ .../Entities/Shared/AdjustmentStatus.php | 28 + .../Entities/Shared/AdjustmentTimePeriod.php | 31 ++ .../Entities/Shared/AdjustmentTotals.php | 37 ++ .../Entities/Shared/AdjustmentType.php | 28 + .../Shared/AvailablePaymentMethods.php | 34 ++ .../Entities/Shared/BillingDetails.php | 33 ++ src/Notifications/Entities/Shared/Card.php | 37 ++ .../Entities/Shared/CatalogType.php | 24 + .../Entities/Shared/ChargebackFee.php | 26 + .../Entities/Shared/Checkout.php | 25 + .../Entities/Shared/CollectionMode.php | 24 + .../Entities/Shared/CountryCode.php | 478 ++++++++++++++++++ .../Entities/Shared/CurrencyCode.php | 80 +++ .../Shared/CurrencyCodeAdjustments.php | 26 + .../Entities/Shared/CurrencyCodePayouts.php | 46 ++ .../Entities/Shared/CustomData.php | 25 + src/Notifications/Entities/Shared/Data.php | 20 + .../Entities/Shared/ErrorCode.php | 54 ++ .../Entities/Shared/ImportMeta.php | 29 ++ .../Entities/Shared/Interval.php | 28 + .../Entities/Shared/MethodDetails.php | 29 ++ src/Notifications/Entities/Shared/Money.php | 31 ++ .../Entities/Shared/Original.php | 26 + .../Entities/Shared/PaymentAttemptStatus.php | 40 ++ .../Shared/PayoutTotalsAdjustment.php | 39 ++ .../Entities/Shared/PriceQuantity.php | 26 + src/Notifications/Entities/Shared/Status.php | 24 + .../Entities/Shared/TaxCategory.php | 38 ++ src/Notifications/Entities/Shared/TaxMode.php | 26 + .../Entities/Shared/TaxRatesUsed.php | 29 ++ .../Entities/Shared/TimePeriod.php | 29 ++ src/Notifications/Entities/Shared/Totals.php | 33 ++ .../Entities/Shared/TransactionOrigin.php | 32 ++ .../Shared/TransactionPaymentAttempt.php | 48 ++ .../Shared/TransactionPayoutTotals.php | 47 ++ .../TransactionPayoutTotalsAdjusted.php | 39 ++ .../Entities/Shared/TransactionStatus.php | 34 ++ .../Entities/Shared/TransactionTotals.php | 47 ++ .../Shared/TransactionTotalsAdjusted.php | 39 ++ src/Notifications/Entities/Shared/Type.php | 42 ++ .../Entities/Shared/UnitPriceOverride.php | 29 ++ .../Entities/Shared/UnitTotals.php | 33 ++ .../Entities/Subscription.php} | 30 +- .../SubscriptionAdjustmentItem.php | 39 ++ .../Subscription/SubscriptionCharge.php | 28 + .../Subscription/SubscriptionCredit.php | 28 + .../Subscription/SubscriptionDiscount.php | 33 ++ .../SubscriptionEffectiveFrom.php | 24 + .../Subscription/SubscriptionItem.php | 49 ++ .../Subscription/SubscriptionItemStatus.php | 26 + .../SubscriptionOnPaymentFailure.php | 24 + .../Subscription/SubscriptionPrice.php | 43 ++ .../SubscriptionScheduledChange.php | 33 ++ .../SubscriptionScheduledChangeAction.php | 26 + .../Subscription/SubscriptionStatus.php | 32 ++ .../Subscription/SubscriptionTimePeriod.php | 31 ++ src/Notifications/Entities/Transaction.php | 89 ++++ .../TransactionAdjustmentsTotals.php | 41 ++ .../Transaction/TransactionBreakdown.php | 27 + .../Transaction/TransactionCardType.php | 40 ++ .../Transaction/TransactionDetails.php | 47 ++ .../Entities/Transaction/TransactionItem.php | 35 ++ .../Transaction/TransactionLineItem.php | 45 ++ .../Transaction/TransactionProration.php | 29 ++ .../Transaction/TransactionTimePeriod.php | 31 ++ src/Notifications/Events/AddressCreated.php | 2 +- src/Notifications/Events/AddressUpdated.php | 2 +- .../Events/AdjustmentCreated.php | 2 +- .../Events/AdjustmentUpdated.php | 2 +- src/Notifications/Events/BusinessCreated.php | 2 +- src/Notifications/Events/BusinessUpdated.php | 2 +- src/Notifications/Events/CustomerCreated.php | 2 +- src/Notifications/Events/CustomerUpdated.php | 2 +- src/Notifications/Events/DiscountCreated.php | 4 +- src/Notifications/Events/DiscountImported.php | 4 +- src/Notifications/Events/DiscountUpdated.php | 4 +- src/Notifications/Events/PayoutCreated.php | 4 +- src/Notifications/Events/PayoutPaid.php | 4 +- src/Notifications/Events/PriceCreated.php | 2 +- src/Notifications/Events/PriceUpdated.php | 2 +- src/Notifications/Events/ProductCreated.php | 2 +- src/Notifications/Events/ProductUpdated.php | 2 +- .../Events/SubscriptionActivated.php | 4 +- .../Events/SubscriptionCanceled.php | 4 +- .../Events/SubscriptionCreated.php | 4 +- .../Events/SubscriptionImported.php | 4 +- .../Events/SubscriptionPastDue.php | 4 +- .../Events/SubscriptionPaused.php | 4 +- .../Events/SubscriptionResumed.php | 4 +- .../Events/SubscriptionTrialing.php | 4 +- .../Events/SubscriptionUpdated.php | 4 +- .../Events/TransactionBilled.php | 2 +- .../Events/TransactionCanceled.php | 2 +- .../Events/TransactionCompleted.php | 2 +- .../Events/TransactionCreated.php | 2 +- src/Notifications/Events/TransactionPaid.php | 2 +- .../Events/TransactionPastDue.php | 2 +- .../Events/TransactionPaymentFailed.php | 2 +- src/Notifications/Events/TransactionReady.php | 2 +- .../Events/TransactionUpdated.php | 2 +- 127 files changed, 3637 insertions(+), 84 deletions(-) create mode 100644 src/Notifications/Entities/Address.php create mode 100644 src/Notifications/Entities/Adjustment.php create mode 100644 src/Notifications/Entities/Adjustment/AdjustmentItem.php create mode 100644 src/Notifications/Entities/Business.php create mode 100644 src/Notifications/Entities/Business/BusinessesContacts.php create mode 100644 src/Notifications/Entities/Customer.php create mode 100644 src/Notifications/Entities/DateTime.php rename src/{Entities/Notification/NotificationDiscount.php => Notifications/Entities/Discount.php} (84%) create mode 100644 src/Notifications/Entities/Discount/DiscountStatus.php create mode 100644 src/Notifications/Entities/Discount/DiscountType.php create mode 100644 src/Notifications/Entities/Entity.php create mode 100644 src/Notifications/Entities/Payout.php create mode 100644 src/Notifications/Entities/Payout/PayoutStatus.php create mode 100644 src/Notifications/Entities/Price.php create mode 100644 src/Notifications/Entities/Product.php create mode 100644 src/Notifications/Entities/Report.php create mode 100644 src/Notifications/Entities/Report/ReportFilter.php create mode 100644 src/Notifications/Entities/Report/ReportFilterName.php create mode 100644 src/Notifications/Entities/Report/ReportFilterOperator.php create mode 100644 src/Notifications/Entities/Report/ReportStatus.php create mode 100644 src/Notifications/Entities/Report/ReportType.php create mode 100644 src/Notifications/Entities/Shared/Action.php create mode 100644 src/Notifications/Entities/Shared/AddressPreview.php create mode 100644 src/Notifications/Entities/Shared/AdjustmentItemTotals.php create mode 100644 src/Notifications/Entities/Shared/AdjustmentProration.php create mode 100644 src/Notifications/Entities/Shared/AdjustmentStatus.php create mode 100644 src/Notifications/Entities/Shared/AdjustmentTimePeriod.php create mode 100644 src/Notifications/Entities/Shared/AdjustmentTotals.php create mode 100644 src/Notifications/Entities/Shared/AdjustmentType.php create mode 100644 src/Notifications/Entities/Shared/AvailablePaymentMethods.php create mode 100644 src/Notifications/Entities/Shared/BillingDetails.php create mode 100644 src/Notifications/Entities/Shared/Card.php create mode 100644 src/Notifications/Entities/Shared/CatalogType.php create mode 100644 src/Notifications/Entities/Shared/ChargebackFee.php create mode 100644 src/Notifications/Entities/Shared/Checkout.php create mode 100644 src/Notifications/Entities/Shared/CollectionMode.php create mode 100644 src/Notifications/Entities/Shared/CountryCode.php create mode 100644 src/Notifications/Entities/Shared/CurrencyCode.php create mode 100644 src/Notifications/Entities/Shared/CurrencyCodeAdjustments.php create mode 100644 src/Notifications/Entities/Shared/CurrencyCodePayouts.php create mode 100644 src/Notifications/Entities/Shared/CustomData.php create mode 100644 src/Notifications/Entities/Shared/Data.php create mode 100644 src/Notifications/Entities/Shared/ErrorCode.php create mode 100644 src/Notifications/Entities/Shared/ImportMeta.php create mode 100644 src/Notifications/Entities/Shared/Interval.php create mode 100644 src/Notifications/Entities/Shared/MethodDetails.php create mode 100644 src/Notifications/Entities/Shared/Money.php create mode 100644 src/Notifications/Entities/Shared/Original.php create mode 100644 src/Notifications/Entities/Shared/PaymentAttemptStatus.php create mode 100644 src/Notifications/Entities/Shared/PayoutTotalsAdjustment.php create mode 100644 src/Notifications/Entities/Shared/PriceQuantity.php create mode 100644 src/Notifications/Entities/Shared/Status.php create mode 100644 src/Notifications/Entities/Shared/TaxCategory.php create mode 100644 src/Notifications/Entities/Shared/TaxMode.php create mode 100644 src/Notifications/Entities/Shared/TaxRatesUsed.php create mode 100644 src/Notifications/Entities/Shared/TimePeriod.php create mode 100644 src/Notifications/Entities/Shared/Totals.php create mode 100644 src/Notifications/Entities/Shared/TransactionOrigin.php create mode 100644 src/Notifications/Entities/Shared/TransactionPaymentAttempt.php create mode 100644 src/Notifications/Entities/Shared/TransactionPayoutTotals.php create mode 100644 src/Notifications/Entities/Shared/TransactionPayoutTotalsAdjusted.php create mode 100644 src/Notifications/Entities/Shared/TransactionStatus.php create mode 100644 src/Notifications/Entities/Shared/TransactionTotals.php create mode 100644 src/Notifications/Entities/Shared/TransactionTotalsAdjusted.php create mode 100644 src/Notifications/Entities/Shared/Type.php create mode 100644 src/Notifications/Entities/Shared/UnitPriceOverride.php create mode 100644 src/Notifications/Entities/Shared/UnitTotals.php rename src/{Entities/Notification/NotificationSubscription.php => Notifications/Entities/Subscription.php} (78%) create mode 100644 src/Notifications/Entities/Subscription/SubscriptionAdjustmentItem.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionCharge.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionCredit.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionDiscount.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionEffectiveFrom.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionItem.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionItemStatus.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionOnPaymentFailure.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionPrice.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionScheduledChange.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionScheduledChangeAction.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionStatus.php create mode 100644 src/Notifications/Entities/Subscription/SubscriptionTimePeriod.php create mode 100644 src/Notifications/Entities/Transaction.php create mode 100644 src/Notifications/Entities/Transaction/TransactionAdjustmentsTotals.php create mode 100644 src/Notifications/Entities/Transaction/TransactionBreakdown.php create mode 100644 src/Notifications/Entities/Transaction/TransactionCardType.php create mode 100644 src/Notifications/Entities/Transaction/TransactionDetails.php create mode 100644 src/Notifications/Entities/Transaction/TransactionItem.php create mode 100644 src/Notifications/Entities/Transaction/TransactionLineItem.php create mode 100644 src/Notifications/Entities/Transaction/TransactionProration.php create mode 100644 src/Notifications/Entities/Transaction/TransactionTimePeriod.php diff --git a/rector.php b/rector.php index 625082e..8b3a1f3 100644 --- a/rector.php +++ b/rector.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Rector\Config\RectorConfig; +use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector; use Rector\Set\ValueObject\LevelSetList; use Rector\Set\ValueObject\SetList; @@ -22,6 +23,7 @@ $rectorConfig->skip([ '*.json', '*/Fixture/*', + MyCLabsClassToEnumRector::class, ]); $rectorConfig->importNames(); diff --git a/src/Entities/Event.php b/src/Entities/Event.php index 7fb94c1..165369f 100644 --- a/src/Entities/Event.php +++ b/src/Entities/Event.php @@ -5,8 +5,7 @@ namespace Paddle\SDK\Entities; use Paddle\SDK\Entities\Event\EventTypeName; -use Paddle\SDK\Entities\Notification\NotificationDiscount; -use Paddle\SDK\Entities\Notification\NotificationSubscription; +use Paddle\SDK\Notifications\Entities\Entity as NotificationEntity; class Event implements Entity { @@ -17,7 +16,7 @@ protected function __construct( public string $eventId, public EventTypeName $eventType, public \DateTimeInterface $occurredAt, - public Entity $data, + public NotificationEntity $data, ) { } @@ -25,15 +24,10 @@ public static function from(array $data): self { $type = explode('.', (string) $data['event_type'])[0] ?? ''; - /** @var class-string $entity */ - $entity = match ($type) { - 'discount' => NotificationDiscount::class, - 'subscription' => NotificationSubscription::class, - 'adjustment' => Adjustment::class, - default => sprintf('\Paddle\SDK\Entities\%s', ucfirst($type)), - }; + /** @var class-string $entity */ + $entity = sprintf('\Paddle\SDK\Notifications\Entities\%s', ucfirst($type)); - if (! class_exists($entity) || ! in_array(Entity::class, class_implements($entity), true)) { + if (! class_exists($entity) || ! in_array(NotificationEntity::class, class_implements($entity), true)) { throw new \UnexpectedValueException("Event type '{$type}' cannot be mapped to an object"); } diff --git a/src/Notifications/Entities/Address.php b/src/Notifications/Entities/Address.php new file mode 100644 index 0000000..f3728c5 --- /dev/null +++ b/src/Notifications/Entities/Address.php @@ -0,0 +1,59 @@ + $items + */ + protected function __construct( + public string $id, + public Action $action, + public string $transactionId, + public string|null $subscriptionId, + public string $customerId, + public string $reason, + public bool|null $creditAppliedToBalance, + public CurrencyCode $currencyCode, + public AdjustmentStatus $status, + public array $items, + public AdjustmentTotals $totals, + public PayoutTotalsAdjustment|null $payoutTotals, + public \DateTimeInterface $createdAt, + public \DateTimeInterface|null $updatedAt, + ) { + } + + public static function from(array $data): self + { + return new self( + id: $data['id'], + action: Action::from($data['action']), + transactionId: $data['transaction_id'], + subscriptionId: $data['subscription_id'] ?? null, + customerId: $data['customer_id'], + reason: $data['reason'], + creditAppliedToBalance: $data['credit_applied_to_balance'] ?? null, + currencyCode: CurrencyCode::from($data['currency_code']), + status: AdjustmentStatus::from($data['status']), + items: array_map(fn (array $item): AdjustmentItem => AdjustmentItem::from($item), $data['items']), + totals: AdjustmentTotals::from($data['totals']), + payoutTotals: isset($data['payout_totals']) ? PayoutTotalsAdjustment::from($data['payout_totals']) : null, + createdAt: DateTime::from($data['created_at']), + updatedAt: isset($data['updated_at']) ? DateTime::from($data['updated_at']) : null, + ); + } +} diff --git a/src/Notifications/Entities/Adjustment/AdjustmentItem.php b/src/Notifications/Entities/Adjustment/AdjustmentItem.php new file mode 100644 index 0000000..31d27a3 --- /dev/null +++ b/src/Notifications/Entities/Adjustment/AdjustmentItem.php @@ -0,0 +1,41 @@ + $contacts + */ + protected function __construct( + public string $id, + public string $name, + public string|null $companyNumber, + public string|null $taxIdentifier, + public Status $status, + public array $contacts, + public \DateTimeInterface $createdAt, + public \DateTimeInterface $updatedAt, + public CustomData|null $customData, + public ImportMeta|null $importMeta, + ) { + } + + public static function from(array $data): self + { + return new self( + id: $data['id'], + name: $data['name'], + companyNumber: $data['company_number'] ?? null, + taxIdentifier: $data['tax_identifier'] ?? null, + status: Status::from($data['status']), + contacts: array_map(fn (array $contact): BusinessesContacts => BusinessesContacts::from($contact), $data['contacts']), + createdAt: DateTime::from($data['created_at']), + updatedAt: DateTime::from($data['updated_at']), + customData: isset($data['custom_data']) ? new CustomData($data['custom_data']) : null, + importMeta: isset($data['import_meta']) ? ImportMeta::from($data['import_meta']) : null, + ); + } +} diff --git a/src/Notifications/Entities/Business/BusinessesContacts.php b/src/Notifications/Entities/Business/BusinessesContacts.php new file mode 100644 index 0000000..8030ee2 --- /dev/null +++ b/src/Notifications/Entities/Business/BusinessesContacts.php @@ -0,0 +1,26 @@ +format(self::PADDLE_RFC3339); + + try { + return new self($date); + } catch (\Exception) { + return null; + } + } +} diff --git a/src/Entities/Notification/NotificationDiscount.php b/src/Notifications/Entities/Discount.php similarity index 84% rename from src/Entities/Notification/NotificationDiscount.php rename to src/Notifications/Entities/Discount.php index 3f58545..54d23d4 100644 --- a/src/Entities/Notification/NotificationDiscount.php +++ b/src/Notifications/Entities/Discount.php @@ -9,17 +9,15 @@ * |-------------------------------------------------------------|. */ -namespace Paddle\SDK\Entities\Notification; +namespace Paddle\SDK\Notifications\Entities; -use Paddle\SDK\Entities\DateTime; -use Paddle\SDK\Entities\Discount\DiscountStatus; -use Paddle\SDK\Entities\Discount\DiscountType; -use Paddle\SDK\Entities\Entity; -use Paddle\SDK\Entities\Shared\CurrencyCode; -use Paddle\SDK\Entities\Shared\CustomData; -use Paddle\SDK\Entities\Shared\ImportMeta; +use Paddle\SDK\Notifications\Entities\Discount\DiscountStatus; +use Paddle\SDK\Notifications\Entities\Discount\DiscountType; +use Paddle\SDK\Notifications\Entities\Shared\CurrencyCode; +use Paddle\SDK\Notifications\Entities\Shared\CustomData; +use Paddle\SDK\Notifications\Entities\Shared\ImportMeta; -class NotificationDiscount implements Entity +class Discount implements Entity { /** * @internal diff --git a/src/Notifications/Entities/Discount/DiscountStatus.php b/src/Notifications/Entities/Discount/DiscountStatus.php new file mode 100644 index 0000000..7b68c66 --- /dev/null +++ b/src/Notifications/Entities/Discount/DiscountStatus.php @@ -0,0 +1,21 @@ + $unitPriceOverrides + */ + protected function __construct( + public string $id, + public string $productId, + public string|null $name, + public string $description, + public CatalogType|null $type, + public TimePeriod|null $billingCycle, + public TimePeriod|null $trialPeriod, + public TaxMode|null $taxMode, + public Money $unitPrice, + public array $unitPriceOverrides, + public PriceQuantity $quantity, + public Status $status, + public CustomData|null $customData, + public ImportMeta|null $importMeta, + ) { + } + + public static function from(array $data): self + { + return new self( + id: $data['id'], + productId: $data['product_id'], + name: $data['name'] ?? null, + description: $data['description'], + type: isset($data['type']) ? CatalogType::from($data['type']) : null, + billingCycle: isset($data['billing_cycle']) ? TimePeriod::from($data['billing_cycle']) : null, + trialPeriod: isset($data['trial_period']) ? TimePeriod::from($data['trial_period']) : null, + taxMode: isset($data['tax_mode']) ? TaxMode::from($data['tax_mode']) : null, + unitPrice: Money::from($data['unit_price']), + unitPriceOverrides: array_map( + fn (array $override): UnitPriceOverride => UnitPriceOverride::from($override), + $data['unit_price_overrides'] ?? [], + ), + quantity: PriceQuantity::from($data['quantity']), + status: Status::from($data['status']), + customData: isset($data['custom_data']) ? new CustomData($data['custom_data']) : null, + importMeta: isset($data['import_meta']) ? ImportMeta::from($data['import_meta']) : null, + ); + } +} diff --git a/src/Notifications/Entities/Product.php b/src/Notifications/Entities/Product.php new file mode 100644 index 0000000..d6588aa --- /dev/null +++ b/src/Notifications/Entities/Product.php @@ -0,0 +1,54 @@ + $filters + */ + protected function __construct( + public string $id, + public ReportStatus $status, + public int|null $rows, + public ReportType $type, + public array $filters, + public \DateTimeInterface|null $expiresAt, + public \DateTimeInterface $createdAt, + public \DateTimeInterface $updatedAt, + ) { + } + + public static function from(array $data): self + { + return new self( + id: $data['id'], + status: ReportStatus::from($data['status']), + rows: $data['rows'] ?? null, + type: ReportType::from($data['type']), + filters: array_map(fn (array $filter): ReportFilter => ReportFilter::from($filter), $data['filters'] ?? []), + expiresAt: isset($data['expires_at']) ? DateTime::from($data['expires_at']) : null, + createdAt: DateTime::from($data['created_at']), + updatedAt: DateTime::from($data['updated_at']), + ); + } +} diff --git a/src/Notifications/Entities/Report/ReportFilter.php b/src/Notifications/Entities/Report/ReportFilter.php new file mode 100644 index 0000000..4861b01 --- /dev/null +++ b/src/Notifications/Entities/Report/ReportFilter.php @@ -0,0 +1,31 @@ +data; + } +} diff --git a/src/Notifications/Entities/Shared/Data.php b/src/Notifications/Entities/Shared/Data.php new file mode 100644 index 0000000..0e01203 --- /dev/null +++ b/src/Notifications/Entities/Shared/Data.php @@ -0,0 +1,20 @@ + $countryCodes + */ + public function __construct( + public array $countryCodes, + public Money $unitPrice, + ) { + } + + public static function from(array $data): self + { + return new self($data['country_codes'], Money::from($data['unit_price'])); + } +} diff --git a/src/Notifications/Entities/Shared/UnitTotals.php b/src/Notifications/Entities/Shared/UnitTotals.php new file mode 100644 index 0000000..e394a8f --- /dev/null +++ b/src/Notifications/Entities/Shared/UnitTotals.php @@ -0,0 +1,33 @@ + $items + * @param array $payments + */ + protected function __construct( + public string $id, + public TransactionStatus $status, + public string|null $customerId, + public string|null $addressId, + public string|null $businessId, + public CustomData|null $customData, + public CurrencyCode $currencyCode, + public TransactionOrigin $origin, + public string|null $subscriptionId, + public string|null $invoiceId, + public string|null $invoiceNumber, + public CollectionMode $collectionMode, + public string|null $discountId, + public BillingDetails|null $billingDetails, + public TransactionTimePeriod|null $billingPeriod, + public array $items, + public TransactionDetails $details, + public array $payments, + public Checkout|null $checkout, + public \DateTimeInterface $createdAt, + public \DateTimeInterface $updatedAt, + public \DateTimeInterface|null $billedAt, + public string|null $receiptData, + ) { + } + + public static function from(array $data): self + { + return new self( + id: $data['id'], + status: TransactionStatus::from($data['status']), + customerId: $data['customer_id'] ?? null, + addressId: $data['address_id'] ?? null, + businessId: $data['business_id'] ?? null, + customData: isset($data['custom_data']) ? new CustomData($data['custom_data']) : null, + currencyCode: CurrencyCode::from($data['currency_code']), + origin: TransactionOrigin::from($data['origin']), + subscriptionId: $data['subscription_id'] ?? null, + invoiceId: $data['invoice_id'] ?? null, + invoiceNumber: $data['invoice_number'] ?? null, + collectionMode: CollectionMode::from($data['collection_mode']), + discountId: $data['discount_id'] ?? null, + billingDetails: isset($data['billing_details']) ? BillingDetails::from($data['billing_details']) : null, + billingPeriod: isset($data['billing_period']) ? TransactionTimePeriod::from($data['billing_period']) : null, + items: array_map(fn (array $item): TransactionItem => TransactionItem::from($item), $data['items'] ?? []), + details: TransactionDetails::from($data['details']), + payments: array_map(fn (array $payment): TransactionPaymentAttempt => TransactionPaymentAttempt::from($payment), $data['payments'] ?? []), + checkout: isset($data['checkout']) ? new Checkout($data['checkout']['url'] ?? null) : null, + createdAt: DateTime::from($data['created_at']), + updatedAt: DateTime::from($data['updated_at']), + billedAt: isset($data['billed_at']) ? DateTime::from($data['billed_at']) : null, + receiptData: $data['receipt_data'] ?? null, + ); + } +} diff --git a/src/Notifications/Entities/Transaction/TransactionAdjustmentsTotals.php b/src/Notifications/Entities/Transaction/TransactionAdjustmentsTotals.php new file mode 100644 index 0000000..4084ce2 --- /dev/null +++ b/src/Notifications/Entities/Transaction/TransactionAdjustmentsTotals.php @@ -0,0 +1,41 @@ + $taxRatesUsed + * @param array $lineItems + */ + public function __construct( + public array $taxRatesUsed, + public TransactionTotals $totals, + public TransactionTotalsAdjusted|null $adjustedTotals, + public TransactionPayoutTotals|null $payoutTotals, + public TransactionPayoutTotalsAdjusted|null $adjustedPayoutTotals, + public array $lineItems, + ) { + } + + public static function from(array $data): self + { + return new self( + array_map(fn (array $taxRateUsed): TaxRatesUsed => TaxRatesUsed::from($taxRateUsed), $data['tax_rates_used']), + TransactionTotals::from($data['totals']), + isset($data['adjusted_totals']) ? TransactionTotalsAdjusted::from($data['adjusted_totals']) : null, + isset($data['payout_totals']) ? TransactionPayoutTotals::from($data['payout_totals']) : null, + isset($data['adjusted_payout_totals']) ? TransactionPayoutTotalsAdjusted::from($data['adjusted_payout_totals']) : null, + array_map(fn (array $lineItem): TransactionLineItem => TransactionLineItem::from($lineItem), $data['line_items']), + ); + } +} diff --git a/src/Notifications/Entities/Transaction/TransactionItem.php b/src/Notifications/Entities/Transaction/TransactionItem.php new file mode 100644 index 0000000..e7cd014 --- /dev/null +++ b/src/Notifications/Entities/Transaction/TransactionItem.php @@ -0,0 +1,35 @@ +