I faced a problem (error) when I create payment endpoint the error is:
"{"detail":"400: {'name': 'COMPLIANCE_VIOLATION', 'message': 'Transaction is declined due to compliance violation.', 'information_link': 'https://developer.paypal.com/docs/api/payments/v1/#error-COMPLIANCE_VIOLATION', 'debug_id': 'cf7fd8c0fd383'}"} "
I searched about this error and I found that it is caused by many reasons such as:
- the currency
- restrictions on the sandbox account
- the value of the order is greater than in the accountbut I checked those reasons then the same error and checked client_ID and client_secret
I use paypalrestsdk library.
I made a static web page that make on it the checkout.
I am from egypt default paypal currency EUR.
I expected to process this payment only to test the feature.
from fastapi import FastAPI, HTTPException, Request, Queryfrom fastapi.responses import HTMLResponseimport paypalrestsdkimport osfrom dotenv import load_dotenvload_dotenv() # Load environment variables from .env fileapp = FastAPI()# PayPal configurationpaypalrestsdk.configure({"mode": "sandbox", # or "live" for production"client_id": os.getenv("PAYPAL_CLIENT_ID"),"client_secret": os.getenv("PAYPAL_CLIENT_SECRET")})@app.post("/create-order")async def create_order(request: Request): body = await request.json() cart = body.get('cart', []) # Summarize cart total amount total_amount = sum(item['price'] for item in cart) payment = paypalrestsdk.Payment({"intent": "sale","payer": {"payment_method": "paypal" },"transactions": [{"amount": {"total": f"{total_amount:.2f}","currency": "EUR" # Change currency to EUR },"description": "This is the payment transaction description." }],"redirect_urls": {"return_url": "http://localhost:8000/execute-payment","cancel_url": "http://localhost:8000/" } }) if payment.create(): for link in payment.links: if link.rel == "approval_url": approval_url = str(link.href) return {"approval_url": approval_url, "paymentID": payment.id} else: raise HTTPException(status_code=400, detail=payment.error)@app.get("/execute-payment")async def execute_payment(paymentId: str = Query(...), PayerID: str = Query(...)): try: payment = paypalrestsdk.Payment.find(paymentId) if payment.execute({"payer_id": PayerID}): return {"status": "Payment executed successfully"} else: error_details = payment.error print(f"PayPal Payment Execution Error: {error_details}") raise HTTPException(status_code=400, detail=error_details) except Exception as e: print(f"An error occurred: {e}") raise HTTPException(status_code=400, detail=str(e))@app.post("/paypal/webhook")async def handle_webhook(request: Request): body = await request.json() event_type = body.get("event_type") if event_type == "PAYMENT.SALE.COMPLETED": # Handle payment sale completed event pass return {"status": "success"}@app.get("/", response_class=HTMLResponse)async def read_index(): with open("static/index.html") as f: return HTMLResponse(content=f.read())