Created
March 20, 2019 20:05
-
-
Save grepwood/839a58bd1846e02c2cfa72d026bd77a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.6 | |
import requests | |
import json | |
import re | |
from datetime import datetime | |
from datetime import timedelta | |
def token(): | |
headers = { | |
'Accept': 'application/json', | |
'Accept-Language': 'en_US', | |
} | |
data = [ | |
('grant_type', 'client_credentials'), | |
] | |
response = requests.post('https://api.paypal.com/v1/oauth2/token', headers=headers, data=data, auth=( | |
client_id, | |
secret_id)) | |
data = json.loads(response.text) | |
return data['access_token'] | |
def transaction_history(token, start_time, end_time): | |
headers = { | |
'Content-Type': 'application/json', | |
'Authorization': 'Bearer ' + token, | |
} | |
params = ( | |
('start_date', start_time), # dates must look like this: '2018-06-10T23:20:50.52Z' | |
('end_date', end_time), | |
('fields', 'transaction_info, payer_info, cart_info'), #all | |
) | |
results = [] | |
response = requests.get('https://api.paypal.com/v1/reporting/transactions', headers=headers, params=params) | |
if response.status_code != 200: | |
print('transaction_history: failed with HTTP code',response.status_code) | |
return results | |
data = json.loads(response.text) | |
print(data) | |
return 0 | |
for transaction in data['transaction_details']: | |
if "T0006" not in transaction['transaction_info']['transaction_event_code'] \ | |
and bool(transaction['payer_info']['payer_name'])\ | |
and float(transaction['transaction_info']['transaction_amount']['value']) > 0: | |
results.append({ | |
'item': transaction['cart_info']['item_details'][0]['item_name'] if len(transaction['cart_info']['item_details']) > 0 and 'item_name' in transaction['cart_info']['item_details'][0] else '', | |
'date': transaction['transaction_info']['transaction_initiation_date'] if 'transaction_initiation_date' in transaction['transaction_info'] else '', | |
'name': transaction['payer_info']['payer_name']['alternate_full_name'] if 'payer_name' in transaction['payer_info'] and 'alternate_full_name' in transaction['payer_info']['payer_name'] else '', | |
'value': transaction['transaction_info']['transaction_amount']['value'], | |
'currency': transaction['transaction_info']['transaction_amount']['currency_code'] | |
}) | |
return results | |
i = token() | |
time_end = datetime.utcnow() | |
time_start = time_end + timedelta(days=-30) | |
time_start = re.sub(r" +","T",time_start.strftime('%Y-%m-%dT%H:%M:%S.%f'))[:-4] + 'Z' | |
time_end = re.sub(r" +","T",time_end.strftime('%Y-%m-%dT%H:%M:%S.%f'))[:-4] + 'Z' | |
x = transaction_history(i,time_start,time_end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Nosskirneh, sorry, no idea on what would work.
My code is slightly different, but it works just fine...
If the code below (or the above), doesn't work, I would guess it may be a PayPal configuration issue maybe...
Maybe the token is for the sandbox environment, only?
`
import requests
import json
import re
from dateutil import parser
from datetime import datetime
from datetime import timedelta
from backend import db, config
from backend.database.models import PayPal
now = datetime.now()
def token():
headers = {
'Accept': 'application/json',
'Accept-Language': 'en_US',
}
def transaction_history(token, start_time):
def get_transactions(time_start=datetime(2018,1,1)):
def process_data(all_transactions):
def convert_date_format(date):
date = re.sub(r" +","T",date.strftime('%Y-%m-%dT%H:%M:%S.%f'))[:-4] + 'Z'
return date
def make_history():
def last_entry():
def update_db():
`