def _send_notification_email(email, payment, template_name, reference_prefix):
    personalisation = {
        'site_url':
        settings.START_PAGE_URL,
        'help_url':
        site_url(reverse('help_area:help')),
        'compliance_contact':
        settings.COMPLIANCE_CONTACT_EMAIL
        or site_url(reverse('help_area:help')),
        'today':
        timezone.localdate().strftime('%d/%m/%Y'),
        'short_payment_ref':
        payment['uuid'][:8].upper(),
        'prisoner_name':
        payment['recipient_name'],
        'prisoner_number':
        payment['prisoner_number'],
        'amount':
        currency_format(Decimal(payment['amount']) / 100),
    }
    personalisation = {
        field: personalisation[field]
        for field in SendMoneyNotifyTemplates.templates[template_name]
        ['personalisation']
    }
    send_email(
        template_name=template_name,
        to=email,
        personalisation=personalisation,
        reference='%s-%s' % (reference_prefix, payment['uuid']),
        staff_email=False,
    )
    def get(self, request):
        prisoner_details = self.valid_form_data[
            DebitCardPrisonerDetailsView.url_name]
        amount_details = self.valid_form_data[DebitCardAmountView.url_name]

        amount_pence = int(amount_details['amount'] * 100)
        service_charge_pence = int(
            get_service_charge(amount_details['amount']) * 100)
        user_ip = request.META.get('HTTP_X_FORWARDED_FOR', '')
        user_ip = user_ip.split(',')[0].strip() or None

        payment_ref = None
        failure_context = {'short_payment_ref': _('Not known')}
        try:
            payment_client = PaymentClient()
            new_payment = {
                'amount': amount_pence,
                'service_charge': service_charge_pence,
                'recipient_name': prisoner_details['prisoner_name'],
                'prisoner_number': prisoner_details['prisoner_number'],
                'prisoner_dob': prisoner_details['prisoner_dob'].isoformat(),
                'ip_address': user_ip,
            }
            payment_ref = payment_client.create_payment(new_payment)
            failure_context['short_payment_ref'] = payment_ref[:8]

            new_govuk_payment = {
                'delayed_capture':
                should_be_capture_delayed(),
                'amount':
                amount_pence + service_charge_pence,
                'reference':
                payment_ref,
                'description':
                gettext('To this prisoner: %(prisoner_number)s' %
                        prisoner_details),
                'return_url':
                site_url(
                    build_view_url(self.request,
                                   DebitCardConfirmationView.url_name)) +
                '?payment_ref=' + payment_ref,
            }
            if new_govuk_payment['delayed_capture']:
                logger.info('Starting delayed capture for %(payment_ref)s',
                            {'payment_ref': payment_ref})

            govuk_payment = payment_client.create_govuk_payment(
                payment_ref, new_govuk_payment)
            if govuk_payment:
                return redirect(get_link_by_rel(govuk_payment, 'next_url'))
        except OAuth2Error:
            logger.exception('Authentication error')
        except RequestException:
            logger.exception('Failed to create new payment (ref %s)',
                             payment_ref)

        return render(request, 'send_money/debit-card-error.html',
                      failure_context)
def send_email_for_card_payment_rejected(email, payment):
    context = _get_email_context_for_payment(payment)
    context[
        'compliance_contact'] = settings.COMPLIANCE_CONTACT_EMAIL or site_url(
            reverse('help_area:help'))

    _send_notification_email(
        email,
        'debit-card-payment-rejected',
        gettext('your payment has NOT been sent to the prisoner'),
        ['dc-rejected'],
        context,
    )
def _send_notification_email(email, template_name, subject, tags, context):
    context.update({
        'site_url': settings.START_PAGE_URL,
        'help_url': site_url(reverse('help_area:help')),
    })
    send_email(
        email,
        f'send_money/email/{template_name}.txt',
        gettext('Send money to someone in prison: %(subject)s') %
        {'subject': subject},
        context=context,
        html_template=f'send_money/email/{template_name}.html',
        anymail_tags=tags,
    )
    def get(self, request):
        prisoner_details = self.valid_form_data[DebitCardPrisonerDetailsView.url_name]
        amount_details = self.valid_form_data[DebitCardAmountView.url_name]

        amount_pence = int(amount_details['amount'] * 100)
        service_charge_pence = int(get_service_charge(amount_details['amount']) * 100)
        payment_ref = None
        failure_context = {
            'short_payment_ref': _('Not known')
        }
        try:
            payment_client = PaymentClient()
            new_payment = {
                'amount': amount_pence,
                'service_charge': service_charge_pence,
                'recipient_name': prisoner_details['prisoner_name'],
                'prisoner_number': prisoner_details['prisoner_number'],
                'prisoner_dob': prisoner_details['prisoner_dob'].isoformat(),
            }
            payment_ref = payment_client.create_payment(new_payment)
            failure_context['short_payment_ref'] = payment_ref[:8]

            new_govuk_payment = {
                'amount': amount_pence + service_charge_pence,
                'reference': payment_ref,
                'description': gettext('To this prisoner: %(prisoner_number)s' % prisoner_details),
                'return_url': site_url(
                    self.build_view_url(DebitCardConfirmationView.url_name) + '?payment_ref=' + payment_ref
                ),
            }
            govuk_payment = payment_client.create_govuk_payment(payment_ref, new_govuk_payment)
            if govuk_payment:
                return redirect(get_link_by_rel(govuk_payment, 'next_url'))
        except OAuth2Error:
            logger.exception('Authentication error')
        except SlumberHttpBaseException:
            logger.exception('Failed to create new payment')
        except RequestsTimeout:
            logger.exception(
                'GOV.UK Pay payment initiation timed out for %s' % payment_ref
            )

        return render(request, 'send_money/debit-card-failure.html', failure_context)