Sets a transaction to void, ending the transaction before it's processed.
Cancel a transaction before batching (grouping transactions for processing) and settlement (transferring payment funds through the payment network). When you void a transaction, Stax creates a child transaction (a related record) with type = void and links it to the original transaction.
Void a transaction with Python
- Install Python 3. The example uses only the standard library.
- Obtain a merchant API key and confirm that your merchant team is active and enabled. Use a sandbox key first: sandbox and live keys use the same base URL, but sandbox payments do not use real money. Keep your API key out of client-side code and public repositories.
- Copy the transaction's
idfrom an existing transaction response for the same merchant. For example,"ba9bf00c-c54d-48f8-8400-902cb3aacd21"illustrates the UUID format; use your own transaction ID rather than this example value. - Save the following code as
void_transaction.pyand runpython3 void_transaction.py. The script checksis_voidablebefore asking you to confirm a full void. It sends no amount because partial voids are not supported.
import getpass
import json
import sys
from urllib.error import HTTPError, URLError
from urllib.parse import quote
from urllib.request import Request, urlopen
def request_json(url, api_key, method):
request = Request(
url,
headers={
"Authorization": "Bearer " + api_key,
"Accept": "application/json",
},
method=method,
)
try:
with urlopen(request, timeout=30) as response:
return json.load(response)
except HTTPError as error:
detail = error.read().decode("utf-8", errors="replace")
guidance = {
400: "Check the request parameters.",
401: "Check your merchant API key.",
404: "Check that the transaction ID belongs to this merchant.",
422: "Check eligibility and the API error before trying again.",
}.get(error.code, "Check the transaction status before retrying.")
sys.exit(f"HTTP {error.code}: {guidance}\n{detail}")
except (URLError, TimeoutError, OSError) as error:
sys.exit(
f"Connection failed: {error}. "
"Retrieve the transaction status before retrying a void."
)
except (ValueError, UnicodeError):
sys.exit("Invalid JSON response. Check the transaction status before retrying.")
def main():
api_key = getpass.getpass("Merchant sandbox API key: ").strip()
# Copy id from an existing transaction response for this merchant.
transaction_id = input("Transaction ID: ").strip()
if not api_key or not transaction_id:
sys.exit("Provide both an API key and a transaction ID.")
url = "https://apiprod.fattlabs.com/transaction/" + quote(
transaction_id, safe=""
)
transaction = request_json(url, api_key, "GET")
if not isinstance(transaction, dict):
sys.exit("Unexpected transaction response; no void was requested.")
if transaction.get("is_voidable") is not True:
sys.exit("Transaction is not voidable; no void was requested.")
if input("Void the entire transaction? Type VOID: ").strip() != "VOID":
sys.exit("Cancelled; no void was requested.")
result = request_json(url + "/void", api_key, "POST")
if not isinstance(result, dict):
sys.exit("Unexpected void response. Retrieve the transaction to confirm its status.")
children = result.get("child_transactions")
has_void = isinstance(children, list) and any(
isinstance(child, dict) and child.get("type") == "void"
for child in children
)
if result.get("success") is not True or result.get("is_voided") is not True or not has_void:
print(json.dumps(result, indent=2))
sys.exit("Void not confirmed. Retrieve the transaction before trying again.")
print("Void confirmed.")
print(json.dumps({
"id": result.get("id"),
"success": result.get("success"),
"is_voided": result.get("is_voided"),
"is_voidable": result.get("is_voidable"),
}, indent=2))
if __name__ == "__main__":
main()For the transaction in the documented response, successful output is:
Void confirmed.
{
"id": "ba9bf00c-c54d-48f8-8400-902cb3aacd21",
"success": true,
"is_voided": true,
"is_voidable": false
}Your transaction ID will differ. Eligibility can change between the lookup and the void request; if the API rejects the void, check the returned error and retrieve the transaction's current status before trying again.
Key Points
Timing is Critical: Void transactions before batching (grouping transactions for processing). Batching is not the same as settlement (transferring payment funds through the payment network). Transactions typically batch around 8 pm EST, but timing varies by merchant configuration, so your window to void a transaction may be very short.
Irreversible Action: Once a transaction is batched, it cannot be voided. You must issue a refund instead.
Child Transactions: The void action results in a new child transaction linked to the original transaction for tracking purposes.
Testing Environment:
In a sandbox account, transactions are not refundable until 12 am EST.
Optional: Handle Settlement Tokenization
Settlement Tokenization creates a reusable card reference after settlement (the transfer of payment funds through the payment network). Tokenization means using a reference to stored card details rather than submitting those details again. This feature lets you reuse a card presented at an integrated terminal for later payments or refunds without the original terminal.
If your merchant account has this feature enabled and the transaction is not yet available for a refund, this endpoint returns status code 422 with the message:
This transaction is not yet available to be refunded via API. You can still complete this transaction through the card present flow.
In this case, use POST /terminal/void-or-refund for the card-present flow (completing the operation with the card at a terminal).
Usage Notes
Void Eligibility: Before attempting to void a transaction, check if it is eligible by verifying it has not been batched. The is_voidable flag on the transaction object can help determine this.
No Partial Voids: Partial voids are not supported. You must void the entire transaction amount.
Error Handling: If a void request is sent after the transaction has been batched, the API will return an error indicating the transaction cannot be voided.
Contact Support: For any uncertainties regarding the void window or transaction statuses, reach out to support for assistance.
Response Structure
| Code | Response | Reason |
|---|---|---|
| 200 | Success | Returns the original transaction object with an added child transaction of type = void. |
| 400 | Bad Request | Invalid request parameters. |
| 401 | Unauthorized | Authentication failed. |
| 404 | Not Found | Transaction ID does not exist. |
| 422 | Unprocessable Entity | Transaction cannot be voided (e.g., already batched). |
| 500 | Internal Server Error | An unexpected error occurred on the server. |
Alternative Endpoint
POST /transaction/{id}/void-or-refund: This endpoint automatically determines whether to void or refund the transaction based on its status.
Here, {id} is API route notation, not a literal value to send. Replace it with the transaction's id from an existing transaction response, without braces. For example, an ID in the documented UUID format produces /transaction/ba9bf00c-c54d-48f8-8400-902cb3aacd21/void-or-refund; use an ID belonging to your merchant account.
Important Considerations
Voiding vs. Refunding
Void a transaction before batching (grouping transactions for processing). After batching, use a refund to return funds to the customer, and check is_refundable before requesting it. Settlement (the transfer of payment funds through the payment network) is separate from batching; do not use the two terms interchangeably when checking eligibility.
Batch Times
Be aware of your merchant account's batch processing times to effectively manage voids.
Terminal Transactions
For transactions processed through terminals, you may need a terminal-specific endpoint when Settlement Tokenization (creating a reusable card reference after payment funds transfer through the payment network) is enabled. Use the terminal flow described above if the API returns the card-present error.
After a successful void, the original transaction has is_voided: true and includes a child transaction with type: "void" in child_transactions.
