# -*- encoding: utf-8 -*- """ @File : PayPalUtil.py @Time : 2022/9/1 14:41 @Author : stephen @Email : zhangdongming@asj6.wecom.work @Software: PyCharm """ import requests class PayPalService: API_BASE_URL = 'https://api-m.paypal.com' CONTENT_TYPE = 'application/json' def __init__(self, client_id, client_secret): self.auth = client_id, client_secret self.headers = { 'Content-Type': self.CONTENT_TYPE, } self.access_token = self._fetch_access_token() def _fetch_access_token(self): data = {'grant_type': 'client_credentials'} url = f'{self.API_BASE_URL}/v1/oauth2/token' response = requests.post(url, data=data, headers=self.headers, auth=self.auth) assert response.status_code == 200 return response.json()['access_token'] def _get_headers(self): headers = { 'Authorization': 'Bearer ' + self.access_token, 'Content-Type': self.CONTENT_TYPE, } return headers def get_transactions(self, params): """ 交易历史搜索 api:https://developer.paypal.com/docs/api/transaction-search/v1/ @return: 交易历史列表 """ headers = self._get_headers() response = requests.get(f'{self.API_BASE_URL}/v1/reporting/transactions', headers=headers, params=params) # 解析响应 assert response.status_code == 200 return response.json() if __name__ == '__main__': pass # params = ( # ('start_date', '2023-09-15T00:00:00-0800'), # ('end_date', '2023-09-15T23:59:59-0800'), # ('fields', 'all'), # ('page_size', '20'), # ('page', '1'), # ) # trans_res = PayPalService().get_transactions(params) # print(trans_res)