# 退款------------------------------
code = json_data.get(\'code\', None)
kind = json_data.get(\'kind\', None)
print(1111111111111111112222222222222222)
if code is None:
return HttpResponse(json.dumps({\'state\': -2, \'msg\': \'参数缺失\'}), status=200, content_type=\'application/json; charset=utf-8\')
"""产生随机字符串,不长于32位"""
chars = \'abcdefghijklmnopqrstuvwxyz0123456789\'
strs = []
for x in range(32):
strs.append(chars[random.randrange(0, len(chars))])
strs = \'\'.join(strs)
strs1 = []
for x in range(32):
strs1.append(chars[random.randrange(0, len(chars))])
strs1 = \'\'.join(strs)
stringA = "appid={}&mch_id={}&nonce_str={}&out_refund_no={}&out_trade_no={}&refund_fee={}&total_fee={}" \
.format(global_settings.WX_APPID, global_settings.WX_MCH_ID, strs,
strs1, code, 1, 1)
stringSignTemp = stringA + \'&key=Heibeizhidaguangdian123456789000\'
print(stringSignTemp)
sign = hashlib.md5(stringSignTemp.encode(\'utf-8\')).hexdigest().upper()
print(sign)
params = "<xml><appid>{}</appid><mch_id>{}</mch_id><nonce_str>{}</nonce_str><out_refund_no>{}</out_refund_no><out_trade_no>{}</out_trade_no><refund_fee>{}</refund_fee><total_fee>{}</total_fee><sign>{}</sign></xml>".format(
global_settings.WX_APPID, global_settings.WX_MCH_ID, strs,
strs1, code, \'1\', \'1\', sign)
print(params)
import requests
cert_path = "{}/rsa/cert/apiclient_cert.pem".format(settings.BASE_DIR)
key_path = "{}/rsa/cert/apiclient_key.pem".format(settings.BASE_DIR)
res = requests.post(global_settings.WX_REFUND_URL, cert=(cert_path, key_path), data=params.encode(\'utf-8\')) # 退款需要证书
print(res, \'退款返回\')
array_data = {}
root = fromstring(res.content)
for child in root:
value = child.text
array_data[child.tag] = value
print(array_data)
#支付-----------------------------
# 被调用class==========
# 微信下单API
class WechatOrderAPI(Base):
def __init__(self, body, out_trade_no, total_fee, spbill_create_ip=None, notify_url=None, device_info=None,
sign_type=None, detail=None, attach=None, fee_type=None, time_start=None, time_expire=None,
goods_tag=None, limit_pay=None, receipt=None):
super().__init__()
if notify_url is None:
notify_url = self.config.WX_NOTIFY_URL
if spbill_create_ip is None:
spbill_create_ip = \'127.0.0.1\'
self.dic = {\'appid\': self.config.WX_APPID, \'mch_id\': self.config.WX_MCH_ID,
\'nonce_str\': self.create_nonce_str(), \'body\': body,
\'out_trade_no\': out_trade_no, \'notify_url\': notify_url,
\'total_fee\': total_fee, \'spbill_create_ip\': spbill_create_ip,
\'trade_type\': \'APP\'}
if device_info is not None:
self.dic[\'device_info\'] = device_info
if sign_type is not None:
self.dic[\'sign_type\'] = sign_type
if detail is not None:
self.dic[\'detail\'] = detail
if attach is not None:
self.dic[\'attach\'] = attach
if fee_type is not None:
self.dic[\'fee_type\'] = fee_type
if time_start is not None:
self.dic[\'time_start\'] = time_start
if time_expire is not None:
self.dic[\'time_expire\'] = time_expire
if goods_tag is not None:
self.dic[\'goods_tag\'] = goods_tag
if limit_pay is not None:
self.dic[\'limit_pay\'] = limit_pay
if receipt is not None:
self.dic[\'receipt\'] = receipt
print(self.dic,9999999999999)
def order_post(self):
xml_ = self.array_to_xml(self.dic, sign_name=\'sign\')
data = requests.post(self.config.WX_ORDER_URL, data=xml_.encode(\'utf-8\'),
headers={\'Content-Type\': \'text/xml\'})
print(xml_,1111111111111111)
print(self.xml_to_array(data.content))
return self.process_response_pay(data.content)
def get_dic(self, prepayid):
time_stamp = self.create_time_stamp()
nonce_str = self.create_nonce_str()
res_dic = {\'appid\': self.config.WX_APPID, \'partnerid\': self.config.WX_MCH_ID,
\'timestamp\': \'{0}\'.format(time_stamp),
\'noncestr\': nonce_str, \'prepayid\': prepayid, \'package\': \'Sign=WXPay\'}
res_dic[\'sign\'] = self.get_sign(res_dic)
return res_dic
# 支付接口----------调用上面的-----------
class WxPayView(View):
"""微信支付接口"""
@csrf_exempt
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
@login_user
def post(self, _, json_data, user):
try:
uuid_o = json_data.get(\'code\', None)
kind = json_data.get(\'kind\', \'0\')
if kind is None:
kind = \'0\'
if uuid_o is None:
return HttpResponse(json.dumps({\'state\': -2, \'msg\': \'code参数必须哟\'}),
status=200,
content_type=\'application/json; charset=utf-8\')
# 商城订单
if kind == \'0\':
total_order = TotalOrder.objects.filter(is_effective=True, uuid=uuid_o).first()
if total_order is None:
print(\'rrrrrrrrrrrrrr\')
product_order = ProductOrder.objects.filter(is_effective=True, uuid=uuid_o).first()
if product_order is None:
return HttpResponse(json.dumps({\'state\': -2, \'msg\': \'没有这个订单请核对后再试\'}),
status=200,
content_type=\'application/json; charset=utf-8\')
# if OrdersBelong.objects.filter(is_effective=True, uuid=code, order__state=\'0\').count() != count:
# return HttpResponse(json.dumps({\'state\': -2, \'msg\': \'该订单无法再进行支付,请重新选择商品下单\'}),
# status=200,
# content_type=\'application/json; charset=utf-8\')
else:
code = product_order.order_code
total_money = 0.0
count_product = 0
product_str = None
total_money += product_order.total_money
query = OrderRelated.objects.filter(is_effective=True, order=product_order)
if product_str is None:
rel = query.first()
product_str = rel.product.name
count_product += query.count()
if count_product > 1:
product_str += \'等{}种商品\'.format(count_product)
total_money = round(total_money, 2)
print(total_money)
else:
print(\'ttttttttttttt\')
code = total_order.code
total_money = 0.0
count_product = 0
product_str = None
orders = ProductOrder.objects.filter(is_effective=True, total_order=total_order, user=user).all()
for o in orders:
total_money += o.total_money
query = OrderRelated.objects.filter(is_effective=True, order=o)
if product_str is None:
rel = query.first()
product_str = rel.product.name
count_product += query.count()
if count_product > 1:
product_str += \'等{}种商品\'.format(count_product)
# total_money = round(total_money, 2)
total_money = 0.01
print(total_money)
# e+商品订单
elif kind == \'1\':
print(\'qqqqqqqqqqqqqqqqqq\')
order = EaddProductOrder.objects.filter(is_effective=True, uuid=uuid_o).first()
if order is None:
return HttpResponse(json.dumps({\'state\': -2, \'msg\': \'没有这个订单请核对后再试\'}),
status=200,
content_type=\'application/json; charset=utf-8\')
query = EaddOrderRelated.objects.filter(is_effective=True, order=order)
rel = query.first()
code = order.order_code
product_str = rel.product.name
total_money = round(order.total_money, 2)
# E+服务
elif kind == \'3\':
print(\'------E+服务------------\')
order = EaddServeOrder.objects.filter(is_effective=True, uuid=uuid_o).first()
if order is None:
return HttpResponse(json.dumps({\'state\': -2, \'msg\': \'没有这个订单请核实后再试\'}),
status=200,
content_type=\'application/json; charset=utf-8\')
code = order.order_code
total_money = 0.0
total_money += order.total_money
total_money = round(total_money, 2)
product_str = order.serve.name
print(total_money)
# 到家服务
elif kind == \'4\':
print(\'-------到家---------\')
order = SServeOrder.objects.filter(is_effective=True, order_code=uuid_o).first()
if order is None:
return HttpResponse(json.dumps({\'state\': -2, \'msg\': \'没有这个订单请核实后再试\'}),
status=200,
content_type=\'application/json; charset=utf-8\')
code = order.order_code
total_money = 0.0
total_money += order.total_money
# total_money = round(total_money, 2)
total_money = 0.01
product_str = order.serve.name
print(total_money)
elif kind == \'5\':
print("----------购买会员卡---------")
order = PurchaseCard.objects.filter(order_code=uuid_o).first()
if order is None:
return HttpResponse(json.dumps({\'state\': -2, \'msg\': \'没有这个订单请核实后再试\'}),
status=200,
content_type=\'application/json; charset=utf-8\')
code = order.order_code
total_money = float(order.card_price)
product_str = "会员卡"
print(total_money)
elif kind == "6":
print("----------会员充值----------")
order = MemberCenterRecharge.objects.filter(order_code=uuid_o).first()
if order is None:
return HttpResponse(json.dumps({\'state\': -2, \'msg\': \'没有这个订单请核实后再试\'}),
status=200,
content_type=\'application/json; charset=utf-8\')
code = order.order_code
# total_money = float(order.recharge)
product_str = "会员卡充值"
total_money = 0.01
print(total_money)
else:
return
info = WechatPayInfo.objects.filter(is_effective=True, out_trade_no=code).first()
prepay_id = None
if info is not None:
if int(time.time()) - info.time_start <= 7000:
prepay_id = info.prepay_id
if prepay_id is None:
total_fee = int(total_money * 100)
print(int(total_money * 100))
# total_fee = total_money
wxpay = WechatOrderAPI(
body=product_str,
out_trade_no=code,
total_fee=total_fee,
attach=kind
)
res, error = wxpay.order_post()
print(res, error)
if not error:
prepay_id = res.get(\'prepay_id\', None)
if prepay_id is None:
return HttpResponse(json.dumps({\'state\': -2, \'msg\': \'下单失败\'}),
status=200,
content_type=\'application/json; charset=utf-8\')
prepay_id = prepay_id.encode(\'iso8859-1\').decode(\'utf-8\')
dic = wxpay.get_dic(prepayid=prepay_id)
WechatPayInfo.objects.create(
time_start=int(time.time()),
prepay_id=prepay_id,
out_trade_no=code
)
return HttpResponse(json.dumps({\'state\': 0, \'msg\': \'下单成功\', \'data\': dic}),
status=200,
content_type=\'application/json; charset=utf-8\')
else:
return HttpResponse(json.dumps({\'state\': -2, \'msg\': error.get(\'msg\', \'未知错误\')}),
status=200,
content_type=\'application/json; charset=utf-8\')
wxpay = WechatOrderAPI(
body=product_str,
out_trade_no=code,
total_fee=int(total_money * 100),
attach=kind
)
dic = wxpay.get_dic(prepayid=prepay_id)
return HttpResponse(json.dumps({\'state\': 0, \'msg\': \'下单成功\', \'data\': dic}),
status=200,
content_type=\'application/json; charset=utf-8\')
except Exception as e:
print(str(e))
return HttpResponse(json.dumps({\'state\': -2, \'msg\': str(e)}),
status=200,
content_type=\'application/json; charset=utf-8\')