Account_payment_stripe in combination with account_payment_clearing

Hi Guys!,
I’m experiencing an issue when using the account_payment_stripe in combination with account_payment_clearing. The point is that when a webhook call comes from stripe for a payment_intent_succeeded event the system is not able to assign a number to the account move, generating the following traceback:

Thu Jul 29 15:18:49 2021] ERROR:trytond.protocols.wrappers:<JSONRequest (invalid WSGI environ)>
Traceback (most recent call last):
  File "/usr/local/tryton-env-6.0/trytond/trytond/model/modelsql.py", line 614, in create
    cursor.execute(*table.insert(insert_columns,
  File "/usr/local/tryton-env-6.0/trytond/trytond/backend/postgresql/database.py", line 72, in execute
    cursor.execute(self, sql, args)
psycopg2.errors.NotNullViolation: null value in column "number" violates not-null constraint
DETAIL:  Failing row contains (207, 1, 2021-07-29 15:18:48.963785, 0, 2021-07-29, null, 8, null, account.payment,10, 19, null, null, d
raft, null, null).


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/tryton-env-6.0/trytond/trytond/protocols/wrappers.py", line 188, in wrapper
    result = func(request, pool, *args, **kwargs)
  File "/usr/local/tryton-env-6.0/trytond/trytond/modules/account_payment_stripe/routes.py", line 79, in webhooks_endpoint
    result = account.webhook(payload)
  File "/usr/local/tryton-env-6.0/trytond/trytond/modules/account_payment_stripe/payment.py", line 1060, in webhook
    return self.webhook_payment_intent_succeeded(data)
  File "/usr/local/tryton-env-6.0/trytond/trytond/modules/account_payment_stripe/payment.py", line 1281, in webhook_payment_intent_suc
ceeded
    Payment.succeed([payment])
  File "/usr/local/tryton-env-6.0/trytond/trytond/model/modelview.py", line 774, in wrapper
    return func(cls, records, *args, **kwargs)
  File "/usr/local/tryton-env-6.0/trytond/trytond/model/workflow.py", line 37, in wrapper
    result = func(cls, filtered, *args, **kwargs)
  File "/usr/local/tryton-env-6.0/trytond/trytond/modules/account_payment_clearing/payment.py", line 161, in succeed
    Move.save(moves)
  File "/usr/local/tryton-env-6.0/trytond/trytond/model/descriptors.py", line 33, in newfunc
    return self.func(owner, *args, **kwargs)
  File "/usr/local/tryton-env-6.0/trytond/trytond/model/modelstorage.py", line 1771, in save
    news = cls.create([save_values[r] for r in to_create])
  File "/usr/local/tryton-env-6.0/trytond/trytond/modules/account/move.py", line 286, in create
    moves = super(Move, cls).create(vlist)
  File "/usr/local/tryton-env-6.0/trytond/trytond/model/modelsql.py", line 159, in wrapper
    return func(cls, *args, **kwargs)
  File "/usr/local/tryton-env-6.0/trytond/trytond/model/modelsql.py", line 634, in create
    cls.__raise_integrity_error(
  File "/usr/local/tryton-env-6.0/trytond/trytond/model/modelsql.py", line 352, in __raise_integrity_error
    raise RequiredValidationError(
trytond.model.modelstorage.RequiredValidationError: A value is required for field "Number" in "Account Move". - 
Thu Jul 29 15:18:49 2021] INFO:werkzeug:54.187.205.235 - - [29/Jul/2021 15:18:49] "POST /innobiz/account_payment_stripe/webhook/ff87b3
412fcd452f9c168b8552fe4f35 HTTP/1.0" 400 -
Thu Jul 29 15:18:49 2021] INFO:werkzeug:76.111.197.240 - - [29/Jul/2021 15:18:49] "POST /innobiz/ HTTP/1.0" 200 -
Thu Jul 29 15:19:05 2021] ERROR:trytond.protocols.wrappers:<JSONRequest (invalid WSGI environ)>
Traceback (most recent call last):
  File "/usr/local/tryton-env-6.0/trytond/trytond/model/modelsql.py", line 614, in create
    cursor.execute(*table.insert(insert_columns,
  File "/usr/local/tryton-env-6.0/trytond/trytond/backend/postgresql/database.py", line 72, in execute
    cursor.execute(self, sql, args)
psycopg2.errors.NotNullViolation: null value in column "number" violates not-null constraint
DETAIL:  Failing row contains (208, 1, 2021-07-29 15:19:05.645049, 0, 2021-07-29, null, 8, null, account.payment,10, 19, null, null, d
raft, null, null).

I noted that when I manually succeed the payment form Sao the problem didn’t occurred… Trying to understand what was happening I realized that when the webservice call arrives there is no company in the context, so in the ‘account.move’ create method the operation fails given that system is unable to find the right sequence for the 'account.move"… (that doesn’t happen in the sao environment because the user context has the company attribute).

I was trying to solve the issue updating the context in the stripe webservice module (that seems to be the right place to fixit) but I had no success with this approach (I suspect that the transaction changes from this point to the moment when the move is created, though I was unable to identify where this happens if it happens at all)… anyway, the the workaround that I found was to modify the ‘account.move’ create method (in the modules/account/move.py file) as follows:

    @classmethod
    def create(cls, vlist):
        pool = Pool()
        Journal = pool.get('account.journal')

        vlist = [x.copy() for x in vlist]
        for vals in vlist:
            if not vals.get('number'):
                journal_id = (vals.get('journal')
                        or Transaction().context.get('journal'))
                if journal_id:
                    if not Transaction().context.get('company'):
                        Transaction().context['company'] = vals['company']
                    journal = Journal(journal_id)
                    if journal.sequence:
                        vals['number'] = journal.sequence.get()

        moves = super(Move, cls).create(vlist)
        cls.validate_move(moves)
        return moves

however I’m quite convinced that the right place to fix the issue is in the account_payment_stripe module instead of the account module…

any suggestions??

BR,

You are right the problem is getting the proper sequence from the journal.
But you should never modify the context directly but use Transaction.set_context() as context manager.
Anyway as Journal.sequence is a MultiValue field, it should be retrieved with the get_multivalue like it is done for example in Shipment.create.

Could you fill a bug report following Tryton - How to Develop?

I filled Issue 10634: Move numbering from journal sequence does not use the right company - Tryton issue tracker