-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for customer portal sessions
- Loading branch information
1 parent
d3807a1
commit 23716aa
Showing
15 changed files
with
422 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* |------ | ||
* | ! Generated code ! | ||
* | Altering this code will result in changes being overwritten | | ||
* |-------------------------------------------------------------|. | ||
*/ | ||
|
||
namespace Paddle\SDK\Entities; | ||
|
||
use Paddle\SDK\Entities\CustomerPortalSession\CustomerPortalSessionUrls; | ||
use Paddle\SDK\Notifications\Entities\Entity; | ||
|
||
class CustomerPortalSession implements Entity | ||
{ | ||
private function __construct( | ||
public string $id, | ||
public string $customerId, | ||
public CustomerPortalSessionUrls $urls, | ||
public \DateTimeInterface $createdAt, | ||
) { | ||
} | ||
|
||
public static function from(array $data): self | ||
{ | ||
return new self( | ||
id: $data['id'], | ||
customerId: $data['customer_id'], | ||
urls: CustomerPortalSessionUrls::from($data['urls']), | ||
createdAt: DateTime::from($data['created_at']), | ||
); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Entities/CustomerPortalSession/CustomerPortalSessionGeneralUrl.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\CustomerPortalSession; | ||
|
||
use Paddle\SDK\Notifications\Entities\Entity; | ||
|
||
class CustomerPortalSessionGeneralUrl implements Entity | ||
{ | ||
private function __construct( | ||
public string $overview, | ||
) { | ||
} | ||
|
||
public static function from(array $data): self | ||
{ | ||
return new self( | ||
overview: $data['overview'], | ||
); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Entities/CustomerPortalSession/CustomerPortalSessionSubscriptionUrl.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* |------ | ||
* | ! Generated code ! | ||
* | Altering this code will result in changes being overwritten | | ||
* |-------------------------------------------------------------|. | ||
*/ | ||
|
||
namespace Paddle\SDK\Entities\CustomerPortalSession; | ||
|
||
use Paddle\SDK\Notifications\Entities\Entity; | ||
|
||
class CustomerPortalSessionSubscriptionUrl implements Entity | ||
{ | ||
private function __construct( | ||
public string $id, | ||
public string $cancelSubscription, | ||
public string $updateSubscriptionPaymentMethod, | ||
) { | ||
} | ||
|
||
public static function from(array $data): self | ||
{ | ||
return new self( | ||
id: $data['id'], | ||
cancelSubscription: $data['cancel_subscription'], | ||
updateSubscriptionPaymentMethod: $data['update_subscription_payment_method'], | ||
); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Entities/CustomerPortalSession/CustomerPortalSessionUrls.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* |------ | ||
* | ! Generated code ! | ||
* | Altering this code will result in changes being overwritten | | ||
* |-------------------------------------------------------------|. | ||
*/ | ||
|
||
namespace Paddle\SDK\Entities\CustomerPortalSession; | ||
|
||
use Paddle\SDK\Notifications\Entities\Entity; | ||
|
||
class CustomerPortalSessionUrls implements Entity | ||
{ | ||
/** | ||
* @param CustomerPortalSessionSubscriptionUrl[] $subscriptions | ||
*/ | ||
private function __construct( | ||
public CustomerPortalSessionGeneralUrl $general, | ||
public array $subscriptions, | ||
) { | ||
} | ||
|
||
public static function from(array $data): self | ||
{ | ||
return new self( | ||
general: CustomerPortalSessionGeneralUrl::from($data['general']), | ||
subscriptions: array_map( | ||
fn (array $item): CustomerPortalSessionSubscriptionUrl => CustomerPortalSessionSubscriptionUrl::from($item), | ||
$data['subscriptions'] ?? [], | ||
), | ||
); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Resources/CustomerPortalSessions/CustomerPortalSessionsClient.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* |------ | ||
* | ! Generated code ! | ||
* | Altering this code will result in changes being overwritten | | ||
* |-------------------------------------------------------------|. | ||
*/ | ||
|
||
namespace Paddle\SDK\Resources\CustomerPortalSessions; | ||
|
||
use Paddle\SDK\Client; | ||
use Paddle\SDK\Entities\CustomerPortalSession; | ||
use Paddle\SDK\Exceptions\ApiError; | ||
use Paddle\SDK\Exceptions\SdkExceptions\MalformedResponse; | ||
use Paddle\SDK\Resources\CustomerPortalSessions\Operations\CreateCustomerPortalSession; | ||
use Paddle\SDK\ResponseParser; | ||
|
||
class CustomerPortalSessionsClient | ||
{ | ||
public function __construct( | ||
private readonly Client $client, | ||
) { | ||
} | ||
|
||
/** | ||
* @throws ApiError On a generic API error | ||
* @throws MalformedResponse If the API response was not parsable | ||
*/ | ||
public function create(string $customerId, CreateCustomerPortalSession $createOperation): CustomerPortalSession | ||
{ | ||
$parser = new ResponseParser( | ||
$this->client->postRaw("/customers/{$customerId}/portal-sessions", $createOperation), | ||
); | ||
|
||
return CustomerPortalSession::from($parser->getData()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Resources/CustomerPortalSessions/Operations/CreateCustomerPortalSession.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Paddle\SDK\Resources\CustomerPortalSessions\Operations; | ||
|
||
use Paddle\SDK\FiltersUndefined; | ||
use Paddle\SDK\Undefined; | ||
|
||
class CreateCustomerPortalSession implements \JsonSerializable | ||
{ | ||
use FiltersUndefined; | ||
|
||
/** | ||
* @param string[] $subscriptionIds | ||
*/ | ||
public function __construct( | ||
public readonly array|Undefined $subscriptionIds = new Undefined(), | ||
) { | ||
} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
return $this->filterUndefined([ | ||
'subscription_ids' => $this->subscriptionIds, | ||
]); | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
tests/Functional/Resources/CustomerPortalSessions/CustomerPortalSessionsClientTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Paddle\SDK\Tests\Functional\Resources\CustomerPortalSessions; | ||
|
||
use GuzzleHttp\Psr7\Response; | ||
use Http\Mock\Client as MockClient; | ||
use Paddle\SDK\Client; | ||
use Paddle\SDK\Environment; | ||
use Paddle\SDK\Options; | ||
use Paddle\SDK\Resources\CustomerPortalSessions\Operations\CreateCustomerPortalSession; | ||
use Paddle\SDK\Tests\Utils\ReadsFixtures; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class CustomerPortalSessionsClientTest extends TestCase | ||
{ | ||
use ReadsFixtures; | ||
|
||
private MockClient $mockClient; | ||
private Client $client; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->mockClient = new MockClient(); | ||
$this->client = new Client( | ||
apiKey: 'API_KEY_PLACEHOLDER', | ||
options: new Options(Environment::SANDBOX), | ||
httpClient: $this->mockClient); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @dataProvider createOperationsProvider | ||
*/ | ||
public function it_uses_expected_payload_on_create( | ||
CreateCustomerPortalSession $operation, | ||
ResponseInterface $response, | ||
string $expectedBody, | ||
): void { | ||
$this->mockClient->addResponse($response); | ||
$this->client->customerPortalSessions->create('ctm_01h844p3h41s12zs5mn4axja51', $operation); | ||
$request = $this->mockClient->getLastRequest(); | ||
|
||
self::assertInstanceOf(RequestInterface::class, $request); | ||
self::assertEquals('POST', $request->getMethod()); | ||
self::assertEquals( | ||
Environment::SANDBOX->baseUrl() . '/customers/ctm_01h844p3h41s12zs5mn4axja51/portal-sessions', | ||
urldecode((string) $request->getUri()), | ||
); | ||
self::assertJsonStringEqualsJsonString($expectedBody, (string) $request->getBody()); | ||
} | ||
|
||
public static function createOperationsProvider(): \Generator | ||
{ | ||
yield 'Create portal session with single subscription ID' => [ | ||
new CreateCustomerPortalSession(['sub_01h04vsc0qhwtsbsxh3422wjs4']), | ||
new Response(201, body: self::readRawJsonFixture('response/full_entity_single')), | ||
self::readRawJsonFixture('request/create_single'), | ||
]; | ||
|
||
yield 'Create portal session with multiple subscription IDs' => [ | ||
new CreateCustomerPortalSession(['sub_01h04vsc0qhwtsbsxh3422wjs4', 'sub_02h04vsc0qhwtsbsxh3422wjs4']), | ||
new Response(201, body: self::readRawJsonFixture('response/full_entity_multiple')), | ||
self::readRawJsonFixture('request/create_multiple'), | ||
]; | ||
|
||
yield 'Create portal session with empty subscription IDs' => [ | ||
new CreateCustomerPortalSession([]), | ||
new Response(201, body: self::readRawJsonFixture('response/full_entity_empty')), | ||
self::readRawJsonFixture('request/create_empty'), | ||
]; | ||
|
||
yield 'Create portal session with omitted subscription IDs' => [ | ||
new CreateCustomerPortalSession(), | ||
new Response(201, body: self::readRawJsonFixture('response/full_entity_empty')), | ||
'{}', | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_returns_expected_response_on_create(): void | ||
{ | ||
$operation = new CreateCustomerPortalSession(['sub_01h04vsc0qhwtsbsxh3422wjs4', 'sub_02h04vsc0qhwtsbsxh3422wjs4']); | ||
$response = new Response(201, body: self::readRawJsonFixture('response/full_entity_multiple')); | ||
|
||
$this->mockClient->addResponse($response); | ||
$portalSession = $this->client->customerPortalSessions->create('ctm_01gysfvfy7vqhpzkq8rjmrq7an', $operation); | ||
|
||
self::assertEquals('cpls_01h4ge9r64c22exjsx0fy8b48b', $portalSession->id); | ||
self::assertEquals('ctm_01gysfvfy7vqhpzkq8rjmrq7an', $portalSession->customerId); | ||
self::assertEquals('2024-10-25T06:53:58+00:00', $portalSession->createdAt->format(DATE_RFC3339)); | ||
|
||
self::assertEquals( | ||
'https://customer-portal.paddle.com/cpl_01j7zbyqs3vah3aafp4jf62qaw?action=overview&token=pga_eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjdG1fMDFncm5uNHp0YTVhMW1mMDJqanplN3kyeXMiLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE3Mjc2NzkyMzh9._oO12IejzdKmyKTwb7BLjmiILkx4_cSyGjXraOBUI_g', | ||
$portalSession->urls->general->overview, | ||
); | ||
|
||
self::assertEquals( | ||
'sub_01h04vsc0qhwtsbsxh3422wjs4', | ||
$portalSession->urls->subscriptions[0]->id, | ||
); | ||
self::assertEquals( | ||
'https://customer-portal.paddle.com/cpl_01j7zbyqs3vah3aafp4jf62qaw?action=cancel_subscription&subscription_id=sub_01h04vsc0qhwtsbsxh3422wjs4&token=pga_eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjdG1fMDFncm5uNHp0YTVhMW1mMDJqanplN3kyeXMiLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE3Mjc2NzkyMzh9._oO12IejzdKmyKTwb7BLjmiILkx4_cSyGjXraOBUI_g', | ||
$portalSession->urls->subscriptions[0]->cancelSubscription, | ||
); | ||
self::assertEquals( | ||
'https://customer-portal.paddle.com/cpl_01j7zbyqs3vah3aafp4jf62qaw?action=update_subscription_payment_method&subscription_id=sub_01h04vsc0qhwtsbsxh3422wjs4&token=pga_eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjdG1fMDFncm5uNHp0YTVhMW1mMDJqanplN3kyeXMiLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE3Mjc2NzkyMzh9._oO12IejzdKmyKTwb7BLjmiILkx4_cSyGjXraOBUI_g', | ||
$portalSession->urls->subscriptions[0]->updateSubscriptionPaymentMethod, | ||
); | ||
|
||
self::assertEquals( | ||
'sub_02h04vsc0qhwtsbsxh3422wjs4', | ||
$portalSession->urls->subscriptions[1]->id, | ||
); | ||
self::assertEquals( | ||
'https://customer-portal.paddle.com/cpl_01j7zbyqs3vah3aafp4jf62qaw?action=cancel_subscription&subscription_id=sub_02h04vsc0qhwtsbsxh3422wjs4&token=pga_eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjdG1fMDFncm5uNHp0YTVhMW1mMDJqanplN3kyeXMiLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE3Mjc2NzkyMzh9._oO12IejzdKmyKTwb7BLjmiILkx4_cSyGjXraOBUI_g', | ||
$portalSession->urls->subscriptions[1]->cancelSubscription, | ||
); | ||
self::assertEquals( | ||
'https://customer-portal.paddle.com/cpl_01j7zbyqs3vah3aafp4jf62qaw?action=update_subscription_payment_method&subscription_id=sub_02h04vsc0qhwtsbsxh3422wjs4&token=pga_eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjdG1fMDFncm5uNHp0YTVhMW1mMDJqanplN3kyeXMiLCJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE3Mjc2NzkyMzh9._oO12IejzdKmyKTwb7BLjmiILkx4_cSyGjXraOBUI_g', | ||
$portalSession->urls->subscriptions[1]->updateSubscriptionPaymentMethod, | ||
); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/Functional/Resources/CustomerPortalSessions/_fixtures/request/create_empty.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"subscription_ids": [] | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/Functional/Resources/CustomerPortalSessions/_fixtures/request/create_multiple.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"subscription_ids": [ | ||
"sub_01h04vsc0qhwtsbsxh3422wjs4", | ||
"sub_02h04vsc0qhwtsbsxh3422wjs4" | ||
] | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/Functional/Resources/CustomerPortalSessions/_fixtures/request/create_single.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"subscription_ids": [ | ||
"sub_01h04vsc0qhwtsbsxh3422wjs4" | ||
] | ||
} |
Oops, something went wrong.