From 8afc3e25883c49b78b71382716d4caf574533ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Fern=C3=A1ndez=20Saborit?= Date: Tue, 6 Feb 2024 15:19:40 +0100 Subject: [PATCH] Fix/Sync API Spec (#26) --- src/Entities/Event/EventTypeName.php | 1 + src/Entities/Report/ReportFilterName.php | 1 + .../Shared/TransactionPaymentAttempt.php | 5 +++ src/Resources/Customers/CustomersClient.php | 5 +-- .../Operations/ListCreditBalances.php | 34 +++++++++++++++++++ .../Operations/ListNotifications.php | 20 +++++++---- .../Operations/CreateOneTimeCharge.php | 2 ++ .../Operations/PreviewOneTimeCharge.php | 2 ++ .../_fixtures/response/list_default.json | 5 +++ .../Notifications/NotificationsClientTest.php | 8 ++--- .../_fixtures/response/list_default.json | 1 + .../response/list_paginated_page_one.json | 1 + .../response/list_paginated_page_two.json | 1 + 13 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 src/Resources/Customers/Operations/ListCreditBalances.php diff --git a/src/Entities/Event/EventTypeName.php b/src/Entities/Event/EventTypeName.php index ae28a0e..f0e4dde 100644 --- a/src/Entities/Event/EventTypeName.php +++ b/src/Entities/Event/EventTypeName.php @@ -13,6 +13,7 @@ enum EventTypeName: string case BusinessCreated = 'business.created'; case BusinessUpdated = 'business.updated'; case CustomerCreated = 'customer.created'; + case CustomerImported = 'customer.imported'; case CustomerUpdated = 'customer.updated'; case DiscountCreated = 'discount.created'; case DiscountImported = 'discount.imported'; diff --git a/src/Entities/Report/ReportFilterName.php b/src/Entities/Report/ReportFilterName.php index 54517a2..f173eba 100644 --- a/src/Entities/Report/ReportFilterName.php +++ b/src/Entities/Report/ReportFilterName.php @@ -13,6 +13,7 @@ enum ReportFilterName: string { + case Action = 'action'; case CollectionMode = 'collection_mode'; case CurrencyCode = 'currency_code'; case Origin = 'origin'; diff --git a/src/Entities/Shared/TransactionPaymentAttempt.php b/src/Entities/Shared/TransactionPaymentAttempt.php index 3b7be01..9e4204b 100644 --- a/src/Entities/Shared/TransactionPaymentAttempt.php +++ b/src/Entities/Shared/TransactionPaymentAttempt.php @@ -15,8 +15,12 @@ class TransactionPaymentAttempt { + /** + * @deprecated $storedPaymentMethodId + */ public function __construct( public string $paymentAttemptId, + public string $paymentMethodId, public string $storedPaymentMethodId, public string $amount, public StatusPaymentAttempt $status, @@ -31,6 +35,7 @@ public static function from(array $data): self { return new self( $data['payment_attempt_id'], + $data['payment_method_id'], $data['stored_payment_method_id'], $data['amount'], StatusPaymentAttempt::from($data['status']), diff --git a/src/Resources/Customers/CustomersClient.php b/src/Resources/Customers/CustomersClient.php index 6365c3f..7bf598a 100644 --- a/src/Resources/Customers/CustomersClient.php +++ b/src/Resources/Customers/CustomersClient.php @@ -20,6 +20,7 @@ use Paddle\SDK\Exceptions\ApiError; use Paddle\SDK\Exceptions\SdkExceptions\MalformedResponse; use Paddle\SDK\Resources\Customers\Operations\CreateCustomer; +use Paddle\SDK\Resources\Customers\Operations\ListCreditBalances; use Paddle\SDK\Resources\Customers\Operations\ListCustomers; use Paddle\SDK\Resources\Customers\Operations\UpdateCustomer; use Paddle\SDK\ResponseParser; @@ -103,10 +104,10 @@ public function archive(string $id): Customer * @throws ApiError\CustomerApiError On a customer specific API error * @throws MalformedResponse If the API response was not parsable */ - public function creditBalances(string $id): CreditBalanceCollection + public function creditBalances(string $id, ListCreditBalances $operation = new ListCreditBalances()): CreditBalanceCollection { $parser = new ResponseParser( - $this->client->getRaw("/customers/{$id}/credit-balances"), + $this->client->getRaw("/customers/{$id}/credit-balances", $operation), ); return CreditBalanceCollection::from($parser->getData()); diff --git a/src/Resources/Customers/Operations/ListCreditBalances.php b/src/Resources/Customers/Operations/ListCreditBalances.php new file mode 100644 index 0000000..3570454 --- /dev/null +++ b/src/Resources/Customers/Operations/ListCreditBalances.php @@ -0,0 +1,34 @@ + $currencyCodes + * + * @throws InvalidArgumentException On invalid array contents + */ + public function __construct( + private readonly array $currencyCodes = [], + ) { + if ($invalid = array_filter($this->currencyCodes, fn ($value): bool => ! $value instanceof CurrencyCode)) { + throw InvalidArgumentException::arrayContainsInvalidTypes('currencyCodes', CurrencyCode::class, implode(', ', $invalid)); + } + } + + public function getParameters(): array + { + $enumStringify = fn ($enum) => $enum->value; + + return array_filter([ + 'currency_code' => implode(',', array_map($enumStringify, $this->currencyCodes)), + ]); + } +} diff --git a/src/Resources/Notifications/Operations/ListNotifications.php b/src/Resources/Notifications/Operations/ListNotifications.php index e774087..1e39b86 100644 --- a/src/Resources/Notifications/Operations/ListNotifications.php +++ b/src/Resources/Notifications/Operations/ListNotifications.php @@ -6,24 +6,32 @@ use Paddle\SDK\Entities\DateTime; use Paddle\SDK\Entities\Notification\NotificationStatus; +use Paddle\SDK\Exceptions\SdkExceptions\InvalidArgumentException; use Paddle\SDK\HasParameters; use Paddle\SDK\Resources\Shared\Operations\List\Pager; class ListNotifications implements HasParameters { /** - * @param array $notificationSettingId - * @param array $status + * @param array $notificationSettingIds + * @param array $statuses */ public function __construct( private readonly Pager|null $pager = null, - private readonly array $notificationSettingId = [], + private readonly array $notificationSettingIds = [], private readonly string|null $search = null, - private readonly array $status = [], + private readonly array $statuses = [], private readonly string|null $filter = null, private readonly \DateTimeInterface|null $to = null, private readonly \DateTimeInterface|null $from = null, ) { + if ($invalid = array_filter($this->notificationSettingIds, fn ($value): bool => ! is_string($value))) { + throw InvalidArgumentException::arrayContainsInvalidTypes('notificationSettingIds', 'string', implode(', ', $invalid)); + } + + if ($invalid = array_filter($this->statuses, fn ($value): bool => ! $value instanceof NotificationStatus)) { + throw InvalidArgumentException::arrayContainsInvalidTypes('statuses', NotificationStatus::class, implode(', ', $invalid)); + } } public function getParameters(): array @@ -33,9 +41,9 @@ public function getParameters(): array return array_merge( $this->pager?->getParameters() ?? [], array_filter([ - 'notification_setting_id' => implode(',', $this->notificationSettingId), + 'notification_setting_id' => implode(',', $this->notificationSettingIds), 'search' => $this->search, - 'status' => implode(',', array_map($enumStringify, $this->status)), + 'status' => implode(',', array_map($enumStringify, $this->statuses)), 'filter' => $this->filter, 'to' => isset($this->to) ? DateTime::from($this->to)?->format() : null, 'from' => isset($this->from) ? DateTime::from($this->from)?->format() : null, diff --git a/src/Resources/Subscriptions/Operations/CreateOneTimeCharge.php b/src/Resources/Subscriptions/Operations/CreateOneTimeCharge.php index dc316f9..3ce0b30 100644 --- a/src/Resources/Subscriptions/Operations/CreateOneTimeCharge.php +++ b/src/Resources/Subscriptions/Operations/CreateOneTimeCharge.php @@ -22,6 +22,7 @@ public function __construct( public readonly SubscriptionEffectiveFrom $effectiveFrom, public readonly array $items, public readonly SubscriptionOnPaymentFailure|Undefined $onPaymentFailure = new Undefined(), + public readonly string|Undefined $receiptData = new Undefined(), ) { } @@ -31,6 +32,7 @@ public function jsonSerialize(): array 'effective_from' => $this->effectiveFrom, 'items' => $this->items, 'on_payment_failure' => $this->onPaymentFailure, + 'receipt_data' => $this->receiptData, ]); } } diff --git a/src/Resources/Subscriptions/Operations/PreviewOneTimeCharge.php b/src/Resources/Subscriptions/Operations/PreviewOneTimeCharge.php index 0d7d5b6..d29bc4c 100644 --- a/src/Resources/Subscriptions/Operations/PreviewOneTimeCharge.php +++ b/src/Resources/Subscriptions/Operations/PreviewOneTimeCharge.php @@ -22,6 +22,7 @@ public function __construct( public readonly SubscriptionEffectiveFrom $effectiveFrom, public readonly array $items, public readonly SubscriptionOnPaymentFailure|Undefined $onPaymentFailure = new Undefined(), + public readonly string|Undefined $receiptData = new Undefined(), ) { } @@ -31,6 +32,7 @@ public function jsonSerialize(): array 'effective_from' => $this->effectiveFrom, 'items' => $this->items, 'on_payment_failure' => $this->onPaymentFailure, + 'receipt_data' => $this->receiptData, ]); } } diff --git a/tests/Functional/Resources/Events/_fixtures/response/list_default.json b/tests/Functional/Resources/Events/_fixtures/response/list_default.json index 54f36f6..f92c59b 100644 --- a/tests/Functional/Resources/Events/_fixtures/response/list_default.json +++ b/tests/Functional/Resources/Events/_fixtures/response/list_default.json @@ -406,6 +406,7 @@ "type": "card" }, "payment_attempt_id": "afa07161-601d-4a65-8c1a-6993ae1ae027", + "payment_method_id": "paymtd_01hkm9xwqpbbpr1ksmvg3sx3v1", "stored_payment_method_id": "d3218a38-1cff-44a7-8a47-6a809a5ae9ad" } ], @@ -737,6 +738,7 @@ "type": "card" }, "payment_attempt_id": "afa07161-601d-4a65-8c1a-6993ae1ae027", + "payment_method_id": "paymtd_01hkm9xwqpbbpr1ksmvg3sx3v1", "stored_payment_method_id": "d3218a38-1cff-44a7-8a47-6a809a5ae9ad" } ], @@ -1068,6 +1070,7 @@ "type": "card" }, "payment_attempt_id": "afa07161-601d-4a65-8c1a-6993ae1ae027", + "payment_method_id": "paymtd_01hkm9xwqpbbpr1ksmvg3sx3v1", "stored_payment_method_id": "d3218a38-1cff-44a7-8a47-6a809a5ae9ad" } ], @@ -2408,6 +2411,7 @@ "type": "card" }, "payment_attempt_id": "0bec49bd-dd4d-45e3-a91e-46992b09c5e2", + "payment_method_id": "paymtd_01hkm9xwqpbbpr1ksmvg3sx3v1", "stored_payment_method_id": "8009a718-30f7-4718-a7f6-4d3fdecf559b" } ], @@ -2753,6 +2757,7 @@ "type": "card" }, "payment_attempt_id": "0bec49bd-dd4d-45e3-a91e-46992b09c5e2", + "payment_method_id": "paymtd_01hkm9xwqpbbpr1ksmvg3sx3v1", "stored_payment_method_id": "8009a718-30f7-4718-a7f6-4d3fdecf559b" } ], diff --git a/tests/Functional/Resources/Notifications/NotificationsClientTest.php b/tests/Functional/Resources/Notifications/NotificationsClientTest.php index 15ad1b5..ed31277 100644 --- a/tests/Functional/Resources/Notifications/NotificationsClientTest.php +++ b/tests/Functional/Resources/Notifications/NotificationsClientTest.php @@ -79,13 +79,13 @@ public static function listOperationsProvider(): \Generator ]; yield 'Notification Setting ID Filtered' => [ - new ListNotifications(notificationSettingId: ['nftset_01h83xenpcfjyhkqr4x214m02']), + new ListNotifications(notificationSettingIds: ['nftset_01h83xenpcfjyhkqr4x214m02']), new Response(200, body: self::readRawJsonFixture('response/list_default')), sprintf('%s/notifications?notification_setting_id=nftset_01h83xenpcfjyhkqr4x214m02', Environment::SANDBOX->baseUrl()), ]; yield 'Multiple Notification Setting ID Filtered' => [ - new ListNotifications(notificationSettingId: ['nftset_01h83xenpcfjyhkqr4x214m02', 'nftset_01h8brhckjd6qk4n7e4py2340t']), + new ListNotifications(notificationSettingIds: ['nftset_01h83xenpcfjyhkqr4x214m02', 'nftset_01h8brhckjd6qk4n7e4py2340t']), new Response(200, body: self::readRawJsonFixture('response/list_default')), sprintf( '%s/notifications?notification_setting_id=nftset_01h83xenpcfjyhkqr4x214m02,nftset_01h8brhckjd6qk4n7e4py2340t', @@ -94,13 +94,13 @@ public static function listOperationsProvider(): \Generator ]; yield 'NotificationStatus Filtered' => [ - new ListNotifications(status: [NotificationStatus::Delivered]), + new ListNotifications(statuses: [NotificationStatus::Delivered]), new Response(200, body: self::readRawJsonFixture('response/list_default')), sprintf('%s/notifications?status=delivered', Environment::SANDBOX->baseUrl()), ]; yield 'Multiple NotificationStatus Filtered' => [ - new ListNotifications(status: [NotificationStatus::Delivered, NotificationStatus::NotAttempted]), + new ListNotifications(statuses: [NotificationStatus::Delivered, NotificationStatus::NotAttempted]), new Response(200, body: self::readRawJsonFixture('response/list_default')), sprintf('%s/notifications?status=delivered,not_attempted', Environment::SANDBOX->baseUrl()), ]; diff --git a/tests/Functional/Resources/Transactions/_fixtures/response/list_default.json b/tests/Functional/Resources/Transactions/_fixtures/response/list_default.json index b4dd964..7ced131 100644 --- a/tests/Functional/Resources/Transactions/_fixtures/response/list_default.json +++ b/tests/Functional/Resources/Transactions/_fixtures/response/list_default.json @@ -906,6 +906,7 @@ "payments": [ { "payment_attempt_id": "1f8e8302-b6fa-4290-b457-4c201eb3d53f", + "payment_method_id": "paymtd_01hkm9xwqpbbpr1ksmvg3sx3v1", "stored_payment_method_id": "5f85a637-efa7-4c6b-88ec-9cc05301bb48", "amount": "40000", "status": "captured", diff --git a/tests/Functional/Resources/Transactions/_fixtures/response/list_paginated_page_one.json b/tests/Functional/Resources/Transactions/_fixtures/response/list_paginated_page_one.json index ebf1ba8..dca96b0 100644 --- a/tests/Functional/Resources/Transactions/_fixtures/response/list_paginated_page_one.json +++ b/tests/Functional/Resources/Transactions/_fixtures/response/list_paginated_page_one.json @@ -892,6 +892,7 @@ "payments": [ { "payment_attempt_id": "1f8e8302-b6fa-4290-b457-4c201eb3d53f", + "payment_method_id": "paymtd_01hkm9xwqpbbpr1ksmvg3sx3v1", "stored_payment_method_id": "5f85a637-efa7-4c6b-88ec-9cc05301bb48", "amount": "40000", "status": "captured", diff --git a/tests/Functional/Resources/Transactions/_fixtures/response/list_paginated_page_two.json b/tests/Functional/Resources/Transactions/_fixtures/response/list_paginated_page_two.json index ed144ff..a4746f6 100644 --- a/tests/Functional/Resources/Transactions/_fixtures/response/list_paginated_page_two.json +++ b/tests/Functional/Resources/Transactions/_fixtures/response/list_paginated_page_two.json @@ -892,6 +892,7 @@ "payments": [ { "payment_attempt_id": "1f8e8302-b6fa-4290-b457-4c201eb3d53f", + "payment_method_id": "paymtd_01hkm9xwqpbbpr1ksmvg3sx3v1", "stored_payment_method_id": "5f85a637-efa7-4c6b-88ec-9cc05301bb48", "amount": "40000", "status": "captured",