Guest User

Untitled

a guest
Sep 12th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. import stripe
  2. import json
  3. import mysql.connector
  4. from datetime import datetime
  5.  
  6. stripe.api_key = ""
  7.  
  8.  
  9. def cents_to_dollars(cents):
  10. if cents is not None:
  11. return cents / 100
  12.  
  13.  
  14. def unix_to_readable(ts):
  15. if ts is not None:
  16. return datetime.utcfromtimestamp(ts).strftime('%m/%d/%Y')
  17.  
  18.  
  19. # Get a list of successful payouts from Stripe
  20. payouts = stripe.Payout.list(limit=20)
  21. payout_count = 0
  22. for p in payouts:
  23. payout_count += 1
  24. charge_count = 0
  25. payout_id = p['id']
  26. payout_amount = cents_to_dollars(p['amount'])
  27. expected_by = unix_to_readable(p['arrival_date'])
  28. # print("({})".format(payout_count))
  29. print('Date: {}\nPayout amount: {:.2f}'.format(
  30. expected_by, payout_amount))
  31. print('Transactions in payout:')
  32. charges = stripe.BalanceTransaction.list(
  33. limit=30, payout=payout_id)
  34. for charge in charges:
  35. if charge['amount'] > 0:
  36. charge_id = charge['source']
  37. charge_count += 1
  38. amount_billed = cents_to_dollars(charge['amount'])
  39. fee_meta = charge['fee_details']
  40. for fm in fee_meta:
  41. fee = cents_to_dollars(fm['amount'])
  42. # Reconstruct client data based on charge id in database
  43. # Prabaly should use JOINs
  44. cnx = mysql.connector.connect(
  45. user='',
  46. password='',
  47. host='localhost',
  48. port=3306,
  49. database='')
  50. cursor = cnx.cursor()
  51. client_id_query = (
  52. "SELECT client_id FROM invoices WHERE charge_id = %s")
  53. cursor.execute(client_id_query, (charge_id,))
  54. invoice_data = cursor.fetchone()
  55. client_id = invoice_data[0]
  56. # note = invoice_data[1]
  57. client_address_query = (
  58. "SELECT country_code, administrative_area, "
  59. "locality, postal_code, thoroughfare, premise "
  60. "FROM client_addresses WHERE client_id = %s")
  61. client_query = (
  62. "SELECT first_name, last_name, email "
  63. "FROM clients WHERE id = %s")
  64.  
  65. # Get client name, email
  66. cursor.execute(client_query, (client_id))
  67. print(client_id)
  68. client = cursor.fetchone()
  69. first_name = client[0]
  70. last_name = client[1]
  71. email = client[2]
  72. if not email:
  73. email = ''
  74. if not first_name:
  75. first_name = ''
  76. if not last_name:
  77. last_name = ''
  78. # Get client address
  79. cursor.execute(client_address_query, (client_id))
  80. client_address = cursor.fetchone()
  81. country_code = client_address[0]
  82. administrative_area = client_address[1]
  83. locality = client_address[2]
  84. postal_code = client_address[3]
  85. premise = client_address[5]
  86. if not premise:
  87. premise = ''
  88. thoroughfare = client_address[4] + premise
  89.  
  90. # print(" "*4 + "({})".format(charge_count))
  91. print(" "*4 + "Amount billed: {:.2f}".format(amount_billed))
  92. print(" "*4 + "Fee: {:.2f}".format(fee))
  93. print(" "*4 + "Type of Service: ")
  94. print(" "*4 + "Name: {} {}".format(first_name, last_name))
  95. print(" "*4 + "Address:")
  96. print(" "*4 + "{} {}".format(thoroughfare, premise))
  97. print(" "*4 + "{} {}".format(locality, administrative_area))
  98. print(" "*4 + "{}".format(postal_code))
  99. print("\n")
  100. # Clear database connection
  101. close_cursor = cursor.close()
Add Comment
Please, Sign In to add comment