寰俊app鏀粯python浠g爜瀹炵幇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了寰俊app鏀粯python浠g爜瀹炵幇相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/html' title='html'>html   chapter   pen   result   鏌ヨ   pid   鍚庡彴   瑕佹眰   except   

寰俊app鏀粯python浠g爜(浣跨敤weixin-python==0.5.4)

寰俊app鏀粯python浠g爜(python2)
python3搴旇宸笉澶?

瀹樻柟鏂囨。:
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1

涓氬姟娴佺▼:

鍟嗘埛绯荤粺鍜屽井淇℃敮浠樼郴缁熶富瑕佷氦浜掕鏄庯細

#姝ラ1锛氱敤鎴峰湪鍟嗘埛APP涓€夋嫨鍟嗗搧锛屾彁浜よ鍗曪紝閫夋嫨寰俊鏀粯銆?鐢ㄦ埛鍦╝pp椤甸潰閫夋嫨鍟嗗搧,纭畾鏁伴噺,鎻愪氦璁㈠崟,鎻愪緵蹇呰鐨勫弬鏁?app鏀粯闇€瑕佺殑鍓嶇鍙傛暟
1. body  鍟嗗搧鎻忚堪
2. total_fee 鍟嗗搧浠锋牸

#姝ラ2: 鍟嗘埛鍚庡彴鐢熸垚鏈湴璁㈠崟
鐢熸垚璁㈠崟鍙?淇濊瘉鍞竴鎬?order_sn = "%s%s%s" % (request.user.id, datetime.datetime.now().strftime('%Y%m%d%H%M%S'),
                                   random.randint(1, 99))
鏍规嵁璁㈠崟鍙?鐢熸垚鏈湴璁㈠崟(鐢熸垚璁㈠崟瀵硅薄,璁㈠崟鐘舵€佽偗瀹氭槸鏈敮浠?鏀粯鎴愬姛鐨勫洖璋冩椂鍐嶆敼涓烘敮浠樻垚鍔熺姸鎬?
def generate_order(user, post_data, order_sn, nickname, openid):
    good_kind = post_data.get("good_kind")
    good_id = post_data.get('good_id')
    paid_amount = post_data.get('paid_amount', 0)
    try:
        good_kind = int(good_kind)
        good_id = int(good_id)
        paid_amount = float(paid_amount)
        #1 璇剧▼  2 浼氬憳鍗?        if good_kind == 1:
            course = Course.objects.get(pk=good_id)
            record = Record.objects.create(
                user=user, course=course, telephone=user.phone, center=get_center_for_appid(),
                price=paid_amount, order_sn=order_sn, nickname=nickname, openid=openid)
            if not paid_amount:
                record.status = 2
                record.save()
        elif good_kind == 2:
            card = Vip.objects.get(pk=good_id)
            record = Record.objects.create(
                user=user, card=card, price=paid_amount, center=get_center_for_appid(),
                order_sn=order_sn, nickname=nickname, telephone=user.phone, openid=openid)
        else:
            record = None
    except Exception as e:
        record = None
    return record

#姝ラ3:璋冪敤寰俊鏀粯缁熶竴涓嬪崟鎺ュ彛銆傚弬瑙併€愮粺涓€涓嬪崟API銆戙€?浣跨敤pip install weixin-python==0.5.4(妯″潡)
1.瀵煎叆from weixin import Weixin
2.鍒濆鍖?https://www.cnblogs.com/gjh99/p/10536905.html)
we_chat = Weixin(dict(
    #app_id 寰俊寮€鏀惧钩鍙板鏍搁€氳繃鐨勫簲鐢ˋPPID锛堣鐧诲綍open.weixin.qq.com鏌ョ湅锛屾敞鎰忎笌鍏紬鍙风殑APPID涓嶅悓锛?    WEIXIN_APP_ID=WECHAT_APP_ID,
    #mch_id 寰俊鏀粯鍒嗛厤鐨勫晢鎴峰彿
    WEIXIN_MCH_ID=WECHAT_MCH_ID,
    #mch_key 寰俊鏀粯瀵嗛挜
    WEIXIN_MCH_KEY=WECHAT_MCH_KEY,
    #鍥炶皟url
    WEIXIN_NOTIFY_URL=WECHAT_NOTIFY_URL,
    #鍏挜鏂囦欢
    WEIXIN_MCH_KEY_FILE='',
    #绉侀挜鏂囦欢
    WEIXIN_MCH_CERT_FILE=''
))

3.璋冪敤妯″潡灏佽濂界殑鏂规硶
蹇呬紶鐨勫弬鏁板涓?寰俊鐨勬敮浠樹负鍒?jsdict = we_chat.jsapi(
    #璁㈠崟鍙?    out_trade_no=order_sn,
    #鍟嗗搧鎻忚堪
    body=body, 
    #鏀粯閲戦
    total_fee=int(float(total_fee)*100),
    #浜ゆ槗绫诲瀷
    trade_type='APP'
)

4.鎺ユ敹寰俊鍚庡彴杩斿洖鐨勬暟鎹瓧鍏竕sdict,闇€瑕佽繑鍥炵粰鍓嶅彴
{
    "status": 1,
    "data": {
        "appId": "浣犵殑appid",
        "package": "prepay_id=wx11162703519516e69f7864b91520314800",
        "prepay_id": "wx11162703519516e69f7864b91520314800",
        "timeStamp": "1576052824",
        "nonceStr": "pqfQOyiMs8d9aAqhS7g8n9WCCyjHPYc6",
        "signType": "MD5",
        "sign": "396451156447F8DB4590E4ADE5C041AD"
    }
}
5.寰俊app鏀粯闇€瑕侀噸鏂扮敓鎴愮鍚?缁熶竴涓嬪崟鎺ュ彛杩斿洖姝e父鐨刾repay_id锛屽啀鎸夌鍚嶈鑼冮噸鏂扮敓鎴愮鍚嶅悗锛屽皢鏁版嵁浼犺緭缁橝PP銆傚弬涓庣鍚嶇殑瀛楁鍚嶄负appid锛宲artnerid锛宲repayid锛宯oncestr锛宼imestamp锛宲ackage銆傛敞鎰忥細package鐨勫€兼牸寮忎负Sign=WXPay

new_sign = app_pay_sign(jsdict, we_chat)

#娉ㄦ剰 瀛楀吀鐨勫弬鏁拌璺熷井淇¤姹傜殑涓€妯′竴鏍?灏辨槸data鐨刱ey瑕佸畬鍏ㄥ拰涓嬮潰鐨勪唬鐮佷竴鏍?def app_pay_sign(jsdict, we_chat):
    try:
        data = dict()
        data["appid"] = jsdict["appId"]
        data["partnerid"] = WECHAT_MCH_ID
        data["prepayid"] = jsdict["prepay_id"]
        data["noncestr"] = jsdict["nonceStr"]
        data["timestamp"] = jsdict["timeStamp"]
        data["package"] = jsdict["package"]
        return we_chat.sign(data)
    except KeyError:
        return

6. 閲嶆柊绛惧悕鍚庡皢鏁版嵁杩斿洖缁欏墠绔?鍚庡彴灏辩畻瀹屾垚浜?jsdict["sign"] = new_sign


7. 鍐欏洖璋冨嚱鏁?@api_view(['POST'])
@permission_classes([AllowAny])
def wechat_callback(request):
    if request.method == 'POST':
        try:
            data = we_chat.to_dict(request.body)
            order_sn = data.get('out_trade_no', '')
            # 鏀粯缁撴灉锛岀敤鎴锋甯告敮浠橈紝浼氳繑鍥濻UCCESS
            result_code = data.get('result_code', '')
            # check 妫€鏌ュ井淇″洖浼犳暟鎹槸鍚﹀彲闈?            if not we_chat.check(data):
                return HttpResponse(we_chat.reply("绛惧悕楠岃瘉澶辫触", False))
            if result_code == 'SUCCESS':
                return HttpResponse(we_chat.reply("OK", True), content_type='text/xml')
        except Exception as e:
            return HttpResponse(we_chat.reply("绛惧悕楠岃瘉澶辫触", False), content_type='text/xml')


#鍚庡彴python浠g爜
class WeChatPayViewSet(viewsets.ViewSet):
    permission_classes = (IsAuthenticated,)

    @list_route(methods=["POST"])
    def app_pay(self, request, *args, **kwargs):
        try:
            post_data = request.DATA
            body = post_data.get('body', '寰俊鏀粯娴嬭瘯')
            total_fee = post_data.get('total_fee', 0.01)
            if not total_fee:
                return Response({"status": 0, "errormsg": "缂哄皯鍙傛暟"})
            # 璁㈠崟鍙风敓鎴愯鍒欙細鐢ㄦ埛id+褰撳墠鏃堕棿+闅忔満鏁?            order_sn = "%s%s%s" % (request.user.id, datetime.datetime.now().strftime('%Y%m%d%H%M%S'),
                                   random.randint(1, 99))
            # 鐢熸垚鏈湴璁㈠崟
            order = generate_order()
            jsdict = we_chat.jsapi(out_trade_no=order_sn, body=body, total_fee=int(float(total_fee)*100),
                                   trade_type='APP')
            jsdict["prepay_id"] = jsdict.get("package").split("=")[-1]
            new_sign = app_pay_sign(jsdict, we_chat)
            if not new_sign:
                return Response({"status": 0, "errormsg": "璋冪敤寰俊缁熶竴涓嬪崟鎺ュ彛澶辫触,缂哄皯蹇呰鍙傛暟"})
            jsdict["sign"] = new_sign
            return Response({"status": 1, "data": jsdict})
        except Exception as e:
            return Response({"status": 0, "errormsg": e})


#姝ラ4锛氬晢鎴稟PP璋冭捣寰俊鏀粯銆俛pi鍙傝鏈珷鑺傘€恆pp绔紑鍙戞楠よ鏄庛€?#姝ラ5锛氬晢鎴峰悗鍙版帴鏀舵敮浠橀€氱煡銆俛pi鍙傝銆愭敮浠樼粨鏋滈€氱煡API銆?#姝ラ6锛氬晢鎴峰悗鍙版煡璇㈡敮浠樼粨鏋溿€傦紝api鍙傝銆愭煡璇㈣鍗旳PI銆?

以上是关于寰俊app鏀粯python浠g爜瀹炵幇的主要内容,如果未能解决你的问题,请参考以下文章

娣卞害瀛︿範鍘熺悊璇﹁В鍙奝ython浠g爜瀹炵幇

Dubbo鎺ュ彛涓嶇煡閬撴€庝箞娴嬶紵鏁欎綘涓夎浠g爜瀹炵幇dubbo鎺ュ彛娴嬭瘯

HttpClient 瀹炵幇 socks 浠g悊

鏀寔鍚戦噺鏈猴細Stata 鍜?Python 瀹炵幇

瀹炵幇涓€涓唬鐮佺紪璇戝櫒锛?00琛孞S浠g爜锛?/h1>

Mybatis 浠g爜瀹炶返