Skip to content

Commit

Permalink
feat: Add support for saved payment methods
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgrayston-paddle committed Nov 15, 2024
1 parent f3be3d4 commit e362095
Show file tree
Hide file tree
Showing 30 changed files with 1,165 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

Check our main [developer changelog](https://developer.paddle.com/?utm_source=dx&utm_medium=paddle-php-sdk) for information about changes to the Paddle Billing platform, the Paddle API, and other developer tools.

## [Unreleased]

### Added

- Support for saved payment methods, see [related changelog](https://developer.paddle.com/changelog/2024/saved-payment-methods?utm_source=dx&utm_medium=paddle-php-sdk)
- `Client->paymentMethods->list()`
- `Client->paymentMethods->get()`
- `Client->paymentMethods->delete()`
- `Client->customers->createAuthToken()`

## [1.4.0] - 2024-10-17

### Added
Expand Down
3 changes: 3 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Paddle\SDK\Resources\NotificationLogs\NotificationLogsClient;
use Paddle\SDK\Resources\Notifications\NotificationsClient;
use Paddle\SDK\Resources\NotificationSettings\NotificationSettingsClient;
use Paddle\SDK\Resources\PaymentMethods\PaymentMethodsClient;
use Paddle\SDK\Resources\Prices\PricesClient;
use Paddle\SDK\Resources\PricingPreviews\PricingPreviewsClient;
use Paddle\SDK\Resources\Products\ProductsClient;
Expand Down Expand Up @@ -75,6 +76,7 @@ class Client
public readonly EventTypesClient $eventTypes;
public readonly EventsClient $events;
public readonly PricingPreviewsClient $pricingPreviews;
public readonly PaymentMethodsClient $paymentMethods;
public readonly NotificationSettingsClient $notificationSettings;
public readonly NotificationsClient $notifications;
public readonly NotificationLogsClient $notificationLogs;
Expand Down Expand Up @@ -122,6 +124,7 @@ public function __construct(
$this->eventTypes = new EventTypesClient($this);
$this->events = new EventsClient($this);
$this->pricingPreviews = new PricingPreviewsClient($this);
$this->paymentMethods = new PaymentMethodsClient($this);
$this->notificationSettings = new NotificationSettingsClient($this);
$this->notifications = new NotificationsClient($this);
$this->notificationLogs = new NotificationLogsClient($this);
Expand Down
30 changes: 30 additions & 0 deletions src/Entities/Collections/PaymentMethodCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

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

namespace Paddle\SDK\Entities\Collections;

use Paddle\SDK\Entities\PaymentMethod;

class PaymentMethodCollection extends Collection
{
public static function from(array $itemsData, Paginator|null $paginator = null): self
{
return new self(
array_map(fn (array $item): PaymentMethod => PaymentMethod::from($item), $itemsData),
$paginator,
);
}

public function current(): PaymentMethod
{
return parent::current();
}
}
32 changes: 32 additions & 0 deletions src/Entities/CustomerAuthToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

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

namespace Paddle\SDK\Entities;

use Paddle\SDK\Notifications\Entities\DateTime;
use Paddle\SDK\Notifications\Entities\Entity;

class CustomerAuthToken implements Entity
{
private function __construct(
public string $customerAuthToken,
public \DateTimeInterface $expiresAt,
) {
}

public static function from(array $data): self
{
return new self(
customerAuthToken: $data['customer_auth_token'],
expiresAt: DateTime::from($data['expires_at']),
);
}
}
48 changes: 48 additions & 0 deletions src/Entities/PaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

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

namespace Paddle\SDK\Entities;

use Paddle\SDK\Entities\Shared\Card;
use Paddle\SDK\Entities\Shared\Paypal;
use Paddle\SDK\Entities\Shared\SavedPaymentMethodOrigin;
use Paddle\SDK\Entities\Shared\SavedPaymentMethodType;

class PaymentMethod implements Entity
{
private function __construct(
public string $id,
public string $customerId,
public string $addressId,
public SavedPaymentMethodType $type,
public Card|null $card,
public Paypal|null $paypal,
public SavedPaymentMethodOrigin $origin,
public \DateTimeInterface|null $savedAt,
public \DateTimeInterface|null $updatedAt,
) {
}

public static function from(array $data): self
{
return new self(
id: $data['id'],
customerId: $data['customer_id'],
addressId: $data['address_id'],
type: SavedPaymentMethodType::from($data['type']),
card: isset($data['card']) ? Card::from($data['card']) : null,
paypal: isset($data['paypal']) ? Paypal::from($data['paypal']) : null,
origin: SavedPaymentMethodOrigin::from($data['origin']),
savedAt: isset($data['saved_at']) ? DateTime::from($data['saved_at']) : null,
updatedAt: isset($data['updated_at']) ? DateTime::from($data['updated_at']) : null,
);
}
}
29 changes: 29 additions & 0 deletions src/Entities/Shared/Paypal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

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

namespace Paddle\SDK\Entities\Shared;

class Paypal
{
private function __construct(
public string $email,
public string $reference,
) {
}

public static function from(array $data): self
{
return new self(
$data['email'],
$data['reference'],
);
}
}
24 changes: 24 additions & 0 deletions src/Entities/Shared/SavedPaymentMethodOrigin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

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

namespace Paddle\SDK\Entities\Shared;

use Paddle\SDK\PaddleEnum;

/**
* @method static SavedPaymentMethodOrigin SavedDuringPurchase()
* @method static SavedPaymentMethodOrigin Subscription()
*/
final class SavedPaymentMethodOrigin extends PaddleEnum
{
private const SavedDuringPurchase = 'saved_during_purchase';
private const Subscription = 'subscription';
}
30 changes: 30 additions & 0 deletions src/Entities/Shared/SavedPaymentMethodType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

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

namespace Paddle\SDK\Entities\Shared;

use Paddle\SDK\PaddleEnum;

/**
* @method static SavedPaymentMethodType Alipay()
* @method static SavedPaymentMethodType ApplePay()
* @method static SavedPaymentMethodType Card()
* @method static SavedPaymentMethodType GooglePay()
* @method static SavedPaymentMethodType Paypal()
*/
final class SavedPaymentMethodType extends PaddleEnum
{
private const Alipay = 'alipay';
private const ApplePay = 'apple_pay';
private const Card = 'card';
private const GooglePay = 'google_pay';
private const Paypal = 'paypal';
}
46 changes: 46 additions & 0 deletions src/Notifications/Entities/DeletedPaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

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

namespace Paddle\SDK\Notifications\Entities;

use Paddle\SDK\Entities\DateTime;
use Paddle\SDK\Notifications\Entities\Shared\SavedPaymentMethodDeletionReason;
use Paddle\SDK\Notifications\Entities\Shared\SavedPaymentMethodOrigin;
use Paddle\SDK\Notifications\Entities\Shared\SavedPaymentMethodType;

class DeletedPaymentMethod implements Entity
{
private function __construct(
public string $id,
public string $customerId,
public string $addressId,
public SavedPaymentMethodType $type,
public SavedPaymentMethodOrigin $origin,
public \DateTimeInterface|null $savedAt,
public \DateTimeInterface|null $updatedAt,
public SavedPaymentMethodDeletionReason $deletionReason,
) {
}

public static function from(array $data): self
{
return new self(
id: $data['id'],
customerId: $data['customer_id'],
addressId: $data['address_id'],
type: SavedPaymentMethodType::from($data['type']),
origin: SavedPaymentMethodOrigin::from($data['origin']),
savedAt: isset($data['saved_at']) ? DateTime::from($data['saved_at']) : null,
updatedAt: isset($data['updated_at']) ? DateTime::from($data['updated_at']) : null,
deletionReason: SavedPaymentMethodDeletionReason::from($data['deletion_reason']),
);
}
}
26 changes: 23 additions & 3 deletions src/Notifications/Entities/EntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@
class EntityFactory
{
public static function create(string $eventType, array $data): Entity
{
// Map specific event entity types.
$eventEntityTypes = [
'payment_method.deleted' => DeletedPaymentMethod::class,
];

$entity = $eventEntityTypes[$eventType] ?? self::resolveEntityClass($eventType);

return $entity::from($data);
}

/**
* @return class-string<Entity>
*/
private static function resolveEntityClass(string $eventType): string
{
$type = explode('.', $eventType);
$entity = $type[0] ?? 'Unknown';
$identifier = str_replace('_', '', ucwords(implode('_', $type), '_'));
$entity = self::snakeToPascalCase($type[0] ?? 'Unknown');
$identifier = self::snakeToPascalCase(implode('_', $type));

/** @var class-string<Entity> $entity */
$entity = sprintf('\Paddle\SDK\Notifications\Entities\%s', ucfirst($entity));
Expand All @@ -22,6 +37,11 @@ public static function create(string $eventType, array $data): Entity
throw new \UnexpectedValueException("Event type '{$identifier}' cannot be mapped to an object");
}

return $entity::from($data);
return $entity;
}

private static function snakeToPascalCase(string $string): string
{
return str_replace('_', '', ucwords($string, '_'));
}
}
43 changes: 43 additions & 0 deletions src/Notifications/Entities/PaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

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

namespace Paddle\SDK\Notifications\Entities;

use Paddle\SDK\Entities\DateTime;
use Paddle\SDK\Notifications\Entities\Shared\SavedPaymentMethodOrigin;
use Paddle\SDK\Notifications\Entities\Shared\SavedPaymentMethodType;

class PaymentMethod implements Entity
{
private function __construct(
public string $id,
public string $customerId,
public string $addressId,
public SavedPaymentMethodType $type,
public SavedPaymentMethodOrigin $origin,
public \DateTimeInterface|null $savedAt,
public \DateTimeInterface|null $updatedAt,
) {
}

public static function from(array $data): self
{
return new self(
id: $data['id'],
customerId: $data['customer_id'],
addressId: $data['address_id'],
type: SavedPaymentMethodType::from($data['type']),
origin: SavedPaymentMethodOrigin::from($data['origin']),
savedAt: isset($data['saved_at']) ? DateTime::from($data['saved_at']) : null,
updatedAt: isset($data['updated_at']) ? DateTime::from($data['updated_at']) : null,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

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

namespace Paddle\SDK\Notifications\Entities\Shared;

use Paddle\SDK\PaddleEnum;

/**
* @method static SavedPaymentMethodDeletionReason ReplacedByNewerVersion()
* @method static SavedPaymentMethodDeletionReason Api()
*/
final class SavedPaymentMethodDeletionReason extends PaddleEnum
{
private const ReplacedByNewerVersion = 'replaced_by_newer_version';
private const Api = 'api';
}
Loading

0 comments on commit e362095

Please sign in to comment.