def prepay(self, **kwargs): """微信支付预付款下单,对接口进行封装。 :param body: 订单名称 :param detail: 订单详情 :param out_trade_no: 订单号 :param openid: 用户OpenID :param type: 订单分类,默认为normal :rtype: 微信接口返回结果 """ config = kwargs.pop('config', {}) self.config['appid'] = config.get('appid') self.config['mchid'] = config.get('mchid') self.config['key'] = config.get('key') type = kwargs.pop('type', 'normal') kwargs.setdefault('appid', self.get_config('appid', config=config)) kwargs.setdefault('mch_id', self.get_config('mchid', config=config)) kwargs.setdefault('spbill_create_ip', get_ip()) kwargs.setdefault('notify_url', url_for(self.endpoint, type=type, _external=True)) kwargs.setdefault('trade_type', 'JSAPI') kwargs.setdefault('body', '微信支付') kwargs.setdefault('out_trade_no', 'wxtest') kwargs.setdefault('total_fee', 100) kwargs.setdefault('nonce_str', randstr(32)) kwargs.setdefault('sign', self.sign(config=config, **kwargs)) data = dicttoxml(kwargs, custom_root='xml', attr_type=False) try: xml = requests.post(self.PREPAY_URL, data=data).content return self.xml2dict(xml) except Exception, e: current_app.logger.error(traceback.format_exc()) return dict(return_code='ERROR', return_msg=str(e))
def refund(self, **kwargs): """微信支付申请退款,对接口进行封装。 :param out_trade_no: 订单号 :param out_refund_no: 退款记录ID :param total_fee: 订单金额 :param refund_fee: 退款金额 :rtype: 微信接口返回结果 """ if 'out_trade_no' not in kwargs: raise ValueError('out_trade_no is required.') kwargs.setdefault('appid', self.get_config('appid')) kwargs.setdefault('mch_id', self.get_config('mchid')) kwargs.setdefault('device_info', 'WEB') kwargs.setdefault('out_refund_no', kwargs.get('out_trade_no')) kwargs.setdefault('total_fee', 1) kwargs.setdefault('refund_fee', kwargs.get('total_fee')) kwargs.setdefault('refund_fee_type', 'CNY') kwargs.setdefault('op_user_id', self.get_config('mchid')) kwargs.setdefault('nonce_str', randstr(32)) kwargs.setdefault('sign', self.sign(**kwargs)) data = dicttoxml(kwargs, custom_root='xml', attr_type=False) try: xml = requests.post(self.REFUND_URL, data=data, cert=self.get_config('cert')).content return self.xml2dict(xml) except Exception, e: current_app.logger.error(traceback.format_exc()) return dict(return_code='ERROR', return_msg=str(e))
def send_red_pack(self, **kwargs): """微信支付发红包,对接口进行封装。 :param total_amount: 金额总数 :param total_num: 红包数量 :param wishing: 祝福语 :param act_name: 活动名称 :param remark: 备注 :param re_openid: 用户OpenID :rtype: 微信接口返回结果 """ kwargs.setdefault('wxappid', self.config.get('appid')) kwargs.setdefault('mch_id', self.config.get('mchid')) kwargs.setdefault('client_ip', self.config.get('client_ip')) kwargs.setdefault('send_name', self.config.get('send_name', '小酷科技')) kwargs.setdefault('total_amount', 100) kwargs.setdefault('total_num', 1) kwargs.setdefault('nonce_str', randstr(32)) kwargs.setdefault('wishing', '恭喜发财') kwargs.setdefault('act_name', '现金红包') kwargs.setdefault('remark', '备注') kwargs['mch_billno'] = kwargs['mch_id'] + kwargs.get('mch_billno', '') kwargs.setdefault('sign', self.sign(**kwargs)) if 're_openid' not in kwargs: raise ValueError('re_openid is required.') data = dicttoxml(kwargs, custom_root='xml', attr_type=False) try: xml = requests.post(self.SEND_RED_PACK, data=data, cert=self.config.get('cert')).content return self.xml2dict(xml) except Exception, e: return dict(return_code='ERROR', return_msg=str(e))
def prepay(self, **kwargs): """微信支付预付款下单,对接口进行封装。 :param body: 订单名称 :param detail: 订单详情 :param out_trade_no: 订单号 :param openid: 用户OpenID :param type: 订单分类,默认为normal :rtype: 微信接口返回结果 """ type = kwargs.pop('type', 'normal') kwargs.setdefault('appid', self.config.get('appid')) kwargs.setdefault('mch_id', self.config.get('mchid')) kwargs.setdefault('spbill_create_ip', get_ip()) kwargs.setdefault('notify_url', url_for(self.endpoint, type=type, _external=True)) kwargs.setdefault('trade_type', 'JSAPI') kwargs.setdefault('body', '微信支付') kwargs.setdefault('out_trade_no', 'wxtest') kwargs.setdefault('total_fee', 100) kwargs.setdefault('nonce_str', randstr(32)) kwargs.setdefault('sign', self.sign(**kwargs)) if 'openid' not in kwargs: raise ValueError('openid is required.') data = dicttoxml(kwargs, custom_root='xml', attr_type=False) try: xml = requests.post(self.PREPAY_URL, data=data).content return self.xml2dict(xml) except Exception, e: return dict(return_code='ERROR', return_msg=str(e))
def refund(self, **kwargs): """微信支付申请退款,对接口进行封装。 :param out_trade_no: 订单号 :param out_refund_no: 退款记录ID :param total_fee: 订单金额 :param refund_fee: 退款金额 :rtype: 微信接口返回结果 """ if 'out_trade_no' not in kwargs: raise ValueError('out_trade_no is required.') kwargs.setdefault('appid', self.config.get('appid')) kwargs.setdefault('mch_id', self.config.get('mchid')) kwargs.setdefault('device_info', 'WEB') kwargs.setdefault('out_refund_no', kwargs.get('out_trade_no')) kwargs.setdefault('total_fee', 1) kwargs.setdefault('refund_fee', kwargs.get('total_fee')) kwargs.setdefault('refund_fee_type', 'CNY') kwargs.setdefault('op_user_id', self.config.get('mchid')) kwargs.setdefault('nonce_str', randstr(32)) kwargs.setdefault('sign', self.sign(**kwargs)) data = dicttoxml(kwargs, custom_root='xml', attr_type=False) try: xml = requests.post(self.REFUND_URL, data=data, cert=self.config.get('cert')).content return self.xml2dict(xml) except Exception, e: return dict(return_code='ERROR', return_msg=str(e))
def prepay(self, **kwargs): """微信支付预付款下单,对接口进行封装。 :param body: 订单名称 :param detail: 订单详情 :param out_trade_no: 订单号 :param openid: 用户OpenID :param type: 订单分类,默认为normal :rtype: 微信接口返回结果 """ type = kwargs.pop('type', 'normal') kwargs.setdefault('appid', self.config.get('appid')) kwargs.setdefault('mch_id', self.config.get('mchid')) kwargs.setdefault('spbill_create_ip', get_ip()) kwargs.setdefault('notify_url', url_for('wxpay_callback', type=type, _external=True)) kwargs.setdefault('trade_type', 'JSAPI') kwargs.setdefault('body', '微信支付') kwargs.setdefault('out_trade_no', 'wxtest') kwargs.setdefault('total_fee', 100) kwargs.setdefault('nonce_str', randstr(32)) kwargs.setdefault('sign', self.sign(**kwargs)) if 'openid' not in kwargs: raise ValueError('openid is required.') data = dicttoxml(kwargs, custom_root='xml', attr_type=False) try: xml = requests.post(self.PREPAY_URL, data=data).content return self.xml2dict(xml) except Exception, e: return dict(return_code='ERROR', return_msg=str(e))
def get_conf(self, prepay, tojson=True): conf = dict( appId=self.config.get('appid'), timeStamp=str(int(time.time())), nonceStr=randstr(32), package='prepay_id=%s' % prepay, signType='MD5', ) conf['paySign'] = self.sign(**conf) return json.dumps(conf) if tojson else conf
def get_conf(self, prepay, tojson=True): conf = dict( appId=self.get_config('appid'), timeStamp=str(int(time.time())), nonceStr=randstr(32), package='prepay_id=%s' % prepay, signType='MD5', ) conf['paySign'] = self.sign(**conf) return json.dumps(conf) if tojson else conf
def get_app_conf(self, prepay, tojson=False): conf = dict( appid=self.get_config('appid'), partnerid=self.get_config('mchid'), prepayid=prepay, package='Sign=WXPay', noncestr=randstr(32), timestamp=str(int(time.time())), ) conf['sign'] = self.sign(**conf) return json.dumps(conf) if tojson else conf
def get_app_conf(self, prepay, tojson=False): conf = dict( appid=self.config.get('appid'), partnerid=self.config.get('mchid'), prepayid=prepay, package='Sign=WXPay', noncestr=randstr(32), timestamp=str(int(time.time())), ) conf['sign'] = self.sign(**conf) return json.dumps(conf) if tojson else conf
def transfers(self, **kwargs): kwargs.setdefault('mch_appid', self.config.get('appid')) kwargs.setdefault('mchid', self.config.get('mchid')) kwargs.setdefault('check_name', 'NO_CHECK') kwargs.setdefault('amount', 1) kwargs.setdefault('desc', '企业打款') kwargs.setdefault('spbill_create_ip', get_ip()) kwargs.setdefault('nonce_str', randstr(32)) kwargs.setdefault('sign', self.sign(**kwargs)) data = dicttoxml(kwargs, custom_root='xml', attr_type=False) try: xml = requests.post(self.TRANSFERS, data=data, cert=self.config.get('cert')).content return self.xml2dict(xml) except Exception, e: current_app.logger.error(traceback.format_exc()) return dict(return_code='ERROR', return_msg=str(e))
def prepay(self, **kwargs): kwargs.setdefault('title', '云计费') kwargs.setdefault('mch_id', self.config.get('mchid')) kwargs.setdefault('amount_fee', 1) kwargs.setdefault('sign_type', 'MD5') host = self.callback_host if self.callback_host else request.host backurl = 'http://%s%s' % (host, url_for('yedadou_callback')) kwargs.setdefault('notify_url', backurl) kwargs.setdefault('return_url', 'http://%s/' % request.host) kwargs.setdefault('nonce_str', randstr(32)) kwargs['sign'] = self.sign(**kwargs) try: return requests.post(self.PREPAY_URL % self.host, data=kwargs).json() except Exception, e: return dict(result_code=-1, err_msg=str(e))
def transfers(self, **kwargs): kwargs.setdefault('mch_appid', self.config.get('appid')) kwargs.setdefault('mchid', self.config.get('mchid')) kwargs.setdefault('check_name', 'NO_CHECK') kwargs.setdefault('amount', 1) kwargs.setdefault('desc', '企业打款') kwargs.setdefault('spbill_create_ip', self.config.get('client_ip')) kwargs.setdefault('nonce_str', randstr(32)) kwargs.setdefault('sign', self.sign(**kwargs)) data = dicttoxml(kwargs, custom_root='xml', attr_type=False) try: xml = requests.post(self.TRANSFERS, data=data, cert=self.config.get('cert')).content return self.xml2dict(xml) except Exception, e: current_app.logger.error(traceback.format_exc()) return dict(return_code='ERROR', return_msg=str(e))
def refund_query(self, **kwargs): """微信支付退款查询,对接口进行封装。 :param out_trade_no: 订单号 :rtype: 微信接口返回结果 """ kwargs.setdefault('appid', self.config.get('appid')) kwargs.setdefault('mch_id', self.config.get('mchid')) kwargs.setdefault('device_info', 'WEB') kwargs.setdefault('nonce_str', randstr(32)) kwargs.setdefault('sign', self.sign(**kwargs)) data = dicttoxml(kwargs, custom_root='xml', attr_type=False) try: xml = requests.post(self.REFUND_QUERY, data=data).content return self.xml2dict(xml) except Exception, e: return dict(return_code='ERROR', return_msg=str(e))
def prepay(self, **kwargs): kwargs.setdefault('body', '云计费') kwargs.setdefault('total_fee', '1') kwargs.setdefault('product_id', '20170101') kwargs.setdefault('goods_tag', 'default') kwargs.setdefault('op_user_id', self.config.get('op_user_id')) kwargs.setdefault('nonce_str', randstr(32)) host = self.callback_host if self.callback_host else request.host backurl = 'http://%s%s' % (host, url_for(self.endpoint)) kwargs.setdefault('notify_url', backurl) kwargs.setdefault('spbill_create_ip', self.config.get('spbill_create_ip', get_ip())) kwargs['sign'] = self.sign(**kwargs) try: res = requests.post(self.PREPAY_URL % self.host, data=json.dumps(kwargs)) return res.json() except Exception, e: return dict(errcode=500, msg=str(e))
def prepay(self, **kwargs): kwargs.setdefault('appid', self.config.get('appid')) kwargs.setdefault('mch_id', self.config.get('mchid')) kwargs.setdefault('spbill_create_ip', get_ip()) kwargs.setdefault('notify_url', url_for('wxpay_callback', _external=True)) kwargs.setdefault('trade_type', 'JSAPI') kwargs.setdefault('body', '微信支付') kwargs.setdefault('out_trade_no', 'wxtest') kwargs.setdefault('total_fee', 100) kwargs.setdefault('nonce_str', randstr(32)) kwargs.setdefault('sign', self.sign(**kwargs)) if 'openid' not in kwargs: raise ValueError('openid is required.') data = dicttoxml(kwargs, custom_root='xml', attr_type=False) try: xml = requests.post(self.PREPAY_URL, data=data).content return self.xml2dict(xml) except Exception, e: return dict(return_code='ERROR', return_msg=str(e))
def prepay(self, **kwargs): kwargs.setdefault('service', 'pay.weixin.jspay') kwargs.setdefault('mch_id', self.config.get('mchid')) kwargs.setdefault('body', '云计费') kwargs.setdefault('total_fee', 1) kwargs.setdefault('mch_create_ip', self.config.get('mch_create_ip', '127.0.0.1')) host = self.callback_host if self.callback_host else request.host backurl = 'http://%s%s' % (host, url_for(self.endpoint)) kwargs.setdefault('notify_url', backurl) kwargs.setdefault('callback_url', 'http://%s/' % request.host) kwargs.setdefault('nonce_str', randstr(32)) kwargs['sign'] = self.sign(**kwargs) data = dicttoxml(kwargs, custom_root='xml', attr_type=False) current_app.logger.error('data:' + data) try: xml = requests.post(self.PREPAY_URL % self.host, data=data).content current_app.logger.error('xml:' + xml) return self.xml2dict(xml) except Exception, e: current_app.logger.error(traceback.format_exc()) return dict(status=-1, message=str(e))