python-3.xdjangoxero-api

error in deleting payments in Xero, confused what else am I missing


first time poster and really desperate to find a solution to this. I am running a django project, one of the functions will delete payments in Xero, reading through the documentation, I thought it should be easy as it only needs one parameter and to include in the resourceID. but I got an error back saying

{"Title":"An error occurred","Detail":"An error occurred in Xero. Check the API Status page http://status.developer.xero.com for current service status.","Status":500,"Instance":"f137f1e1-4011-43ed-b921-1e1827a90dad"}

This is my code snippet.

        for paymentID in payment_IDs:
            token = XeroOAuth2Token.objects.latest('id')
            delete_payment_endpoint = f'https://api.xero.com/api.xro/2.0/Payments/{paymentID}'

            headers_del_payments = {
                "Authorization":f"Bearer {token.access_token}",
                "Xero-Tenant-Id": TENANT_ID,
                "Accept": "application/json",
                "Content-Type": "application/json"
            }
        
            response = requests.post(delete_payment_endpoint, headers=headers_del_payments)
            return HttpResponse(response.text)

I tried adding Status:DELETE or paymentID:{ID here} as a payload but it just gave me an Error14

{ "ErrorNumber": 14, "Type": "PostDataInvalidException", "Message": "Invalid Json data" }

Solution

  • According to the Xero payments endpoint API documentation, you need a post body included. Your API call currently doesn't have this. Once you include this, the payment should be deleted:

    for paymentID in payment_IDs:
                token = XeroOAuth2Token.objects.latest('id')
                delete_payment_endpoint = f'https://api.xero.com/api.xro/2.0/Payments/{paymentID}'
    
                headers_del_payments = {
                    "Authorization":f"Bearer {token.access_token}",
                    "Xero-Tenant-Id": TENANT_ID,
                    "Accept": "application/json",
                    "Content-Type": "application/json"
                }
    
                body = {
                    "Status": "DELETED"
                }
            
                response = requests.post(delete_payment_endpoint, headers=headers_del_payments,json=body)
                return HttpResponse(response.text)