-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add support for adjustment type and VND currency #83
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
items
cannot be set toUndefined()
by default without moving the argument, which would be a breaking change.CreateAdjustment.full()
andCreateAdjustment.partial()
have been introduced to make it easier to create this operation, e.g.The constructor can still be used, but items must always be provided, e.g.