Ejemplo n.º 1
0
    def refund_web(self, trade_no, refund_no, order_no, total_fee, refund_fee):
        """
        公众号退款
        :param trade_no:
        :param refund_no:
        :param order_no:
        :param total_fee:
        :param refund_fee:
        :return:
        """
        nonce_str = self.__create_nonce_str()
        prepay_data = dict(
            appid=self._app_id,
            mch_id=self.__partner_id,
            nonce_str=nonce_str,
            transaction_id=trade_no,
            out_trade_no=order_no,
            total_fee=total_fee,
            out_refund_no=refund_no,
            refund_fee=refund_fee,
            op_user_id=self.__partner_id
        )
        sign = self.make_sign(**prepay_data).upper()
        prepay_data["sign"] = sign
        xml_dict = {"xml": prepay_data}
        prepay_xml = xml_dict_manager.unparse(xml_dict)
        dir_path = os.getcwd()
        cert_file = dir_path + self.__ssl_cert_path
        key_file = dir_path + self.__ssh_key_path
        url = 'https://api.mch.weixin.qq.com/secapi/pay/refund'
        refund_logger_manager.logger.info(u"[WX Refund Input] url:{}, prepay_xml: {}".format(
            url, prepay_xml
        ))

        rep_xml = self._post(
            url=url,
            data=prepay_xml.encode('utf-8'),
            cert=(cert_file, key_file)
        )

        refund_logger_manager.logger.info(u"[WX Refund Out] rep_xml: {}".format(rep_xml))

        notify = xml_dict_manager.parse(rep_xml).get("xml", {})
        old_sign = notify.get("sign")
        if not old_sign:
            return False, None
        sign_param = self.params_filter(**notify)
        sign_string = '{sign_param}&key={sign_key}'.format(
            sign_param=sign_param, sign_key=self.__partner_key)
        new_sign = self.encrypt_sign(sign_string).upper()

        if new_sign == old_sign:
            return True, notify
        else:
            return False, None
Ejemplo n.º 2
0
    def wap_pay_v3(self, order_name, notify_url, order_no, client_ip, total_fee, user_open_id=None,
                   trade_type=None, product_id=None):
        """
        v3版本, 公众号支付
        :param order_name:
        :param notify_url:
        :param order_no:
        :param client_ip:
        :param total_fee:
        :param user_open_id:
        :return:
        """
        if trade_type == "APP" and not product_id:
            raise NeedParamError(u"product_id 参数不能为空。")

        if trade_type == "JSAPI" and not user_open_id:
            raise NeedParamError(u"user_open_id参数不能为空")

        nonce_str = self.__create_nonce_str()
        prepay_data = dict(
            appid=self._app_id,
            mch_id=self.__partner_id,
            nonce_str=nonce_str,
            body=order_name,
            out_trade_no=order_no,
            total_fee=total_fee,
            spbill_create_ip=client_ip,
            notify_url=notify_url,
            trade_type=trade_type
        )
        if trade_type == "APP":
            prepay_data["product_id"] = product_id

        elif trade_type == "JSAPI":
            prepay_data["device_info"] = "WEB"
            prepay_data["openid"] = user_open_id

        sign = self.make_sign(**prepay_data).upper()
        prepay_data["sign"] = sign
        xml_dict = {"xml": prepay_data}
        prepay_xml = xml_dict_manager.unparse(xml_dict)
        rep_xml = self._post(url=self.__uri_v3, data=prepay_xml.encode('utf-8'))
        rep_dict = xml_dict_manager.parse(rep_xml)
        prepay_id = rep_dict.get("xml", {}).get('prepay_id')
        now_time = int(time.time())
        if trade_type == "APP":
            prepay = {
                'appid': self._app_id,
                'prepayid': prepay_id,
                'noncestr': nonce_str,
                'timestamp': now_time,
                'package': 'Sign=WXPay',
                'partnerid': self.__partner_id
            }
        else:
            prepay = {'timeStamp': now_time,
                      'nonceStr': nonce_str,
                      'appId': self._app_id,
                      'package': 'prepay_id={}'.format(prepay_id),
                      'signType': 'MD5'
                      }
        sign = self.make_sign(**prepay).upper()
        prepay['sign'] = sign
        return prepay