-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpayment.py
71 lines (61 loc) · 2.26 KB
/
payment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests
from requests.auth import HTTPBasicAuth
import json
import uuid
def process_payment(merchant_email, amount, details):
details_dict = {
'email': details[0][0],
'clientId': details[0][1],
'secret': details[0][2],
'ref': details[0][3]
}
# Generate unique UUID's for sender_batch_id
batch_id = uuid.uuid4()
# Parameters for making API requests
url = "https://api.sandbox.paypal.com/v1/oauth2/token"
payload = 'grant_type=client_credentials'
headers = {'Accept': 'application/json'}
auth_header = {'Username': details_dict['clientId'],
'Password': details_dict['secret']}
# POST request to get access token
response = requests.post(url, headers=headers, data=payload, auth=HTTPBasicAuth(
auth_header['Username'], auth_header['Password']))
access_token = response.json()['access_token']
print(f'ACCESS TOKEN {access_token}')
# Use the access token from response to create a batch payout
url = "https://api.sandbox.paypal.com/v1/payments/payouts"
payload = {
"sender_batch_header": {
"sender_batch_id": "Payouts_2020_"+str(batch_id),
"email_subject": "You have a payout!",
"email_message": "You have received a payout! Thanks for using our service!"
},
"items": [
{
"recipient_type": "EMAIL",
"amount": {
"value": amount,
"currency": "USD"
},
"note": "Thanks for your patronage!",
"sender_item_id": "201403140001",
"receiver": merchant_email,
"alternate_notification_method": {
"phone": {
"country_code": "91",
"national_number": "9999988888"
}
}
}
]
}
payload_json = json.dumps(payload)
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + access_token
}
# POST request to make a payout
response = requests.post(url, headers=headers, data=payload_json)
print(f'PAYOUT {response.json()}')
print(response.status_code)
return response.status_code