dkanavis

hooks

Feb 4th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. """
  2.    Payments processor: hooks configuration.
  3.  
  4.    Hook names are (in execution order):
  5.        before_user_resolution
  6.        before_validation
  7.          -- exit unless it's a pay (not test) mode and payment is valid
  8.        before_save
  9.        after_save
  10.  
  11.    Hooks config must have a format of a dict {name: hooks} where hooks
  12.    is a single callable or a list/tuple of callables. I'ts recommended to
  13.    put hooks to the same directory as hooks config and to absolutely import
  14.    them from the payments module, for example:
  15.        ----
  16.            from payments.hooks.receipt import prepare_receipt, print_receipt
  17.            from payments.hooks.split_payment import split_payment
  18.  
  19.            hooks = {
  20.                "before_save": (prepare_receipt, split_payment)
  21.                "after_save": print_receipt
  22.            }
  23.        ----
  24.  
  25.    Each hook should be a callable with a prototype of:
  26.        callable(request: Request) -> None
  27.    - "request" is an object of payments.Request (payments/classes/Request.py)
  28.      (payments/classes/Request.py) class. It contains input data and also
  29.      may contain the following properties:
  30.          .valid (bool) - is request valid
  31.          .user (:obj:User) - payments.User object (payments/classes/User.py)
  32.          .incomes (:obj:IncomeCollection) - payments.IncomeCollection object
  33.  
  34.    Inside hooks you may use:
  35.        - Core object: get it with
  36.            ----
  37.                from payments import get_core
  38.                core = get_core()
  39.            ----
  40.            It contains a logger "core.out" (payments.core.logger.Logger class)
  41.            Also it contains a .db(name) function to return database connection
  42.        - Models: payments/models. They use sqlalchemy framework.
  43.        - Basic classes: payments/classes. They may be needed to set .user or
  44.            .incomes field
  45.  
  46.    Hooks:
  47.    - before_user_resolution - happens in the first place right after the
  48.    request object is created by an entrypoint. It may set .user property.
  49.    If it does, internal user resolution procedure is ommited.
  50.  
  51.    - before_validation - happens after .user is resolved one way or another
  52.    and before payment is validated*. You may set .valid to True or False
  53.    here to skip internal validation. If .valid is False, processor will exit.
  54.    Also you are highly encouraged to set .reason string field for log.
  55.    !!! If .valid is True, you are OBLIGED TO set .incomes field yourself!
  56.  
  57.    * Validation process's point is to resolve whether the payment is able to
  58.    enroll (e.g. cash payment for non-cash users may be invalid).
  59.  
  60.    - before_save - happens after payment is validated and right before the
  61.    database saving process. From here, .incomes field will be set.
  62.    Here you can manipulate incomes and their amounts before they are saved.
  63.  
  64.    - after_save - happens after payment is saved to database and right
  65.    before the processor exists. Here you have a guarantee of enrollment.
  66. """
  67.  
  68. # Hooks config
  69. hooks = {
  70.     # Hooks here
  71. }
Advertisement
Add Comment
Please, Sign In to add comment