Exemplo n.º 1
0
def act(action, batch_db, event_data, plugin_config, logger, config):
    assert batch_db
    descriptors = filter(lambda x: x['name'] == action, get_actions())
    action_descriptor = descriptors[0] if descriptors else None
    if not action_descriptor:
        raise ValueError(u'Invalid action: %s for %s plugin' % (action, PLUGIN_NAME))

    if action == 'get_policy_info_async':
        policy_series_field_name = plugin_config['policy_series_field_name']
        policy_number_field_name = plugin_config['policy_number_field_name']

        policy_series = batch_db.data.get(policy_series_field_name, None)
        policy_number = batch_db.data.get(policy_number_field_name, None)

        if not policy_number or not policy_series:
            return False
        get_policy_info_async.delay(policy_series, policy_number, event_data, batch_db.id)
    elif action == 'get_policy_info_first_try':
        policy_series_field_name = plugin_config['policy_series_field_name']
        policy_number_field_name = plugin_config['policy_number_field_name']

        policy_series = batch_db.data.get(policy_series_field_name, None)
        policy_number = batch_db.data.get(policy_number_field_name, None)

        if not policy_number or not policy_series:
            return False
        try:
            get_policy_info_async(policy_series, policy_number, event_data, batch_db.id, async=False, logger=logger)
        except Exception:
            BatchManager.handle_event(batch_db.id, "on_policy_info_receive_fail", event_data, logger, config=config)
            return False
    else:
        raise Exception(u"Invalid action %s for plugin %s" % (action, PLUGIN_NAME))

    return True
Exemplo n.º 2
0
def go_back(batch_id=None):
    batch = DocumentBatchDbObject.query.filter_by(id=batch_id, _owner=current_user, deleted=False).first()
    if not batch:
        raise errors.BatchNotFound()
    if batch.batch_type != DocumentBatchTypeEnum.DBT_NEW_LLC:
        BatchManager.handle_event(batch_id, 'go_back', {
            'batch_id': batch_id
        }, logger=current_app.logger, config=current_app.config)
        return {"result": True}
    raise NotImplementedError()
Exemplo n.º 3
0
def touch_batch_plugin(batch_id, event_data):
    from fw.documents.batch_manager import BatchManager
    config = celery.conf.get('config')
    app = celery.conf['flask_app']()
    logger = celery.log.get_default_logger()
    with app.app_context():
        batch = DocumentBatchDbObject.query.filter_by(id=batch_id).scalar()
        if not batch:
            raise Exception("Batch not found: %s" % batch_id)
        BatchManager.handle_event(batch_id, "batch_manager.touch", event_data, logger, config=config)
    return True
Exemplo n.º 4
0
def go_back(batch_id=None):
    batch = DocumentBatchDbObject.query.filter_by(id=batch_id,
                                                  _owner=current_user,
                                                  deleted=False).first()
    if not batch:
        raise errors.BatchNotFound()
    if batch.batch_type != DocumentBatchTypeEnum.DBT_NEW_LLC:
        BatchManager.handle_event(batch_id,
                                  'go_back', {'batch_id': batch_id},
                                  logger=current_app.logger,
                                  config=current_app.config)
        return {"result": True}
    raise NotImplementedError()
Exemplo n.º 5
0
def touch_batch_plugin(batch_id, event_data):
    from fw.documents.batch_manager import BatchManager
    config = celery.conf.get('config')
    app = celery.conf['flask_app']()
    logger = celery.log.get_default_logger()
    with app.app_context():
        batch = DocumentBatchDbObject.query.filter_by(id=batch_id).scalar()
        if not batch:
            raise Exception("Batch not found: %s" % batch_id)
        BatchManager.handle_event(batch_id,
                                  "batch_manager.touch",
                                  event_data,
                                  logger,
                                  config=config)
    return True
Exemplo n.º 6
0
def act(action, batch_db, event_data, plugin_config, logger, config):
    assert batch_db
    descriptors = filter(lambda x: x['name'] == action, get_actions())
    action_descriptor = descriptors[0] if descriptors else None
    if not action_descriptor:
        raise ValueError(u'Invalid action: %s for %s plugin' %
                         (action, PLUGIN_NAME))

    if action == 'get_policy_info_async':
        policy_series_field_name = plugin_config['policy_series_field_name']
        policy_number_field_name = plugin_config['policy_number_field_name']

        policy_series = batch_db.data.get(policy_series_field_name, None)
        policy_number = batch_db.data.get(policy_number_field_name, None)

        if not policy_number or not policy_series:
            return False
        get_policy_info_async.delay(policy_series, policy_number, event_data,
                                    batch_db.id)
    elif action == 'get_policy_info_first_try':
        policy_series_field_name = plugin_config['policy_series_field_name']
        policy_number_field_name = plugin_config['policy_number_field_name']

        policy_series = batch_db.data.get(policy_series_field_name, None)
        policy_number = batch_db.data.get(policy_number_field_name, None)

        if not policy_number or not policy_series:
            return False
        try:
            get_policy_info_async(policy_series,
                                  policy_number,
                                  event_data,
                                  batch_db.id,
                                  async=False,
                                  logger=logger)
        except Exception:
            BatchManager.handle_event(batch_db.id,
                                      "on_policy_info_receive_fail",
                                      event_data,
                                      logger,
                                      config=config)
            return False
    else:
        raise Exception(u"Invalid action %s for plugin %s" %
                        (action, PLUGIN_NAME))

    return True
Exemplo n.º 7
0
def send(batch_id, event, event_data=None):
    event_data = event_data or {}
    from fw.documents.batch_manager import BatchManager
    app = celery.conf['flask_app']()
    logger = app.logger
    logger.info(u"PROCESSING event %s for batch %s" % (event, batch_id))
    with app.app_context():
        result = BatchManager.handle_event(batch_id, event, event_data, logger=logger, config=app.config)
        logger.info(u"FINISH PROCESSING event %s for batch %s" % (event, batch_id))
        return result
Exemplo n.º 8
0
    def test_transit_on_event(self):
        batch = self.create_batch(DocumentBatchTypeEnum.DBT_TEST_TYPE, self.user)
        new_data = {
            'short_name': u'Тест нейм'
        }
        new_batch_data = {
            'data': new_data,
            'batch_type': DocumentBatchTypeEnum.DBT_TEST_TYPE,
            'metadata': {}
        }

        new_batch = DocumentBatch.parse_raw_value(new_batch_data, api_data=False)
        result = self.test_client.post('/batch/update/', data={
            'batch_id': batch.id,
            'batch': json.dumps(new_batch.get_api_structure())
        })
        self.assertEqual(result.status_code, 200)
        doc = BatchDocumentDbObject.query.scalar()
        d = json.loads(result.data)
        del d['result']['creation_date']
        self.assertEqual(d, {u'result': {
            u'all_docs': [{
                u'caption': u'Тестовый документ 1',
                u'document_id': doc.id,
                u'document_type': u'test_doc_1',
                u'file_link': None
            }],
            u'batch_type': u'_test',
            u'data': {u'short_name': u'Тест нейм'},
            u'id': batch.id,
            u'metadata': {},
            u'name': u'Тестовый батч',
            u'paid': u'false',
            u'result_fields': {u'name': u'Тест нейм'},
            u'status': u'new',
            u'status_data': {'finalisation_count': u'0'}
        }
        })

        BatchManager.handle_event(batch.id, 'simple_event', {}, current_app.logger, config=self.config)

        doc = DocumentBatchDbObject.query.scalar()
        self.assertEqual(doc.status, 'after_simple_event')
Exemplo n.º 9
0
def render_document_plugin(batch_id, event_data):
    doc_id = event_data['doc_id']
    from fw.documents.batch_manager import BatchManager
    config = celery.conf.get('config')
    app = celery.conf['flask_app']()
    logger = celery.log.get_default_logger()
    with app.app_context():
        batch = DocumentBatchDbObject.query.filter_by(id=batch_id).scalar()
        if not batch:
            BatchManager.handle_event(batch_id, "doc_render_fail", event_data, logger, config=config)
            batch_group_gen_check_task.delay()
            raise Exception("Batch not found: %s" % batch_id)
        try:
            render_batch_document_raw(batch_id, doc_id, config)
            doc = BatchDocumentDbObject.query.filter_by(id=doc_id).scalar()
            assert(doc)
            event = "doc_render_success" if doc.status == UserDocumentStatus.DS_RENDERED else "doc_render_fail"
            logger.info(u"render_document_plugin event %s for document %s" % (event, doc.id))
            BatchManager.handle_event(batch_id, event, event_data, logger, config=config)
            batch_group_gen_check_task.delay()
        except Exception:
            zabbix_sender.send("celery_failures", 1)
            BatchManager.handle_event(batch_id, "doc_render_fail", event_data, logger, config=config)
            batch_group_gen_check_task.delay()
            raise
Exemplo n.º 10
0
def batch_group_gen_check_task():
    from fw.documents.batch_manager import BatchManager
    app = celery.conf['flask_app']()
    logger = celery.log.get_default_logger()
    with app.app_context():
        for batch_check in DocGroupRenderTaskCheck.query.filter_by(
                check_completed=False):
            logger.info(u"Checking check %s" % str(batch_check.id))
            batch = DocumentBatchDbObject.query.filter_by(
                id=batch_check.batch_id).scalar()
            if not batch:
                BatchManager.handle_event(batch_check.batch_id,
                                          "doc_group_render_fail", {},
                                          logger,
                                          config=app.config)
                raise Exception("Batch not found: %s" % batch_check.batch_id)
            doc_id_list = batch_check.doc_id_list
            all_rendered = True
            logger.info(u"Checking. Doc id list length: %s" %
                        str(len(doc_id_list)))
            for doc_id in doc_id_list:
                doc = BatchDocumentDbObject.query.filter_by(id=doc_id).scalar()
                logger.info(u"Checking doc %s." % str(doc_id))
                if not doc or doc.status == UserDocumentStatus.DS_RENDERING_FAILED or \
                   doc._celery_task_id and abs((datetime.utcnow() - doc._celery_task_started).total_seconds()) > 60:
                    res = DocGroupRenderTaskCheck.query.filter_by(
                        id=batch_check.id, check_completed=False).update(
                            {'check_completed': True})
                    sqldb.session.commit()
                    logger.info(u"Checking -> checked %s." % str(res))
                    if res:
                        all_rendered = False
                        logger.info(
                            u"Checking: handling doc_group_render_fail for %s"
                            % str(batch_check.batch_id))
                        BatchManager.handle_event(batch_check.batch_id,
                                                  "doc_group_render_fail",
                                                  batch_check.event_data,
                                                  logger,
                                                  config=app.config)
                    break
                if doc.status != UserDocumentStatus.DS_RENDERED:
                    logger.info(u"Checking: doc %s is not rendered" %
                                str(doc.id))
                    all_rendered = False
            if all_rendered:
                logger.info(u"Checking: All rendered")
                res = DocGroupRenderTaskCheck.query.filter_by(
                    id=batch_check.id,
                    check_completed=False).update({'check_completed': True})
                sqldb.session.commit()
                if res:
                    BatchManager.handle_event(batch_check.batch_id,
                                              "doc_group_render_success",
                                              batch_check.event_data,
                                              logger,
                                              config=app.config)

    return True
Exemplo n.º 11
0
    def test_send_email_on_transition(self):
        batch = self.create_batch(DocumentBatchTypeEnum.DBT_TEST_TYPE, self.user)
        new_data = {
            'short_name': u'Тест нейм'
        }
        new_batch_data = {
            'data': new_data,
            'batch_type': DocumentBatchTypeEnum.DBT_TEST_TYPE,
            'metadata': {}
        }

        new_batch = DocumentBatch.parse_raw_value(new_batch_data, api_data=False)
        result = self.test_client.post('/batch/update/', data={
            'batch_id': batch.id,
            'batch': json.dumps(new_batch.get_api_structure())
        })
        self.assertEqual(result.status_code, 200)

        self.assertEqual(len(self.mailer.mails), 0)
        BatchManager.handle_event(batch.id, 'simple_event', None, logger=current_app.logger, config=self.config)

        self.assertEqual(len(self.mailer.mails), 1)
Exemplo n.º 12
0
def send(batch_id, event, event_data=None):
    event_data = event_data or {}
    from fw.documents.batch_manager import BatchManager
    app = celery.conf['flask_app']()
    logger = app.logger
    logger.info(u"PROCESSING event %s for batch %s" % (event, batch_id))
    with app.app_context():
        result = BatchManager.handle_event(batch_id,
                                           event,
                                           event_data,
                                           logger=logger,
                                           config=app.config)
        logger.info(u"FINISH PROCESSING event %s for batch %s" %
                    (event, batch_id))
        return result
Exemplo n.º 13
0
def batch_group_gen_check_task():
    from fw.documents.batch_manager import BatchManager
    app = celery.conf['flask_app']()
    logger = celery.log.get_default_logger()
    with app.app_context():
        for batch_check in DocGroupRenderTaskCheck.query.filter_by(check_completed=False):
            logger.info(u"Checking check %s" % str(batch_check.id))
            batch = DocumentBatchDbObject.query.filter_by(id=batch_check.batch_id).scalar()
            if not batch:
                BatchManager.handle_event(batch_check.batch_id, "doc_group_render_fail", {}, logger, config=app.config)
                raise Exception("Batch not found: %s" % batch_check.batch_id)
            doc_id_list = batch_check.doc_id_list
            all_rendered = True
            logger.info(u"Checking. Doc id list length: %s" % str(len(doc_id_list)))
            for doc_id in doc_id_list:
                doc = BatchDocumentDbObject.query.filter_by(id=doc_id).scalar()
                logger.info(u"Checking doc %s." % str(doc_id))
                if not doc or doc.status == UserDocumentStatus.DS_RENDERING_FAILED or \
                   doc._celery_task_id and abs((datetime.utcnow() - doc._celery_task_started).total_seconds()) > 60:
                    res = DocGroupRenderTaskCheck.query.filter_by(id=batch_check.id, check_completed=False).update({
                        'check_completed': True
                    })
                    sqldb.session.commit()
                    logger.info(u"Checking -> checked %s." % str(res))
                    if res:
                        all_rendered = False
                        logger.info(u"Checking: handling doc_group_render_fail for %s" % str(batch_check.batch_id))
                        BatchManager.handle_event(batch_check.batch_id, "doc_group_render_fail", batch_check.event_data, logger, config=app.config)
                    break
                if doc.status != UserDocumentStatus.DS_RENDERED:
                    logger.info(u"Checking: doc %s is not rendered" % str(doc.id))
                    all_rendered = False
            if all_rendered:
                logger.info(u"Checking: All rendered")
                res = DocGroupRenderTaskCheck.query.filter_by(id=batch_check.id, check_completed=False).update({
                    'check_completed': True
                })
                sqldb.session.commit()
                if res:
                    BatchManager.handle_event(batch_check.batch_id, "doc_group_render_success", batch_check.event_data, logger, config=app.config)

    return True
Exemplo n.º 14
0
def render_document_plugin(batch_id, event_data):
    doc_id = event_data['doc_id']
    from fw.documents.batch_manager import BatchManager
    config = celery.conf.get('config')
    app = celery.conf['flask_app']()
    logger = celery.log.get_default_logger()
    with app.app_context():
        batch = DocumentBatchDbObject.query.filter_by(id=batch_id).scalar()
        if not batch:
            BatchManager.handle_event(batch_id,
                                      "doc_render_fail",
                                      event_data,
                                      logger,
                                      config=config)
            batch_group_gen_check_task.delay()
            raise Exception("Batch not found: %s" % batch_id)
        try:
            render_batch_document_raw(batch_id, doc_id, config)
            doc = BatchDocumentDbObject.query.filter_by(id=doc_id).scalar()
            assert (doc)
            event = "doc_render_success" if doc.status == UserDocumentStatus.DS_RENDERED else "doc_render_fail"
            logger.info(u"render_document_plugin event %s for document %s" %
                        (event, doc.id))
            BatchManager.handle_event(batch_id,
                                      event,
                                      event_data,
                                      logger,
                                      config=config)
            batch_group_gen_check_task.delay()
        except Exception:
            zabbix_sender.send("celery_failures", 1)
            BatchManager.handle_event(batch_id,
                                      "doc_render_fail",
                                      event_data,
                                      logger,
                                      config=config)
            batch_group_gen_check_task.delay()
            raise
Exemplo n.º 15
0
def yad_payment_aviso():
    dt_str = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")  # u"2011-05-04T20:38:01.000+04:00"

    logger = current_app.logger

    request_datetime = request.form.get('requestDatetime', "")
    md5 = request.form.get('md5', "")
    shop_id = request.form.get('shopId', "")
    shop_article_id = request.form.get('shopArticleId', "")
    invoice_id = request.form.get('invoiceId', "")
    orderId = request.form.get('orderId', "")
    customer_number = request.form.get('customerNumber', "")
    order_created_datetime = request.form.get('orderCreatedDatetime', "")
    order_sum_amount = request.form.get('orderSumAmount', "")
    order_sum_currency_paycash = request.form.get('orderSumCurrencyPaycash', "")
    order_sum_bank_paycash = request.form.get('orderSumBankPaycash', "")
    shop_sum_amount = request.form.get('shopSumAmount', "")
    shop_sum_currency_paycash = request.form.get('shopSumCurrencyPaycash', "")
    shop_sum_bank_paycash = request.form.get('shopSumBankPaycash', "")
    payment_payer_code = request.form.get('paymentPayerCode', "")
    payment_type = request.form.get('paymentType', "")
    action = request.form.get('action', "")
    payment_datetime = request.form.get('paymentDatetime', "")
    cps_user_country_code = request.form.get('cps_user_country_code', "")

    invalid_request_error = u"""<?xml version="1.0" encoding="UTF-8"?>
<paymentAvisoResponse  performedDatetime="%s" code="200" invoiceId="%s" shopId="%s" message="msg"/>""" % (
        dt_str, invoice_id, shop_id)

    authorization_error = u"""<?xml version="1.0" encoding="UTF-8"?>
<paymentAvisoResponse  performedDatetime="%s" code="1" invoiceId="%s" shopId="%s" message="Invalid request: md5 sum does not match provided value"/>""" % (
        dt_str, invoice_id, shop_id)

    admins_emails = current_app.config['ADMIN_EMAIL_LIST']
    if not md5 or not shop_id or not action or not order_sum_amount or not order_sum_currency_paycash \
            or not order_sum_bank_paycash or not invoice_id or not customer_number or not orderId:
        current_app.logger.warn(u"Invalid request from yad: %s" % unicode(request.form))
        _notify_admin(action, u"missing one of required arguments", admins_emails)
        return _xml_resp(invalid_request_error.replace(u'msg', u"missing one of required arguments"))

    shop_password = current_app.config['YAD_ESHOP_PASSWORD']
    yad_ip_list = current_app.config['YAD_IP_LIST']

    # MD5 calc
    # action;orderSumAmount;orderSumCurrencyPaycash;orderSumBankPaycash;shopId;invoiceId;customerNumber;shopPassword
    our_md5_string = "%s;%s;%s;%s;%s;%s;%s;%s" % (action, order_sum_amount, order_sum_currency_paycash,
                                                  order_sum_bank_paycash, shop_id, invoice_id, customer_number,
                                                  shop_password)

    m = hashlib.md5()
    m.update(our_md5_string)

    ip = None
    if 'X-Forwarded-For' in request.headers:
        ip = request.headers['X-Forwarded-For']
    if not ip and 'X-Real-Ip' in request.headers:
        ip = request.headers['X-Real-Ip']
    if not ip:
        ip = request.remote_addr

    new_item = YadRequestsObject(
        ip=ip,
        created=datetime.utcnow(),
        request_datetime=parse_iso_dt(request_datetime),
        md5=md5,
        shop_id=int(shop_id),
        shop_article_id=int(shop_article_id) if shop_article_id else 0,
        invoice_id=int(invoice_id),
        order_number=orderId,
        customer_number=customer_number,
        order_created_datetime=parse_iso_dt(order_created_datetime),
        order_sum_amount=Decimal(order_sum_amount),
        order_sum_currency_paycash=order_sum_currency_paycash,
        order_sum_bank_paycash=order_sum_bank_paycash,
        shop_sum_amount=Decimal(shop_sum_amount),
        shop_sum_currency_paycash=shop_sum_currency_paycash,
        shop_sum_bank_paycash=shop_sum_bank_paycash,
        payment_payer_code=payment_payer_code,
        payment_type=payment_type,
        action=action,
        payment_datetime=parse_iso_dt(payment_datetime),
        cps_user_country_code=cps_user_country_code
    )
    sqldb.session.add(new_item)
    sqldb.session.commit()

    if action != u'paymentAviso':
        current_app.logger.warn(u"Invalid request from yad: %s" % unicode(request.form))
        _notify_admin(action, u"invalid action id: %s" % unicode(action), admins_emails)
        return _xml_resp(invalid_request_error.replace(u'msg', u"invalid action id: %s" % unicode(action)))

    if yad_ip_list:
        if ip not in yad_ip_list:
            current_app.logger.warn(u"Invalid request from yad: %s" % unicode(request.form))
            _notify_admin(action, u"sender ip (%s) not in whitelist" % ip, admins_emails)
            return _xml_resp(invalid_request_error.replace(u'msg', u"sender ip not in whitelist"))
    else:
        current_app.logger.warn(u"Can't check IP address: YAD_IP_LIST config option is empty")

    if m.hexdigest().upper() != md5:
        current_app.logger.warn(u"Invalid request from yad: %s" % unicode(request.form))
        _notify_admin(action, u"arguments md5 digests do not match", admins_emails)
        return _xml_resp(authorization_error)

    try:
        auth_user_id = customer_number
        batch_id = orderId if orderId not in ('subscription_3', 'subscription_1') else None
        subs_type = orderId if orderId in ('subscription_3', 'subscription_1') else None
        if not batch_id and not subs_type:
            raise Exception("Invalid order number:%s" % orderId)
    except Exception:
        current_app.logger.warn(u"Invalid request from yad: %s" % unicode(request.form))
        _notify_admin(action, u"Invalid user id or batch id", admins_emails)
        return _xml_resp(invalid_request_error.replace(u'msg', u"Invalid user id or batch id"))

    user = AuthUser.query.filter_by(uuid=auth_user_id).scalar()
    if not user:
        current_app.logger.warn(u"Invalid request from yad: %s" % unicode(request.form))
        _notify_admin(action, u"User with id %s not found" % unicode(auth_user_id), admins_emails)
        return _xml_resp(invalid_request_error.replace(u'msg', u"User not found"))

    success_result = u"""<?xml version="1.0" encoding="UTF-8"?>
    <paymentAvisoResponse performedDatetime ="%s" code="0" invoiceId="%s" shopId="%s"/>""" % (
        dt_str, invoice_id, shop_id)

    pay_info = {
        'dt': datetime.now(),
        'shop_id': shop_id,
        'invoice_id': invoice_id,
        'order_sum_amount': order_sum_amount,
        'order_sum_currency_paycash': order_sum_currency_paycash,
        'order_sum_bank_paycash': order_sum_bank_paycash
    }

    if shop_article_id is not None:
        pay_info['shop_article_id'] = shop_article_id
    if order_created_datetime:
        pay_info['order_created_datetime'] = order_created_datetime
    if shop_sum_amount:
        pay_info['shop_sum_amount'] = shop_sum_amount
    if shop_sum_currency_paycash:
        pay_info['shop_sum_currency_paycash'] = shop_sum_currency_paycash
    if shop_sum_bank_paycash:
        pay_info['shop_sum_bank_paycash'] = shop_sum_bank_paycash
    if payment_payer_code:
        pay_info['payment_payer_code'] = payment_payer_code
    if payment_type:
        pay_info['payment_type'] = payment_type
    if payment_datetime:
        pay_info['payment_datetime'] = payment_datetime
    if cps_user_country_code:
        pay_info['cps_user_country_code'] = cps_user_country_code
    if request_datetime:
        pay_info['request_datetime'] = request_datetime

    if batch_id:
        batch = DocumentBatchDbObject.query.filter_by(id=batch_id).scalar()

        if not batch:
            current_app.logger.warn(u"Invalid request from yad: %s" % unicode(request.form))
            _notify_admin(action, u"Batch with id %s not found" % batch_id, admins_emails)
            return _xml_resp(invalid_request_error.replace(u'msg', u"Batch not found"))

        modify_result = DocumentBatchDbObject.query.filter_by(id=batch_id).update({
            "pay_info": pay_info,
            "paid": True
        })
        if batch.batch_type == DocumentBatchTypeEnum.DBT_NEW_LLC:
            pay_info = PayInfoObject(
                user=batch._owner,
                batch=batch,
                pay_record_id=new_item.id,
                payment_provider=PaymentProvider.YAD,
                service_type=PurchaseServiceType.LLC_PURCHASE
            )
            sqldb.session.add(pay_info)
            sqldb.session.commit()
        elif batch.batch_type == DocumentBatchTypeEnum.DBT_OSAGO:
            count = PayInfoObject.query.filter_by(batch=batch).count()
            osago_service_code = PurchaseServiceType.OSAGO_PART1 if count < 1 else PurchaseServiceType.OSAGO_PART2
            pay_info = PayInfoObject(
                user=batch._owner,
                batch=batch,
                pay_record_id=new_item.id,
                payment_provider=PaymentProvider.YAD,
                service_type=osago_service_code
            )
            sqldb.session.add(pay_info)
            batch.paid = True
            sqldb.session.commit()
            event = {
                PurchaseServiceType.OSAGO_PART1: 'rerender_pretension',
                PurchaseServiceType.OSAGO_PART2: 'rerender_claim'
            }.get(osago_service_code, None)
            if event:
                BatchManager.handle_event(batch_id, event, {'batch_id': batch_id}, current_app.logger, current_app.config)

        if modify_result is None:
            logger.error(u"Failed to set payment info to batch")
            _notify_admin(action, u"Failed to set payment info to batch", admins_emails)
            return _xml_resp(invalid_request_error.replace(u'msg', u"Failed to process"))

        try:
            for doc in BatchDocumentDbObject.query.filter_by(batch=batch, status=UserDocumentStatus.DS_RENDERED):
                if not doc.file:
                    continue

                file_obj = doc.file
                if not file_obj:
                    logger.error(u"Can't replace watermarked file: Failed to find file of batch %s" % batch_id)
                    continue

                file_path = FileStorage.get_path(file_obj, current_app.config)
                if not file_path or not os.path.exists(file_path) or not os.path.exists(file_path + '.src'):
                    logger.error(
                        u"Can't replace watermarked file: Failed to find original or source file %s of batch %s" % (
                            unicode(file_path + '.src'), batch_id))
                    continue
                os.rename(file_path + '.src', file_path)
        except Exception:
            current_app.logger.exception(u"FAILED TO REPLACE WATERMARKED DOCS")

        if current_app.config.get('PROD', False):
            appcraft.send_stat(batch.batch_type + '_payment_received', batch._owner, batch.id, batch.batch_type, int(invoice_id))
            mixpanel_metrics.send_stat(batch.batch_type + '_payment_received', batch._owner, batch.id, batch.batch_type)

        try:
            if batch.batch_type == DocumentBatchTypeEnum.DBT_NEW_LLC:
                BatchManager.send_batch_docs_to_user(batch_id, current_app.config)
        except Exception:
            logger.exception(u"Failed to send documents to user.")
    elif subs_type:
        user_subs = PaymentSubscriptionObject.query.filter(
            PaymentSubscriptionObject.user == user,
            PaymentSubscriptionObject.end_dt.__ge__(datetime.utcnow())
        )

        if not user_subs.count():
            end_date = datetime.utcnow()
            if subs_type == 'subscription_3':
                end_date += relativedelta(months=3)
            elif subs_type == 'subscription_1':
                end_date += relativedelta(months=1)

            new_subs = PaymentSubscriptionObject(
                pay_info=pay_info,
                created=datetime.utcnow(),
                end_dt=end_date,
                user=user,
                type=subs_type
            )
            sqldb.session.add(new_subs)
            sqldb.session.commit()

            from fw.async_tasks import not_paid_check_send

            not_paid_check_send.make_all_user_fin_batch_paid_and_replace_watermarked_docs_with_normal.delay(
                user_id=user.id)

    current_app.logger.info(u"yad - success")
    return _xml_resp(success_result)
Exemplo n.º 16
0
def yad_payment_aviso():
    dt_str = datetime.utcnow().strftime(
        "%Y-%m-%dT%H:%M:%SZ")  # u"2011-05-04T20:38:01.000+04:00"

    logger = current_app.logger

    request_datetime = request.form.get('requestDatetime', "")
    md5 = request.form.get('md5', "")
    shop_id = request.form.get('shopId', "")
    shop_article_id = request.form.get('shopArticleId', "")
    invoice_id = request.form.get('invoiceId', "")
    orderId = request.form.get('orderId', "")
    customer_number = request.form.get('customerNumber', "")
    order_created_datetime = request.form.get('orderCreatedDatetime', "")
    order_sum_amount = request.form.get('orderSumAmount', "")
    order_sum_currency_paycash = request.form.get('orderSumCurrencyPaycash',
                                                  "")
    order_sum_bank_paycash = request.form.get('orderSumBankPaycash', "")
    shop_sum_amount = request.form.get('shopSumAmount', "")
    shop_sum_currency_paycash = request.form.get('shopSumCurrencyPaycash', "")
    shop_sum_bank_paycash = request.form.get('shopSumBankPaycash', "")
    payment_payer_code = request.form.get('paymentPayerCode', "")
    payment_type = request.form.get('paymentType', "")
    action = request.form.get('action', "")
    payment_datetime = request.form.get('paymentDatetime', "")
    cps_user_country_code = request.form.get('cps_user_country_code', "")

    invalid_request_error = u"""<?xml version="1.0" encoding="UTF-8"?>
<paymentAvisoResponse  performedDatetime="%s" code="200" invoiceId="%s" shopId="%s" message="msg"/>""" % (
        dt_str, invoice_id, shop_id)

    authorization_error = u"""<?xml version="1.0" encoding="UTF-8"?>
<paymentAvisoResponse  performedDatetime="%s" code="1" invoiceId="%s" shopId="%s" message="Invalid request: md5 sum does not match provided value"/>""" % (
        dt_str, invoice_id, shop_id)

    admins_emails = current_app.config['ADMIN_EMAIL_LIST']
    if not md5 or not shop_id or not action or not order_sum_amount or not order_sum_currency_paycash \
            or not order_sum_bank_paycash or not invoice_id or not customer_number or not orderId:
        current_app.logger.warn(u"Invalid request from yad: %s" %
                                unicode(request.form))
        _notify_admin(action, u"missing one of required arguments",
                      admins_emails)
        return _xml_resp(
            invalid_request_error.replace(
                u'msg', u"missing one of required arguments"))

    shop_password = current_app.config['YAD_ESHOP_PASSWORD']
    yad_ip_list = current_app.config['YAD_IP_LIST']

    # MD5 calc
    # action;orderSumAmount;orderSumCurrencyPaycash;orderSumBankPaycash;shopId;invoiceId;customerNumber;shopPassword
    our_md5_string = "%s;%s;%s;%s;%s;%s;%s;%s" % (
        action, order_sum_amount, order_sum_currency_paycash,
        order_sum_bank_paycash, shop_id, invoice_id, customer_number,
        shop_password)

    m = hashlib.md5()
    m.update(our_md5_string)

    ip = None
    if 'X-Forwarded-For' in request.headers:
        ip = request.headers['X-Forwarded-For']
    if not ip and 'X-Real-Ip' in request.headers:
        ip = request.headers['X-Real-Ip']
    if not ip:
        ip = request.remote_addr

    new_item = YadRequestsObject(
        ip=ip,
        created=datetime.utcnow(),
        request_datetime=parse_iso_dt(request_datetime),
        md5=md5,
        shop_id=int(shop_id),
        shop_article_id=int(shop_article_id) if shop_article_id else 0,
        invoice_id=int(invoice_id),
        order_number=orderId,
        customer_number=customer_number,
        order_created_datetime=parse_iso_dt(order_created_datetime),
        order_sum_amount=Decimal(order_sum_amount),
        order_sum_currency_paycash=order_sum_currency_paycash,
        order_sum_bank_paycash=order_sum_bank_paycash,
        shop_sum_amount=Decimal(shop_sum_amount),
        shop_sum_currency_paycash=shop_sum_currency_paycash,
        shop_sum_bank_paycash=shop_sum_bank_paycash,
        payment_payer_code=payment_payer_code,
        payment_type=payment_type,
        action=action,
        payment_datetime=parse_iso_dt(payment_datetime),
        cps_user_country_code=cps_user_country_code)
    sqldb.session.add(new_item)
    sqldb.session.commit()

    if action != u'paymentAviso':
        current_app.logger.warn(u"Invalid request from yad: %s" %
                                unicode(request.form))
        _notify_admin(action, u"invalid action id: %s" % unicode(action),
                      admins_emails)
        return _xml_resp(
            invalid_request_error.replace(
                u'msg', u"invalid action id: %s" % unicode(action)))

    if yad_ip_list:
        if ip not in yad_ip_list:
            current_app.logger.warn(u"Invalid request from yad: %s" %
                                    unicode(request.form))
            _notify_admin(action, u"sender ip (%s) not in whitelist" % ip,
                          admins_emails)
            return _xml_resp(
                invalid_request_error.replace(u'msg',
                                              u"sender ip not in whitelist"))
    else:
        current_app.logger.warn(
            u"Can't check IP address: YAD_IP_LIST config option is empty")

    if m.hexdigest().upper() != md5:
        current_app.logger.warn(u"Invalid request from yad: %s" %
                                unicode(request.form))
        _notify_admin(action, u"arguments md5 digests do not match",
                      admins_emails)
        return _xml_resp(authorization_error)

    try:
        auth_user_id = customer_number
        batch_id = orderId if orderId not in ('subscription_3',
                                              'subscription_1') else None
        subs_type = orderId if orderId in ('subscription_3',
                                           'subscription_1') else None
        if not batch_id and not subs_type:
            raise Exception("Invalid order number:%s" % orderId)
    except Exception:
        current_app.logger.warn(u"Invalid request from yad: %s" %
                                unicode(request.form))
        _notify_admin(action, u"Invalid user id or batch id", admins_emails)
        return _xml_resp(
            invalid_request_error.replace(u'msg',
                                          u"Invalid user id or batch id"))

    user = AuthUser.query.filter_by(uuid=auth_user_id).scalar()
    if not user:
        current_app.logger.warn(u"Invalid request from yad: %s" %
                                unicode(request.form))
        _notify_admin(action,
                      u"User with id %s not found" % unicode(auth_user_id),
                      admins_emails)
        return _xml_resp(
            invalid_request_error.replace(u'msg', u"User not found"))

    success_result = u"""<?xml version="1.0" encoding="UTF-8"?>
    <paymentAvisoResponse performedDatetime ="%s" code="0" invoiceId="%s" shopId="%s"/>""" % (
        dt_str, invoice_id, shop_id)

    pay_info = {
        'dt': datetime.now(),
        'shop_id': shop_id,
        'invoice_id': invoice_id,
        'order_sum_amount': order_sum_amount,
        'order_sum_currency_paycash': order_sum_currency_paycash,
        'order_sum_bank_paycash': order_sum_bank_paycash
    }

    if shop_article_id is not None:
        pay_info['shop_article_id'] = shop_article_id
    if order_created_datetime:
        pay_info['order_created_datetime'] = order_created_datetime
    if shop_sum_amount:
        pay_info['shop_sum_amount'] = shop_sum_amount
    if shop_sum_currency_paycash:
        pay_info['shop_sum_currency_paycash'] = shop_sum_currency_paycash
    if shop_sum_bank_paycash:
        pay_info['shop_sum_bank_paycash'] = shop_sum_bank_paycash
    if payment_payer_code:
        pay_info['payment_payer_code'] = payment_payer_code
    if payment_type:
        pay_info['payment_type'] = payment_type
    if payment_datetime:
        pay_info['payment_datetime'] = payment_datetime
    if cps_user_country_code:
        pay_info['cps_user_country_code'] = cps_user_country_code
    if request_datetime:
        pay_info['request_datetime'] = request_datetime

    if batch_id:
        batch = DocumentBatchDbObject.query.filter_by(id=batch_id).scalar()

        if not batch:
            current_app.logger.warn(u"Invalid request from yad: %s" %
                                    unicode(request.form))
            _notify_admin(action, u"Batch with id %s not found" % batch_id,
                          admins_emails)
            return _xml_resp(
                invalid_request_error.replace(u'msg', u"Batch not found"))

        modify_result = DocumentBatchDbObject.query.filter_by(
            id=batch_id).update({
                "pay_info": pay_info,
                "paid": True
            })
        if batch.batch_type == DocumentBatchTypeEnum.DBT_NEW_LLC:
            pay_info = PayInfoObject(
                user=batch._owner,
                batch=batch,
                pay_record_id=new_item.id,
                payment_provider=PaymentProvider.YAD,
                service_type=PurchaseServiceType.LLC_PURCHASE)
            sqldb.session.add(pay_info)
            sqldb.session.commit()
        elif batch.batch_type == DocumentBatchTypeEnum.DBT_OSAGO:
            count = PayInfoObject.query.filter_by(batch=batch).count()
            osago_service_code = PurchaseServiceType.OSAGO_PART1 if count < 1 else PurchaseServiceType.OSAGO_PART2
            pay_info = PayInfoObject(user=batch._owner,
                                     batch=batch,
                                     pay_record_id=new_item.id,
                                     payment_provider=PaymentProvider.YAD,
                                     service_type=osago_service_code)
            sqldb.session.add(pay_info)
            batch.paid = True
            sqldb.session.commit()
            event = {
                PurchaseServiceType.OSAGO_PART1: 'rerender_pretension',
                PurchaseServiceType.OSAGO_PART2: 'rerender_claim'
            }.get(osago_service_code, None)
            if event:
                BatchManager.handle_event(batch_id, event,
                                          {'batch_id': batch_id},
                                          current_app.logger,
                                          current_app.config)

        if modify_result is None:
            logger.error(u"Failed to set payment info to batch")
            _notify_admin(action, u"Failed to set payment info to batch",
                          admins_emails)
            return _xml_resp(
                invalid_request_error.replace(u'msg', u"Failed to process"))

        try:
            for doc in BatchDocumentDbObject.query.filter_by(
                    batch=batch, status=UserDocumentStatus.DS_RENDERED):
                if not doc.file:
                    continue

                file_obj = doc.file
                if not file_obj:
                    logger.error(
                        u"Can't replace watermarked file: Failed to find file of batch %s"
                        % batch_id)
                    continue

                file_path = FileStorage.get_path(file_obj, current_app.config)
                if not file_path or not os.path.exists(
                        file_path) or not os.path.exists(file_path + '.src'):
                    logger.error(
                        u"Can't replace watermarked file: Failed to find original or source file %s of batch %s"
                        % (unicode(file_path + '.src'), batch_id))
                    continue
                os.rename(file_path + '.src', file_path)
        except Exception:
            current_app.logger.exception(u"FAILED TO REPLACE WATERMARKED DOCS")

        if current_app.config.get('PROD', False):
            appcraft.send_stat(batch.batch_type + '_payment_received',
                               batch._owner, batch.id, batch.batch_type,
                               int(invoice_id))
            mixpanel_metrics.send_stat(batch.batch_type + '_payment_received',
                                       batch._owner, batch.id,
                                       batch.batch_type)

        try:
            if batch.batch_type == DocumentBatchTypeEnum.DBT_NEW_LLC:
                BatchManager.send_batch_docs_to_user(batch_id,
                                                     current_app.config)
        except Exception:
            logger.exception(u"Failed to send documents to user.")
    elif subs_type:
        user_subs = PaymentSubscriptionObject.query.filter(
            PaymentSubscriptionObject.user == user,
            PaymentSubscriptionObject.end_dt.__ge__(datetime.utcnow()))

        if not user_subs.count():
            end_date = datetime.utcnow()
            if subs_type == 'subscription_3':
                end_date += relativedelta(months=3)
            elif subs_type == 'subscription_1':
                end_date += relativedelta(months=1)

            new_subs = PaymentSubscriptionObject(pay_info=pay_info,
                                                 created=datetime.utcnow(),
                                                 end_dt=end_date,
                                                 user=user,
                                                 type=subs_type)
            sqldb.session.add(new_subs)
            sqldb.session.commit()

            from fw.async_tasks import not_paid_check_send

            not_paid_check_send.make_all_user_fin_batch_paid_and_replace_watermarked_docs_with_normal.delay(
                user_id=user.id)

    current_app.logger.info(u"yad - success")
    return _xml_resp(success_result)
Exemplo n.º 17
0
celery.config_from_envvar('CELERY_CONFIG_MODULE')

@celery.task()
def get_policy_info_async(policy_series, policy_number, event_data, batch_id, async=True, logger = None):
    from fw.documents.batch_manager import BatchManager
    app = celery.conf['flask_app']()
    logger = logger or celery.log.get_default_logger()
    if not policy_number or not policy_series:
        return
    with app.app_context():
        batch = DocumentBatchDbObject.query.filter_by(id=batch_id, deleted=False).scalar()
        try:
            try:
                result_data = app.external_tools.check_car_policy(policy_series, policy_number, timeout=2.0 if not async else 20.0)
            except requests.exceptions.RequestException, ex:
                BatchManager.handle_event(batch_id, "on_policy_info_receive_timeout", event_data, logger, config=app.config)
                return False

            if not result_data:
                BatchManager.handle_event(batch_id, "on_policy_info_receive_fail", event_data, logger, config=app.config)
                return False

            logger.info(u"get policy info returned: %s" % json.dumps(result_data, default=lambda x: unicode(x)))
            insurance_name = result_data.get('insCompanyName', u"")
            if not insurance_name:
                raise Exception(u"Failed to get insurance: empty name returned")

            insurance = CarAssurance.query.filter_by(connection_name=insurance_name).first()
            if not insurance:
                raise Exception(u"Failed to get insurance from db by name: %s" % insurance_name)
            insurance_id = insurance.id