Exemplo n.º 1
0
def send_transactions(
    payments: List[Payment],
    pass_culture_iban: Optional[str],
    pass_culture_bic: Optional[str],
    pass_culture_remittance_code: Optional[str],
    recipients: List[str],
) -> None:
    if not pass_culture_iban or not pass_culture_bic or not pass_culture_remittance_code:
        raise Exception(
            "[BATCH][PAYMENTS] Missing PASS_CULTURE_IBAN[%s], PASS_CULTURE_BIC[%s] or "
            "PASS_CULTURE_REMITTANCE_CODE[%s] in environment variables" %
            (pass_culture_iban, pass_culture_bic,
             pass_culture_remittance_code))

    message_name = "passCulture-SCT-%s" % datetime.strftime(
        datetime.utcnow(), "%Y%m%d-%H%M%S")
    xml_file = generate_message_file(payments, pass_culture_iban,
                                     pass_culture_bic, message_name,
                                     pass_culture_remittance_code)

    logger.info("[BATCH][PAYMENTS] Payment message name : %s", message_name)

    try:
        validate_message_file_structure(xml_file)
    except DocumentInvalid as exception:
        for payment in payments:
            payment.setStatus(TransactionStatus.NOT_PROCESSABLE,
                              detail=str(exception))
        repository.save(*payments)
        raise

    checksum = generate_file_checksum(xml_file)
    message = generate_payment_message(message_name, checksum, payments)

    logger.info(
        "[BATCH][PAYMENTS] Sending file with message ID [%s] and checksum [%s]",
        message.name, message.checksum.hex())
    logger.info("[BATCH][PAYMENTS] Recipients of email : %s", recipients)

    successfully_sent_payments = send_payment_message_email(
        xml_file, checksum, recipients)
    logger.info("[BATCH][PAYMENTS] Updating status of %d payments",
                len(payments))
    if successfully_sent_payments:
        for payment in payments:
            payment.setStatus(TransactionStatus.SENT)
    else:
        for payment in payments:
            payment.setStatus(TransactionStatus.ERROR,
                              detail="Erreur d'envoi à MailJet")
    repository.save(message, *payments)
Exemplo n.º 2
0
    def test_returns_false_if_not_email_was_sent(
            self, make_payment_transaction_email):
        # given
        xml = '<?xml version="1.0" encoding="UTF-8"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"></Document>'
        checksum = b"\x16\x91\x0c\x11~Hs\xc5\x1a\xa3W1\x13\xbf!jq@\xea  <h&\xef\x1f\xaf\xfc\x7fO\xc8\x82"
        recipients = ["test@email"]
        mocked_send_email = Mock()
        mocked_send_email.return_value = False
        # when
        successfully_sent = send_payment_message_email(xml, checksum,
                                                       recipients,
                                                       mocked_send_email)

        # then
        assert not successfully_sent
Exemplo n.º 3
0
def send_transactions(
    payment_query,
    batch_date: datetime,
    pass_culture_iban: Optional[str],
    pass_culture_bic: Optional[str],
    pass_culture_remittance_code: Optional[str],
    recipients: list[str],
) -> None:
    if not pass_culture_iban or not pass_culture_bic or not pass_culture_remittance_code:
        raise Exception(
            "[BATCH][PAYMENTS] Missing PASS_CULTURE_IBAN[%s], PASS_CULTURE_BIC[%s] or "
            "PASS_CULTURE_REMITTANCE_CODE[%s] in environment variables" %
            (pass_culture_iban, pass_culture_bic,
             pass_culture_remittance_code))

    logger.info("[BATCH][PAYMENTS] Generating venues file")
    venues_csv = generate_venues_csv(payment_query)

    logger.info("[BATCH][PAYMENTS] Generating XML file")

    message_name = "passCulture-SCT-%s" % datetime.strftime(
        datetime.utcnow(), "%Y%m%d-%H%M%S")
    xml_file = generate_message_file(payment_query, batch_date,
                                     pass_culture_iban, pass_culture_bic,
                                     message_name,
                                     pass_culture_remittance_code)

    logger.info("[BATCH][PAYMENTS] Payment message name : %s", message_name)

    # The following may raise a DocumentInvalid exception. This is
    # usually because the data is incorrect. In that case, let the
    # exception bubble up and stop the calling function so that we can
    # fix the data and run the function again.
    validate_message_file_structure(xml_file)

    checksum = hashlib.sha256(xml_file.encode("utf-8")).digest()
    message = PaymentMessage(name=message_name, checksum=checksum)
    db.session.add(message)
    db.session.commit()
    # We cannot directly call "update()" when "join()" has been called.
    # fmt: off
    (db.session.query(Payment).filter(
        Payment.id.in_(payment_query.with_entities(Payment.id))).update(
            {"paymentMessageId": message.id}, synchronize_session=False))
    # fmt: on
    db.session.commit()

    logger.info(
        "[BATCH][PAYMENTS] Sending file with message ID [%s] and checksum [%s]",
        message.name, message.checksum.hex())
    logger.info("[BATCH][PAYMENTS] Recipients of email: %s", recipients)

    venues_csv_path = _save_file_on_disk("venues", venues_csv, "csv")
    xml_path = _save_file_on_disk("banque_de_france", xml_file, "xml")
    if not send_payment_message_email(xml_file, venues_csv, checksum,
                                      recipients):
        logger.info(
            "[BATCH][PAYMENTS] Could not send payment message email. Files have been stored at %s and %s",
            venues_csv_path,
            xml_path,
        )
    logger.info(
        "[BATCH][PAYMENTS] Updating status of payments to UNDER_REVIEW")
    payments_api.bulk_create_payment_statuses(payment_query,
                                              TransactionStatus.UNDER_REVIEW,
                                              detail=None)