-
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.
- Loading branch information
1 parent
e0999fc
commit 7e12d21
Showing
1 changed file
with
69 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
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}'") |