Api reference
components.payment_method.public.api ¶
Entry point of the public API.
NotificationDispatchResult
dataclass
¶
Result of dispatching billing notification events.
Attributes:
| Name | Type | Description |
|---|---|---|
entities_found |
int
|
Number of entities found |
notifications_dispatched |
int
|
Number of notifications dispatched |
users_notified |
int
|
Number of users notified |
empty
staticmethod
¶
Return an empty dispatch result.
PaymentMethodService ¶
A service class to encapsulate the public API of the payment method component.
attach_debit_mandate_to_sepa_direct_debit_payment_method
staticmethod
¶
attach_debit_mandate_to_sepa_direct_debit_payment_method(
*,
payment_method_id,
mandate_info,
session=None,
commit=True
)
Attaches a SEPA mandate to an existing payment method.
When to Use
Use this function when you want to orchestrate the creation of the payment method
from outside PaymentMethodService and use the PaymentMethodService exclusively for
storage of the result.
Example use cases: - UCE creating an unsigned payment method from Marmot and attaching a mandate later via the Proposal Builder - Optimistic flow: Client confirms signature, create mandate immediately before signature provider calback
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payment_method_id
|
UUID
|
UUID of the payment method to attach the mandate to |
required |
mandate_info
|
_ClientMandateInfo | _ProviderMandateInfo
|
Dictionary containing mandate information. Must be one of: For client signature (_ClientMandateInfo):
For provider signature (_ProviderMandateInfo):
|
required |
session
|
Session | None
|
Optional database session to use |
None
|
commit
|
bool
|
Whether or not to commit. |
True
|
Returns:
| Type | Description |
|---|---|
tuple[BillingCustomer, SepaDirectDebitPaymentMethod]
|
The billing customer and the payment method with attached mandate. |
Raises:
| Type | Description |
|---|---|
`BaseErrorCode.missing_resource`
|
If the payment method does not exist |
Examples:
# Standard flow - provider signature
customer, payment_method = (
PaymentMethodService.attach_debit_mandate_to_sepa_direct_debit_payment_method(
payment_method_id=pm.id,
mandate_info={
"uri": "s3://alan-documents/mandates/signed-mandate-789012.pdf",
"unique_reference": "MANDATE789012",
"signer": Signer(
email="john@example.com",
first_name="John",
last_name="Doe"
)
}
)
)
# Optimistic flow - client signature
customer, payment_method = (
PaymentMethodService.attach_debit_mandate_to_sepa_direct_debit_payment_method(
payment_method_id=pm.id,
mandate_info={
"unique_reference": "MANDATE789012",
"signed_at_from_client": datetime.utcnow(),
}
)
)
Source code in components/payment_method/public/api.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 | |
create_sepa_direct_debit_payment_method
staticmethod
¶
create_sepa_direct_debit_payment_method(
*,
profile_id,
iban,
display_name=None,
force_create_new_payment_method=False,
session=None,
commit=True
)
Creates a SEPA direct debit payment method and attaches it to a billing customer.
The created payment method does not have a DebitMandate attached.
When to Use
Use this function when you want to orchestrate the creation of the payment method
from outside PaymentMethodService and use the PaymentMethodService exclusively for
storage of the result.
Example use case: UCE creating an unsigned payment method from Marmot and attaching a mandate later via the Proposal Builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile_id
|
UUID
|
UUID of the profile to attach the payment method to |
required |
iban
|
str
|
IBAN of the bank account for direct debit |
required |
display_name
|
str | None
|
Optional display name for the payment method |
None
|
force_create_new_payment_method
|
bool
|
Optional flag to force the creation of a new payment method even if the iban is the same as an existing payment method, this is useful to make sure we create a new local iban model to ensure the sepa mandate reference and the Stripe payment method id are different in the new payment method |
False
|
session
|
Session | None
|
Optional database session to use |
None
|
commit
|
bool
|
Whether to commit the transaction (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
tuple[BillingCustomer, SepaDirectDebitPaymentMethod]
|
The billing customer and the newly created payment method. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the customer already has a payment method (temporary 1 PM limit) |
Examples:
customer, payment_method = (
PaymentMethodService.create_sepa_direct_debit_payment_method(
profile_id=user.profile_id,
iban="DE89370400440532013000",
)
)
Source code in components/payment_method/public/api.py
create_sepa_mandate_signature_request
staticmethod
¶
Creates a signature request on Dropbox Sign, for a SEPA mandate authorizing Marmot BE (the creditor) to withdraw money from the account described by the IBAN, belonging to the owner of the profile_id (the debtor).
The only side effect of this function is sending the request to Dropbox Sign, we expect it doesn't write to the database.
When to Use
When you want PaymentMethodService to orchestrate the creation of the payment
method from start to finish, this function is the first step in the creation process.
Complete Process Overview
The process happens live working hand-in-hand with a frontend application, on-session. It is a 6 step process:
- SEPA MANDATE GENERATION: A SEPA mandate is generated from a legally compliant template, in the preferred language of the profile's owner
- SIGNATURE REQUEST: A signature request (containing information necessary for the creation of the payment method) is sent to Dropbox Sign
- SIGNATURE URL RETURN: The signature URL is returned to the frontend client. Visiting this URL in a web browser will open a signature page on the Dropbox Sign domain. Our accounts on Dropbox Sign are configured to only accepts connections coming from alan.eu and alan.com domains
- USER SIGNATURE: The frontend client displays the signature to the user, and the user signs all fields and confirms
- WEBHOOK NOTIFICATION: Dropbox Sign sends a webhook to notify us the document is fully signed
- PAYMENT METHOD CREATION: This webhook triggers an event handler within the
PaymentMethodService: it creates the payment method and saves the signed mandate (seehandle_hellosign_callback())
Security Considerations
The signature request sent to Dropbox Sign contains both personal and sensitive information:
- IBAN of the debtor is visible on the PDF document and present in the request's metadata
- SEPA mandate's unique reference is visible on the PDF document and present in the request's metadata
- Address of the debtor is visible on the PDF document
- Name of the debtor is visible on the PDF document
- Name and Email address is present in the request's metadata
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_name
|
AppName
|
The application name for the signature request |
required |
iban
|
str
|
IBAN of the bank account for the SEPA mandate |
required |
profile_id
|
UUID
|
UUID of the profile requesting the signature |
required |
signer_information
|
SignerInformation | None
|
Optional signer information (email, address, name, language). If not provided, all information is deduced from the profile. |
None
|
Returns:
| Type | Description |
|---|---|
_SignatureRequest
|
A dictionary with the following fields:
|
Raises:
| Type | Description |
|---|---|
`PaymentMethodError.invalid_direct_debit_iban`
|
If the IBAN is not valid or not direct debit compliant |
`BaseErrorCode.missing_resource`
|
If profile does not have an email and no signer information is provided (mandatory for signature on Dropbox Sign) |
`BaseErrorCode.missing_resource`
|
If profile does not have an address and no signer information is provided (mandatory for SEPA mandate generation) |
Examples:
signature_request = (
PaymentMethodService.create_sepa_mandate_signature_request(
iban="DE89370400440532013000",
profile_id=profile_id
)
)
sign_url = signature_request["sign_url"]
request_id = signature_request["signature_request_id"]
# Once completed, we receive a webhook from _Dropbox Sign_
event = server.listen()
PaymentMethodService.handle_hellosign_callback(
event=event,
)
Source code in components/payment_method/public/api.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
dispatch_missing_sepa_mandate_signature_notifications
staticmethod
¶
Dispatch missing SEPA mandate signature notification events to Customer.io.
- Finds debit mandates missing signature:
- Client has confirmed signature (signed_at_from_client is set)
- Provider hasn't confirmed yet (signed_at is NULL)
- Threshold time has passed since client signature
- Payment method is still un-chargeable (no other mandate was signed)
- No notification event was previously sent
- Send 'missing_sepa_mandate_signature' notification event to Customer.io for profile users
- Log the notification in BillingNotificationLog to prevent duplicates
Source code in components/payment_method/public/api.py
get_billing_customer
staticmethod
¶
get_billing_customer(
*,
billing_customer_id: UUID,
exclusively_use_global_repository: bool = False,
raise_if_missing: Literal[True],
session: Session | None = None
) -> BillingCustomer
get_billing_customer(
*,
billing_customer_id: UUID,
exclusively_use_global_repository: bool = False,
raise_if_missing: Literal[False],
session: Session | None = None
) -> BillingCustomer | None
get_billing_customer(
*,
billing_customer_id: UUID,
exclusively_use_global_repository: bool = False,
session: Session | None = None
) -> BillingCustomer | None
get_billing_customer(
*,
profile_id: UUID,
exclusively_use_global_repository: bool = False,
raise_if_missing: Literal[True],
session: Session | None = None
) -> BillingCustomer
get_billing_customer(
*,
profile_id: UUID,
exclusively_use_global_repository: bool = False,
raise_if_missing: Literal[False],
session: Session | None = None
) -> BillingCustomer | None
get_billing_customer(
*,
profile_id: UUID,
exclusively_use_global_repository: bool = False,
session: Session | None = None
) -> BillingCustomer | None
get_billing_customer(
*,
payment_method_id: UUID,
exclusively_use_global_repository: bool = False,
raise_if_missing: Literal[True],
session: Session | None = None
) -> BillingCustomer
get_billing_customer(
*,
billing_customer_id=None,
profile_id=None,
payment_method_id=None,
exclusively_use_global_repository=False,
raise_if_missing=False,
session=None
)
Return a billing customer representing someone paying for Alan services.
This method accepts exactly one of billing_customer_id, profile_id, or payment_method_id.
Its return type depends on the raise_if_missing parameter:
- If
raise_if_missingis specified, it returnsBillingCustomeror raises if it does not exist - Otherwise, it returns
BillingCustomer | None
This flexibility is reflected in the type system using overload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
billing_customer_id
|
UUID | None
|
Optional UUID of the billing customer to retrieve |
None
|
profile_id
|
UUID | None
|
Optional UUID of the profile associated with the billing customer to retrieve |
None
|
payment_method_id
|
UUID | None
|
Optional UUID of a payment method associated with the billing customer to retrieve |
None
|
exclusively_use_global_repository
|
bool
|
Optional boolean indicating whether to use global repository |
False
|
raise_if_missing
|
bool
|
If True, raises an exception when the customer is not found |
False
|
session
|
Session | None
|
Optional database session to use for the query |
None
|
Returns:
| Type | Description |
|---|---|
BillingCustomer | None
|
The billing customer if found, None if not found (unless |
Raises:
| Type | Description |
|---|---|
`BaseErrorCode.missing_resource`
|
If the customer does not exist and |
Examples:
# Returns BillingCustomer | None
customer = PaymentMethodService.get_billing_customer(
billing_customer_id=customer_id
)
# Returns BillingCustomer | None
customer = PaymentMethodService.get_billing_customer(
profile_id=user.profile_id
)
# Returns BillingCustomer or raises
customer = PaymentMethodService.get_billing_customer(
payment_method_id=pm.id,
raise_if_missing=True
)
Source code in components/payment_method/public/api.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
handle_hellosign_callback
staticmethod
¶
Completes the creation of a SEPA direct debit payment method from a Dropbox Sign signature_request_all_signed event.
Process Steps
This function executes the following steps, in order:
- Retrieves payment method creation data from the event
- Downloads the signed document from Dropbox Sign
- Stores the signed mandate document in S3
- Creates the
SepaDirectDebitPaymentMethod - Creates a signed
DebitMandateand attaches it to the payment method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
HellosignEvent
|
Must be of type |
required |
session
|
Session | None
|
Database session to use, defaults to current session |
None
|
Returns:
| Type | Description |
|---|---|
tuple[BillingCustomer, SepaDirectDebitPaymentMethod]
|
The billing customer and the newly created payment method. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the request leading to the event does not originate from |
`BaseErrorCode.missing_resource`
|
If required metadata is missing |
Examples:
if (
event.metadata.get("origin_component") == "PaymentMethodService"
and event.signature_type == SignatureType.sepa_mandate
and event.event_type == (
HellosignEventType.signature_request_all_signed
)
):
customer, payment_method = (
PaymentMethodService.handle_hellosign_callback(
event=event,
)
)
Source code in components/payment_method/public/api.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
set_preferred_payment_method
staticmethod
¶
Set the preferred payment method for a specific payment method.
Source code in components/payment_method/public/api.py
set_stripe_customer_id
staticmethod
¶
Set the Stripe customer ID of a specific billing customer.
Planned Deprecation
The only purpose for this function's existence is helping us migrate
countries from existing local models to global modals.
Ideally, the entire creation of the customer and the payment method should
be encapsulated within the PaymentMethodService and such crude set
operations should be forbidden because they would breach the ownership of
the component.
This function will not be part of the final API of the component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile_id
|
UUID
|
UUID of the profile to set the Stripe customer ID for |
required |
stripe_customer_id
|
str
|
The Stripe customer ID to attach |
required |
session
|
Session | None
|
Optional database session to use |
None
|
commit
|
bool
|
Whether to commit the transaction (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
BillingCustomer
|
The billing customer with the attached Stripe customer ID. |
Raises:
| Type | Description |
|---|---|
`BaseErrorCode.missing_resource`
|
If the billing customer does not exist for the |
Examples:
customer = PaymentMethodService.set_stripe_customer_id(
profile_id=user.profile_id,
stripe_customer_id="cus_1234567890",
)
Source code in components/payment_method/public/api.py
set_stripe_payment_method_id
staticmethod
¶
set_stripe_payment_method_id(
*,
payment_method_id,
stripe_payment_method_id,
session=None,
commit=True
)
Set the Stripe payment method ID of a specific payment method.
Planned Deprecation
The only purpose for this function's existence is helping us migrate
countries from existing local models to global modals.
Ideally, the entire creation of the customer and the payment method should
be encapsulated within the PaymentMethodService and such crude set
operations should be forbidden because they would breach the ownership of
the component.
This function will not be part of the final API of the component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payment_method_id
|
UUID
|
UUID of the payment method to set the Stripe ID for |
required |
stripe_payment_method_id
|
str
|
The Stripe payment method ID to attach |
required |
session
|
Session | None
|
Optional database session to use |
None
|
commit
|
bool
|
Whether to commit the transaction (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
tuple[BillingCustomer, SepaDirectDebitPaymentMethod]
|
Tuple of (billing customer, updated payment method) with Stripe payment method ID set. |
Raises:
| Type | Description |
|---|---|
`BaseErrorCode.missing_resource`
|
If the billing customer does not exist for the |
Examples:
customer, payment_method = PaymentMethodService.set_stripe_payment_method_id(
payment_method_id=some_uuid,
stripe_payment_method_id="pm_1234567890"
)
Source code in components/payment_method/public/api.py
components.payment_method.public.commands ¶
This component does not have any commands.
components.payment_method.public.dependencies ¶
COMPONENT_NAME
module-attribute
¶
Canonical name of the payment method component.
PaymentMethodDependency ¶
Bases: ABC
Dependencies injected into the payment method component. Every component depending on the payment method component is responsible for injecting its own implementation of these dependencies.
get_billing_customer_repository ¶
Returns the BillingCustomerRepository to use for the given session.
Source code in components/payment_method/public/dependencies.py
get_suggested_iban
abstractmethod
¶
The suggested IBAN is used to pre-fill forms on frontends when creating a SepaDirectDebitPaymentMethod. In practice, we expect each country to inject a function that returns the settlement IBAN of the profile's owner.
Source code in components/payment_method/public/dependencies.py
get_app_dependency ¶
Function used to fetch the dependencies from the flask app.
Source code in components/payment_method/public/dependencies.py
set_app_dependency ¶
Function used to actually inject the dependency class in the component.
Source code in components/payment_method/public/dependencies.py
components.payment_method.public.entities ¶
BillingCustomer
dataclass
¶
Bases: DataClassJsonMixin
A member or company paying for Alan services.
Temporary Single Payment Method Limitation
Currently, a BillingCustomer may only have one payment method, which is
implicitly the active payment method. This limitation is temporary and
multiple payment methods will be allowed in the future.
payment_cards
instance-attribute
¶
List of payment card methods owned by this customer.
Not Implemented
Payment cards are not yet implemented. This field exists for future development and testing polymorphism of payment methods. Will be empty in current implementation.
profile_id
instance-attribute
¶
Profile ID of the member corresponding to the BillingCustomer
sepa_debit_payment_methods
instance-attribute
¶
List of SEPA direct debit payment methods owned by this customer.
Each payment method represents a bank account from which Alan can automatically withdraw payments via the SEPA direct debit system.
stripe_customer_id
instance-attribute
¶
ID of the customer on Stripe.
Planned Deprecation
This property is exposed temporarily during the migration from local to global models.
Ideally, all operations requiring this ID should be encapsulated within the
PaymentMethodService or billing related services rather than exposing internal Stripe identifiers.
This field will be removed from the public API once the migration is complete.
BillingPaymentMethod
dataclass
¶
Base class representing a payment method used by a BillingCustomer to pay for Alan services.
display_name
instance-attribute
¶
A name chosen by the owner of the payment method, used for in-app display and communications.
is_chargeable
instance-attribute
¶
Indicates whether this payment method can be charged, with reason if not.
Examples:
method_type
instance-attribute
¶
The type of payment method: card, SEPA direct debit, etc ...
stripe_payment_method_id
instance-attribute
¶
ID of the payment method on Stripe.
Planned Deprecation
This property is exposed temporarily during the migration from local to global models.
Ideally, all operations requiring this ID should be encapsulated within the
PaymentMethodService rather than exposing internal Stripe identifiers.
This field will be removed from the public API once the migration is complete.
DebitMandate
dataclass
¶
DebitMandate(
id,
is_active,
unique_reference,
signed_at=optional_isodatetime_field(),
signed_at_from_client=optional_isodatetime_field(),
)
A document representing an authorisation to debit from a bank account.
A debit mandate is attached to SepaDirectDebitPaymentMethod.
The creditor is implicitely Marmot BE.
The debtor is implicitely the BillingCustomer owning the DebitMandate.
is_active
instance-attribute
¶
If is_active is False, the attached SepaDirectDebitPaymentMethod cannot be used
for charging.
signed_at
class-attribute
instance-attribute
¶
The date and time the mandate was signed, provided by the document signature service.
signed_at_from_client
class-attribute
instance-attribute
¶
The date and time the mandate was signed, provided by the client.
unique_reference
instance-attribute
¶
Unique reference of the mandate. This is a reference written in the debit mandate document and is used by all parties involved to identify it (Alan, PSP, Bank).
PaymentCardPaymentMethod
dataclass
¶
Bases: BillingPaymentMethod
Payment card payment method for credit/debit card transactions.
Not Implemented
This payment method type is not yet implemented. It exists only for testing polymorphism of payment methods and future development. Do not use in production code.
PaymentMethodType ¶
Bases: AlanBaseEnum
All variants of payment methods supported by PaymentMethodService.
from_payment_method_enum
classmethod
¶
Convert a PaymentMethod enum value (from core billing) to the corresponding
PaymentMethodType enum value, compatible with PaymentMethodService.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payment_method
|
PaymentMethod
|
The core billing |
required |
Returns:
| Type | Description |
|---|---|
PaymentMethodType
|
The corresponding |
Raises:
| Type | Description |
|---|---|
`BaseErrorCode.model_validation`
|
If the payment method is not supported by |
Examples:
pm_type = PaymentMethodType.from_payment_method_enum(
PaymentMethod.sepa_debit
)
print(pm_type) # PaymentMethodType.SEPA_DIRECT_DEBIT
pm_type = PaymentMethodType.from_payment_method_enum(
PaymentMethod.payment_card
)
print(pm_type) # PaymentMethodType.PAYMENT_CARD
Source code in components/payment_method/public/entities.py
to_payment_method_enum ¶
Convert a PaymentMethodType enum value to the corresponding
PaymentMethod enum value (for core billing).
Returns:
| Type | Description |
|---|---|
PaymentMethod
|
The equivalent core billing |
Raises:
| Type | Description |
|---|---|
`ValueError`
|
If this payment method type cannot be mapped |
Examples:
pm_enum = PaymentMethodType.SEPA_DIRECT_DEBIT.to_payment_method_enum()
print(pm_enum) # PaymentMethod.sepa_debit
pm_enum = PaymentMethodType.PAYMENT_CARD.to_payment_method_enum()
print(pm_enum) # PaymentMethod.payment_card
Source code in components/payment_method/public/entities.py
SepaDirectDebitPaymentMethod
dataclass
¶
SepaDirectDebitPaymentMethod(
id,
method_type,
display_name,
is_chargeable,
stripe_payment_method_id,
iban,
debit_mandates,
)
Bases: BillingPaymentMethod
SEPA direct debit payment method for automated bank account withdrawals.
This payment method allows Alan to automatically withdraw payments from a customer's bank account using the SEPA direct debit system. Requires signed mandates to authorize debits from the specified IBAN.
debit_mandates
instance-attribute
¶
List of signed mandates authorizing debits from the IBAN.
Each mandate, if signed, represents a legal authorization for Marmot BE (as creditor) to withdraw money from the debtor's bank account (owner of the payment method). Multiple mandates may exist, covering different purposes or simply because we accidentally sent too many documents to sign.
iban
instance-attribute
¶
International Bank Account Number for the direct debit withdrawals.
Security Considerations
This contains sensitive financial information that must be handled with care. Do not transmit nor log this data carelessly. Ensure proper encryption and access controls when processing IBAN data.
obfuscated_iban
property
¶
Return the IBAN in obfuscated format for display purposes.
Returns:
| Type | Description |
|---|---|
str
|
IBAN with middle characters replaced by bullets, showing only first 5 |
str
|
and last 3 characters (e.g., "FR123 •••• •••• •••• •••• 456") |
UnchargeableReason ¶
Bases: AlanBaseEnum
Reasons why a payment method might not be chargeable.