langxin_pay.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. """
  4. 后记
  5. 最后一笔收入发生在 2019年6月26日10点5分59秒 订单号 20190626100559Q00000000037501545
  6. (在此之前的所有的订单收入 - 经销商已经提走的钱 ) * (1 - 0.006 (微信和支付宝的手续费) )
  7. (9449.93 - 6070.14 )* 0.994 = 3359.51
  8. * 首先从支付宝和微信账单取出6月所有的外部订单号 (取出所有订单较为麻烦和耗时)
  9. * 合并两者得到集合 -> orders_paid_to_us
  10. * 得到该厂商下所有6月支付成功的充值记录,包含recharge, chargeCard -> langxin_orders_in_june
  11. * 筛选一次,[ x | x <- filter(\x -> x.orderNo, orders_paid_to_us) ]
  12. * 取到6月没有支付给我们的订单号
  13. * 取出该厂家从一开始到6月的所有订单 - 6月没有付给我们的订单
  14. * 算出数据 - 所有提现的金额
  15. * 相关数据可以查看 /var/archive
  16. """
  17. import datetime
  18. from apilib.monetary import RMB, sum_rmb
  19. from apps.web.management.models import Manager
  20. from apps.web.dealer.models import Dealer, WithdrawRecord
  21. from apps.web.agent.models import Agent
  22. from apps.web.user.models import RechargeRecord
  23. WECHAT_OUT_TRADE_ORDER_FIELD_NAME = ALIPAY_OUT_TRADE_ORDER_FIELD_NAME = u'商户订单号'
  24. WECHAT_REMARK_FIELD_NAME = u'商户数据包'
  25. ALIPAY_REMARK_FIELD_NAME = u'备注'
  26. WECHAT_TRADE_STATUS_FIELD_NAME = u'交易状态'
  27. ALIPAY_TRADE_STATUS_FIELD_NAME = u''
  28. langxin_manager = Manager.objects(username='13738357542').get()
  29. agentIds = [ str(_.id) for _ in Agent.objects(managerId=str(langxin_manager.id)) ]
  30. dealerIds = [ str(_.id) for _ in Dealer.objects(agentId__in=agentIds) ]
  31. started_datetime = datetime.datetime.strptime('2019-06-26', '%Y-%m-%d')
  32. langxin_agent = Agent.objects(username='13738357542').get()
  33. langxin_wechat_gateway = get_wechat_payment_gateway_for_user(langxin_agent)
  34. langxin_alipay_gateway = get_alipay_payment_gateway(langxin_agent)
  35. langxin_raw = {
  36. 'appid': langxin_wechat_gateway.appid,
  37. 'mch_id': langxin_wechat_gateway.mchid
  38. }
  39. our_agent = Agent.objects().first()
  40. our_wechat_gateway = get_wechat_payment_gateway_for_user(our_agent)
  41. our_alipay_gateway = get_alipay_payment_gateway(our_agent)
  42. our_raw = {
  43. 'appid': langxin_wechat_gateway.appid,
  44. 'mch_id': langxin_wechat_gateway.mchid
  45. }
  46. #: 此列表通过下载支付宝和微信的订单,然后
  47. langxin_orders_havent_paid_to_us_in_june = []
  48. after_june = datetime.datetime.now().replace(month=6, day=1, hour=0, second=0)
  49. before_july = datetime.datetime.now().replace(month=7, day=1, hour=0, second=0)
  50. langxin_orders_in_june = RechargeRecord.objects(result='success',
  51. via__in=['recharge', 'chargeCard'],
  52. dateTimeAdded__lt=before_july,
  53. dateTimeAdded__gt=after_june,
  54. ownerId__in=dealerIds).order_by('dateTimeAdded')
  55. langxin_orders_till_june = RechargeRecord.objects(result='success',
  56. via__in=['recharge', 'chargeCard'],
  57. dateTimeAdded__lt=before_july,
  58. ownerId__in=dealerIds).order_by('dateTimeAdded')
  59. langxin_orders_till_june_paid_to_us = list(RechargeRecord.objects(result='success',
  60. via__in=['recharge', 'chargeCard'],
  61. dateTimeAdded__lt=before_july,
  62. orderNo__nin=langxin_orders_havent_paid_to_us_in_june,
  63. ownerId__in=dealerIds).order_by('dateTimeAdded'))
  64. def sum_previous_orders(records):
  65. return sum_rmb( _.money for _ in records)
  66. confirmed_paid_to_us = set()
  67. def confirm_third_party(records):
  68. for record in records:
  69. if record.id in confirmed_paid_to_us:
  70. continue
  71. if record.gateway == 'wechat':
  72. result = our_wechat_gateway.order_query(record.orderNo)
  73. if result.get('trade_state_desc') == u'支付成功':
  74. confirmed_paid_to_us.add(record.id)
  75. elif record.gateway == 'alipay':
  76. result = our_alipay_gateway.api_alipay_trade_query(record.orderNo)
  77. if result.get('trade_status') == 'TRADE_SUCCESS' or result.get('trade_status') == 'TRADE_FINISHED':
  78. confirmed_paid_to_us.add(record.id)
  79. else:
  80. raise ValueError()
  81. def sum_previous_withdraw():
  82. return RMB(WithdrawRecord.get_succeeded_records(ownerId__in=dealerIds, postTime__lt=before_july).sum('amount'))
  83. if __name__ == '__main__':
  84. assert len(langxin_orders_havent_paid_to_us_in_june), 'get data from pickle or download bills from wechat/alipay'
  85. confirm_third_party(langxin_orders_till_june_paid_to_us)
  86. how_much_we_got_paid = sum_previous_orders( _ for _ in langxin_orders_till_june_paid_to_us if _.id in confirmed_paid_to_us )
  87. print('how_much_we_got_paid=%s' % (how_much_we_got_paid,))
  88. how_much_we_paid = sum_previous_withdraw()
  89. print('how_much_we_paid=%s' % (how_much_we_paid,))
  90. how_much_we_should_pay_to_langxin = how_much_we_paid - how_much_we_got_paid
  91. print('how_much_we_should_pay_to_langxin=%s' % (how_much_we_should_pay_to_langxin,))