Pular para conteúdo

Criando um cupom de desconto

Com o SDK da AbacatePay, você pode criar cupons de desconto e listá-los com facilidade.

from abacatepay import AbacatePay

client = AbacatePay("<sua chave de API>")

cupom = client.coupons.create(
    code="DEV10",  # (1)
    discount_kind="PERCENTAGE",  # (2)
    discount=10,  # (3)
    notes="Desconto exclusivo para devs",  # (4)
    max_redeems=100  # (5)
    metadata={"campanha": "abril"}  # (6)
)

print(cupom.code)
# > DEV10
  1. Código único do cupom (ex: "BLACKFRIDAY").
  2. Pode ser "PERCENTAGE" (percentual) ou "FIXED" (valor fixo em centavos).
  3. Valor do desconto (em % ou centavos, dependendo do tipo).
  4. Descrição interna (opcional).
  5. Limite de usos (opcional, -1 = ilimitado).
  6. Dados extras (opcionais).
O objeto Coupon

Bases: BaseModel

Represents a coupon model for retrieval.

Attributes:

Name Type Description
id str

The unique identifier for the coupon.

discount_kind str

The type of discount (e.g., percentage or fixed amount).

discount int

The value of the discount.

status str

The current status of the coupon (e.g., ACTIVE, INACTIVE).

notes Optional[str]

A description or note about the coupon.

max_redeems int

The maximum number of times the coupon can be redeemed.

redeems_count int

The number of times the coupon has been redeemed.

created_at str

The timestamp when the coupon was created.

updated_at str

The timestamp when the coupon was last updated.

Source code in abacatepay/coupons/models.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Coupon(BaseModel):
    """
    Represents a coupon model for retrieval.

    Attributes:
        id (str): The unique identifier for the coupon.
        discount_kind (str): The type of discount (e.g., percentage or fixed amount).
        discount (int): The value of the discount.
        status (str): The current status of the coupon (e.g., ACTIVE, INACTIVE).
        notes (Optional[str]): A description or note about the coupon.
        max_redeems (int): The maximum number of times the coupon can be redeemed.
        redeems_count (int): The number of times the coupon has been redeemed.
        created_at (str): The timestamp when the coupon was created.
        updated_at (str): The timestamp when the coupon was last updated.
    """
    id: str
    discount_kind: str = Field(..., alias='discountKind', description="Type of discount (e.g., percentage or fixed amount)")
    discount: int
    status: str
    notes: Optional[str] = Field(None, validation='notes', description="Coupon's description")
    max_redeems: int = Field(-1, validation_alias='maxRedeems', description="Maximum number of times the coupon can be redeemed.")
    redeems_count: int = Field(0, validation_alias='redeemsCount', description="Number of times the coupon has been redeemed.")
    created_at: str = Field(..., validation_alias='createdAt', description="Timestamp when the coupon was created.")
    updated_at: str = Field(..., validation_alias='updatedAt', description="Timestamp when the coupon was last updated.")
    dev_mode: bool = Field(..., validation_alias='devMode', description="Indicates if the coupon is in development mode.")

Você pode buscar todos os cupons criados com:

cupons = client.coupons.list()

for cupom in cupons:
    print(cupom.code, cupom.discount_kind, cupom.discount)

Se não houver nenhum cupom, o SDK retorna uma lista vazia.


Agora você já pode distribuir cupons promocionais no seu sistema usando a AbacatePay 🎟️🚀