Skip to content

Commit

Permalink
fix: Allow null product IDs in transaction preview responses (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgrayston-paddle authored Oct 11, 2024
1 parent 807fd31 commit c4549fc
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 14 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ Check our main [developer changelog](https://developer.paddle.com/?utm_source=dx
### Fixed

- Dropped `receipt_data` on create and preview of a one-time charge for Subscriptions and Transactions
- `TransactionsClient::preview()` `TransactionPreview` response now allows null price ID for non-catalog prices:
- `TransactionPreview` `items[]->price` can now return `Price` (with `id`) or `TransactionPreviewPrice` (with nullable `id`)
- `TransactionPreview` `details->lineItems[]->priceId` is now nullable
- `TransactionsClient::preview()` `TransactionPreview` response now allows null IDs for non-catalog prices and products:
- `items[]->price` can now return `Price` (with `id`) or `TransactionPreviewPrice` (with nullable `id`)
- `details->lineItems[]->priceId` is now nullable
- `items[]->priceId` is now nullable
- `details->lineItems[]->product` can now return `Product` (with `id`) or `TransactionPreviewProduct` (with nullable `id`)

### Added
- `TransactionsClient::create()` now supports operation items with optional properties:
Expand Down
9 changes: 6 additions & 3 deletions src/Entities/Shared/TransactionLineItemPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Paddle\SDK\Entities\Shared;

use Paddle\SDK\Entities\Product;
use Paddle\SDK\Entities\Transaction\TransactionPreviewProduct;

class TransactionLineItemPreview
{
Expand All @@ -21,19 +22,21 @@ private function __construct(
public string $taxRate,
public UnitTotals $unitTotals,
public Totals $totals,
public Product $product,
public Product|TransactionPreviewProduct $product,
) {
}

public static function from(array $data): self
{
return new self(
$data['price_id'],
$data['price_id'] ?? null,
$data['quantity'],
$data['tax_rate'],
UnitTotals::from($data['unit_totals']),
Totals::from($data['totals']),
Product::from($data['product']),
isset($data['product']['id'])
? Product::from($data['product'])
: TransactionPreviewProduct::from($data['product']),
);
}
}
4 changes: 3 additions & 1 deletion src/Entities/Transaction/TransactionItemPreviewWithPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ private function __construct(
public static function from(array $data): self
{
return new self(
isset($data['price']['id']) ? Price::from($data['price']) : TransactionPreviewPrice::from($data['price']),
isset($data['price']['id']) && isset($data['price']['product_id'])
? Price::from($data['price'])
: TransactionPreviewPrice::from($data['price']),
$data['quantity'],
$data['include_in_totals'],
isset($data['proration']) ? TransactionProration::from($data['proration']) : null,
Expand Down
9 changes: 3 additions & 6 deletions src/Entities/Transaction/TransactionPreviewPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Paddle\SDK\Entities\DateTime;
use Paddle\SDK\Entities\Entity;
use Paddle\SDK\Entities\Product;
use Paddle\SDK\Entities\Shared\CatalogType;
use Paddle\SDK\Entities\Shared\CustomData;
use Paddle\SDK\Entities\Shared\ImportMeta;
Expand All @@ -31,7 +30,7 @@ class TransactionPreviewPrice implements Entity
*/
private function __construct(
public string|null $id,
public string $productId,
public string|null $productId,
public string|null $name,
public string $description,
public CatalogType|null $type,
Expand All @@ -44,7 +43,6 @@ private function __construct(
public Status $status,
public CustomData|null $customData,
public ImportMeta|null $importMeta,
public Product|null $product,
public \DateTimeInterface $createdAt,
public \DateTimeInterface $updatedAt,
) {
Expand All @@ -53,8 +51,8 @@ private function __construct(
public static function from(array $data): self
{
return new self(
id: $data['id'],
productId: $data['product_id'],
id: $data['id'] ?? null,
productId: $data['product_id'] ?? null,
name: $data['name'] ?? null,
description: $data['description'],
type: CatalogType::from($data['type'] ?? ''),
Expand All @@ -70,7 +68,6 @@ public static function from(array $data): self
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,
product: isset($data['product']) ? Product::from($data['product']) : null,
createdAt: DateTime::from($data['created_at']),
updatedAt: DateTime::from($data['updated_at']),
);
Expand Down
55 changes: 55 additions & 0 deletions src/Entities/Transaction/TransactionPreviewProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/**
* |------
* | ! Generated code !
* | Altering this code will result in changes being overwritten |
* |-------------------------------------------------------------|.
*/

namespace Paddle\SDK\Entities\Transaction;

use Paddle\SDK\Entities\DateTime;
use Paddle\SDK\Entities\Entity;
use Paddle\SDK\Entities\Shared\CatalogType;
use Paddle\SDK\Entities\Shared\CustomData;
use Paddle\SDK\Entities\Shared\ImportMeta;
use Paddle\SDK\Entities\Shared\Status;
use Paddle\SDK\Entities\Shared\TaxCategory;

class TransactionPreviewProduct implements Entity
{
private function __construct(
public string|null $id,
public string $name,
public string|null $description,
public CatalogType|null $type,
public TaxCategory $taxCategory,
public string|null $imageUrl,
public CustomData|null $customData,
public Status $status,
public ImportMeta|null $importMeta,
public \DateTimeInterface $createdAt,
public \DateTimeInterface $updatedAt,
) {
}

public static function from(array $data): self
{
return new self(
id: $data['id'] ?? null,
name: $data['name'],
description: $data['description'] ?? null,
type: CatalogType::from($data['type'] ?? ''),
taxCategory: TaxCategory::from($data['tax_category']),
imageUrl: $data['image_url'] ?? null,
customData: isset($data['custom_data']) ? new CustomData($data['custom_data']) : null,
status: Status::from($data['status']),
importMeta: isset($data['import_meta']) ? ImportMeta::from($data['import_meta']) : null,
createdAt: DateTime::from($data['created_at']),
updatedAt: DateTime::from($data['updated_at']),
);
}
}
17 changes: 17 additions & 0 deletions tests/Functional/Resources/Transactions/TransactionsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Http\Mock\Client as MockClient;
use Paddle\SDK\Client;
use Paddle\SDK\Entities\Price;
use Paddle\SDK\Entities\Product;
use Paddle\SDK\Entities\Shared\BillingDetails;
use Paddle\SDK\Entities\Shared\CollectionMode;
use Paddle\SDK\Entities\Shared\CurrencyCode;
Expand All @@ -29,6 +30,7 @@
use Paddle\SDK\Entities\Transaction\TransactionNonCatalogPriceWithProduct as DeprecatedTransactionNonCatalogPriceWithProduct;
use Paddle\SDK\Entities\Transaction\TransactionNonCatalogProduct as DeprecatedTransactionNonCatalogProduct;
use Paddle\SDK\Entities\Transaction\TransactionPreviewPrice;
use Paddle\SDK\Entities\Transaction\TransactionPreviewProduct;
use Paddle\SDK\Entities\Transaction\TransactionUpdateTransactionItem as DeprecatedTransactionUpdateTransactionItem;
use Paddle\SDK\Environment;
use Paddle\SDK\Options;
Expand Down Expand Up @@ -875,13 +877,28 @@ public function it_has_prices_with_and_without_id_on_preview(): void
$price = $preview->items[0]->price;
self::assertInstanceOf(Price::class, $price);
self::assertSame('pri_01gsz8z1q1n00f12qt82y31smh', $price->id);
self::assertSame('pro_01gsz4t5hdjse780zja8vvr7jg', $price->productId);

$transactionPreviewPrice = $preview->items[1]->price;
self::assertInstanceOf(TransactionPreviewPrice::class, $transactionPreviewPrice);
self::assertNull($transactionPreviewPrice->id);
self::assertSame('pro_01gsz97mq9pa4fkyy0wqenepkz', $transactionPreviewPrice->productId);

$transactionPreviewPriceWithoutProductId = $preview->items[2]->price;
self::assertInstanceOf(TransactionPreviewPrice::class, $transactionPreviewPriceWithoutProductId);
self::assertSame('pri_01gsz8z1q1n00f12qt82y31smh', $price->id);
self::assertNull($transactionPreviewPriceWithoutProductId->productId);

self::assertNull($preview->details->lineItems[0]->priceId);
self::assertSame('pri_01gsz8z1q1n00f12qt82y31smh', $preview->details->lineItems[1]->priceId);

$product = $preview->details->lineItems[1]->product;
self::assertInstanceOf(Product::class, $product);
self::assertSame('pro_01gsz97mq9pa4fkyy0wqenepkz', $product->id);

$previewProduct = $preview->details->lineItems[0]->product;
self::assertInstanceOf(TransactionPreviewProduct::class, $previewProduct);
self::assertNull($previewProduct->id);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@
"quantity": 1,
"proration": null,
"include_in_totals": false
},
{
"price": {
"id": "pri_01gsz8z1q1n00f12qt82y31smh",
"description": "One-time charge",
"name": "One-time charge",
"product_id": null,
"billing_cycle": null,
"trial_period": null,
"tax_mode": "account_setting",
"unit_price": {
"amount": "19900",
"currency_code": "USD"
},
"unit_price_overrides": [
{
"country_codes": ["AU"],
"unit_price": {
"amount": "40000",
"currency_code": "AUD"
}
}
],
"quantity": {
"minimum": 1,
"maximum": 1
},
"status": "active",
"custom_data": null,
"created_at": "2023-08-16T14:38:08.3Z",
"updated_at": "2023-08-16T14:38:08.3Z"
},
"quantity": 1,
"proration": null,
"include_in_totals": false
}
],
"details": {
Expand Down Expand Up @@ -113,7 +148,7 @@
"total": "540000"
},
"product": {
"id": "pro_01gsz4t5hdjse780zja8vvr7jg",
"id": null,
"name": "ChatApp Pro",
"description": "Everything in basic, plus access to a suite of powerful tools and features designed to take your team's productivity to the next level.",
"tax_category": "standard",
Expand Down

0 comments on commit c4549fc

Please sign in to comment.