Skip to content

Commit

Permalink
feat: Add support for Adjustment Credit Note (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeymike authored Sep 30, 2024
1 parent 21cfb68 commit 4da706b
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ 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.

## [1.3.0] - 2024-09-30

### Added

- Added `AdjustmentsClient::getCreditNote`, see [related changelog](https://developer.paddle.com/changelog/2024/generate-adjustments-credit-notes)

## [1.2.0] - 2024-09-18

### Added
Expand Down
25 changes: 25 additions & 0 deletions src/Entities/AdjustmentCreditNote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

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

namespace Paddle\SDK\Entities;

class AdjustmentCreditNote
{
private function __construct(
public string $url,
) {
}

public static function from(array $data): self
{
return new self($data['url']);
}
}
16 changes: 16 additions & 0 deletions src/Resources/Adjustments/AdjustmentsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

use Paddle\SDK\Client;
use Paddle\SDK\Entities\Adjustment;
use Paddle\SDK\Entities\AdjustmentCreditNote;
use Paddle\SDK\Entities\Collections\AdjustmentCollection;
use Paddle\SDK\Entities\Collections\Paginator;
use Paddle\SDK\Exceptions\ApiError;
use Paddle\SDK\Exceptions\SdkExceptions\MalformedResponse;
use Paddle\SDK\Resources\Adjustments\Operations\CreateAdjustment;
use Paddle\SDK\Resources\Adjustments\Operations\GetAdjustmentCreditNote;
use Paddle\SDK\Resources\Adjustments\Operations\ListAdjustments;
use Paddle\SDK\ResponseParser;

Expand Down Expand Up @@ -57,4 +59,18 @@ public function create(CreateAdjustment $createOperation): Adjustment

return Adjustment::from($parser->getData());
}

/**
* @throws ApiError On a generic API error
* @throws ApiError\AdjustmentApiError On an adjustment specific API error
* @throws MalformedResponse If the API response was not parsable
*/
public function getCreditNote(string $id, GetAdjustmentCreditNote $getOperation = new GetAdjustmentCreditNote()): AdjustmentCreditNote
{
$parser = new ResponseParser(
$this->client->getRaw("/adjustments/{$id}/credit-note", $getOperation),
);

return AdjustmentCreditNote::from($parser->getData());
}
}
23 changes: 23 additions & 0 deletions src/Resources/Adjustments/Operations/GetAdjustmentCreditNote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Resources\Adjustments\Operations;

use Paddle\SDK\Entities\Shared\Disposition;
use Paddle\SDK\HasParameters;

class GetAdjustmentCreditNote implements HasParameters
{
public function __construct(
private readonly Disposition|null $disposition = null,
) {
}

public function getParameters(): array
{
return array_filter([
'disposition' => $this->disposition?->getValue(),
]);
}
}
64 changes: 64 additions & 0 deletions tests/Functional/Resources/Adjustments/AdjustmentsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use Paddle\SDK\Entities\Shared\Action;
use Paddle\SDK\Entities\Shared\AdjustmentStatus;
use Paddle\SDK\Entities\Shared\AdjustmentType;
use Paddle\SDK\Entities\Shared\Disposition;
use Paddle\SDK\Environment;
use Paddle\SDK\Options;
use Paddle\SDK\Resources\Adjustments\Operations\Create\AdjustmentItem;
use Paddle\SDK\Resources\Adjustments\Operations\CreateAdjustment;
use Paddle\SDK\Resources\Adjustments\Operations\GetAdjustmentCreditNote;
use Paddle\SDK\Resources\Adjustments\Operations\ListAdjustments;
use Paddle\SDK\Resources\Shared\Operations\List\Pager;
use Paddle\SDK\Tests\Utils\ReadsFixtures;
Expand Down Expand Up @@ -208,4 +210,66 @@ public static function listOperationsProvider(): \Generator
sprintf('%s/adjustments?action=refund', Environment::SANDBOX->baseUrl()),
];
}

/**
* @test
*/
public function get_credit_note_has_correct_url(): void
{
$fixture = self::readRawJsonFixture('response/get_credit_note_default');
$this->mockClient->addResponse(new Response(200, body: $fixture));

$creditNote = $this->client->adjustments->getCreditNote(
'adj_01h8c65c2ggq5nxswnnwv78e75',
new GetAdjustmentCreditNote(),
);

$expectedCreditNoteUrl = 'https://paddle-production-invoice-service-pdfs.s3.amazonaws.com/credit_notes/15839/crdnt_01j4scmgpbtbxap16573dtck9n/credit_notes_296-10016_Paddle-com.pdf';

self::assertSame($expectedCreditNoteUrl, $creditNote->url);
}

/**
* @test
*
* @dataProvider getCreditNoteOperationsProvider
*/
public function get_credit_note_hits_expected_uri(
string $id,
GetAdjustmentCreditNote $getOperation,
ResponseInterface $response,
string $expectedUri,
): void {
$this->mockClient->addResponse($response);
$this->client->adjustments->getCreditNote($id, $getOperation);
$request = $this->mockClient->getLastRequest();

self::assertInstanceOf(RequestInterface::class, $request);
self::assertEquals('GET', $request->getMethod());
self::assertEquals($expectedUri, urldecode((string) $request->getUri()));
}

public static function getCreditNoteOperationsProvider(): \Generator
{
yield 'Default' => [
'adj_01h8c65c2ggq5nxswnnwv78e75',
new GetAdjustmentCreditNote(),
new Response(200, body: self::readRawJsonFixture('response/get_credit_note_default')),
sprintf('%s/adjustments/adj_01h8c65c2ggq5nxswnnwv78e75/credit-note', Environment::SANDBOX->baseUrl()),
];

yield 'Disposition Inline' => [
'adj_01h8c65c2ggq5nxswnnwv78e75',
new GetAdjustmentCreditNote(Disposition::Inline()),
new Response(200, body: self::readRawJsonFixture('response/get_credit_note_default')),
sprintf('%s/adjustments/adj_01h8c65c2ggq5nxswnnwv78e75/credit-note?disposition=inline', Environment::SANDBOX->baseUrl()),
];

yield 'Disposition Attachment' => [
'adj_01h8c65c2ggq5nxswnnwv78e75',
new GetAdjustmentCreditNote(Disposition::Attachment()),
new Response(200, body: self::readRawJsonFixture('response/get_credit_note_default')),
sprintf('%s/adjustments/adj_01h8c65c2ggq5nxswnnwv78e75/credit-note?disposition=attachment', Environment::SANDBOX->baseUrl()),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"url": "https://paddle-production-invoice-service-pdfs.s3.amazonaws.com/credit_notes/15839/crdnt_01j4scmgpbtbxap16573dtck9n/credit_notes_296-10016_Paddle-com.pdf"
},
"meta": {
"request_id": "e34d4a9c-2088-447d-a3a1-1da5ce74f507"
}
}

0 comments on commit 4da706b

Please sign in to comment.