import requests from rest_framework import status from rest_framework.decorators import action from rest_framework.request import Request from rest_framework.response import Response from rest_framework.routers import DefaultRouter from rest_framework.viewsets import ViewSet from sixGuoDjango import BaseResponse, ResData from wechat_数据库.雪花算法.雪花算法工具 import generator from wechat_项目工具包.微信支付工具包.微信支付类 import paymentCategory, wechatPay class 代金券接口ViewSets(ViewSet): @action(detail=False, methods=["get", "post", "put", "delete"], url_path="send_coupons") def 发放指定代金券接口(self, request: Request): """""" self_mini_openid = request.data.get("self_mini_openid", None) self_mini_unionid = request.data.get("self_mini_unionid", None) 发放接口 = f"https://api.mch.weixin.qq.com/v3/marketing/favor/users/{self_mini_openid}/coupons" out_request_no = generator.generate_str() 发放的数据 = { "stock_id": "22512292", # 发放的批次号 "out_request_no": out_request_no, # 【商户单据号】商户此次发放凭据号(格式:商户id+日期+流水号),商户侧需保持唯一性 "appid": "wxf612e825377328f0", "stock_creator_mchid": "1700383419", # 创建批次的商户号】批次创建方商户号。 # "coupon_value": 100, # 指定面额发券,面额】指定面额发券场景,券面额,其他场景不需要填,单位:分。 } 返回签名后的 = wechatPay(payment=paymentCategory).构造签名串(uri=发放接口, body_data=发放的数据, method="POST") data = requests.post(url=发放接口, json=发放的数据, headers=返回签名后的) print(data.json()) httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"couponsMessages": "发券成功"}), ).model_dump() return Response(data=data.json(), status=status.HTTP_200_OK) @action(detail=False, methods=["get", "post", "put", "delete"], url_path="setting_url") def 设置代金券消息通知地址(self, request: Request): """""" self_user_unionid = request.data.get("self_user_unionid", None) self_mini_openid = request.data.get("self_mini_openid", None) 请求接口 = f"https://api.mch.weixin.qq.com//v3/marketing/favor/callbacks" 请求数据 = { "mchid": paymentCategory.mchid, "notify_url": "https://xiao.sixgou.com/coupons/coupons_callback/", } headers = wechatPay(payment=paymentCategory).构造签名串(uri=请求接口, body_data=请求数据) data = requests.post(url=请求接口, json=请求数据, headers=headers) print("请求后的结果", data.json()) # 请求后的结果 {'notify_url': 'https://xiao.sixgou.com/coupons/coupons_callback/', 'update_time': '2026-03-25T23:51:54+08:00'} httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"couponsMessages": "发券成功"}), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK) @action(detail=False, methods=["post"], url_path="coupons_callback") def 代金券的回调接口(self, request: Request): """""" self_user_unionid = request.data.get("self_user_unionid", None) self_mini_openid = request.data.get("self_mini_openid", None) # 回调解密内容 = wechatPay(payment=paymentCategory).callBack回调解密(headers=request.headers, response_body=request.body) # print("回调解密内容", 回调解密内容) wechat_data支付回调数据 = wechatPay(payment=paymentCategory).callBack回调解密(headers=request.headers, response_body=request.data, ) print(wechat_data支付回调数据, "wechat_data支付回调数据🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 ") print("🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏🍎 🍏") httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK) 代金券接口ViewSetsRouter = DefaultRouter() 代金券接口ViewSetsRouter.register(prefix="", viewset=代金券接口ViewSets, basename="")