from onegov.activity import InvoiceCollection, InvoiceItem
from onegov.core.security import Private
from onegov.feriennet import FeriennetApp
from onegov.pay import Payment, PaymentProviderCollection
from onegov.org.views.payment_provider import sync_payments
from onegov.org.views.payment import refund
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from onegov.feriennet.request import FeriennetRequest
from webob import Response
@FeriennetApp.view(
model=PaymentProviderCollection,
name='sync',
permission=Private)
[docs]
def sync_payments_and_reconcile(
self: PaymentProviderCollection,
request: 'FeriennetRequest'
) -> 'Response':
result = sync_payments(self, request)
# we keep two payment paid states, one in the invoice items table and one
# in the payment (for online payments only).
#
# when syncing the payment states through the provider, we reconcile
# those two states
#
# the reason we don't keep this all in the payment table is mainly a
# performance one - we don't want to load the payments when rendering
# the potentially large list of invoice items
InvoiceCollection(self.session).sync()
return result
@FeriennetApp.view(
model=Payment,
name='refund',
request_method='POST',
permission=Private)
[docs]
def refund_and_reconcile(self: Payment, request: 'FeriennetRequest') -> None:
result = refund(self, request)
for link in self.links:
if isinstance(link, InvoiceItem):
link.paid = self.state == 'paid'
return result