Advertisement
Guest User

Export to IIF payout and charges

a guest
Jun 26th, 2019
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. import os
  2. import stripe
  3. import json
  4. import time
  5. #https://stripe.com/docs/connect/express-dashboard
  6. #https://stripe.com/docs/recipes/seller-dashboard#building-a-seller-dashboard
  7. # This is using a modified version of https://github.com/ddehghan/python-QuickBooks
  8.  
  9. stripe.api_key = "sk_test_Change_ME"
  10.  
  11. def date_converter(date_string):#converts YYYY-MM-DD to Epoch time
  12. target_timestamp = time.strptime(date_string, '%Y-%m-%d')
  13. return round(time.mktime(target_timestamp))
  14.  
  15. date_start=(date_converter('2019-05-01')) #Start export range
  16. date_end=(date_converter('2019-06-20')) #End export range
  17.  
  18. export_file="ExportToQB" #name for IIF file
  19. connected_id="sk_test_Change_me_to_connected_account"
  20. is_connected=False # True (for connected account put connected_id) or False (for standalone does not read the connected_id)
  21. head = "!ACCNT NAME ACCNTTYPE SCD\r\n"\
  22. + "ACCNT Stripe Account BANK\r\n"\
  23. + "ACCNT Stripe Payment Processing Fees EXP\r\n"\
  24. + "ACCNT Stripe Returns INC\r\n"\
  25. + "ACCNT Stripe Sales INC\r\n"\
  26. + "ACCNT Stripe Checking Account BANK\r\n"\
  27. + "ACCNT Stripe Third-party Account EXP 561\r\n"\
  28. + "ACCNT Stripe Other Fees EXP\r\n"\
  29. + "ACCNT Stripe Processing Fees Adj EXP\r\n"\
  30. + "ACCNT Stripe Other Income INC\r\n"\
  31. + "!VEND NAME 1099\r\n"\
  32. + "!TRNS TRNSID TRNSTYPE DATE ACCNT AMOUNT MEMO NAME\r\n"\
  33. + "!SPL SPLID TRNSTYPE DATE ACCNT AMOUNT MEMO NAME\r\n"\
  34. + "!ENDTRNS\r\n"
  35. template = "TRNS DEPOSIT %s Stripe Checking Account %s Transfer ID: %s | STRIPE PAYOUT\r\n"\
  36. + "SPL DEPOSIT %s Stripe Account -%s Transfer ID: %s | STRIPE PAYOUT\r\n"\
  37. + "ENDTRNS\r\n"
  38. template2 = "TRNS GENERAL JOURNAL %s Stripe Sales -%s Charge ID: %s | %s | %s %s\r\n"
  39. template3 = "SPL GENERAL JOURNAL %s Stripe Payment Processing Fees %s Fees for charge ID: %s \r\n"
  40. template4 = "SPL GENERAL JOURNAL %s Stripe Account %s Net for charge ID: %s \r\n"\
  41. + "ENDTRNS\r\n"
  42.  
  43. PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
  44. def main(input_file_name,is_connected):
  45. output_file = open(os.path.join(PROJECT_ROOT, input_file_name + '.iif'), 'w')
  46. if is_connected is True:
  47. data=stripe.Payout.list(arrival_date={'gte': date_start,'lte': date_end},status="paid", stripe_account=connected_id);
  48. else:
  49. data=stripe.Payout.list(arrival_date={'gte': date_start,'lte': date_end},status="paid");
  50.  
  51. json.dumps(data);
  52. x=len(data["data"])
  53.  
  54. output_file.write(head)
  55.  
  56. payoutlist=[]
  57. for trans in range(x):
  58. date=(data["data"][trans]["arrival_date"])
  59. date=time.strftime('%m/%d/%Y', time.localtime(date))
  60. amount=(float(data["data"][trans]["amount"])/ 100)
  61. payoutid=(data["data"][trans]["id"])
  62. json.dumps(payoutlist)
  63. payoutlist.append((str(trans),payoutid))
  64. output_file.write(template % (date,amount,payoutid,date,amount, payoutid))
  65. if trans==x:
  66. continue
  67.  
  68. z=(json.dumps(payoutlist));
  69. qq=json.loads(z)
  70.  
  71. for wi in range(len(qq)):
  72. if is_connected is True:
  73. balance=stripe.BalanceTransaction.list(payout = qq[wi][1], type="charge", stripe_account=connected_id);
  74. else:
  75. balance=stripe.BalanceTransaction.list(payout = qq[wi][1], type="charge");
  76. json.dumps(balance)
  77. try:
  78. ch_date=balance["data"][wi]["available_on"]
  79. ch_date=time.strftime('%m/%d/%Y', time.localtime(ch_date))
  80. ch_amount=float((balance["data"][wi]["amount"])/ 100)
  81. ch_net=float((balance["data"][wi]["net"])/ 100)
  82. ch_id=balance["data"][wi]["source"]
  83. except:
  84. continue
  85. for foo in range(len(balance["data"])):
  86. if is_connected is True:
  87. balance=stripe.BalanceTransaction.list(payout = qq[wi][1], type="charge", stripe_account=connected_id);
  88. else:
  89. balance=stripe.BalanceTransaction.list(payout = qq[wi][1], type="charge");
  90. json.dumps(balance)
  91. ch_desc=balance["data"][foo]["description"]
  92. ch_id=balance["data"][foo]["source"]
  93. stripefee=float((balance["data"][foo]["fee"])/ 100)
  94. #getting charge information from payoutCharge
  95. if is_connected is True:
  96. ch_data=stripe.Charge.retrieve(ch_id, stripe_account=connected_id);
  97. else:
  98. ch_data=stripe.Charge.retrieve(ch_id);
  99. json.dumps(ch_data)
  100. if is_connected is True:
  101. in_data=stripe.Invoice.retrieve(ch_data["invoice"], stripe_account=connected_id);
  102. else:
  103. in_data=stripe.Invoice.retrieve(ch_data["invoice"]);
  104. json.dumps(in_data)
  105. in_email=(in_data["customer_email"])
  106. in_plan=(in_data["lines"]["data"])
  107. json.dumps(in_plan)
  108. ch_data_date=time.strftime('%m/%d/%Y', time.localtime(ch_data["created"]))
  109. try:
  110. in_planname=in_plan[0]["plan"]["nickname"]
  111. output_file.write(template2 % (ch_data_date,ch_amount,ch_id,ch_desc + "| Plan ID: " + in_planname, ch_desc,in_email))
  112. except:
  113. continue
  114. output_file.write(template2 % (ch_data_date,ch_amount,ch_id,ch_desc, ch_desc,in_email))
  115.  
  116. output_file.write(template3 % (ch_data_date,stripefee,ch_id))
  117. output_file.write(template4 % (ch_data_date,ch_net,ch_id))
  118. continue
  119. print("The file named " + PROJECT_ROOT + "/" + input_file_name + ".iif has been updated for your review")
  120.  
  121. main(export_file,is_connected)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement