Пример #1
0
    def response_to_json(self, hpp_response, encoded=False):
        """
        Method produces JSON from HppResponse object.
        Carries out the following actions:
            * Generate security hash
            * Base64 encodes inputs
            * Serialises response object to JSON
        :param hpp_response: HppResponse
        :param encoded: bool
        :return: string
        """
        logger.info("Converting HppResponse to JSON.")

        # generate hash
        logger.debug("Generating hash.")
        self.get_response_hash(hpp_response)

        logger.debug("Encoding to base64 and converting HppResponse to JSON.")
        try:
            json_response = JsonUtils.to_json(hpp_response,
                                              self.encoding_charset, encoded)
        except Exception as e:
            error_msg = "Exception json encoding HPP response"
            logger.error("{}: {}".format(error_msg, e))
            raise SdkError(error_msg, e)
        return json_response
Пример #2
0
    def request_to_json(self, hpp_request, encoded=True):
        """
        Method produces JSON from HppRequest object.
        Carries out the following actions:
            * Generate security hash
            * Validates inputs
            * Base64 encodes inputs
            * Serialises request object to JSON
        :param hpp_request: HppRequest
        :param encoded: bool
        :return: string
        """
        logger.info("Converting HppRequest to JSON.")

        # generate hash
        logger.debug("Generating hash.")

        # set request object and calculate hash from it
        self.get_request_hash(hpp_request)

        # validate request
        logger.debug("Validating request.")
        attr.validate(hpp_request)

        logger.debug("Encoding to base64 and converting HppRequest to JSON.")
        try:
            json_request = JsonUtils.to_json(hpp_request,
                                             self.encoding_charset, encoded)
        except Exception as e:
            error_msg = "Exception json encoding HPP request"
            logger.error("{}: {}".format(error_msg, e))
            raise SdkError(error_msg, e)
        return json_request
    def test_to_json_hpp_card_storage_request_supp_data(
            self, json_hpp_card_storage_request_supp_data):
        """
        Test CardStorageRequest with supplementary data () generated to JSON
        :param json_hpp_card_storage_request_supp_data: CardStorageRequest
        """
        assert isinstance(json_hpp_card_storage_request_supp_data,
                          CardStorageRequest)
        json_base64_request = json.loads(
            JsonUtils.to_json(json_hpp_card_storage_request_supp_data, 'utf-8',
                              True))
        assert isinstance(json_base64_request, dict)

        assert json_base64_request.get('AUTO_SETTLE_FLAG') == 'MQ=='
        assert json_base64_request.get('CURRENCY') == 'RVVS'
        assert json_base64_request.get('PROD_ID') == 'UHJvZHVjdElE'
        assert json_base64_request.get('SHIPPING_CO') == 'SVJFTEFORA=='
        assert json_base64_request.get('MERCHANT_ID') == 'TWVyY2hhbnRJRA=='
        assert json_base64_request.get('ORDER_ID') == 'T3JkZXJJRA=='
        assert json_base64_request.get('PAYER_EXIST') == 'MQ=='
        assert json_base64_request.get('PMT_REF') == 'UGF5bWVudFJlZg=='
        assert json_base64_request.get('AMOUNT') == 'MTAw'
        assert json_base64_request.get('CUST_NUM') == 'MTIzNDU2'
        assert json_base64_request.get('COMMENT2') == 'Q29tbWVudCBUd28='
        assert json_base64_request.get('OFFER_SAVE_CARD') == 'MQ=='
        assert json_base64_request.get(
            'SHA1HASH'
        ) == 'NWQ4ZjA1YWJkNjE4ZTUwZGI0ODYxYTYxY2M5NDAxMTI3ODY0NzRjZg=='
        assert json_base64_request.get('SHIPPING_CODE') == 'NTZ8OTg3'
        assert json_base64_request.get(
            'CARD_PAYMENT_BUTTON') == 'U3VibWl0IFBheW1lbnQ='
        assert json_base64_request.get('HPP_LANG') == 'RU4='
        assert json_base64_request.get('PAYER_REF') == 'UGF5ZXJSZWY='
        assert json_base64_request.get('ACCOUNT') == 'bXlBY2NvdW50'
        comment = 'YS16IEEtWiAwLTkgJyAiLCArIOKAnOKAnSAuXyAtICYgXCAvIEAgISA/ICUgKCApKiA6IMKjICQgJiDigqwgIyBbIF0gfCA9'
        assert json_base64_request.get('COMMENT1') == comment
        assert json_base64_request.get('VAR_REF') == 'VmFyaWFibGVSZWY='
        assert json_base64_request.get('BILLING_CODE') == 'MTIzfDU2'
        assert json_base64_request.get('CARD_STORAGE_ENABLE') == 'MQ=='
        assert json_base64_request.get('TIMESTAMP') == 'MjA5OTAxMDExMjAwMDA='
        assert json_base64_request.get('BILLING_CO') == 'SVJFTEFORA=='
        assert json_base64_request.get('SUPPLEMENTARY_1') == 'U3VwcDE='
        assert json_base64_request.get('SUPPLEMENTARY_2') == 'U3VwcDI='
        assert json_base64_request.get('SUPPLEMENTARY_3') == 'U3VwcDM='
        assert json_base64_request.get('SUPPLEMENTARY_4') == 'U3VwcDQ='
    def test_to_json_hpp_card_storage_request(
            self, json_hpp_card_storage_request_valid):
        """
        Test PaymentRequest generated to JSON
        :param json_hpp_card_storage_request_valid: PaymentRequest
        """
        assert isinstance(json_hpp_card_storage_request_valid,
                          CardStorageRequest)

        json_base64_request = json.loads(
            JsonUtils.to_json(json_hpp_card_storage_request_valid, 'utf-8',
                              True))
        assert isinstance(json_base64_request, dict)
        assert json_base64_request.get('BILLING_CO') == 'SVJFTEFORA=='
        assert json_base64_request.get('ACCOUNT') == 'bXlBY2NvdW50'
        assert json_base64_request.get('VAR_REF') == 'VmFyaWFibGVSZWY='
        assert json_base64_request.get('SHIPPING_CO') == 'SVJFTEFORA=='
        comment = 'YS16IEEtWiAwLTkgJyAiLCArIOKAnOKAnSAuXyAtICYgXCAvIEAgISA/ICUgKCApKiA6IMKjICQgJiDigqwgIyBbIF0gfCA9I' \
                  'DvDgMOBw4LDg8OEw4XDhsOHw4jDicOKw4vDjMONw47Dj8OQw5HDksOTw5TDlcOWw5fDmMOZw5rDm8Ocw53DnsOfw6DDocOiw6' \
                  'PDpMOlw6bDp8Oow6nDqsOrw6zDrcOuw6/DsMOxw7LDs8O0w7XDtsO3w7jCpMO5w7rDu8O8w73DvsO/xZLFvcWhxZPFvsW4wqU='
        assert json_base64_request.get('COMMENT1') == comment
        assert json_base64_request.get('COMMENT2') == 'Q29tbWVudCBUd28='
        assert json_base64_request.get('CURRENCY') == 'RVVS'
        assert json_base64_request.get('PAYER_EXIST') == 'MQ=='
        assert json_base64_request.get('MERCHANT_ID') == 'TWVyY2hhbnRJRA=='
        assert json_base64_request.get(
            'CARD_PAYMENT_BUTTON') == 'U3VibWl0IFBheW1lbnQ='
        assert json_base64_request.get('ORDER_ID') == 'T3JkZXJJRA=='
        assert json_base64_request.get('SHIPPING_CODE') == 'NTZ8OTg3'
        assert json_base64_request.get('AMOUNT') == 'MTAw'
        assert json_base64_request.get(
            'SHA1HASH'
        ) == 'NWQ4ZjA1YWJkNjE4ZTUwZGI0ODYxYTYxY2M5NDAxMTI3ODY0NzRjZg=='
        assert json_base64_request.get('AUTO_SETTLE_FLAG') == 'MQ=='
        assert json_base64_request.get('OFFER_SAVE_CARD') == 'MQ=='
        assert json_base64_request.get('BILLING_CODE') == 'MTIzfDU2'
        assert json_base64_request.get('PROD_ID') == 'UHJvZHVjdElE'
        assert json_base64_request.get('PAYER_REF') == 'UGF5ZXJSZWY='
        assert json_base64_request.get('CARD_STORAGE_ENABLE') == 'MQ=='
        assert json_base64_request.get('TIMESTAMP') == 'MjA5OTAxMDExMjAwMDA='
        assert json_base64_request.get('CUST_NUM') == 'MTIzNDU2'
        assert json_base64_request.get('PMT_REF') == 'UGF5bWVudFJlZg=='
        assert json_base64_request.get('HPP_LANG') == 'RU4='