Ir para o conteúdo

tempest_fastapi_sdk.integrations.payment.routing

Rodar dois provedores de Pix lado a lado durante a migração de um para o outro.

« Integrações e pagamentos


PixProviderRouter

PixProviderRouter(
    providers: Mapping[str, PixProvider] | Sequence[PixProvider],
    *,
    primary: str,
    candidate: str | None = None,
    candidate_percent: int = 0,
)

Send a percentage of new charges to a candidate provider.

Not itself a :class:PixProvider: the contract's get_pix_charge takes only a charge id, and an id alone does not say which provider to ask. The router takes the stored provider name alongside it instead.

Attributes:

Name Type Description
primary str

The provider that takes every charge outside the candidate's slice.

candidate str | None

The provider being rolled out, or None.

candidate_percent int

Share of new charges, 0 to 100, that go to :attr:candidate.

Register the providers and the rollout.

Parameters:

Name Type Description Default
providers Mapping[str, PixProvider] | Sequence[PixProvider]

Every provider a stored charge may belong to — including one that no longer takes new charges, so its open charges can still be read and cancelled. A sequence is keyed by each provider's provider_name; a mapping must use the same keys.

required
primary str

Name of the provider that takes new charges outside the candidate's slice.

required
candidate str | None

Name of the provider being rolled out.

None
candidate_percent int

Share of new charges, 0 to 100, sent to candidate. 0 is the kill switch: nothing new goes there, and what is already there is still served.

0

Raises:

Type Description
ValueError

If a mapping key differs from its provider's provider_name, primary or candidate is not registered, or candidate_percent is outside 0..100 or non-zero without a candidate.

bucket staticmethod

bucket(reference: str) -> int

Place a reference in one of :data:BUCKETS stable slices.

Parameters:

Name Type Description Default
reference str

The service's identifier for the charge.

required

Returns:

Name Type Description
int int

A slice in 0..99, the same for the same reference on

int

every process and every run. SHA-256 rather than hash(),

int

which Python salts per process.

select

select(request: PixChargeRequest) -> PixProvider

Pick the provider that should create this charge.

Parameters:

Name Type Description Default
request PixChargeRequest

The charge about to be created.

required

Returns:

Name Type Description
PixProvider PixProvider

The candidate when the reference falls in its

PixProvider

slice, the primary otherwise. Raising the percentage only adds

PixProvider

references to the candidate's slice; a reference already there

PixProvider

stays there.

provider_for

provider_for(provider_name: str) -> PixProvider

Return the provider a stored charge belongs to.

Parameters:

Name Type Description Default
provider_name str

The :attr:PixCharge.provider the service stored when the charge was created.

required

Returns:

Name Type Description
PixProvider PixProvider

That provider.

Raises:

Type Description
KeyError

If no provider with that name is registered. A stored charge from a provider the router no longer knows cannot be read or cancelled, and saying so beats asking another provider about an id it never issued.

create_pix_charge async

create_pix_charge(request: PixChargeRequest) -> PixCharge

Create the charge at the provider :meth:select picks.

Parameters:

Name Type Description Default
request PixChargeRequest

What to charge.

required

Returns:

Name Type Description
PixCharge PixCharge

The created charge. Store its provider next to

PixCharge

its provider_charge_id: reads and cancellations are routed

PixCharge

by it.

get_pix_charge async

get_pix_charge(provider_name: str, charge_id: str) -> PixCharge

Read a stored charge from the provider that created it.

Parameters:

Name Type Description Default
provider_name str

The stored :attr:PixCharge.provider.

required
charge_id str

The stored :attr:PixCharge.provider_charge_id.

required

Returns:

Name Type Description
PixCharge PixCharge

The charge as that provider reports it.

cancel_pix_charge async

cancel_pix_charge(provider_name: str, charge_id: str) -> PixCharge

Cancel a stored charge at the provider that created it.

Parameters:

Name Type Description Default
provider_name str

The stored :attr:PixCharge.provider.

required
charge_id str

The stored :attr:PixCharge.provider_charge_id.

required

Returns:

Name Type Description
PixCharge PixCharge

The charge after cancellation.

refund_provider_for

refund_provider_for(provider_name: str) -> PixRefundProvider

Return the provider a stored charge belongs to, as a refunder.

Parameters:

Name Type Description Default
provider_name str

The stored :attr:PixCharge.provider.

required

Returns:

Name Type Description
PixRefundProvider PixRefundProvider

That provider.

Raises:

Type Description
KeyError

If no provider with that name is registered.

TypeError

If the provider has no refund_pix_charge or no get_refunded_cents. PixRefundProvider is not runtime_checkable (see :class:PixProvider), so the check is on the two names, at the call that needs them, instead of an AttributeError halfway through a refund.

refund_pix_charge async

refund_pix_charge(
    provider_name: str,
    charge_id: str,
    amount_cents: int | None = None,
    *,
    refund_reference: str,
) -> PixRefund

Refund a stored charge at the provider that created it.

Parameters:

Name Type Description Default
provider_name str

The stored :attr:PixCharge.provider.

required
charge_id str

The stored :attr:PixCharge.provider_charge_id.

required
amount_cents int | None

How much to refund; None refunds what is left.

None
refund_reference str

The refund's idempotency key.

required

Returns:

Name Type Description
PixRefund PixRefund

The refund, or the existing one for that reference.

Raises:

Type Description
KeyError

If no provider with that name is registered.

TypeError

If that provider cannot refund.

get_refunded_cents async

get_refunded_cents(provider_name: str, charge_id: str) -> int

Answer how much of a stored charge has been refunded.

Parameters:

Name Type Description Default
provider_name str

The stored :attr:PixCharge.provider.

required
charge_id str

The stored :attr:PixCharge.provider_charge_id.

required

Returns:

Name Type Description
int int

The total, as :meth:PixRefundProvider.get_refunded_cents

int

counts it.

Raises:

Type Description
KeyError

If no provider with that name is registered.

TypeError

If that provider cannot refund.