'Completes an application service transaction.
Args:
txn_id(str): The transaction ID being completed.
service(ApplicationService): The application service which was sent
this transaction.
Returns:
A Deferred which resolves if this transaction was stored
successfully.'

def complete_appservice_txn(self, txn_id, service):

    txn_id = int(txn_id)
    def _complete_appservice_txn(txn):
        last_txn_id = self._get_last_txn(txn, service.id)
        if ((last_txn_id + 1) != txn_id):
            logger.error("appservice:    Completing    a    transaction    which    has    an    ID    >    1    from    the    last    ID    sent    to    this    AS.    We've    either    dropped    events    or    sent    it    to    the    AS    out    of    order.    FIX    ME.    last_txn=%s    completing_txn=%s    service_id=%s", last_txn_id, txn_id, service.id)
        self._simple_upsert_txn(txn, 'application_services_state', dict(as_id=service.id), dict(last_txn=txn_id))
        self._simple_delete_txn(txn, 'application_services_txns', dict(txn_id=txn_id, as_id=service.id))
    return self.runInteraction('complete_appservice_txn', _complete_appservice_txn)
