Calculate surcharge information with GET /surcharge/review using a runnable Python example, payment method ID guidance, response handling, and compliance support notes.
Calculate a surcharge (an additional fee for using a payment method) with GET /surcharge/review so you can display the fee before your customer completes a payment.
This API reference is for developers integrating surcharge calculations into an application. The request reviews surcharge information; it does not charge the payment method.
Calculate and display a surcharge
- Contact Stax to enable surcharging for your merchant account if it is not already enabled.
- Run Python 3 on your server or local development machine. The example uses only the Python standard library and prompts for your merchant API key without displaying it. Keep the key out of browser code and source control.
- Obtain the payment method's
idfrom its creation response or thedata[].idfield returned byGET /payment-method. For example,7e3a83ea-6770-41f7-9b28-7ea1a3ae33e7illustrates the ID format; use an ID returned for your own merchant account, not this example value. This ID comes from an API response, not a Dashboard navigation step. - Save the code below as
review_surcharge.pyand runpython3 review_surcharge.py. Enter your merchant API key and payment method ID when prompted. The example reviews a subtotal of $12.00 using the production API athttps://apiprod.fattlabs.com/and a 30-second timeout. - Display the returned
surcharge_amountandtotal_with_surcharge_amountto your customer before completing the payment. Do not treat an error or missing surcharge information as confirmation that a surcharge applies.
The endpoint accepts these query parameters:
| Parameter | Type | Requirement and value |
|---|---|---|
payment_method_id | string | Required. Use the saved payment method's id for the merchant associated with your API key. No default is documented. |
total | string | Optional in the specification; explicitly send the transaction subtotal for this calculation. The example uses "12.00", excluding any surcharge. No default or numeric limits are documented. |
import getpass
import json
import math
import sys
from urllib.error import HTTPError, URLError
from urllib.parse import urlencode
from urllib.request import Request, urlopen
def main():
api_key = getpass.getpass("Merchant API key: ").strip()
# Use id from the creation response or data[].id from GET /payment-method.
# Example format: "7e3a83ea-6770-41f7-9b28-7ea1a3ae33e7"; use your own ID.
payment_method_id = input("Payment method ID: ").strip()
if not api_key or not payment_method_id:
print("Enter both your merchant API key and payment method ID.", file=sys.stderr)
return 1
query = urlencode({
"payment_method_id": payment_method_id,
"total": "12.00", # Exclude the surcharge to avoid counting it twice.
})
request = Request(
"https://apiprod.fattlabs.com/surcharge/review?" + query,
headers={"Authorization": "Bearer " + api_key, "Accept": "application/json"},
method="GET",
)
try:
with urlopen(request, timeout=30) as response:
raw = response.read().decode("utf-8")
except HTTPError as error:
guidance = {
400: "Check the payment method ID and subtotal.",
401: "Check your merchant API key.",
404: "Check that the payment method exists for this merchant.",
500: "Retry later; do not display an unconfirmed surcharge.",
}
print(f"HTTP {error.code}: " + guidance.get(
error.code, "Inspect the error response before retrying."
), file=sys.stderr)
print(error.read().decode("utf-8", errors="replace"), file=sys.stderr)
return 1
except (URLError, TimeoutError, OSError, UnicodeError) as error:
print(f"Could not retrieve surcharge information: {error}", file=sys.stderr)
return 1
if not raw.strip():
print("No surcharge information returned; do not add a surcharge.")
return 0
try:
result = json.loads(raw)
except ValueError:
print("The API returned unreadable JSON; do not add a surcharge.", file=sys.stderr)
return 1
if result is None or result == {} or result == []:
print("No surcharge information returned; do not add a surcharge.")
return 0
if not isinstance(result, dict) or not isinstance(result.get("bin_type"), str):
print("Card type is unavailable; do not add a surcharge.", file=sys.stderr)
return 1
if result["bin_type"] != "CREDIT":
print("The payment method is not identified as CREDIT; do not add a surcharge.")
return 0
fields = ("surcharge_rate", "surcharge_amount", "total_with_surcharge_amount")
if any(type(result.get(field)) not in (int, float)
or not math.isfinite(result[field]) or result[field] < 0
for field in fields):
print("Surcharge values are missing or invalid; do not add a surcharge.",
file=sys.stderr)
return 1
print(json.dumps({"bin_type": result["bin_type"],
**{field: result[field] for field in fields}}, indent=2))
if result["surcharge_amount"] == 0:
print("The returned surcharge is zero; do not add a surcharge.")
return 0
if __name__ == "__main__":
sys.exit(main())For the specification's example rate of 1.5% on a $12.00 subtotal, a successful HTTP 200 response produces:
{
"bin_type": "CREDIT",
"surcharge_rate": 1.5,
"surcharge_amount": 0.18,
"total_with_surcharge_amount": 12.18
}Your rate and amounts depend on your merchant configuration and payment method. Debit cards return no surcharge information; the script handles empty responses and non-credit card types without adding a surcharge. If you later submit a payment, send the original subtotal, not the total including the surcharge, to avoid counting the fee twice.
Optional: Review endpoint capabilities
Review the surcharge rate, fee, and total for a credit card transaction.
Review surcharge information for your merchant account after Stax enables surcharging.
Use the calculated fee to support your compliance efforts through customer disclosure. Consult your legal team regarding specific requirements.
Optional: Review usage and troubleshooting guidance
Usage Notes
Surcharging Enabled: Contact Stax to enable surcharging before using this calculation for your merchant account.
Credit Cards Only: Apply surcharges only to eligible credit card transactions, not debit or prepaid cards. If the card type is unavailable, do not add a surcharge.
Customer Communication: Display the surcharge amount and total including the surcharge before your customer completes the transaction.
Response Codes and Troubleshooting
| Code | Response | Reason |
|---|---|---|
| 200 | Success | Read the returned surcharge information; handle missing information without adding a surcharge. |
| 400 | Bad Request | Check the payment method ID and transaction subtotal. |
| 401 | Unauthorized | Check your merchant API key. |
| 404 | Not Found | Use an existing payment method ID for the merchant associated with your API key. |
| 500 | Internal Server Error | Retry later; do not display an unconfirmed surcharge. |
Important Considerations
Compliance Support: Use surcharge calculations and fee disclosure to support your compliance efforts. These features do not establish compliance; consult your legal team regarding local laws and card network requirements.
Fee Disclosure: Show customers the surcharge amount and total before completing their payment.
Non-Surcharged Methods: Do not add a surcharge to debit or prepaid cards. If the response does not identify an eligible credit card and provide valid surcharge amounts, do not add a surcharge.
