Skip to content

Commit 0005443

Browse files
feat: Add support for customer portal sessions (#103)
1 parent de511be commit 0005443

File tree

16 files changed

+466
-0
lines changed

16 files changed

+466
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Check our main [developer changelog](https://developer.paddle.com/?utm_source=dx
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- Support for customer portal sessions, see [related changelog](https://developer.paddle.com/changelog/2024/customer-portal-sessions?utm_source=dx&utm_medium=paddle-php-sdk)
14+
- `Client->customerPortalSessions->create()`
15+
1116
### Fixed
1217

1318
- `Client->notifications->replay()` now calls the correct endpoint
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Paddle\SDK\Exceptions\ApiError;
6+
use Paddle\SDK\Exceptions\SdkExceptions\MalformedResponse;
7+
use Paddle\SDK\Resources\CustomerPortalSessions\Operations\CreateCustomerPortalSession;
8+
9+
require __DIR__ . '/../vendor/autoload.php';
10+
11+
$environment = Paddle\SDK\Environment::tryFrom(getenv('PADDLE_ENVIRONMENT') ?: '') ?? Paddle\SDK\Environment::SANDBOX;
12+
$apiKey = getenv('PADDLE_API_KEY') ?: null;
13+
$customerId = getenv('PADDLE_CUSTOMER_ID') ?: null;
14+
$subscriptionId = getenv('PADDLE_SUBSCRIPTION_ID') ?: null;
15+
16+
if (is_null($apiKey)) {
17+
echo "You must provide the PADDLE_API_KEY in the environment:\n";
18+
echo "PADDLE_API_KEY=your-key php examples/basic_usage.php\n";
19+
exit(1);
20+
}
21+
22+
$paddle = new Paddle\SDK\Client($apiKey, options: new Paddle\SDK\Options($environment));
23+
24+
// ┌───
25+
// │ Create Customer Portal Session │
26+
// └────────────────────────────────┘
27+
try {
28+
$customerPortalSession = $paddle->customerPortalSessions->create(
29+
$customerId,
30+
new CreateCustomerPortalSession([$subscriptionId]),
31+
);
32+
} catch (ApiError|MalformedResponse $e) {
33+
var_dump($e);
34+
exit;
35+
}
36+
37+
echo sprintf("Created Customer Portal Session: %s\n", $customerPortalSession->id);
38+
echo sprintf(" - Customer ID: %s\n", $customerPortalSession->customerId);
39+
echo sprintf(" - Created At: %s\n", $customerPortalSession->createdAt->format(DATE_RFC3339_EXTENDED));
40+
echo sprintf(" - General Overview URL: %s\n", $customerPortalSession->urls->general->overview);
41+
42+
foreach ($customerPortalSession->urls->subscriptions as $subscriptionUrl) {
43+
echo sprintf(" - Subscription URLs: %s\n", $subscriptionUrl->id);
44+
echo sprintf(" - Update Subscription Payment Method URL: %s\n", $subscriptionUrl->updateSubscriptionPaymentMethod);
45+
echo sprintf(" - Cancel URL: %s\n", $subscriptionUrl->cancelSubscription);
46+
}

src/Client.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Paddle\SDK\Resources\Addresses\AddressesClient;
2323
use Paddle\SDK\Resources\Adjustments\AdjustmentsClient;
2424
use Paddle\SDK\Resources\Businesses\BusinessesClient;
25+
use Paddle\SDK\Resources\CustomerPortalSessions\CustomerPortalSessionsClient;
2526
use Paddle\SDK\Resources\Customers\CustomersClient;
2627
use Paddle\SDK\Resources\Discounts\DiscountsClient;
2728
use Paddle\SDK\Resources\Events\EventsClient;
@@ -69,6 +70,7 @@ class Client
6970
public readonly TransactionsClient $transactions;
7071
public readonly AdjustmentsClient $adjustments;
7172
public readonly CustomersClient $customers;
73+
public readonly CustomerPortalSessionsClient $customerPortalSessions;
7274
public readonly AddressesClient $addresses;
7375
public readonly BusinessesClient $businesses;
7476
public readonly DiscountsClient $discounts;
@@ -118,6 +120,7 @@ public function __construct(
118120
$this->adjustments = new AdjustmentsClient($this);
119121
$this->customers = new CustomersClient($this);
120122
$this->addresses = new AddressesClient($this);
123+
$this->customerPortalSessions = new CustomerPortalSessionsClient($this);
121124
$this->businesses = new BusinessesClient($this);
122125
$this->discounts = new DiscountsClient($this);
123126
$this->subscriptions = new SubscriptionsClient($this);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* |------
7+
* | ! Generated code !
8+
* | Altering this code will result in changes being overwritten |
9+
* |-------------------------------------------------------------|.
10+
*/
11+
12+
namespace Paddle\SDK\Entities;
13+
14+
use Paddle\SDK\Entities\CustomerPortalSession\CustomerPortalSessionUrls;
15+
use Paddle\SDK\Notifications\Entities\Entity;
16+
17+
class CustomerPortalSession implements Entity
18+
{
19+
private function __construct(
20+
public string $id,
21+
public string $customerId,
22+
public CustomerPortalSessionUrls $urls,
23+
public \DateTimeInterface $createdAt,
24+
) {
25+
}
26+
27+
public static function from(array $data): self
28+
{
29+
return new self(
30+
id: $data['id'],
31+
customerId: $data['customer_id'],
32+
urls: CustomerPortalSessionUrls::from($data['urls']),
33+
createdAt: DateTime::from($data['created_at']),
34+
);
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* |------
7+
* | ! Generated code !
8+
* | Altering this code will result in changes being overwritten |
9+
* |-------------------------------------------------------------|.
10+
*/
11+
12+
namespace Paddle\SDK\Entities\CustomerPortalSession;
13+
14+
use Paddle\SDK\Notifications\Entities\Entity;
15+
16+
class CustomerPortalSessionGeneralUrl implements Entity
17+
{
18+
private function __construct(
19+
public string $overview,
20+
) {
21+
}
22+
23+
public static function from(array $data): self
24+
{
25+
return new self(
26+
overview: $data['overview'],
27+
);
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* |------
7+
* | ! Generated code !
8+
* | Altering this code will result in changes being overwritten |
9+
* |-------------------------------------------------------------|.
10+
*/
11+
12+
namespace Paddle\SDK\Entities\CustomerPortalSession;
13+
14+
use Paddle\SDK\Notifications\Entities\Entity;
15+
16+
class CustomerPortalSessionSubscriptionUrl implements Entity
17+
{
18+
private function __construct(
19+
public string $id,
20+
public string $cancelSubscription,
21+
public string $updateSubscriptionPaymentMethod,
22+
) {
23+
}
24+
25+
public static function from(array $data): self
26+
{
27+
return new self(
28+
id: $data['id'],
29+
cancelSubscription: $data['cancel_subscription'],
30+
updateSubscriptionPaymentMethod: $data['update_subscription_payment_method'],
31+
);
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* |------
7+
* | ! Generated code !
8+
* | Altering this code will result in changes being overwritten |
9+
* |-------------------------------------------------------------|.
10+
*/
11+
12+
namespace Paddle\SDK\Entities\CustomerPortalSession;
13+
14+
use Paddle\SDK\Notifications\Entities\Entity;
15+
16+
class CustomerPortalSessionUrls implements Entity
17+
{
18+
/**
19+
* @param CustomerPortalSessionSubscriptionUrl[] $subscriptions
20+
*/
21+
private function __construct(
22+
public CustomerPortalSessionGeneralUrl $general,
23+
public array $subscriptions,
24+
) {
25+
}
26+
27+
public static function from(array $data): self
28+
{
29+
return new self(
30+
general: CustomerPortalSessionGeneralUrl::from($data['general']),
31+
subscriptions: array_map(
32+
fn (array $item): CustomerPortalSessionSubscriptionUrl => CustomerPortalSessionSubscriptionUrl::from($item),
33+
$data['subscriptions'] ?? [],
34+
),
35+
);
36+
}
37+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* |------
7+
* | ! Generated code !
8+
* | Altering this code will result in changes being overwritten |
9+
* |-------------------------------------------------------------|.
10+
*/
11+
12+
namespace Paddle\SDK\Resources\CustomerPortalSessions;
13+
14+
use Paddle\SDK\Client;
15+
use Paddle\SDK\Entities\CustomerPortalSession;
16+
use Paddle\SDK\Exceptions\ApiError;
17+
use Paddle\SDK\Exceptions\SdkExceptions\MalformedResponse;
18+
use Paddle\SDK\Resources\CustomerPortalSessions\Operations\CreateCustomerPortalSession;
19+
use Paddle\SDK\ResponseParser;
20+
21+
class CustomerPortalSessionsClient
22+
{
23+
public function __construct(
24+
private readonly Client $client,
25+
) {
26+
}
27+
28+
/**
29+
* @throws ApiError On a generic API error
30+
* @throws MalformedResponse If the API response was not parsable
31+
*/
32+
public function create(string $customerId, CreateCustomerPortalSession $createOperation): CustomerPortalSession
33+
{
34+
$parser = new ResponseParser(
35+
$this->client->postRaw("/customers/{$customerId}/portal-sessions", $createOperation),
36+
);
37+
38+
return CustomerPortalSession::from($parser->getData());
39+
}
40+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Paddle\SDK\Resources\CustomerPortalSessions\Operations;
6+
7+
use Paddle\SDK\FiltersUndefined;
8+
use Paddle\SDK\Undefined;
9+
10+
class CreateCustomerPortalSession implements \JsonSerializable
11+
{
12+
use FiltersUndefined;
13+
14+
/**
15+
* @param string[] $subscriptionIds
16+
*/
17+
public function __construct(
18+
public readonly array|Undefined $subscriptionIds = new Undefined(),
19+
) {
20+
}
21+
22+
public function jsonSerialize(): array
23+
{
24+
return $this->filterUndefined([
25+
'subscription_ids' => $this->subscriptionIds,
26+
]);
27+
}
28+
}

0 commit comments

Comments
 (0)