寰俊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爜瀹炵幇的主要内容,如果未能解决你的问题,请参考以下文章