-
Notifications
You must be signed in to change notification settings - Fork 1
/
paypal_payment.py
50 lines (43 loc) · 1.52 KB
/
paypal_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
import paypalrestsdk
import os
from dotenv import load_dotenv
load_dotenv()
paypalrestsdk.configure({
"mode": "sandbox", # Use "live" for production
"client_id": os.getenv('PAYPAL_CLIENT_ID'),
"client_secret": os.getenv('PAYPAL_CLIENT_SECRET')
})
def create_paypal_payment(total, currency, description, return_url, cancel_url):
"""
Create a PayPal payment.
Args:
total (str): Payment amount.
currency (str): Currency code (e.g., 'USD').
description (str): Description of the payment.
return_url (str): URL to redirect after successful payment.
cancel_url (str): URL to redirect after a failed or canceled payment.
Returns:
dict: PayPal payment response.
"""
paypal_payment = paypalrestsdk.Payment({
"intent": "sale",
"payer": {"payment_method": "paypal"},
"transactions": [{"amount": {"total": total, "currency": currency}, "description": description}],
"redirect_urls": {"return_url": return_url, "cancel_url": cancel_url}
})
if paypal_payment.create():
print(f"Payment created successfully: {paypal_payment.id}")
return paypal_payment
else:
print(paypal_payment.error)
return None
# Example usage
if __name__ == "__main__":
payment = create_paypal_payment(
'10.00',
'USD',
'Test Payment from PayPal',
'https://your-return-url.com',
'https://your-cancel-url.com',
)
print(payment)