|
| 1 | +from logging import getLogger |
| 2 | +from os import getenv |
| 3 | +from sys import exit # You should use classes/functions that returns instead of exits |
| 4 | + |
| 5 | +from paddle_billing import Client, Environment, Options |
| 6 | + |
| 7 | +from paddle_billing.Entities.Shared import ( |
| 8 | + Action, |
| 9 | + AdjustmentType, |
| 10 | +) |
| 11 | + |
| 12 | +from paddle_billing.Exceptions.ApiError import ApiError |
| 13 | + |
| 14 | +from paddle_billing.Resources.Adjustments.Operations import CreateAdjustment, CreateAdjustmentItem |
| 15 | + |
| 16 | +log = getLogger("my_app") |
| 17 | + |
| 18 | +# Verify your Paddle API key was provided by a PADDLE_SECRET_API_KEY environment variable |
| 19 | +# It is strongly advised that you do not include secrets in your source code |
| 20 | +# Use environment variables, and/or secrets management like Vault to obtain your secrets |
| 21 | +api_key: str = getenv("PADDLE_SECRET_API_KEY", None) |
| 22 | +if not api_key: |
| 23 | + raise ValueError("You must provide the PADDLE_SECRET_API_KEY in your environment variables") |
| 24 | + |
| 25 | +transaction_id: str = getenv("PADDLE_TRANSACTION_ID", None) |
| 26 | +transaction_item_id: str = getenv("PADDLE_TRANSACTION_ITEM_ID", None) |
| 27 | +full_adjustment_transaction_id: str = getenv("PADDLE_FULL_ADJUSTMENT_TRANSACTION_ID", None) |
| 28 | + |
| 29 | +# Determine the environment, defaulting to sandbox |
| 30 | +environment = Environment(getenv("PADDLE_ENVIRONMENT", "sandbox")) |
| 31 | + |
| 32 | +# Initialize the Paddle client |
| 33 | +paddle = Client(api_key, options=Options(environment), logger=log) |
| 34 | + |
| 35 | + |
| 36 | +# ┌─── |
| 37 | +# │ Create Partial Adjustment │ |
| 38 | +# └───────────────────────────┘ |
| 39 | +try: |
| 40 | + partial_adjustment = paddle.adjustments.create( |
| 41 | + CreateAdjustment.partial( |
| 42 | + Action.Refund, |
| 43 | + [CreateAdjustmentItem(transaction_item_id, AdjustmentType.Partial, "100")], |
| 44 | + "error", |
| 45 | + transaction_id, |
| 46 | + ) |
| 47 | + ) |
| 48 | +except ApiError as error: |
| 49 | + print(error) |
| 50 | + exit(1) |
| 51 | + |
| 52 | +print(f"Partial Adjustment '{partial_adjustment.id}'") |
| 53 | + |
| 54 | +# ┌─── |
| 55 | +# │ Create Full Adjustment │ |
| 56 | +# └────────────────────────┘ |
| 57 | +try: |
| 58 | + full_adjustment = paddle.adjustments.create( |
| 59 | + CreateAdjustment.full( |
| 60 | + Action.Refund, |
| 61 | + "error", |
| 62 | + full_adjustment_transaction_id, |
| 63 | + ) |
| 64 | + ) |
| 65 | +except ApiError as error: |
| 66 | + print(error) |
| 67 | + exit(1) |
| 68 | + |
| 69 | +print(f"Full Adjustment '{full_adjustment.id}'") |
0 commit comments