tempest_fastapi_sdk.integrations.payment.routing¶
Rodar dois provedores de Pix lado a lado durante a migração de um para o outro.
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 |
candidate_percent |
int
|
Share of new charges, 0 to 100, that go to
:attr: |
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
|
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 |
0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If a mapping key differs from its provider's
|
bucket
staticmethod
¶
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 |
int
|
every process and every run. SHA-256 rather than |
|
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: |
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 |
PixCharge
|
its |
|
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: |
required |
charge_id
|
str
|
The stored :attr: |
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: |
required |
charge_id
|
str
|
The stored :attr: |
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: |
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
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: |
required |
charge_id
|
str
|
The stored :attr: |
required |
amount_cents
|
int | None
|
How much to refund; |
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
¶
Answer how much of a stored charge has been refunded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name
|
str
|
The stored :attr: |
required |
charge_id
|
str
|
The stored :attr: |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The total, as :meth: |
int
|
counts it. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no provider with that name is registered. |
TypeError
|
If that provider cannot refund. |