def order_refund(self, order_id, order_num, re_num, wechat_user_id): mall = self.oMALL.get(self.usr_id_p) app_id = mall.get('appid', '') secret = mall.get('secret', '') wx_mch_id = mall.get('mchid', '') wx_mch_key = mall.get('mchkey', '') base_url = mall.get('base_url', '') #'https://malishop.janedao.cn' api_cert_path = mall.get('cert', '') api_key_path = mall.get('key', '') notify_url = base_url + '/refund/%s/notify' % self.usr_id_p wxpay = WxPay(app_id, wx_mch_id, wx_mch_key, notify_url) sql = "select total_fee from wechat_mall_payment where order_id=%s and payment_number=%s and usr_id=%s " l, t = self.db.select(sql, [order_id, order_num, self.usr_id_p]) if t == 0: return 1 total_fee = l[0][0] data = { # 退款信息 'out_trade_no': order_num, # 商户订单号 'total_fee': total_fee, # 订单金额 'refund_fee': total_fee # 退款金额 } sql = "select out_refund_no from wechat_mall_refund where out_trade_no=%s and usr_id=%s" lT, iN = self.db.select(sql, [order_num, self.usr_id_p]) if iN == 0: data['out_refund_no'] = re_num # 商户退款单号 refund = { # 退款信息 'out_trade_no': order_num, # 商户订单号 'total_fee': total_fee, # 订单金额 'refund_fee': total_fee, # 退款金额 'out_refund_no': re_num, 'usr_id': self.usr_id_p, 'wechat_user_id': wechat_user_id } self.db.insert('wechat_mall_refund', refund) data['notify_url'] = notify_url # 商户退款回调 raw = wxpay.refund(api_cert_path, api_key_path, **data) if raw['return_code'] == 'SUCCESS' and raw[ 'result_code'] == 'SUCCESS': try: sql = "select id from wechat_mall_refund where out_trade_no=%s and out_refund_no=%s and total_fee=%s and usr_id=%s" l, i = self.db.select(sql, [ raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], self.usr_id_p ]) if i == 0: return 1 refund = { 'refund_id': raw['refund_id'], 'result_code': raw['result_code'], 'return_msg': raw['return_msg'], 'status': 1, 'status_str': '成功', 'utime': self.getToday(9) } self.db.update( "wechat_mall_refund", refund, "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], self.usr_id_p)) sql = """insert into wechat_mall_order_log(usr_id,edit_name,edit_memo,edit_remark,cid,ctime) values(%s,%s,%s,%s,%s,now()) """ self.db.query(sql, [ self.usr_id_p, 'wechat_mall_refund', "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], self.usr_id_p), '退款回调更新wechat_mall_refund表数据', self.usr_id ]) return 0 except: return 1 else: try: datas = { 'status_str': '失败', 'result_code': raw['result_code'], 'utime': self.getToday(9) } self.db.update( "wechat_mall_refund", datas, "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], self.usr_id_p)) return 1 except: return 1 out_refund_no = lT[0][0] data['out_refund_no'] = out_refund_no # 商户退款单号 raw = wxpay.refund(api_cert_path, api_key_path, **data) if raw['return_code'] == 'SUCCESS' and raw['result_code'] == 'SUCCESS': try: sql = "select id from wechat_mall_refund where out_trade_no=%s and out_refund_no=%s and total_fee=%s and usr_id=%s" l, i = self.db.select(sql, [ raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], self.usr_id_p ]) if i == 0: return 1 refund = { 'refund_id': raw['refund_id'], 'result_code': raw['result_code'], 'return_msg': raw['return_msg'], 'status': 1, 'status_str': '成功', 'utime': self.getToday(9) } self.db.update( "wechat_mall_refund", refund, "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], self.usr_id_p)) sql = """insert into wechat_mall_order_log(usr_id,edit_name,edit_memo,edit_remark,cid,ctime) values(%s,%s,%s,%s,%s,now()) """ self.db.query(sql, [ self.usr_id_p, 'wechat_mall_refund', "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], self.usr_id_p), '退款回调更新wechat_mall_refund表数据', self.usr_id ]) return 0 except: return 1 else: try: datas = { 'status_str': '失败', 'result_code': raw['result_code'], 'utime': self.getToday(9) } self.db.update( "wechat_mall_refund", datas, "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], self.usr_id_p)) return 1 except: return 1
def order_refund(self, subusr_id, order_id, order_num, wechat_user_id): timeStamp = time.time() timeArray = time.localtime(timeStamp) danhao = time.strftime("%Y%m%d%H%M%S", timeArray) romcode = str(time.time()).split('.')[-1] # [3:] re_num = 'R' + danhao[2:] + romcode mall = self.oMALL.get(subusr_id) app_id = mall.get('appid', '') secret = mall.get('secret', '') wx_mch_id = mall.get('mchid', '') wx_mch_key = mall.get('mchkey', '') base_url = self.base_url # 'https://malishop.janedao.cn' api_cert_path = mall.get('cert', '') api_key_path = mall.get('key', '') notify_url = base_url + '/refund/%s/notify' % subusr_id wxpay = WxPay(app_id, wx_mch_id, wx_mch_key, notify_url) sql = "select total_fee from wechat_mall_payment where order_id=%s and payment_number=%s and usr_id=%s " l, t = self.db.select(sql, [order_id, order_num, subusr_id]) if t == 0: return 1 total_fee = l[0][0] data = { # 退款信息 'out_trade_no': order_num, # 商户订单号 'total_fee': total_fee, # 订单金额 'refund_fee': total_fee # 退款金额 } sql = "select out_refund_no from wechat_mall_refund where out_trade_no=%s and usr_id=%s" lT, iN = self.db.select(sql, [order_num, subusr_id]) if iN == 0: data['out_refund_no'] = re_num # 商户退款单号 refund = { # 退款信息 'out_trade_no': order_num, # 商户订单号 'total_fee': total_fee, # 订单金额 'refund_fee': total_fee, # 退款金额 'out_refund_no': re_num, 'usr_id': subusr_id, 'wechat_user_id': wechat_user_id } self.db.insert('wechat_mall_refund', refund) data['notify_url'] = notify_url # 商户退款回调 raw = wxpay.refund(api_cert_path, api_key_path, **data) if raw['return_code'] == 'SUCCESS' and raw[ 'result_code'] == 'SUCCESS': try: sql = """select id from wechat_mall_refund where out_trade_no=%s and out_refund_no=%s and total_fee=%s and usr_id=%s""" l, i = self.db.select(sql, [ raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], subusr_id ]) if i == 0: return 1 refund = { 'refund_id': raw['refund_id'], 'result_code': raw['result_code'], 'return_msg': raw['return_msg'], 'status': 1, 'status_str': '成功', 'utime': self.getToday(9) } self.db.update( "wechat_mall_refund", refund, "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], subusr_id)) sql = """insert into wechat_mall_order_log(usr_id,edit_name,edit_memo,edit_remark,cid,ctime) values(%s,%s,%s,%s,%s,now()) """ self.db.query(sql, [ subusr_id, 'wechat_mall_refund', "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], subusr_id), '退款回调更新wechat_mall_refund表数据', subusr_id ]) sql = "update wechat_mall_order set status=-1,status_str='已取消' where id=%s and usr_id=%s " self.db.query(sql, [order_id, subusr_id]) return 0 except: return 1 else: try: datas = { 'status_str': '失败', 'result_code': raw['result_code'], 'utime': self.getToday(9) } self.db.update( "wechat_mall_refund", datas, "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], subusr_id)) return 1 except: return 1 out_refund_no = lT[0][0] data['out_refund_no'] = out_refund_no # 商户退款单号 raw = wxpay.refund(api_cert_path, api_key_path, **data) if raw['return_code'] == 'SUCCESS' and raw['result_code'] == 'SUCCESS': try: sql = "select id from wechat_mall_refund where out_trade_no=%s and out_refund_no=%s and total_fee=%s and usr_id=%s" l, i = self.db.select(sql, [ raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], subusr_id ]) if i == 0: return 1 sql = "update wechat_mall_order set status=-1,status_str='已取消' where id=%s and usr_id=%s " self.db.query(sql, [order_id, subusr_id]) refund = { 'refund_id': raw['refund_id'], 'result_code': raw['result_code'], 'return_msg': raw['return_msg'], 'status': 1, 'status_str': '成功', 'utime': self.getToday(9) } self.db.update( "wechat_mall_refund", refund, "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], subusr_id)) sql = """insert into wechat_mall_order_log(usr_id,edit_name,edit_memo,edit_remark,cid,ctime) values(%s,%s,%s,%s,%s,now()) """ self.db.query(sql, [ subusr_id, 'wechat_mall_refund', "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], subusr_id), '退款回调更新wechat_mall_refund表数据', subusr_id ]) return 0 except: return 1 else: try: datas = { 'status_str': '失败', 'result_code': raw['result_code'], 'utime': self.getToday(9) } self.db.update( "wechat_mall_refund", datas, "out_trade_no='%s' and out_refund_no='%s' and total_fee=%s and usr_id=%s" % (raw['out_trade_no'], raw['out_refund_no'], raw['total_fee'], subusr_id)) return 1 except: return 1