upgrade_alipay_app.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import datetime
  4. import os
  5. import sys
  6. import bson
  7. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  8. sys.path.insert(0, PROJECT_ROOT)
  9. from script.base import init_env, get_logger
  10. logger = get_logger(__name__)
  11. init_env(interactive = True)
  12. from apps.web.core.models import AliApp, WithdrawEntity
  13. from apps.web.agent.models import Agent
  14. companyName = u'陕西满溢科技有限公司'
  15. appPrefix = u'陕西满溢科技有限公司'
  16. certPath = u'/var/backups/{}/支付宝'.format(companyName, appPrefix)
  17. withdraw_app_id = '2021003183632080'
  18. pay_app_id = ''
  19. def is_valid_string(str):
  20. try:
  21. bson._make_c_string(str)
  22. return True
  23. except Exception as e:
  24. return False
  25. def new_withdraw_app(withdraw_app_id, companyName, appPrefix, certPath):
  26. # app = AliApp.objects(appid = withdraw_app_id).first()
  27. # return app
  28. app = AliApp(
  29. appid = withdraw_app_id,
  30. companyName = companyName,
  31. appName = u'{}转账'.format(appPrefix),
  32. supportWithdraw = False,
  33. supportWithdrawBank = False,
  34. signKeyType = 'cert')
  35. with open(u'{}/提现/alipayCertPublicKey_RSA2.crt'.format(certPath)) as f:
  36. content = str(f.read())
  37. if is_valid_string(content):
  38. app.publicKeyCert = content
  39. app.dateTimeUpdated = datetime.datetime.now()
  40. else:
  41. print 'error'
  42. with open(u'{}/提现/appCertPublicKey_{}.crt'.format(certPath, withdraw_app_id)) as f:
  43. content = str(f.read())
  44. if is_valid_string(content):
  45. app.appPublicKeyCert = content
  46. app.dateTimeUpdated = datetime.datetime.now()
  47. else:
  48. print 'error'
  49. with open(u'{}/提现/alipayRootCert.crt'.format(certPath)) as f:
  50. content = str(f.read())
  51. if is_valid_string(content):
  52. app.rootCert = content
  53. else:
  54. print 'error'
  55. with open(u'{}/提现/应用公钥RSA2048.txt'.format(certPath)) as f:
  56. content = str(f.read())
  57. content = '-----BEGIN PUBLIC KEY-----\n{}\n-----END PUBLIC KEY-----'.format(content)
  58. if is_valid_string(content):
  59. app.alipayPublicKey = content
  60. else:
  61. print 'error'
  62. with open(u'{}/提现/应用私钥RSA2048-敏感数据,请妥善保管.txt'.format(certPath)) as f:
  63. content = str(f.read())
  64. content = '-----BEGIN RSA PRIVATE KEY-----\n{}\n-----END RSA PRIVATE KEY-----'.format(content)
  65. if is_valid_string(content):
  66. app.appPrivateKey = content
  67. else:
  68. print 'error'
  69. app.dateTimeUpdated = datetime.datetime.now()
  70. app.save()
  71. return app
  72. def new_pay_app(pay_app_id, companyName, appPrefix, certPath):
  73. app = AliApp(appid = pay_app_id,
  74. companyName = companyName,
  75. appName = u'{}支付'.format(appPrefix)) # type: AliApp
  76. with open(u'{}/支付/支付宝公钥.txt'.format(certPath)) as f:
  77. content = str(f.read())
  78. if is_valid_string(content):
  79. content = '-----BEGIN PUBLIC KEY-----\n{}\n-----END PUBLIC KEY-----'.format(content)
  80. app.alipayPublicKey = content
  81. else:
  82. print 'error'
  83. # with open(u'{}/支付/应用公钥RSA2048-敏感数据,请妥善保管.txt'.format(certPath)) as f:
  84. # content = str(f.read())
  85. #
  86. # content = '-----BEGIN PUBLIC KEY-----\n{}\n-----END PUBLIC KEY-----'.format(content)
  87. # if is_valid_string(content):
  88. # app.alipayPublicKey = content
  89. # else:
  90. # print 'error'
  91. with open(u'{}/支付/应用私钥RSA2048-敏感数据,请妥善保管.txt'.format(certPath)) as f:
  92. content = str(f.read())
  93. content = '-----BEGIN RSA PRIVATE KEY-----\n{}\n-----END RSA PRIVATE KEY-----'.format(content)
  94. if is_valid_string(content):
  95. app.appPrivateKey = content
  96. else:
  97. print 'error'
  98. app.dateTimeUpdated = datetime.datetime.now()
  99. app.save()
  100. return app
  101. new_withdraw_app(withdraw_app_id, companyName, appPrefix, certPath)
  102. if pay_app_id:
  103. new_pay_app(pay_app_id, companyName, appPrefix, certPath)