一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久99深爱久久99精品_亚洲一区二区三区四区五区午夜_日本在线观看一区二区

Django1.11配合uni-app發(fā)起微信支付的實(shí)現(xiàn)

Django1.11配合uni-app發(fā)起微信支付!

經(jīng)過三天的斷斷續(xù)續(xù)的奮戰(zhàn),我終于是干動了微信支付。愛掏網(wǎng) - it200.com為了以后不忘記,現(xiàn)在來一篇教程,來來來,開干!!!

一、準(zhǔn)備階段

1、準(zhǔn)備階段我們需要去微信官網(wǎng)申請一個小程序或者公眾號。愛掏網(wǎng) - it200.com獲得AppID和AppSecret。愛掏網(wǎng) - it200.com

2、去微信商戶平臺 成為商家,開通JSAPI用來獲得商戶號和自己配置的鑰匙。愛掏網(wǎng) - it200.com然后再商戶平臺上面綁定小程序appid。愛掏網(wǎng) - it200.com

(點(diǎn)擊下面圖片進(jìn)入官方鏈接!)

在配置里面配置一個自己的key,需要記住后臺開發(fā)的時候需要!

關(guān)聯(lián)后即可在小程序管理頁面開通微信支付!

到此,準(zhǔn)備階段完成!

二、梳理流程

在這里我大概寫一下流程:首先我們在前端發(fā)起微信登陸,此時微信會給我們返回一個openid,這個openid一定要留存在某一個位置。愛掏網(wǎng) - it200.com然后前段發(fā)起微信支付,向后端發(fā)送數(shù)據(jù)請求,后端對結(jié)合前段的數(shù)據(jù)向微信方面發(fā)送一個請求,請求相關(guān)數(shù)據(jù),得到相關(guān)數(shù)據(jù)之后把數(shù)據(jù)發(fā)送給前段,前段收到數(shù)據(jù),利用微信接口再向微信指定連接發(fā)送請求,微信返回請求,即可!這個就是全流程,很多人肯定已經(jīng)懵了。愛掏網(wǎng) - it200.com沒事,咱一步一步來,別步子跨大了——扯到蛋了!

以上就是數(shù)據(jù)處理階段大概流程!

三、代碼實(shí)現(xiàn)

0、用戶登錄根據(jù)用戶code獲取openid

uni.login({
          provider: 'weixin',
          success: function(loginRes) {
            let code = loginRes.code;
            if (!_this.isCanUse) {
              //非第一次授權(quán)獲取用戶信息
              uni.getUserInfo({
                provider: 'weixin',
                success: function(infoRes) { 
                        //獲取用戶信息后向調(diào)用信息更新方法
                  _this.nickName = infoRes.userInfo.nickName; //昵稱
                  _this.avatarUrl = infoRes.userInfo.avatarUrl; //頭像
                    _this.updateUserInfo();//調(diào)用更新信息方法
                }
              });
            }
      
            //2.將用戶登錄code傳遞到后臺置換用戶SessionKey、OpenId等信息
            uni.request({
              url: 'http://127.0.0.1:8000/users/',
              data: {
                code: code,
              },
              method: 'GET',
              header: {
                'content-type': 'application/json' 
              },
              success: (res) => {
                console.log(res.data)
                if ( res.data.state== 1001) {
                  console.log("新注冊的用戶!")
                  _this.OpenId = res.data.openid;
                } else{
                  _this.OpenId = res.data.openid;
                  console.log("注冊過的用戶!開始設(shè)置本地緩存!")
                  console.log(res.data[0].id)
                  if ( res.data[0].id ) {
                    //這里獲得登陸狀態(tài),然后根據(jù)登陸狀態(tài)來改變用戶按鈕信息!!!!
                  } else{
                    
                  };
                  _this.user_id = res.data[0].id
                  uni.setStorage({
                    key: 'user',
                    data: res.data,
                    success: function () {
                      console.log('設(shè)置緩存成功');
                    }
                  });
                  // _this.gotoshopping()
                  // uni.reLaunch({//信息更新成功后跳轉(zhuǎn)到小程序首頁
                  //   url: '/pages/shopping/shopping'
                  // });
                }
                //openId、或SessionKdy存儲//隱藏loading
                uni.hideLoading();
              }
            });
          },
        });
if request.GET.get("code"):
      ret = {"state": 1000}
      code = request.GET.get("code")

      url = "https://api.weixin.qq.com/sns/jscode2session"
      appid = "xxxxxxxxxxxxx"
      secret = "xxxxxxxxxxxxxxxxxxxxx"

      # url一定要拼接,不可用傳參方式
      url = url + "?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code"
      import requests
      r = requests.get(url)
      print("======", r.json())
      openid = r.json()['openid']
      user = users.objects.filter(openid=openid).all()
      if not user:
        ret["state"] = 1001
        ret["msg"] = "用戶第一次登陸"
        ret["openid"] = openid
        return Response(ret)
      else:
        serializer = self.get_serializer(user, many=True)
        return Response(serializer.data)

1、首先需要創(chuàng)建一個confige.py的配置文件!然后寫路由,讓前端找到“門”在哪里!

config.py

# 微信支付的配置參數(shù)
client_appid = 'xxxxxxxxxxxxxx' # 小程序appid
client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx' # 小程序secret

Mch_id = 'xxxxxxxxxxx' # 商戶號
Mch_key = 'xxxxxxxxxxxxxxxxxxx' # 商戶Key
order_url = 'https://api.mch.weixin.qq.com/pay/unifiedorder' # 訂單地址

url.py

router = routers.DefaultRouter()
router.register("users", views.UsersViewSet)
router.register("goods", views.GoodsViewSet)
router.register("comments", views.CommentsViewSet)
router.register("payOrder", views.OrdersViewSet) #這個就是微信支付的接口


urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include(router.urls)),

]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

view.py

class OrdersViewSet(viewsets.ModelViewSet):
  queryset = Order.objects.all()
  serializer_class = OrderModelSerializer

  def create(self, request, *args, **kwargs):
    if request.data.get("user_id"):
      from goods.wxpay.wxpay import payOrder
      data = payOrder(request)
      print(data)
      return Response(data)
    else:
      serializer = self.get_serializer(data=request.data)
      serializer.is_valid(raise_exception=True)
      self.perform_create(serializer)
      headers = self.get_success_headers(serializer.data)
      return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

  def perform_create(self, serializer):
    serializer.save()

  def get_success_headers(self, data):
    try:
      return {'Location': str(data[api_settings.URL_FIELD_NAME])}
    except (TypeError, KeyError):
      return {}

2、然后創(chuàng)建邏輯文件,獲取數(shù)據(jù)請求數(shù)據(jù)返回?cái)?shù)據(jù)!

wxpay.py

# -*- coding: utf-8 -*-
from .config import client_appid, client_secret, Mch_id, Mch_key, order_url
import hashlib
import datetime
import xml.etree.ElementTree as ET
import requests
from ..models import users


# 生成簽名的函數(shù)
def paysign(appid, body, mch_id, nonce_str, notify_url, openid, out_trade_no, spbill_create_ip, total_fee):
  ret = {
    "appid": appid,
    "body": body,
    "mch_id": mch_id,
    "nonce_str": nonce_str,
    "notify_url": notify_url,
    "openid": openid,
    "out_trade_no": out_trade_no,
    "spbill_create_ip": spbill_create_ip,
    "total_fee": total_fee,
    "trade_type": 'JSAPI'
  }
  print(ret)
  # 處理函數(shù),對參數(shù)按照key=value的格式,并按照參數(shù)名ASCII字典序排序
  stringA = '&'.join(["{0}={1}".format(k, ret.get(k)) for k in sorted(ret)])
  stringSignTemp = '{0}&key={1}'.format(stringA, Mch_key)
  sign = hashlib.md5(stringSignTemp.encode("utf-8")).hexdigest()
  print(sign.upper())
  return sign.upper()


# 生成隨機(jī)字符串
def getNonceStr():
  import random
  data = "123456789zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP"
  nonce_str = ''.join(random.sample(data, 30))
  return nonce_str


# 生成商品訂單號
def getWxPayOrdrID():
  date = datetime.datetime.now()
  # 根據(jù)當(dāng)前系統(tǒng)時間來生成商品訂單號。愛掏網(wǎng) - it200.com時間精確到微秒
  payOrdrID = date.strftime("%Y%m%d%H%M%S%f")

  return payOrdrID


# 獲取全部參數(shù)信息,封裝成xml
def get_bodyData(openid, client_ip, price):
  body = 'Mytest' # 商品描述
  notify_url = 'https://127.0.0.1:8000/payOrder/' # 支付成功的回調(diào)地址 可訪問 不帶參數(shù)
  nonce_str = getNonceStr() # 隨機(jī)字符串
  out_trade_no = getWxPayOrdrID() # 商戶訂單號
  total_fee = str(price) # 訂單價格 單位是 分

  # 獲取簽名
  sign = paysign(client_appid, body, Mch_id, nonce_str, notify_url, openid, out_trade_no, client_ip, total_fee)

  bodyData = ''
  bodyData += '' + client_appid + '' # 小程序ID
  bodyData += '' + body + '' # 商品描述
  bodyData += '' + Mch_id + '' # 商戶號
  bodyData += '' + nonce_str + '' # 隨機(jī)字符串
  bodyData += '' + notify_url + '' # 支付成功的回調(diào)地址
  bodyData += '' + openid + '' # 用戶標(biāo)識
  bodyData += '' + out_trade_no + '' # 商戶訂單號
  bodyData += '' + client_ip + '' # 客戶端終端IP
  bodyData += '' + total_fee + '' # 總金額 單位為分
  bodyData += 'JSAPI' # 交易類型 小程序取值如下:JSAPI
  bodyData += '' + sign + ''
  bodyData += ''

  return bodyData


def xml_to_dict(xml_data):
  '''
  xml to dict
  :param xml_data:
  :return:
  '''
  xml_dict = {}
  root = ET.fromstring(xml_data)
  for child in root:
    xml_dict[child.tag] = child.text
  return xml_dict


def dict_to_xml(dict_data):
  '''
  dict to xml
  :param dict_data:
  :return:
  '''
  xml = [""]
  for k, v in dict_data.iteritems():
    xml.append("{1}{0}>".format(k, v))
  xml.append("")
  return "".join(xml)


# 獲取返回給小程序的paySign
def get_paysign(prepay_id, timeStamp, nonceStr):
  pay_data = {
    'appId': client_appid,
    'nonceStr': nonceStr,
    'package': "prepay_id=" + prepay_id,
    'signType': 'MD5',
    'timeStamp': timeStamp
  }
  stringA = '&'.join(["{0}={1}".format(k, pay_data.get(k)) for k in sorted(pay_data)])
  stringSignTemp = '{0}&key={1}'.format(stringA, Mch_key)
  sign = hashlib.md5(stringSignTemp.encode("utf-8")).hexdigest()
  return sign.upper()


# 統(tǒng)一下單支付接口
def payOrder(request):
  import time
  # 獲取價格,和用戶是誰
  price = request.data.get("price")
  user_id = request.data.get("user_id")

  # 獲取客戶端ip
  client_ip, port = request.get_host().split(":")

  # 獲取小程序openid
  openid = users.objects.get(id=user_id).openid

  # 請求微信的url
  url = order_url

  # 拿到封裝好的xml數(shù)據(jù)
  body_data = get_bodyData(openid, client_ip, price)

  # 獲取時間戳
  timeStamp = str(int(time.time()))

  # 請求微信接口下單
  respOne= requests.post(url, body_data.encode("utf-8"), headers={'Content-Type': 'application/xml'})

  # 回復(fù)數(shù)據(jù)為xml,將其轉(zhuǎn)為字典
  cOntent= xml_to_dict(respone.content)
  print(content)
  # 返回給調(diào)用函數(shù)的數(shù)據(jù)
  ret = {"state": 1000}
  if content["return_code"] == 'SUCCESS':
    # 獲取預(yù)支付交易會話標(biāo)識
    prepay_id = content.get("prepay_id")
    # 獲取隨機(jī)字符串
    nOnceStr= content.get("nonce_str")

    # 獲取paySign簽名,這個需要我們根據(jù)拿到的prepay_id和nonceStr進(jìn)行計(jì)算簽名
    paySign = get_paysign(prepay_id, timeStamp, nonceStr)

    # 封裝返回給前端的數(shù)據(jù)
    data = {"prepay_id": prepay_id, "nonceStr": nonceStr, "paySign": paySign, "timeStamp": timeStamp}
    print('=========',data)

    ret["msg"] = "成功"
    return data

  else:
    ret["state"] = 1001
    ret["msg"] = "失敗"
    return ret

3、前段獲取后端返回的數(shù)據(jù)給微信再次發(fā)送數(shù)據(jù)請求!(包含點(diǎn)擊的時候往后端發(fā)送數(shù)據(jù)處理請求)

pay(){
        uni.request({
          url: 'http://127.0.0.1:8000/payOrder/',
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          data: {
            user_id:this.user_id,
            price:128
          },
          success: res => {
            console.log("success")
            console.log(res.data)
            
            uni.requestPayment({
            provider: 'wxpay',
            
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: 'prepay_id='+String(res.data.prepay_id),
            signType: 'MD5',
            paySign: res.data.paySign,
            
            success: function (res) {
              console.log('success:' + JSON.stringify(res));
              // 支付成功,給后臺發(fā)送數(shù)據(jù),保存訂單
              
            },
            fail: function (err) {
              console.log('fail:' + JSON.stringify(err));
              // 支付失敗,給后臺發(fā)送數(shù)據(jù),保存訂單
            }
            });

            
            
          },
          fail: (res) => {
            console.log("fail")
            console.log(res)
          },
          complete: () => {}
        });
        
        
      }

至此相信大家也就會了。愛掏網(wǎng) - it200.com

附上我的目錄結(jié)構(gòu)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。愛掏網(wǎng) - it200.com


聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評論
更多 網(wǎng)友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 久久精品综合 | 久久久久国产精品一区二区 | 毛片视频免费 | 久久久久久亚洲精品 | 91麻豆产精品久久久久久夏晴子 | 亚洲久视频 | 亚洲综合激情 | 天天干狠狠干 | 91视频大全 | 二区视频 | 99热国产免费 | 天天色综 | 国产欧美日韩一区二区三区在线 | 国产98在线 | 免费, | 国产精品 欧美精品 | xxx.在线观看 | 婷婷去俺也去 | 国产精品一区一区 | 久久天天躁狠狠躁夜夜躁2014 | 日韩网 | 国产在视频一区二区三区吞精 | 久久精品亚洲欧美日韩精品中文字幕 | 欧美成人一区二区 | 亚洲一区视频在线 | 成人精品毛片国产亚洲av十九禁 | 天天综合成人网 | 久久网日本 | 91不卡| 国产成人精品久久二区二区 | www.色综合 | 毛片在线看片 | 欧美日韩久久 | 精品三级在线观看 | 麻豆久久久9性大片 | 天天艹| 97精品一区二区 | 久久久2o19精品| 天堂一区在线观看 | 午夜网站视频 | 亚洲一区国产精品 | 久久三区|