django_program.registration.stripe_utils¶
Currency conversion helpers for Stripe API integration and key obfuscation for logging.
Stripe represents monetary amounts as integers in the smallest currency unit (e.g. cents for USD). Most currencies are “normal-decimal” where 1 unit = 100 smallest units, but a subset of currencies are “zero-decimal” where the integer amount is the unit amount.
This module provides bidirectional conversion between Decimal values
used in Django models and the integer representation expected by the Stripe API, as well
as a helper to safely obfuscate API keys for log output.
Functions
|
Convert a Decimal amount to the integer representation expected by the Stripe API. |
|
Convert an integer amount from the Stripe API back to a Decimal for database storage. |
|
Obfuscate an API key so it can be safely written to logs. |
- django_program.registration.stripe_utils.convert_amount_for_api(amount, currency)[source]¶
Convert a Decimal amount to the integer representation expected by the Stripe API.
For most currencies the smallest unit is 1/100 of the standard unit (e.g. cents for USD), so
Decimal("10.00")becomes1000. Zero-decimal currencies such as JPY are returned asint(amount)directly because one unit already is the smallest unit.
- django_program.registration.stripe_utils.convert_amount_for_db(amount, currency)[source]¶
Convert an integer amount from the Stripe API back to a Decimal for database storage.
This is the inverse of
convert_amount_for_api(). For normal-decimal currencies the integer is divided by 100 (e.g.1000becomesDecimal("10.00")). For zero-decimal currencies the integer is returned as-is wrapped in a Decimal.