api.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import base64
  4. import datetime
  5. from apps import serviceCache
  6. from apps.web.agent.models import Agent
  7. from apps.web.core.models import AliApp, WechatPayApp, WechatUserManagerApp, WechatManagerApp, \
  8. WechatUserSubscribeManagerApp, WechatDealerSubscribeManagerApp
  9. from apps.web.core.utils import JsonErrorResponse, JsonOkResponse
  10. from apps.web.core.validation import wechatManagerAppSchema, MultipleInvalid
  11. def bindAliApp(payload):
  12. def alipay_public_key(text):
  13. return '-----BEGIN PUBLIC KEY-----\n' + text + '\n-----END PUBLIC KEY-----'
  14. agent = Agent.objects(id = str(payload['id'])).get() # type: Agent
  15. customizedAlipayCashflowAllowable = bool(payload.pop('customizedAlipayCashflowAllowable', False))
  16. if customizedAlipayCashflowAllowable and not agent.customizedWechatCashflowAllowable:
  17. return JsonErrorResponse(description = u'自主支付宝开启必须首先开启微信支付(提现必须通过微信)')
  18. if customizedAlipayCashflowAllowable:
  19. params = payload['aliPayApp']
  20. if 'appid' not in params or not params['appid']:
  21. return JsonErrorResponse(description = u'支付宝支付定制情况下支付公众号不能为空')
  22. ca_type = params.get('signKeyType', 'normal')
  23. app_name = params.get('appName', '')
  24. company_name = params.get('companyName', '')
  25. shadow = params.get('shadow', False)
  26. new_flag = False
  27. current_app = AliApp.objects(appid = params['appid']).first() # type: AliApp
  28. if not current_app:
  29. new_flag = True
  30. current_app = AliApp(appid = params['appid']).save()
  31. current_app.companyName = company_name
  32. current_app.appName = app_name
  33. current_app.signKeyType = ca_type
  34. current_app.shadow = params.get('shadow')
  35. current_app.dateTimeUpdated = datetime.datetime.now()
  36. if ca_type == 'normal':
  37. if 'alipayPublicKeyContent' in params and params['alipayPublicKeyContent']:
  38. if current_app.alipayPublicKeyContent != params['alipayPublicKeyContent']:
  39. current_app.alipayPublicKeyContent = params['alipayPublicKeyContent']
  40. alipay_public_key_string = alipay_public_key(current_app.alipayPublicKeyContent)
  41. current_app.alipayPublicKey = alipay_public_key_string
  42. if 'appPublicKeyContent' in params and params['appPublicKeyContent']:
  43. current_app.appPublicKeyContent = params['appPublicKeyContent']
  44. if 'app_private_key_path' in params and params['app_private_key_path']:
  45. app_private_key_string = serviceCache.get(params['app_private_key_path'])
  46. if not app_private_key_string:
  47. return JsonErrorResponse(description = u'私钥文件不存在,请重新生成')
  48. current_app.appPrivateKey = app_private_key_string
  49. if not current_app.appPrivateKey:
  50. return JsonErrorResponse(description = u'私钥不能为空')
  51. if not current_app.alipayPublicKey:
  52. return JsonErrorResponse(description = u'支付宝公钥不能为空')
  53. else:
  54. if 'app_publickey_cert' in params and params['app_publickey_cert']:
  55. app_publickey_cert_string = params['app_publickey_cert']
  56. if not app_publickey_cert_string:
  57. return JsonErrorResponse(description = u'应用公钥证书不能为空')
  58. current_app.appPublicKeyCert = app_publickey_cert_string
  59. if 'publickey_cert' in params and params['publickey_cert']:
  60. publickey_cert_string = params['publickey_cert']
  61. if not publickey_cert_string:
  62. return JsonErrorResponse(description = u'支付宝公钥证书不能为空')
  63. current_app.publicKeyCert = publickey_cert_string
  64. if 'root_cert' in params and params['root_cert']:
  65. root_cert_string = params['root_cert']
  66. if not root_cert_string:
  67. return JsonErrorResponse(description = u'支付宝根证书不能为空')
  68. current_app.rootCert = root_cert_string
  69. if not current_app.appPublicKeyCert:
  70. return JsonErrorResponse(description = u'应用公钥证书不能为空')
  71. if not current_app.publicKeyCert:
  72. return JsonErrorResponse(description = u'支付宝公钥证书不能为空')
  73. if not current_app.rootCert:
  74. return JsonErrorResponse(description = u'支付宝根证书不能为空')
  75. current_app.save()
  76. agent.payAppAli = current_app
  77. agent.customizedAlipayCashflowAllowable = True
  78. agent.save()
  79. else:
  80. agent.customizedAlipayCashflowAllowable = False
  81. agent.save()
  82. return JsonOkResponse(payload = {})
  83. def bindWechatFund(payload):
  84. agent = Agent.objects(id = str(payload['id'])).get() # type: Agent
  85. customizedWechatCashflowAllowable = bool(payload.get('customizedWechatCashflowAllowable', False))
  86. if customizedWechatCashflowAllowable:
  87. params = payload['wechatPayApp']
  88. if 'appid' not in params or 'mchid' not in params:
  89. return JsonErrorResponse(description = u'微信支付定制情况下支付公众号不能为空')
  90. if 'ssl_cert' not in params:
  91. return JsonErrorResponse(description = u'证书不能为空')
  92. if 'ssl_key' not in params:
  93. return JsonErrorResponse(description = u'SSL公钥不能为空')
  94. params['sslCert'] = base64.b64decode(params.pop('ssl_cert'))
  95. params['sslKey'] = base64.b64decode(params.pop('ssl_key'))
  96. params['dateTimeUpdated'] = datetime.datetime.now()
  97. agent.payAppWechat = WechatPayApp.objects(appid = params['appid'], mchid = params['mchid']).upsert_one(
  98. **params)
  99. agent.customizedWechatCashflowAllowable = True
  100. agent.save()
  101. else:
  102. agent.customizedWechatCashflowAllowable = False
  103. agent.save()
  104. return JsonOkResponse(payload = {})
  105. def bindWechatCust(payload):
  106. try:
  107. agent = Agent.objects(id = str(payload['id'])).get() # type: Agent
  108. user_allowable = bool(payload.pop('customizedUserGzhAllowable', False))
  109. payload.update({'customizedUserGzhAllowable': user_allowable})
  110. if user_allowable:
  111. user_app_dict = wechatManagerAppSchema(payload.pop('user', None))
  112. payload.update({'wechatUserManagerialApp': WechatUserManagerApp.create(**user_app_dict)})
  113. dealer_allowable = bool(payload.pop('customizedDealerGzhAllowable', False))
  114. payload.update({'customizedDealerGzhAllowable': dealer_allowable})
  115. if dealer_allowable:
  116. dealer_app_dict = wechatManagerAppSchema(payload.pop('dealer', None))
  117. payload.update({'wechatDealerManagerialApp': WechatManagerApp.create(**dealer_app_dict)})
  118. user_sub_allowable = bool(payload.pop('customizedUserSubGzhAllowable', False))
  119. payload.update({'customizedUserSubGzhAllowable': user_sub_allowable})
  120. if user_sub_allowable:
  121. user_sub_app_dict = wechatManagerAppSchema(payload.pop('user_sub', None))
  122. payload.update({'wechatUserSubscribeManagerApp': WechatUserSubscribeManagerApp.create(**user_sub_app_dict)})
  123. dealer_sub_allowable = bool(payload.pop('customizedDealerSubGzhAllowable', False))
  124. payload.update({'customizedDealerSubGzhAllowable': dealer_sub_allowable})
  125. if dealer_sub_allowable:
  126. dealer_sub_app_dict = wechatManagerAppSchema(payload.pop('dealer_sub', None))
  127. payload.update({'wechatDealerSubscribeManagerApp': WechatDealerSubscribeManagerApp.create(**dealer_sub_app_dict)})
  128. updated = agent.update(**payload)
  129. if updated:
  130. return JsonOkResponse()
  131. else:
  132. return JsonErrorResponse(description = u'绑定自定义公众号失败')
  133. except MultipleInvalid as e:
  134. return JsonErrorResponse(description = u'信息填写有误,请检查')