123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import base64
- import datetime
- from apps import serviceCache
- from apps.web.agent.models import Agent
- from apps.web.core.models import AliApp, WechatPayApp, WechatUserManagerApp, WechatManagerApp, \
- WechatUserSubscribeManagerApp, WechatDealerSubscribeManagerApp
- from apps.web.core.utils import JsonErrorResponse, JsonOkResponse
- from apps.web.core.validation import wechatManagerAppSchema, MultipleInvalid
- def bindAliApp(payload):
- def alipay_public_key(text):
- return '-----BEGIN PUBLIC KEY-----\n' + text + '\n-----END PUBLIC KEY-----'
- agent = Agent.objects(id = str(payload['id'])).get() # type: Agent
- customizedAlipayCashflowAllowable = bool(payload.pop('customizedAlipayCashflowAllowable', False))
- if customizedAlipayCashflowAllowable and not agent.customizedWechatCashflowAllowable:
- return JsonErrorResponse(description = u'自主支付宝开启必须首先开启微信支付(提现必须通过微信)')
- if customizedAlipayCashflowAllowable:
- params = payload['aliPayApp']
- if 'appid' not in params or not params['appid']:
- return JsonErrorResponse(description = u'支付宝支付定制情况下支付公众号不能为空')
- ca_type = params.get('signKeyType', 'normal')
- app_name = params.get('appName', '')
- company_name = params.get('companyName', '')
- shadow = params.get('shadow', False)
- new_flag = False
-
- current_app = AliApp.objects(appid = params['appid']).first() # type: AliApp
- if not current_app:
- new_flag = True
- current_app = AliApp(appid = params['appid']).save()
- current_app.companyName = company_name
- current_app.appName = app_name
- current_app.signKeyType = ca_type
- current_app.shadow = params.get('shadow')
- current_app.dateTimeUpdated = datetime.datetime.now()
- if ca_type == 'normal':
- if 'alipayPublicKeyContent' in params and params['alipayPublicKeyContent']:
- if current_app.alipayPublicKeyContent != params['alipayPublicKeyContent']:
- current_app.alipayPublicKeyContent = params['alipayPublicKeyContent']
- alipay_public_key_string = alipay_public_key(current_app.alipayPublicKeyContent)
- current_app.alipayPublicKey = alipay_public_key_string
- if 'appPublicKeyContent' in params and params['appPublicKeyContent']:
- current_app.appPublicKeyContent = params['appPublicKeyContent']
- if 'app_private_key_path' in params and params['app_private_key_path']:
- app_private_key_string = serviceCache.get(params['app_private_key_path'])
- if not app_private_key_string:
- return JsonErrorResponse(description = u'私钥文件不存在,请重新生成')
- current_app.appPrivateKey = app_private_key_string
- if not current_app.appPrivateKey:
- return JsonErrorResponse(description = u'私钥不能为空')
- if not current_app.alipayPublicKey:
- return JsonErrorResponse(description = u'支付宝公钥不能为空')
- else:
- if 'app_publickey_cert' in params and params['app_publickey_cert']:
- app_publickey_cert_string = params['app_publickey_cert']
- if not app_publickey_cert_string:
- return JsonErrorResponse(description = u'应用公钥证书不能为空')
- current_app.appPublicKeyCert = app_publickey_cert_string
- if 'publickey_cert' in params and params['publickey_cert']:
- publickey_cert_string = params['publickey_cert']
- if not publickey_cert_string:
- return JsonErrorResponse(description = u'支付宝公钥证书不能为空')
- current_app.publicKeyCert = publickey_cert_string
- if 'root_cert' in params and params['root_cert']:
- root_cert_string = params['root_cert']
- if not root_cert_string:
- return JsonErrorResponse(description = u'支付宝根证书不能为空')
- current_app.rootCert = root_cert_string
- if not current_app.appPublicKeyCert:
- return JsonErrorResponse(description = u'应用公钥证书不能为空')
- if not current_app.publicKeyCert:
- return JsonErrorResponse(description = u'支付宝公钥证书不能为空')
- if not current_app.rootCert:
- return JsonErrorResponse(description = u'支付宝根证书不能为空')
- current_app.save()
- agent.payAppAli = current_app
- agent.customizedAlipayCashflowAllowable = True
- agent.save()
- else:
- agent.customizedAlipayCashflowAllowable = False
- agent.save()
- return JsonOkResponse(payload = {})
- def bindWechatFund(payload):
- agent = Agent.objects(id = str(payload['id'])).get() # type: Agent
- customizedWechatCashflowAllowable = bool(payload.get('customizedWechatCashflowAllowable', False))
- if customizedWechatCashflowAllowable:
- params = payload['wechatPayApp']
- if 'appid' not in params or 'mchid' not in params:
- return JsonErrorResponse(description = u'微信支付定制情况下支付公众号不能为空')
- if 'ssl_cert' not in params:
- return JsonErrorResponse(description = u'证书不能为空')
- if 'ssl_key' not in params:
- return JsonErrorResponse(description = u'SSL公钥不能为空')
- params['sslCert'] = base64.b64decode(params.pop('ssl_cert'))
- params['sslKey'] = base64.b64decode(params.pop('ssl_key'))
- params['dateTimeUpdated'] = datetime.datetime.now()
- agent.payAppWechat = WechatPayApp.objects(appid = params['appid'], mchid = params['mchid']).upsert_one(
- **params)
- agent.customizedWechatCashflowAllowable = True
- agent.save()
- else:
- agent.customizedWechatCashflowAllowable = False
- agent.save()
- return JsonOkResponse(payload = {})
- def bindWechatCust(payload):
- try:
- agent = Agent.objects(id = str(payload['id'])).get() # type: Agent
- user_allowable = bool(payload.pop('customizedUserGzhAllowable', False))
- payload.update({'customizedUserGzhAllowable': user_allowable})
- if user_allowable:
- user_app_dict = wechatManagerAppSchema(payload.pop('user', None))
- payload.update({'wechatUserManagerialApp': WechatUserManagerApp.create(**user_app_dict)})
- dealer_allowable = bool(payload.pop('customizedDealerGzhAllowable', False))
- payload.update({'customizedDealerGzhAllowable': dealer_allowable})
- if dealer_allowable:
- dealer_app_dict = wechatManagerAppSchema(payload.pop('dealer', None))
- payload.update({'wechatDealerManagerialApp': WechatManagerApp.create(**dealer_app_dict)})
- user_sub_allowable = bool(payload.pop('customizedUserSubGzhAllowable', False))
- payload.update({'customizedUserSubGzhAllowable': user_sub_allowable})
- if user_sub_allowable:
- user_sub_app_dict = wechatManagerAppSchema(payload.pop('user_sub', None))
- payload.update({'wechatUserSubscribeManagerApp': WechatUserSubscribeManagerApp.create(**user_sub_app_dict)})
- dealer_sub_allowable = bool(payload.pop('customizedDealerSubGzhAllowable', False))
- payload.update({'customizedDealerSubGzhAllowable': dealer_sub_allowable})
- if dealer_sub_allowable:
- dealer_sub_app_dict = wechatManagerAppSchema(payload.pop('dealer_sub', None))
- payload.update({'wechatDealerSubscribeManagerApp': WechatDealerSubscribeManagerApp.create(**dealer_sub_app_dict)})
- updated = agent.update(**payload)
- if updated:
- return JsonOkResponse()
- else:
- return JsonErrorResponse(description = u'绑定自定义公众号失败')
- except MultipleInvalid as e:
- return JsonErrorResponse(description = u'信息填写有误,请检查')
|