Skip to content

Commit

Permalink
fix!: Adjustment mapping and model sharing
Browse files Browse the repository at this point in the history
fix: Map subscription transaction adjustments
fix: Remove unused subscription classes
fix: Ensure Adjustment items are mapped
fix!: Transaction include to use core Adjustment
fix!: Refactor Adjustments and related entities
  - CreateAdjustment now has a write specific AdjustmentItem
  - Adjustment entities are shared between Adjustments and Subscriptions
    - AdjustmentProration
    - AdjustmentType
    - AdjustmentTimePeriod

BREAKING CHANGE: See below

- To create an Adjustment you must now use `\Paddle\SDK\Resources\Adjustments\Operations\Create\AdjustmentItem`
- Adjustment entities sub objects now utilise shared models
  • Loading branch information
mikeymike authored Feb 8, 2024
1 parent 917968f commit 349485e
Show file tree
Hide file tree
Showing 19 changed files with 439 additions and 537 deletions.
6 changes: 3 additions & 3 deletions src/Entities/Adjustment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Paddle\SDK\Entities;

use Paddle\SDK\Entities\Adjustment\AdjustmentItemTotals;
use Paddle\SDK\Entities\Adjustment\AdjustmentItem;
use Paddle\SDK\Entities\Shared\Action;
use Paddle\SDK\Entities\Shared\AdjustmentStatus;
use Paddle\SDK\Entities\Shared\CurrencyCode;
Expand All @@ -23,7 +23,7 @@ class Adjustment implements Entity
/**
* @internal
*
* @param array<AdjustmentItemTotals> $items
* @param array<AdjustmentItem> $items
*/
protected function __construct(
public string $id,
Expand Down Expand Up @@ -55,7 +55,7 @@ public static function from(array $data): self
creditAppliedToBalance: $data['credit_applied_to_balance'] ?? null,
currencyCode: CurrencyCode::from($data['currency_code']),
status: AdjustmentStatus::from($data['status']),
items: $data['items'],
items: array_map(fn (array $item) => AdjustmentItem::from($item), $data['items']),
totals: TotalAdjustments::from($data['totals']),
payoutTotals: isset($data['payout_totals']) ? PayoutTotalsAdjustment::from($data['payout_totals']) : null,
createdAt: DateTime::from($data['created_at']),
Expand Down
19 changes: 19 additions & 0 deletions src/Entities/Adjustment/AdjustmentItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,31 @@

namespace Paddle\SDK\Entities\Adjustment;

use Paddle\SDK\Entities\Shared\AdjustmentItemTotals;
use Paddle\SDK\Entities\Shared\AdjustmentProration;
use Paddle\SDK\Entities\Shared\AdjustmentType;

class AdjustmentItem
{
public function __construct(
public string $id,
public string $itemId,
public AdjustmentType $type,
public string|null $amount,
public AdjustmentProration|null $proration,
public AdjustmentItemTotals $totals,
) {
}

public static function from(array $data): self
{
return new self(
id: $data['id'],
itemId: $data['item_id'],
type: AdjustmentType::from($data['type']),
amount: $data['amount'] ?? null,
proration: $data['proration'] ? AdjustmentProration::from($data['proration']) : null,
totals: AdjustmentItemTotals::from($data['totals']),
);
}
}
28 changes: 0 additions & 28 deletions src/Entities/Adjustment/AdjustmentItemTotals.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Entities/Collections/SubscriptionsTransactionCollection.php

This file was deleted.

9 changes: 9 additions & 0 deletions src/Entities/Shared/AdjustmentItemTotals.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ public function __construct(
public string $total,
) {
}

public static function from(array $data): self
{
return new self(
subtotal: $data['subtotal'],
tax: $data['tax'],
total: $data['total'],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* |-------------------------------------------------------------|.
*/

namespace Paddle\SDK\Entities\Adjustment;
namespace Paddle\SDK\Entities\Shared;

class AdjustmentProration
{
Expand All @@ -18,4 +18,12 @@ public function __construct(
public AdjustmentTimePeriod $billingPeriod,
) {
}

public static function from(array $data): self
{
return new self(
rate: $data['rate'],
billingPeriod: AdjustmentTimePeriod::from($data['billing_period']),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* |-------------------------------------------------------------|.
*/

namespace Paddle\SDK\Entities\Adjustment;
namespace Paddle\SDK\Entities\Shared;

use Paddle\SDK\Entities\DateTime;

class AdjustmentTimePeriod
{
Expand All @@ -18,4 +20,12 @@ public function __construct(
public \DateTimeInterface $endsAt,
) {
}

public static function from(array $data): self
{
return new self(
startsAt: DateTime::from($data['starts_at']),
endsAt: DateTime::from($data['ends_at']),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* |-------------------------------------------------------------|.
*/

namespace Paddle\SDK\Entities\Adjustment;
namespace Paddle\SDK\Entities\Shared;

enum AdjustmentType: string
{
Expand Down
42 changes: 0 additions & 42 deletions src/Entities/Subscription/SubscriptionAdjustment.php

This file was deleted.

16 changes: 14 additions & 2 deletions src/Entities/Subscription/SubscriptionAdjustmentItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,29 @@

namespace Paddle\SDK\Entities\Subscription;

use Paddle\SDK\Entities\Adjustment\AdjustmentType;
use Paddle\SDK\Entities\Shared\AdjustmentItemTotals;
use Paddle\SDK\Entities\Shared\AdjustmentProration;
use Paddle\SDK\Entities\Shared\AdjustmentType;

class SubscriptionAdjustmentItem
{
public function __construct(
public string $itemId,
public AdjustmentType $type,
public string|null $amount,
public SubscriptionProration $proration,
public AdjustmentProration $proration,
public AdjustmentItemTotals $totals,
) {
}

public static function from(array $data): self
{
return new self(
itemId: $data['item_id'],
type: AdjustmentType::from($data['type']),
amount: $data['amount'] ?? null,
proration: AdjustmentProration::from($data['proration']),
totals: AdjustmentItemTotals::from($data['totals']),
);
}
}
9 changes: 9 additions & 0 deletions src/Entities/Subscription/SubscriptionAdjustmentPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ public function __construct(
public TotalAdjustments $totals,
) {
}

public static function from(array $data): self
{
return new self(
transactionId: $data['transaction_id'],
items: array_map(fn (array $item) => SubscriptionAdjustmentItem::from($item), $data['items']),
totals: TotalAdjustments::from($data['totals']),
);
}
}
5 changes: 4 additions & 1 deletion src/Entities/Subscription/SubscriptionNextTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public static function from(array $data): self
return new self(
billingPeriod: SubscriptionTimePeriod::from($data['billing_period']),
details: TransactionDetailsPreview::from($data['details']),
adjustments: [],
adjustments: array_map(
fn (array $adjustment) => SubscriptionAdjustmentPreview::from($adjustment),
$data['adjustments'],
),
);
}
}
99 changes: 0 additions & 99 deletions src/Entities/SubscriptionTransaction.php

This file was deleted.

Loading

0 comments on commit 349485e

Please sign in to comment.