Beispiel #1
0
 def create_nomis_transaction(self, form):
     disbursement = self.disbursement
     try:
         nomis_response = nomis.create_transaction(
             prison_id=disbursement['prison'],
             prisoner_number=disbursement['prisoner_number'],
             amount=disbursement['amount'],
             record_id='d%s' % disbursement['id'],
             description=
             'Sent to {recipient_first_name} {recipient_last_name}'.format(
                 recipient_first_name=disbursement['recipient_first_name'],
                 recipient_last_name=disbursement['recipient_last_name'],
             ).replace('  ', ' '),
             transaction_type='RELA',
             retries=1)
         return nomis_response['id']
     except HTTPError as e:
         if e.response.status_code == 409:
             logger.warning('Disbursement %s was already present in NOMIS' %
                            disbursement['id'])
             return None
         elif e.response.status_code >= 500:
             logger.error(
                 'Disbursement %s could not be made as NOMIS is unavailable'
                 % disbursement['id'])
             form.add_error(None, self.error_messages['connection'])
         else:
             logger.warning('Disbursement %s is invalid' %
                            disbursement['id'])
             form.add_error(None, self.error_messages['invalid'])
     except RequestException:
         logger.exception(
             'Disbursement %s could not be made as NOMIS is unavailable' %
             disbursement['id'])
         form.add_error(None, self.error_messages['connection'])
    def test_call(self):
        """
        Test that the function connects to Prison API (i.e. NOMIS) and gets the expected data.
        """
        actual_transaction_data = {
            'id': '6179604-1',
        }
        with responses.RequestsMock() as rsps:
            self._mock_successful_auth_request(rsps)
            rsps.add(
                responses.POST,
                build_prison_api_v1_url(
                    '/prison/BWI/offenders/A1471AE/transactions'),
                json=actual_transaction_data,
                status=200,
            )

            transactions = nomis.create_transaction('BWI', 'A1471AE', 1634,
                                                    'CL123212',
                                                    'Canteen Purchase', 'CANT')

            self.assertEqual(transactions, actual_transaction_data)

            self.assertEqual(
                json.loads(rsps.calls[-1].request.body.decode()),
                {
                    'type': 'CANT',
                    'description': 'Canteen Purchase',
                    'amount': 1634,
                    'client_transaction_id': 'CL123212',
                    'client_unique_ref': 'CL123212',
                },
            )
def credit_individual_credit_to_nomis(user, user_session, credit_id, credit):
    api_session = get_api_session_with_session(user, user_session)
    if not hasattr(thread_local, 'nomis_session'):
        thread_local.nomis_session = requests.Session()

    nomis_response = None
    try:
        nomis_response = nomis.create_transaction(
            prison_id=credit['prison'],
            prisoner_number=credit['prisoner_number'],
            amount=credit['amount'],
            record_id=str(credit_id),
            description='Sent by {sender}'.format(sender=credit['sender_name']),
            transaction_type='MTDS',
            retries=1,
            session=thread_local.nomis_session
        )
    except HTTPError as e:
        if e.response.status_code == 409:
            logger.warning('Credit %s was already present in NOMIS' % credit_id)
        elif e.response.status_code >= 500:
            logger.error('Credit %s could not credited as NOMIS is unavailable' % credit_id)
            return
        else:
            logger.warning('Credit %s cannot be automatically credited to NOMIS' % credit_id)
            api_session.post(
                'credits/actions/setmanual/',
                json={'credit_ids': [int(credit_id)]}
            )
            return
    except RequestException:
        logger.exception('Credit %s could not credited as NOMIS is unavailable' % credit_id)
        return

    credit_update = {'id': credit_id, 'credited': True}
    if nomis_response and 'id' in nomis_response:
        credit_update['nomis_transaction_id'] = nomis_response['id']
    api_session.post('credits/actions/credit/', json=[credit_update])

    if credit.get('sender_email'):
        send_email(
            credit['sender_email'], 'cashbook/email/credited-confirmation.txt',
            _('Send money to someone in prison: the prisoner’s account has been credited'),
            context={
                'amount': credit['amount'],
                'ref_number': credit.get('short_ref_number'),
                'received_at': credit['received_at'],
                'prisoner_name': credit.get('intended_recipient'),
                'help_url': settings.CITIZEN_HELP_PAGE_URL,
                'feedback_url': settings.CITIZEN_CONTACT_PAGE_URL,
                'site_url': settings.START_PAGE_URL,
            },
            html_template='cashbook/email/credited-confirmation.html',
            anymail_tags=['credited'],
        )
 def create_nomis_transaction(self, form):
     disbursement = self.disbursement
     try:
         nomis_response = nomis.create_transaction(
             prison_id=disbursement['prison'],
             prisoner_number=disbursement['prisoner_number'],
             amount=disbursement['amount'],
             record_id='d%s' % disbursement['id'],
             description='Sent to {recipient_first_name} {recipient_last_name}'.format(
                 recipient_first_name=disbursement['recipient_first_name'],
                 recipient_last_name=disbursement['recipient_last_name'],
             ).replace('  ', ' '),
             transaction_type='RELA',
             retries=1
         )
         return nomis_response['id']
     except HTTPError as e:
         if e.response.status_code == 409:
             logger.warning(
                 'Disbursement %s was already present in NOMIS' % disbursement['id']
             )
             return None
         elif e.response.status_code >= 500:
             logger.error(
                 'Disbursement %s could not be made as NOMIS is unavailable'
                 % disbursement['id']
             )
             form.add_error(None, self.error_messages['connection'])
         else:
             logger.warning('Disbursement %s is invalid' % disbursement['id'])
             form.add_error(None, self.error_messages['invalid'])
     except RequestException:
         logger.exception(
             'Disbursement %s could not be made as NOMIS is unavailable'
             % disbursement['id']
         )
         form.add_error(None, self.error_messages['connection'])
Beispiel #5
0
def credit_individual_credit_to_nomis(user, user_session, credit_id, credit):
    api_session = get_api_session_with_session(user, user_session)
    if not hasattr(thread_local, 'nomis_session'):
        thread_local.nomis_session = requests.Session()

    nomis_response = None
    try:
        nomis_response = nomis.create_transaction(
            prison_id=credit['prison'],
            prisoner_number=credit['prisoner_number'],
            amount=credit['amount'],
            record_id=str(credit_id),
            description='Sent by {sender}'.format(
                sender=credit['sender_name']),
            transaction_type='MTDS',
            retries=1,
            session=thread_local.nomis_session)
    except HTTPError as e:
        if e.response.status_code == 409:
            logger.warning('Credit %s was already present in NOMIS' %
                           credit_id)
        elif e.response.status_code >= 500:
            logger.error(
                'Credit %s could not credited as NOMIS is unavailable' %
                credit_id)
            return
        else:
            logger.warning(
                'Credit %s cannot be automatically credited to NOMIS' %
                credit_id)
            api_session.post('credits/actions/setmanual/',
                             json={'credit_ids': [int(credit_id)]})
            return
    except RequestException:
        logger.exception(
            'Credit %s could not credited as NOMIS is unavailable' % credit_id)
        return

    credit_update = {'id': credit_id, 'credited': True}
    if nomis_response and 'id' in nomis_response:
        credit_update['nomis_transaction_id'] = nomis_response['id']
    api_session.post('credits/actions/credit/', json=[credit_update])

    if credit.get('sender_email'):
        send_email(
            credit['sender_email'],
            'cashbook/email/credited-confirmation.txt',
            _('Send money to someone in prison: the prisoner’s account has been credited'
              ),
            context={
                'amount': credit['amount'],
                'ref_number': credit.get('short_payment_ref'),
                'received_at': credit['received_at'],
                'prisoner_name': credit.get('intended_recipient'),
                'help_url': urljoin(settings.SEND_MONEY_URL, '/help/'),
                'feedback_url': urljoin(settings.SEND_MONEY_URL,
                                        '/contact-us/'),
                'site_url': settings.START_PAGE_URL,
            },
            html_template='cashbook/email/credited-confirmation.html',
            anymail_tags=['credited'],
        )