Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.0.0+

PayPal: Delete an unsent draft invoice

See more PayPal Examples

This example deliberately checks for DRAFT before deletion. Use the invoice ID from your own records. A successful DELETE has no response body; use cancellation for a sent, unpaid invoice.

PayPal documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loResp
LOCAL loErrorInfo
LOCAL loResult
LOCAL lnParsed
LOCAL loDeleteError

lnSuccess = 0

*  See Unlock Chilkat before making requests.
*  For the merchant's internal backoffice application. Load the sandbox token
*  from the merchant's credential store. Keep it out of publicly distributed apps.
*  See Get a PayPal access token.
loHttp = CreateObject('Chilkat.Http')
loHttp.AuthToken = "SANDBOX_ACCESS_TOKEN"
loResp = CreateObject('Chilkat.HttpResponse')

loHttp.SetUrlVar("id","INVOICE_ID")

lnSuccess = loHttp.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",loResp)
IF (lnSuccess <> 1) THEN
    ? "No HTTP response: " + loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loResp
    CANCEL
ENDIF

IF ((loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    loErrorInfo = CreateObject('Chilkat.JsonObject')
    loErrorInfo.Load(loResp.BodyStr)
    ? "HTTP " + STR(loResp.StatusCode) + " " + loErrorInfo.StringOf("name") + " " + loErrorInfo.StringOf("details[0].issue")
    ? "PayPal debug ID: " + loErrorInfo.StringOf("debug_id")
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    CANCEL
ENDIF

loResult = CreateObject('Chilkat.JsonObject')
lnParsed = loResult.Load(loResp.BodyStr)
IF (lnParsed <> 1) THEN
    ? "Expected a JSON response; HTTP " + STR(loResp.StatusCode)
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    CANCEL
ENDIF

IF (loResult.StringOfEquals("status","DRAFT",1) <> 1) THEN
    ? "Leaving invoice unchanged: " + loResult.StringOf("status")
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    CANCEL
ENDIF

lnSuccess = loHttp.HttpNoBody("DELETE","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",loResp)
IF (lnSuccess <> 1) THEN
    ? "No HTTP response: " + loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    CANCEL
ENDIF

IF ((loResp.StatusCode < 200) OR (loResp.StatusCode >= 300)) THEN
    loDeleteError = CreateObject('Chilkat.JsonObject')
    loDeleteError.Load(loResp.BodyStr)
    ? "HTTP " + STR(loResp.StatusCode) + " " + loDeleteError.StringOf("name") + " " + loDeleteError.StringOf("details[0].issue")
    ? "PayPal debug ID: " + loDeleteError.StringOf("debug_id")
    RELEASE loHttp
    RELEASE loResp
    RELEASE loErrorInfo
    RELEASE loResult
    RELEASE loDeleteError
    CANCEL
ENDIF

? "Draft deleted; HTTP " + STR(loResp.StatusCode)

RELEASE loHttp
RELEASE loResp
RELEASE loErrorInfo
RELEASE loResult
RELEASE loDeleteError