-
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.
fix: Adjustment items can be omitted when adjustment type is full (#110)
- Loading branch information
1 parent
6f5191e
commit 31b5cc0
Showing
9 changed files
with
219 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Paddle\SDK\Entities\Shared\Action; | ||
use Paddle\SDK\Entities\Shared\AdjustmentType; | ||
use Paddle\SDK\Exceptions\ApiError; | ||
use Paddle\SDK\Exceptions\SdkExceptions\MalformedResponse; | ||
use Paddle\SDK\Resources\Adjustments\Operations\Create\AdjustmentItem; | ||
use Paddle\SDK\Resources\Adjustments\Operations\CreateAdjustment; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$environment = Paddle\SDK\Environment::tryFrom(getenv('PADDLE_ENVIRONMENT') ?: '') ?? Paddle\SDK\Environment::SANDBOX; | ||
$apiKey = getenv('PADDLE_API_KEY') ?: null; | ||
$transactionId = getenv('PADDLE_TRANSACTION_ID') ?: null; | ||
$transactionItemId = getenv('PADDLE_TRANSACTION_ITEM_ID') ?: null; | ||
$fullAdjustmentTransactionId = getenv('PADDLE_FULL_ADJUSTMENT_TRANSACTION_ID') ?: null; | ||
|
||
if (is_null($apiKey)) { | ||
echo "You must provide the PADDLE_API_KEY in the environment:\n"; | ||
echo "PADDLE_API_KEY=your-key php examples/basic_usage.php\n"; | ||
exit(1); | ||
} | ||
|
||
$paddle = new Paddle\SDK\Client($apiKey, options: new Paddle\SDK\Options($environment)); | ||
|
||
// ┌─── | ||
// │ Create Partial Adjustment │ | ||
// └───────────────────────────┘ | ||
try { | ||
$partialAdjustment = $paddle->adjustments->create( | ||
CreateAdjustment::partial( | ||
Action::Refund(), | ||
[ | ||
new AdjustmentItem( | ||
$transactionItemId, | ||
AdjustmentType::Partial(), | ||
'100', | ||
), | ||
], | ||
'error', | ||
$transactionId, | ||
), | ||
); | ||
} catch (ApiError|MalformedResponse $e) { | ||
var_dump($e); | ||
exit; | ||
} | ||
|
||
echo sprintf("Partial Adjustment ID: %s\n", $partialAdjustment->id); | ||
|
||
// ┌─── | ||
// │ Create Full Adjustment │ | ||
// └────────────────────────┘ | ||
try { | ||
$fullAdjustment = $paddle->adjustments->create( | ||
CreateAdjustment::full( | ||
Action::Refund(), | ||
'error', | ||
$fullAdjustmentTransactionId, | ||
), | ||
); | ||
} catch (ApiError|MalformedResponse $e) { | ||
var_dump($e); | ||
exit; | ||
} | ||
|
||
echo sprintf("Full Adjustment ID: %s\n", $fullAdjustment->id); |
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
6 changes: 6 additions & 0 deletions
6
tests/Functional/Resources/Adjustments/_fixtures/request/create_type_full_with_no_items.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 @@ | ||
{ | ||
"action": "refund", | ||
"type": "full", | ||
"reason": "error", | ||
"transaction_id": "txn_01h8bxpvx398a7zbawb77y0kp5" | ||
} |
7 changes: 7 additions & 0 deletions
7
.../Functional/Resources/Adjustments/_fixtures/request/create_type_full_with_null_items.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,7 @@ | ||
{ | ||
"action": "refund", | ||
"type": "full", | ||
"items": null, | ||
"reason": "error", | ||
"transaction_id": "txn_01h8bxpvx398a7zbawb77y0kp5" | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/Functional/Resources/Adjustments/_fixtures/request/create_type_partial_with_items.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,13 @@ | ||
{ | ||
"action": "refund", | ||
"type": "partial", | ||
"items": [ | ||
{ | ||
"item_id": "txnitm_01h8bxryv3065dyh6103p3yg28", | ||
"type": "partial", | ||
"amount": "100" | ||
} | ||
], | ||
"reason": "error", | ||
"transaction_id": "txn_01h8bxpvx398a7zbawb77y0kp5" | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/Unit/Resources/Adjustments/Operations/CreateAdjustmentTest.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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Paddle\SDK\Tests\Unit\Resources\Adjustments\Operations; | ||
|
||
use Paddle\SDK\Entities\Adjustment\AdjustmentType; | ||
use Paddle\SDK\Entities\Shared\Action; | ||
use Paddle\SDK\Exceptions\SdkExceptions\InvalidArgumentException; | ||
use Paddle\SDK\Resources\Adjustments\Operations\CreateAdjustment; | ||
use Paddle\SDK\Undefined; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CreateAdjustmentTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @dataProvider invalidItemsDataProvider | ||
*/ | ||
public function it_validates_items(array|Undefined|null $items, AdjustmentType $type, string $expectedExceptionMessage): void | ||
{ | ||
self::expectException(InvalidArgumentException::class); | ||
self::expectExceptionMessage($expectedExceptionMessage); | ||
|
||
new CreateAdjustment( | ||
Action::Refund(), | ||
$items, | ||
'error', | ||
'txn_01h8bxpvx398a7zbawb77y0kp5', | ||
$type, | ||
); | ||
} | ||
|
||
public static function invalidItemsDataProvider(): \Generator | ||
{ | ||
yield 'Empty' => [ | ||
[], | ||
AdjustmentType::Partial(), | ||
'items cannot be empty', | ||
]; | ||
yield 'Undefined' => [ | ||
new Undefined(), | ||
AdjustmentType::Partial(), | ||
'items cannot be empty', | ||
]; | ||
yield 'Null' => [ | ||
null, | ||
AdjustmentType::Partial(), | ||
'items cannot be empty', | ||
]; | ||
yield 'Items for full type' => [ | ||
[], | ||
AdjustmentType::Full(), | ||
'items are not allowed when the adjustment type is full', | ||
]; | ||
} | ||
} |