Example #1
0
 def test_complaint_amount_str(self):
     self.assertTrue(
         check_complaint_value_amount({"value": {
             "amount": "100"
         }}, {"amount": 100.0}))
Example #2
0
 def test_payment_amount_float(self):
     self.assertTrue(
         check_complaint_value_amount({"value": {
             "amount": 100
         }}, {"amount": 100.0}))
Example #3
0
 def test_no_payment_amount(self):
     self.assertFalse(
         check_complaint_value_amount({"value": {
             "amount": 100
         }}, {}))
Example #4
0
 def test_no_complaint_value(self):
     self.assertFalse(check_complaint_value_amount({}, {"amount": 100}))
Example #5
0
 def test_invalid_amount(self):
     self.assertFalse(
         check_complaint_value_amount({"value": {
             "amount": 90
         }}, {"amount": 100}))
Example #6
0
def process_payment_complaint_data(self,
                                   complaint_params,
                                   payment_data,
                                   cookies=None,
                                   *args,
                                   **kwargs):
    tender_id = complaint_params.get("tender_id")
    item_type = complaint_params.get("item_type")
    item_id = complaint_params.get("item_id")
    complaint_id = complaint_params.get("complaint_id")
    client_request_id = uuid4().hex
    try:
        response = request_cdb_complaint_data(
            tender_id=tender_id,
            item_type=item_type,
            item_id=item_id,
            complaint_id=complaint_id,
            client_request_id=client_request_id,
            cookies=cookies,
        )
    except RETRY_REQUESTS_EXCEPTIONS as exc:
        countdown = get_exponential_request_retry_countdown(self)
        logger.exception("Request failed: {}, next retry in {} seconds".format(
            str(exc), countdown),
                         payment_data=payment_data,
                         task=self,
                         extra={
                             "MESSAGE_ID": PAYMENTS_GET_COMPLAINT_EXCEPTION,
                             "CDB_CLIENT_REQUEST_ID": client_request_id,
                         })
        raise self.retry(countdown=countdown, exc=exc)

    cookies = cookies or {}
    cookies.update(response.cookies.get_dict())

    if response.status_code != 200:
        logger_method = get_task_retry_logger_method(self, logger)
        logger_method(
            "Unexpected status code {} while getting complaint {}".format(
                response.status_code, complaint_id),
            payment_data=payment_data,
            task=self,
            extra={
                "MESSAGE_ID": PAYMENTS_GET_COMPLAINT_CODE_ERROR,
                "STATUS_CODE": response.status_code,
            })
        if response.status_code == 412:
            raise self.retry(countdown=0,
                             kwargs=dict(
                                 complaint_params=complaint_params,
                                 payment_data=payment_data,
                                 cookies=cookies,
                             ))
        countdown = get_exponential_request_retry_countdown(self, response)
        raise self.retry(countdown=countdown)
    else:
        logger.info("Successfully retrieved complaint {}".format(complaint_id),
                    payment_data=payment_data,
                    task=self,
                    extra={"MESSAGE_ID": PAYMENTS_GET_COMPLAINT_SUCCESS})

    complaint_data = response.json()["data"]

    if not check_complaint_status(complaint_data):
        logger.warning("Invalid complaint status: {}".format(
            complaint_data["status"]),
                       payment_data=payment_data,
                       task=self,
                       extra={"MESSAGE_ID": PAYMENTS_INVALID_STATUS})
        return

    if not check_complaint_value(complaint_data):
        logger.info(
            "Invalid complaint value amount or currency for complaint {}".
            format(complaint_id),
            payment_data=payment_data,
            task=self,
            extra={"MESSAGE_ID": PAYMENTS_INVALID_COMPLAINT_VALUE})
        return

    value = complaint_data.get("value", {})

    if not check_complaint_value_amount(complaint_data, payment_data):
        logger.warning(
            "Invalid payment amount for complaint {}: {} not equal {}".format(
                complaint_id, payment_data.get("amount"), value.get("amount")),
            payment_data=payment_data,
            task=self,
            extra={"MESSAGE_ID": PAYMENTS_INVALID_AMOUNT})
        process_payment_complaint_patch.apply_async(
            kwargs=dict(payment_data=payment_data,
                        complaint_params=complaint_params,
                        patch_data={"status": STATUS_COMPLAINT_MISTAKEN},
                        cookies=cookies))
        return

    if not check_complaint_value_currency(complaint_data, payment_data):
        logger.warning(
            "Invalid payment amount for complaint {}: {} not equal {}".format(
                complaint_id, payment_data.get("currency"),
                value.get("currency")),
            payment_data=payment_data,
            task=self,
            extra={"MESSAGE_ID": PAYMENTS_INVALID_CURRENCY})
        process_payment_complaint_patch.apply_async(
            kwargs=dict(payment_data=payment_data,
                        complaint_params=complaint_params,
                        patch_data={"status": STATUS_COMPLAINT_MISTAKEN},
                        cookies=cookies))
        return

    logger.info(
        "Successfully matched payment for complaint {}".format(complaint_id),
        payment_data=payment_data,
        task=self,
        extra={"MESSAGE_ID": PAYMENTS_VALID_PAYMENT})

    process_payment_complaint_patch.apply_async(
        kwargs=dict(payment_data=payment_data,
                    complaint_params=complaint_params,
                    patch_data={"status": STATUS_COMPLAINT_PENDING},
                    cookies=cookies))