Пример #1
0
    def offline_refund(self, refund_params):
        """
        offline payment refund API, refund fee should not be greater than bill total fee;
        need_approval is not allowed for offline refund
        refer to restful API https://beecloud.cn/doc/?index=rest-api-offline #6
        :param refund_params: beecloud.entity.BCRefundReqParams
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('offline_refund')

        attach_app_sign(refund_params, BCReqType.REFUND, self.bc_app)
        tmp_resp = http_post(self._offline_refund_url(), refund_params, self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')
            setattr(bc_result, "refund_result", resp_dict.get('refund_result'))

        return bc_result
Пример #2
0
    def offline_revert(self, bill_no, channel=None, method='REVERT'):
        """
        offline payment revert API, WX_NATIVE is not supported
        refer to restful API https://beecloud.cn/doc/?index=rest-api-offline #4
        :param bill_no: bill number
        :param channel: bill payment channel like ALI_SCAN
        :param method: currently only support 'REVERT'
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('offline_revert')

        tmp_obj = _TmpObject()
        if channel:
            setattr(tmp_obj, 'channel', channel)
        setattr(tmp_obj, 'method', method)

        attach_app_sign(tmp_obj, BCReqType.PAY, self.bc_app)
        tmp_resp = http_post(self._offline_pay_url() + '/' + bill_no, tmp_obj, self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            setattr(bc_result, "revert_status", resp_dict.get('revert_status'))

        return bc_result
Пример #3
0
    def _bill_transfer(self, url, transfer_params):
        attach_app_sign(transfer_params, BCReqType.TRANSFER, self.bc_app)
        req_dict = {}
        for k, v in transfer_params.__dict__.items():
            if v:
                if k == 'redpack_info':  # WX_REDPACK
                    req_dict[k] = obj_to_dict(v)
                elif k == 'transfer_data':  # batch_transfer
                    req_dict[k] = [
                        obj_to_dict(item)
                        for item in transfer_params.transfer_data
                    ]
                else:
                    req_dict[k] = v

        tmp_resp = http_post(url, req_dict, self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')
            # for ali
            bc_result.url = resp_dict.get('url')

        return bc_result
Пример #4
0
    def refund(self, refund_params):
        """
        refund API, refund fee should not be greater than bill total fee;
        need_approval is for pre refund, you have to call [audit_pre_refunds] later on to execute real refund
        if the bill is paid with ali, you have to input your password on the returned url page
        refer to restful API https://beecloud.cn/doc/ #3
        :param refund_params: beecloud.entity.BCRefundReqParams
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('refund')

        attach_app_sign(refund_params, BCReqType.REFUND, self.bc_app)
        tmp_resp = http_post(self._bill_refund_url(), refund_params, self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')
            # url will be returned if bill is refunded and channel is in (ALI_APP, ALI_WEB, ALI_QRCODE)
            bc_result.url = resp_dict.get('url')

        return bc_result
Пример #5
0
    def query_offline_bill_status(self, bill_no, channel=None):
        """
        query offline bill status
        refer to https://beecloud.cn/doc/?index=rest-api-offline #3
        :param bill_no: bill number
        :param channel: bill payment channel like WX_SCAN
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('query_offline_bill_status')

        query_params = _TmpObject()
        setattr(query_params, 'bill_no', bill_no)
        if channel:
            setattr(query_params, 'channel', channel)
        attach_app_sign(query_params, BCReqType.QUERY, self.bc_app)
        url = get_rest_root_url() + 'rest/offline/bill/status'
        tmp_resp = http_post(url, query_params, self.bc_app.timeout)
        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()

        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            setattr(bc_result, 'pay_result', resp_dict.get('pay_result'))

        return bc_result
Пример #6
0
    def _bill_transfer(self, url, transfer_params):
        attach_app_sign(transfer_params, BCReqType.TRANSFER, self.bc_app)
        req_dict = {}
        for k, v in transfer_params.__dict__.items():
            if v:
                if k == 'redpack_info':     # WX_REDPACK
                    req_dict[k] = obj_to_dict(v)
                elif k == 'transfer_data':      # batch_transfer
                    req_dict[k] = [obj_to_dict(item) for item in transfer_params.transfer_data]
                else:
                    req_dict[k] = v

        tmp_resp = http_post(url, req_dict, self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')
            # for ali
            bc_result.url = resp_dict.get('url')

        return bc_result
Пример #7
0
def add_merchant_user(bc_app, buyer_id):
    """
    add merchant user
    :param bc_app: beecloud.entity.BCApp
    :param buyer_id: merchant unique user id, can be used as buyer id for bill
    :return: beecloud.entity.BCResult
    """
    req_param = _TmpObject()
    req_param.buyer_id = buyer_id
    attach_app_sign(req_param, BCReqType.PAY, bc_app)

    url = get_rest_root_url() + 'rest/user'

    tmp_resp = http_post(url, req_param, bc_app.timeout)

    # if err encountered, [0] equals 0
    if not tmp_resp[0]:
        return tmp_resp[1]

    # [1] contains result dict
    resp_dict = tmp_resp[1]

    bc_result = BCResult()
    set_common_attr(resp_dict, bc_result)

    return bc_result
Пример #8
0
def batch_add_merchant_users(bc_app, merchant, buyer_ids):
    """
    add merchant user
    :param bc_app: beecloud.entity.BCApp
    :param merchant: merchant account
    :param buyer_ids: merchant unique user id list
    :return: beecloud.entity.BCResult
    """
    req_param = _TmpObject()
    req_param.email = merchant
    req_param.buyer_ids = buyer_ids
    attach_app_sign(req_param, BCReqType.PAY, bc_app)

    url = get_rest_root_url() + 'rest/users'

    tmp_resp = http_post(url, req_param, bc_app.timeout)

    # if err encountered, [0] equals 0
    if not tmp_resp[0]:
        return tmp_resp[1]

    # [1] contains result dict
    resp_dict = tmp_resp[1]

    bc_result = BCResult()
    set_common_attr(resp_dict, bc_result)

    return bc_result
Пример #9
0
    def offline_refund(self, refund_params):
        """
        offline payment refund API, refund fee should not be greater than bill total fee;
        need_approval is not allowed for offline refund
        refer to restful API https://beecloud.cn/doc/?index=rest-api-offline #6
        :param refund_params: beecloud.entity.BCRefundReqParams
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('offline_refund')

        attach_app_sign(refund_params, BCReqType.REFUND, self.bc_app)
        tmp_resp = http_post(self._offline_refund_url(), refund_params,
                             self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')
            setattr(bc_result, "refund_result", resp_dict.get('refund_result'))

        return bc_result
Пример #10
0
    def query_offline_bill_status(self, bill_no, channel=None):
        """
        query offline bill status
        refer to https://beecloud.cn/doc/?index=rest-api-offline #3
        :param bill_no: bill number
        :param channel: bill payment channel like WX_SCAN
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('query_offline_bill_status')

        query_params = _TmpObject()
        setattr(query_params, 'bill_no', bill_no)
        if channel:
            setattr(query_params, 'channel', channel)
        attach_app_sign(query_params, BCReqType.QUERY, self.bc_app)
        url = get_rest_root_url() + 'rest/offline/bill/status'
        tmp_resp = http_post(url, query_params, self.bc_app.timeout)
        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()

        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            setattr(bc_result, 'pay_result', resp_dict.get('pay_result'))

        return bc_result
Пример #11
0
    def offline_revert(self, bill_no, channel=None, method='REVERT'):
        """
        offline payment revert API, WX_NATIVE is not supported
        refer to restful API https://beecloud.cn/doc/?index=rest-api-offline #4
        :param bill_no: bill number
        :param channel: bill payment channel like ALI_SCAN
        :param method: currently only support 'REVERT'
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('offline_revert')

        tmp_obj = _TmpObject()
        if channel:
            setattr(tmp_obj, 'channel', channel)
        setattr(tmp_obj, 'method', method)

        attach_app_sign(tmp_obj, BCReqType.PAY, self.bc_app)
        tmp_resp = http_post(self._offline_pay_url() + '/' + bill_no, tmp_obj,
                             self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            setattr(bc_result, "revert_status", resp_dict.get('revert_status'))

        return bc_result
Пример #12
0
    def refund(self, refund_params):
        """
        refund API, refund fee should not be greater than bill total fee;
        need_approval is for pre refund, you have to call [audit_pre_refunds] later on to execute real refund
        if the bill is paid with ali, you have to input your password on the returned url page
        refer to restful API https://beecloud.cn/doc/?index=rest-api #3
        :param refund_params: beecloud.entity.BCRefundReqParams
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('refund')

        attach_app_sign(refund_params, BCReqType.REFUND, self.bc_app)
        tmp_resp = http_post(self._bill_refund_url(), refund_params,
                             self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')
            # url will be returned if bill is refunded and channel is in (ALI_APP, ALI_WEB, ALI_QRCODE)
            bc_result.url = resp_dict.get('url')

        return bc_result
Пример #13
0
    def pay(self, pay_params):
        """
        payment API, different channels have different requirements for request params
        and the return params varies.
        refer to restful API https://beecloud.cn/doc/?index=rest-api #2
        :param pay_params: beecloud.entity.BCPayReqParams
        :return: beecloud.entity.BCResult
        """
        self._add_sdk_version(pay_params)

        attach_app_sign(pay_params, BCReqType.PAY, self.bc_app)
        tmp_resp = http_post(self._bill_pay_url(), pay_params,
                             self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]

        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')
            # most channel will return url or html
            if resp_dict.get('html'):
                bc_result.html = resp_dict.get('html')
            if resp_dict.get('url'):
                bc_result.url = resp_dict.get('url')
            # WX_NATIVE or BC_NATIVE or BC_ALI_QRCODE
            if resp_dict.get('code_url'):
                bc_result.code_url = resp_dict.get('code_url')
            # WX_JSAPI
            if pay_params.channel == BCChannelType.WX_JSAPI:
                bc_result.app_id = resp_dict.get('app_id')
                bc_result.package = resp_dict.get('package')
                bc_result.nonce_str = resp_dict.get('nonce_str')
                bc_result.timestamp = resp_dict.get('timestamp')
                bc_result.pay_sign = resp_dict.get('pay_sign')
                bc_result.sign_type = resp_dict.get('sign_type')

        return bc_result
Пример #14
0
    def pay(self, pay_params):
        """
        payment API, different channels have different requirements for request params
        and the return params varies.
        refer to restful API https://beecloud.cn/doc/?index=rest-api #2
        :param pay_params: beecloud.entity.BCPayReqParams
        :return: beecloud.entity.BCResult
        """
        self._add_sdk_version(pay_params)

        attach_app_sign(pay_params, BCReqType.PAY, self.bc_app)
        tmp_resp = http_post(self._bill_pay_url(), pay_params, self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]

        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')
            # most channel will return url or html
            if resp_dict.get('html'):
                bc_result.html = resp_dict.get('html')
            if resp_dict.get('url'):
                bc_result.url = resp_dict.get('url')
            # WX_NATIVE or BC_NATIVE or BC_ALI_QRCODE
            if resp_dict.get('code_url'):
                bc_result.code_url = resp_dict.get('code_url')
            # WX_JSAPI
            if pay_params.channel == BCChannelType.WX_JSAPI:
                bc_result.app_id = resp_dict.get('app_id')
                bc_result.package = resp_dict.get('package')
                bc_result.nonce_str = resp_dict.get('nonce_str')
                bc_result.timestamp = resp_dict.get('timestamp')
                bc_result.pay_sign = resp_dict.get('pay_sign')
                bc_result.sign_type = resp_dict.get('sign_type')

        return bc_result
Пример #15
0
    def international_pay(self, pay_params):
        """
        international pay -- PayPal
        refer to https://github.com/beecloud/beecloud-rest-api/tree/master/international #2
        credit_card_info should be type of beecloud.entity.BCPayPalCreditCard
        :param pay_params: beecloud.entity.BCInternationalPayParams
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('international_pay')

        attach_app_sign(pay_params, BCReqType.PAY, self.bc_app)
        req_dict = {}
        for k, v in pay_params.__dict__.items():
            if v:
                if k == 'credit_card_info':
                    req_dict[k] = obj_to_dict(v)
                else:
                    req_dict[k] = v

        tmp_resp = http_post(self._international_pay_url(), req_dict,
                             self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')
            # url is returned when channel is PAYPAL_PAYPAL
            bc_result.url = resp_dict.get('url')
            # credit_card_id is returned when channel is PAYPAL_CREDITCARD
            bc_result.credit_card_id = resp_dict.get('credit_card_id')

        return bc_result
Пример #16
0
    def international_pay(self, pay_params):
        """
        international pay -- PayPal
        refer to https://github.com/beecloud/beecloud-rest-api/tree/master/international #2
        credit_card_info should be type of beecloud.entity.BCPayPalCreditCard
        :param pay_params: beecloud.entity.BCInternationalPayParams
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('international_pay')

        attach_app_sign(pay_params, BCReqType.PAY, self.bc_app)
        req_dict = {}
        for k, v in pay_params.__dict__.items():
            if v:
                if k == 'credit_card_info':
                    req_dict[k] = obj_to_dict(v)
                else:
                    req_dict[k] = v

        tmp_resp = http_post(self._international_pay_url(), req_dict, self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]
        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')
            # url is returned when channel is PAYPAL_PAYPAL
            bc_result.url = resp_dict.get('url')
            # credit_card_id is returned when channel is PAYPAL_CREDITCARD
            bc_result.credit_card_id = resp_dict.get('credit_card_id')

        return bc_result
Пример #17
0
    def offline_pay(self, pay_params):
        """
        offline payment API, different channels have different requirements for request params
        and the return params varies.
        refer to restful API https://beecloud.cn/doc/?index=rest-api-offline #2
        :param pay_params: beecloud.entity.BCPayReqParams
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('offline_pay')

        self._add_sdk_version(pay_params)

        attach_app_sign(pay_params, BCReqType.PAY, self.bc_app)
        tmp_resp = http_post(self._offline_pay_url(), pay_params,
                             self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]

        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')

            if resp_dict.get('pay_result') is not None:
                setattr(bc_result, 'pay_result', resp_dict.get('pay_result'))
            if resp_dict.get('code_url'):
                setattr(bc_result, 'code_url', resp_dict.get('code_url'))
            if resp_dict.get('channel_type'):
                setattr(bc_result, 'channel_type',
                        resp_dict.get('channel_type'))

        return bc_result
Пример #18
0
    def offline_pay(self, pay_params):
        """
        offline payment API, different channels have different requirements for request params
        and the return params varies.
        refer to restful API https://beecloud.cn/doc/?index=rest-api-offline #2
        :param pay_params: beecloud.entity.BCPayReqParams
        :return: beecloud.entity.BCResult
        """
        if self.bc_app.is_test_mode:
            return report_not_supported_err('offline_pay')

        self._add_sdk_version(pay_params)

        attach_app_sign(pay_params, BCReqType.PAY, self.bc_app)
        tmp_resp = http_post(self._offline_pay_url(), pay_params, self.bc_app.timeout)

        # if err encountered, [0] equals 0
        if not tmp_resp[0]:
            return tmp_resp[1]

        # [1] contains result dict
        resp_dict = tmp_resp[1]

        bc_result = BCResult()
        set_common_attr(resp_dict, bc_result)

        if not bc_result.result_code:
            bc_result.id = resp_dict.get('id')

            if resp_dict.get('pay_result') is not None:
                setattr(bc_result, 'pay_result', resp_dict.get('pay_result'))
            if resp_dict.get('code_url'):
                setattr(bc_result, 'code_url', resp_dict.get('code_url'))
            if resp_dict.get('channel_type'):
                setattr(bc_result, 'channel_type', resp_dict.get('channel_type'))

        return bc_result