小程序微信支付c语言,微信小程序 - 支付(后端代码实现)

from rest_framework.views importAPIViewfrom rest_framework.response importResponsefrom django.core.cache importcacheimporthashlib,timeimportrandomfrom app01.wx importsettingsimportrequestsclassPay(APIView):defpost(self,request):
param=request.dataif param.get("login_key"):
#从redis中拿到小程序端login_key所对应得opendi&session_key值
openid,session_key=cache.get(param.get("login_key")).split("&")
self.openid=openid#获取用户IP
#1.如果是Nginx做的负载就要HTTP_X_FORWARDED_FOR
if request.META.get('HTTP_X_FORWARDED_FOR'):
self.ip=request.META['HTTP_X_FORWARDED_FOR']else:#2.如果没有用Nginx就用REMOTE_ADDR
self.ip = request.META['REMOTE_ADDR']#调用 生成商户订单 方法
data =https://www.it610.com/article/self.pay()return Response({"code":200,"msg":"ok","data":data})else:return Response({"code":200,"msg":"缺少参数"})defget_str(self):
str_all="1234567890abcdefghjklmasdwery" # 注意 开发活动功能时, 去掉1,i,0,ononce_str="".join(random.sample(str_all,20))returnnonce_strdefget_order(self):
order_id=str(time.strftime("%Y%m%d%H%M%S"))returnorder_id#处理返回预付单方法
defxml_to_dict(self,data):importxml.etree.ElementTree as ET
xml_dict={}
data_dic=ET.fromstring(data)for item indata_dic:
【小程序微信支付c语言,微信小程序 - 支付(后端代码实现)】xml_dict[item.tag]=item.textreturnxml_dict#获取sign签名方法
defget_sign(self):
data_dic={"nonce_str": self.nonce_str,"out_trade_no": self.out_trade_no,"spbill_create_ip": self.ip,"notify_url": self.notify_url,"openid": self.openid,"body": self.body,"trade_type": "JSAPI","appid": self.appid,"total_fee": self.total_fee,"mch_id": self.mch_id
}
sign_str= "&".join([f"{k}={data_dic[k]}" for k insorted(data_dic)])
sign_str= f"{sign_str}&key={settings.pay_apikey}"md5=hashlib.md5()
md5.update(sign_str.encode("utf-8"))returnmd5.hexdigest().upper()# 1.生成商户订单 提供 支付统一下单 所需参数
defpay(self):
self.appid=settings.AppId
self.mch_id=settings.pay_mchid
self.nonce_str=self.get_str()
self.body="商品名" #商品名一般由小程序端传到后端
self.out_trade_no=self.get_order()
self.total_fee=1self.spbill_create_ip=self.ip
self.notify_url="http://www.baidu.com"self.trade_type="JSAPI"self.sign= self.get_sign() #获取sign 签名
data=https://www.it610.com/article/f'''
{self.appid}
{self.mch_id}
{self.nonce_str}
{self.notify_url}
{self.openid}
{self.out_trade_no}
{self.spbill_create_ip}
{self.total_fee}
{self.trade_type}
{self.sign}
'''
# 2.支付统一下单
url="https://api.mch.weixin.qq.com/pay/unifiedorder"
# 3.返回预付单信息
response=requests.post(url,data.encode("utf-8"),headers={"content-type":"application/xml"})
res_data=https://www.it610.com/article/self.xml_to_dict(response.content)
data=https://www.it610.com/article/self.two_sign(res_data["prepay_id"])returndata# 4.将组合数据再次签名
deftwo_sign(self,prepay_id):
timeStamp=str(int(time.time()))
nonceStr=self.get_str()
data_dict={"appId":settings.AppId,"timeStamp":timeStamp,"nonceStr":nonceStr,"package":f"prepay_id={prepay_id}","signType":"MD5"}
sign_str= "&".join([f"{k}={data_dict[k]}" for k insorted(data_dict)])
sign_str= f"{sign_str}&key={settings.pay_apikey}"md5=hashlib.md5()
md5.update(sign_str.encode("utf-8"))
sign=md5.hexdigest().upper()
data_dict["paySign"]=sign
data_dict.pop("appId")# 5.返回支付参数到小程序端,小程序端获取所需参数向微信服务器发送 调起支付 方法
return data_dict

    推荐阅读