PayPalUtil.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : PayPalUtil.py
  4. @Time : 2022/9/1 14:41
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import requests
  10. class PayPalService:
  11. API_BASE_URL = 'https://api-m.paypal.com'
  12. CONTENT_TYPE = 'application/json'
  13. def __init__(self, client_id, client_secret):
  14. self.auth = client_id, client_secret
  15. self.headers = {
  16. 'Content-Type': self.CONTENT_TYPE,
  17. }
  18. self.access_token = self._fetch_access_token()
  19. def _fetch_access_token(self):
  20. data = {'grant_type': 'client_credentials'}
  21. url = f'{self.API_BASE_URL}/v1/oauth2/token'
  22. response = requests.post(url, data=data, headers=self.headers, auth=self.auth)
  23. assert response.status_code == 200
  24. return response.json()['access_token']
  25. def _get_headers(self):
  26. headers = {
  27. 'Authorization': 'Bearer ' + self.access_token,
  28. 'Content-Type': self.CONTENT_TYPE,
  29. }
  30. return headers
  31. def get_transactions(self, params):
  32. """
  33. 交易历史搜索
  34. api:https://developer.paypal.com/docs/api/transaction-search/v1/
  35. @return: 交易历史列表
  36. """
  37. headers = self._get_headers()
  38. response = requests.get(f'{self.API_BASE_URL}/v1/reporting/transactions', headers=headers,
  39. params=params)
  40. # 解析响应
  41. assert response.status_code == 200
  42. return response.json()
  43. if __name__ == '__main__':
  44. pass
  45. # params = (
  46. # ('start_date', '2023-09-15T00:00:00-0800'),
  47. # ('end_date', '2023-09-15T23:59:59-0800'),
  48. # ('fields', 'all'),
  49. # ('page_size', '20'),
  50. # ('page', '1'),
  51. # )
  52. # trans_res = PayPalService().get_transactions(params)
  53. # print(trans_res)