(编辑:jimmy 日期: 2024/11/13 浏览:2)
1.下载相关的库
微信官方已经提供了方便开发者的SDK,可是使用pip方式下载:
pip install wechatpy
2. 在项目的settings.py文件添加相关配置
具体的参数需要自己到小程序微信公众平台和微信商户平台获取。
WECHAT = { 'APPID': 'appid', # 小程序ID 'APPSECRET': 'appsecret', # 小程序SECRET 'MCH_ID': 'mch_id', # 商户号 'TOTAL_FEE': '1', # 总金额, 单位为“分” 'SPBILL_CREATE_IP': '127.0.0.1', # 终端IP 'NOTIFY_URL': 'http://127.0.0.1:8000/wechat/payNotify/', # 通知地址 'TRADE_TYPE': 'JSAPI', # 交易类型 'MERCHANT_KEY': 'merchant_key', # 商户KEY 'BODY': '商品描述', # 商品描述 }
3. 给Django项目新建app
Pay
INSTALLED_APPS
添加刚才新建的app4. 编写app/views.py:
from django.http import HttpResponse import requests import json from django.conf import settings from wechatpy.pay import WeChatPay from app_base.base_viewset import BaseAPIView from rest_framework import permissions from lxml import etree as et from rest_framework import status class WeChatPayViewSet(BaseAPIView): """ 通过小程序前端 wx.login() 接口获取临时登录凭证 code 将 code 作为参数传入,调用 get_user_info() 方法获取 openid """ def get_user_info(self, js_code): """ 使用 临时登录凭证code 获取 session_key 和 openid 等 支付部分仅需 openid,如需其他用户信息请按微信官方开发文档自行解密 """ req_params = { 'appid': settings.WECHAT['APPID'], 'secret': settings.WECHAT['APPSECRET'], 'js_code': js_code, 'grant_type': 'authorization_code', } user_info = requests.get('https://api.weixin.qq.com/sns/jscode2session', params=req_params, timeout=3, verify=False) return user_info.json() def get(self, request): code = request.GET.get("code", None) openid = self.get_user_info(code)['openid'] pay = WeChatPay(settings.WECHAT['APPID'], settings.WECHAT['MERCHANT_KEY'], settings.WECHAT['MCH_ID']) order = pay.order.create( trade_type=settings.WECHAT['TRADE_TYPE'], # 交易类型,小程序取值:JSAPI body=settings.WECHAT['BODY'], # 商品描述,商品简单描述 total_fee=settings.WECHAT['TOTAL_FEE'], # 标价金额,订单总金额,单位为分 notify_url=settings.WECHAT['NOTIFY_URL'], # 通知地址,异步接收微信支付结果通知的回调地址,通知url必须为外网可访问的url,不能携带参数。 user_id=openid # 用户标识,trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识。 ) wxpay_params = pay.jsapi.get_jsapi_params(order['prepay_id']) return HttpResponse(json.dumps(wxpay_params)) class WeChatPayNotifyViewSet(BaseAPIView): permission_classes = (permissions.AllowAny, ) def get(self, request): _xml = request.body # 拿到微信发送的xml请求 即微信支付后的回调内容 xml = str(_xml, encoding="utf-8") print("xml", xml) return_dict = {} tree = et.fromstring(xml) # xml 解析 return_code = tree.find("return_code").text try: if return_code == 'FAIL': # 官方发出错误 return_dict['message'] = '支付失败' # return Response(return_dict, status=status.HTTP_400_BAD_REQUEST) elif return_code == 'SUCCESS': # 拿到自己这次支付的 out_trade_no _out_trade_no = tree.find("out_trade_no").text # TODO 这里省略了 拿到订单号后的操作 看自己的业务需求 except Exception as e: pass finally: return HttpResponse(return_dict, status=status.HTTP_200_OK)
补充一些继承的类:
# -*- coding: utf-8 -*- from rest_framework.authentication import TokenAuthentication from rest_framework.views import APIView from rest_framework import permissions __author__ = 'JayChen' class BaseAPIView(APIView): permission_classes = (permissions.IsAuthenticated,) # authentication_classes = (TokenAuthentication,)
5. 给Pay app添加urls.py并编写:
# -*- coding: utf-8 -*- __author__ = 'JayChen' from django.conf.urls import url from rest_framework.urlpatterns import format_suffix_patterns from pay import views app_name = 'pay' urlpatterns = [ # 微信小程序支付 url(r'^pay/', views.WeChatPayViewSet.as_view(), name='pay'), # 支付结果回调 url(r'^payNotify/', views.WeChatPayNotifyViewSet.as_view(), name='pay_notify'), ]
6.在项目的urls.py添加上面新增的urls.py
from django.contrib import admin from django.urls import path, include from rest_framework_jwt.views import obtain_jwt_token urlpatterns = [ path('admin/', admin.site.urls), path('token_auth/', obtain_jwt_token, name='jwt_token'), path('user/', include('auth_jwt.urls')), path('wechat/', include('pay.urls')), # 微信支付相关 ]
7.调试
微信小程序登陆后会得到一个code,把这个code作为参数发送给Django项目的后端: 小程序拿到这些数据就能调起支付。
例如:http://0.0.0.0:8000/wechat/pay/"htmlcode">
{
"appId": "wx14b75285dfe1",
"timeStamp": "1595228",
"nonceStr": "1Wtu5lKb6T3fJLiNzc09ay2Z",
"signType": "MD5",
"package": "prepay_id=wx02158826854686197390000",
"paySign": "89599A11E051D3B20FF57"
}