-
Notifications
You must be signed in to change notification settings - Fork 4
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 adjustment type and VND currency (#83)
- Loading branch information
1 parent
0ef3a03
commit 385c359
Showing
21 changed files
with
296 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 @@ | ||
from logging import getLogger | ||
from os import getenv | ||
from sys import exit # You should use classes/functions that returns instead of exits | ||
|
||
from paddle_billing import Client, Environment, Options | ||
|
||
from paddle_billing.Entities.Shared import ( | ||
Action, | ||
AdjustmentType, | ||
) | ||
|
||
from paddle_billing.Exceptions.ApiError import ApiError | ||
|
||
from paddle_billing.Resources.Adjustments.Operations import CreateAdjustment, CreateAdjustmentItem | ||
|
||
log = getLogger("my_app") | ||
|
||
# Verify your Paddle API key was provided by a PADDLE_SECRET_API_KEY environment variable | ||
# It is strongly advised that you do not include secrets in your source code | ||
# Use environment variables, and/or secrets management like Vault to obtain your secrets | ||
api_key: str = getenv("PADDLE_SECRET_API_KEY", None) | ||
if not api_key: | ||
raise ValueError("You must provide the PADDLE_SECRET_API_KEY in your environment variables") | ||
|
||
transaction_id: str = getenv("PADDLE_TRANSACTION_ID", None) | ||
transaction_item_id: str = getenv("PADDLE_TRANSACTION_ITEM_ID", None) | ||
full_adjustment_transaction_id: str = getenv("PADDLE_FULL_ADJUSTMENT_TRANSACTION_ID", None) | ||
|
||
# Determine the environment, defaulting to sandbox | ||
environment = Environment(getenv("PADDLE_ENVIRONMENT", "sandbox")) | ||
|
||
# Initialize the Paddle client | ||
paddle = Client(api_key, options=Options(environment), logger=log) | ||
|
||
|
||
# ┌─── | ||
# │ Create Partial Adjustment │ | ||
# └───────────────────────────┘ | ||
try: | ||
partial_adjustment = paddle.adjustments.create( | ||
CreateAdjustment.partial( | ||
Action.Refund, | ||
[CreateAdjustmentItem(transaction_item_id, AdjustmentType.Partial, "100")], | ||
"error", | ||
transaction_id, | ||
) | ||
) | ||
except ApiError as error: | ||
print(error) | ||
exit(1) | ||
|
||
print(f"Partial Adjustment '{partial_adjustment.id}'") | ||
|
||
# ┌─── | ||
# │ Create Full Adjustment │ | ||
# └────────────────────────┘ | ||
try: | ||
full_adjustment = paddle.adjustments.create( | ||
CreateAdjustment.full( | ||
Action.Refund, | ||
"error", | ||
full_adjustment_transaction_id, | ||
) | ||
) | ||
except ApiError as error: | ||
print(error) | ||
exit(1) | ||
|
||
print(f"Full Adjustment '{full_adjustment.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from paddle_billing.PaddleStrEnum import PaddleStrEnum, PaddleStrEnumMeta | ||
|
||
|
||
class AdjustmentActionType(PaddleStrEnum, metaclass=PaddleStrEnumMeta): | ||
Full: "AdjustmentActionType" = "full" | ||
Partial: "AdjustmentActionType" = "partial" |
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
paddle_billing/Notifications/Entities/Shared/AdjustmentActionType.py
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 @@ | ||
from paddle_billing.PaddleStrEnum import PaddleStrEnum, PaddleStrEnumMeta | ||
|
||
|
||
class AdjustmentActionType(PaddleStrEnum, metaclass=PaddleStrEnumMeta): | ||
Full: "AdjustmentActionType" = "full" | ||
Partial: "AdjustmentActionType" = "partial" |
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
47 changes: 45 additions & 2 deletions
47
paddle_billing/Resources/Adjustments/Operations/CreateAdjustment.py
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 |
---|---|---|
@@ -1,15 +1,58 @@ | ||
from dataclasses import dataclass | ||
|
||
from paddle_billing.Undefined import Undefined | ||
|
||
from paddle_billing.Operation import Operation | ||
|
||
from paddle_billing.Entities.Shared import Action | ||
from paddle_billing.Entities.Shared import Action, AdjustmentActionType | ||
|
||
from paddle_billing.Resources.Adjustments.Operations import CreateAdjustmentItem | ||
|
||
from paddle_billing.Exceptions.SdkExceptions.InvalidArgumentException import InvalidArgumentException | ||
|
||
|
||
@dataclass | ||
class CreateAdjustment(Operation): | ||
action: Action | ||
items: list[CreateAdjustmentItem] | ||
items: list[CreateAdjustmentItem] | None | Undefined | ||
reason: str | ||
transaction_id: str | ||
type: AdjustmentActionType | Undefined = Undefined() | ||
|
||
def __post_init__(self): | ||
if self.type != AdjustmentActionType.Full and ( | ||
self.items is None or isinstance(self.items, Undefined) or len(self.items) == 0 | ||
): | ||
raise InvalidArgumentException.array_is_empty("items") | ||
|
||
if self.type == AdjustmentActionType.Full and isinstance(self.items, list): | ||
raise InvalidArgumentException("items are not allowed when the adjustment type is full") | ||
|
||
@staticmethod | ||
def full( | ||
action: Action, | ||
reason: str, | ||
transaction_id: str, | ||
) -> "CreateAdjustment": | ||
return CreateAdjustment( | ||
action=action, | ||
reason=reason, | ||
transaction_id=transaction_id, | ||
items=Undefined(), | ||
type=AdjustmentActionType.Full, | ||
) | ||
|
||
@staticmethod | ||
def partial( | ||
action: Action, | ||
items: list[CreateAdjustmentItem], | ||
reason: str, | ||
transaction_id: str, | ||
) -> "CreateAdjustment": | ||
return CreateAdjustment( | ||
action=action, | ||
reason=reason, | ||
transaction_id=transaction_id, | ||
items=items, | ||
type=AdjustmentActionType.Partial, | ||
) |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
|
||
setup( | ||
version="1.2.2", | ||
version="1.3.0", | ||
author="Paddle and contributors", | ||
author_email="[email protected]", | ||
description="Paddle's Python SDK for Paddle Billing", | ||
|
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" | ||
} |
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
Oops, something went wrong.