lyh преди 1 година
родител
ревизия
eb93391deb
променени са 12 файла, в които са добавени 71 реда и са изтрити 23 реда
  1. 0 0
      apps/api/__init__.py
  2. 8 0
      apps/api/urls.py
  3. 46 0
      apps/api/views.py
  4. 5 2
      apps/pay/models.py
  5. 1 0
      apps/wechat/customer/urls.py
  6. 3 9
      apps/wechat/customer/views.py
  7. 2 0
      apps/wechat/urls.py
  8. 2 3
      apps/wechat/views.py
  9. 1 1
      shop/settings.py
  10. 1 1
      shop/urls.py
  11. 1 1
      utils/wechatpay.py
  12. 1 6
      utils/wx/WXBizDataCrypt.py

+ 0 - 0
apps/api/__init__.py


+ 8 - 0
apps/api/urls.py

@@ -0,0 +1,8 @@
+# coding=utf-8
+
+from django.conf.urls import url
+from .views import *
+urlpatterns = [
+    url(r'^wechat_notify/$', WechatNotifyView.as_view()),
+]
+

+ 46 - 0
apps/api/views.py

@@ -0,0 +1,46 @@
+# coding=utf-8
+
+import json
+import xmltodict
+
+from django.db import transaction
+from django.http import HttpResponse
+from apps.account import tenant_log
+from rest_framework.views import APIView
+
+from utils.wx.WXBizMsgCrypt import WXBizMsgCrypt
+from utils.exceptions import CustomError
+from utils import response_ok, response_error
+from utils.wechatpay import WechatPayNotify
+from apps.foundation.models import Config,BizLog
+
+from apps.pay.models import Pay
+
+
+class WechatNotifyView(APIView):
+
+    def dispatch(self, request, *args, **kwargs):
+        param = request.body.decode('utf-8')
+        tenant_key = Config.getConfigValue(Config.KEY_WECHAT_TENANT_KEY)
+        notify = WechatPayNotify(param, tenant_key)
+
+        try:
+            data = notify.handle()
+            if not data:
+                raise CustomError(u'错误的请求!')
+
+            result_code = data['result_code']
+            if result_code != 'SUCCESS':
+                raise CustomError(u'错误的请求!')
+
+            no = data['out_trade_no']
+            amount = int(data['total_fee']) * 100
+
+            with transaction.atomic():
+                pay = Pay.getByNo(no)
+                pay.payConfirm(amount)
+                tenant_log(pay.customer.user, BizLog.INSERT, u'支付成功,no=%s' % no, param)
+        except:
+            return HttpResponse(WechatPayNotify.response_fail())
+        return HttpResponse(WechatPayNotify.response_ok())
+

+ 5 - 2
apps/pay/models.py

@@ -5,7 +5,7 @@ from django.utils import timezone
 
 from apps.customer.models import Customer
 from apps.product_order.models import ProductOrder
-from apps.product.models import Product
+from apps.foundation.models import Config
 from apps.customer.models import CustomerWechat
 
 from utils.exceptions import CustomError
@@ -89,7 +89,10 @@ class Pay(models.Model):
         return instance
 
     def _wechatUnifiedOrder(self, openid):
-        wechatpay = WechatPay(self.wechatapp.authorizer_appid, self.wechatapp.tenant_num, self.wechatapp.tenant_key)
+        wechat_appid = Config.getConfigValue(Config.KEY_WECHAT_APPID)
+        tenant_num = Config.getConfigValue(Config.KEY_WECHAT_TENANT_NUM)
+        tenant_key = Config.getConfigValue(Config.KEY_WECHAT_TENANT_KEY)
+        wechatpay = WechatPay(wechat_appid, tenant_num, tenant_key)
         wechatpay.unifiedOrder(self.no, self.precreate_amount, openid)
         data = wechatpay.getAppString()
         return data

+ 1 - 0
apps/wechat/customer/urls.py

@@ -11,6 +11,7 @@ urlpatterns = [
     url(r'^usedvehicle_inquiry/$', UsedVehicleInquiryViewSet.as_view()),
     url(r'^usedvehicle_estimate/$', UsedVehicleEstimateViewSet.as_view()),
     url(r'^product_order/$', ProductOrderViewSet.as_view()),
+    url(r'^product_order/add/$', ProductOrderSaveViewSet.as_view()),
     url(r'^maint_reserve/$', MaintOrderReserveViewSet.as_view()),
     url(r'^maint_reserve/options/$', MaintReserveOptionsView.as_view()),
     url(r'^usedvehicle/brands/$', UsedVehicleBrandsView.as_view()),

+ 3 - 9
apps/wechat/customer/views.py

@@ -193,8 +193,7 @@ class MaintOrderReserveViewSet(generics.ListCreateAPIView):
 
         return response_ok()
 
-
-class ProductOrderViewSet(generics.ListCreateAPIView):
+class ProductOrderViewSet(generics.ListAPIView):
     permission_classes = [IsCustomerUser, ]
     queryset = ProductOrder.objects.filter(delete=False)
     serializer_class = ProductOrderSerializer
@@ -204,13 +203,8 @@ class ProductOrderViewSet(generics.ListCreateAPIView):
         f = ProductOrderFilter(self.request.GET, queryset=queryset)
         return f.qs
 
-    def list(self, request, *args, **kwargs):
-        try:
-            data = super(ProductOrderViewSet, self).list(request)
-        except NotFound:
-            return response_ok([])
-        return data
 
+class ProductOrderSaveViewSet(generics.CreateAPIView):
     def create(self, request, *args, **kwargs):
         with transaction.atomic():
             serializer = self.get_serializer(data=request.data)
@@ -218,7 +212,7 @@ class ProductOrderViewSet(generics.ListCreateAPIView):
             serializer.save()
             instance = serializer.instance
             validated_data = serializer.validated_data
-            if instance.actual_amount == 0 and instance.xgj_member_id:
+            if instance.amount == 0:
                 instance.status = ProductOrder.PAID
                 instance.save()
                 tenant_log(instance.customer.user, BizLog.INSERT, u'添加商品订单,no=%s' % instance.no, validated_data)

+ 2 - 0
apps/wechat/urls.py

@@ -13,6 +13,8 @@ urlpatterns = [
     url(r'^code2Session/$', WxLoginView.as_view()),
     url(r'^wxbind/$', WxBindView.as_view()),
 
+    url(r'^setUserInfo/$', SetUserInfoView.as_view()),
+
     url(r'^activity/', include('apps.wechat.activity.urls')),
     url(r'^product/', include('apps.wechat.product.urls')),
     url(r'^store/', include('apps.wechat.store.urls')),

+ 2 - 3
apps/wechat/views.py

@@ -71,12 +71,11 @@ class SetUserInfoView(APIView):
     permission_classes = [IsCustomerUser, ]
 
     def post(self, request, *args, **kwargs):
-        appid = request.POST.get('appid')
         openid = request.POST.get('openid')
         encryptedData = request.POST.get('encryptedData')
         iv = request.POST.get('iv')
 
-        customer_wechat = CustomerWechat.objects.filter(openid=openid, wechat_app__authorizer_appid=appid).first()
+        customer_wechat = CustomerWechat.objects.filter(openid=openid).first()
         if not customer_wechat:
             raise CustomError(u'未找到相应的微信客户!')
         customer = request.customer
@@ -85,7 +84,7 @@ class SetUserInfoView(APIView):
         if not customer_wechat.customer:
             customer_wechat.customer = customer
             customer_wechat.save()
-        pc = WXBizDataCrypt(customer_wechat.wechat_app.authorizer_appid, customer_wechat.session_key)
+        pc = WXBizDataCrypt(customer_wechat.session_key)
         result = pc.decrypt(encryptedData, iv)
         with transaction.atomic():
             customer.setInfo(result['nickName'], result['gender'], result['avatarUrl'])

+ 1 - 1
shop/settings.py

@@ -57,7 +57,7 @@ INSTALLED_APPS = [
     'apps.maint_order',
     'apps.wechat',
     'apps.pay',
-    # 'apps.poster',
+    'apps.api',
     # 'apps.activity',
     # 'apps.message',
     # 'apps.vehicle_model',

+ 1 - 1
shop/urls.py

@@ -35,7 +35,7 @@ urlpatterns = [
     url(r'^vehicle_order/', include('apps.vehicle_order.urls')),
     url(r'^maint_order/', include('apps.maint_order.urls')),
     url(r'^wechat/', include('apps.wechat.urls')),
-    # url(r'^api/', include('apps.api.urls')),
+    url(r'^api/', include('apps.api.urls')),
 ]
 
 urlpatterns += static(settings.UIS_URL, document_root=settings.UIS_ROOT)

+ 1 - 1
utils/wechatpay.py

@@ -35,7 +35,7 @@ class WechatPay():
             'out_trade_no': '',
             'total_fee': '',
             'spbill_create_ip': WEIXIN_SPBILL_CREATE_IP,
-            'notify_url': WEIXIN_CALLBACK_API + appid + '/',
+            'notify_url': WEIXIN_CALLBACK_API,
             'trade_type': 'JSAPI'
         }
         self.prepay_id = None

+ 1 - 6
utils/wx/WXBizDataCrypt.py

@@ -4,12 +4,10 @@ from Crypto.Cipher import AES
 
 
 class WXBizDataCrypt:
-    def __init__(self, appId, sessionKey):
-        self.appId = appId
+    def __init__(self, sessionKey):
         self.sessionKey = sessionKey
 
     def decrypt(self, encryptedData, iv):
-        # base64 decode
         sessionKey = base64.b64decode(self.sessionKey)
         encryptedData = base64.b64decode(encryptedData)
         iv = base64.b64decode(iv)
@@ -18,9 +16,6 @@ class WXBizDataCrypt:
 
         decrypted = json.loads(self._unpad(cipher.decrypt(encryptedData)))
 
-        if decrypted['watermark']['appid'] != self.appId:
-            raise Exception('Invalid Buffer')
-
         return decrypted
 
     def _unpad(self, s):