Sample code for 30+ languages & platforms
Go 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 Go Downloads

Go
    success := false

    //  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.
    http := chilkat.NewHttp()
    http.SetAuthToken("SANDBOX_ACCESS_TOKEN")
    resp := chilkat.NewHttpResponse()

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

    success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp)
    if success != true {
        fmt.Println("No HTTP response: ", http.LastErrorText())
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        return
    }

    if (resp.StatusCode() < 200) || (resp.StatusCode() >= 300) {
        errorInfo := chilkat.NewJsonObject()
        errorInfo.Load(resp.BodyStr())
        fmt.Println("HTTP ", resp.StatusCode(), " ", *errorInfo.StringOf("name"), " ", *errorInfo.StringOf("details[0].issue"))
        fmt.Println("PayPal debug ID: ", *errorInfo.StringOf("debug_id"))
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        return
    }

    result := chilkat.NewJsonObject()
    parsed := result.Load(resp.BodyStr())
    if parsed != true {
        fmt.Println("Expected a JSON response; HTTP ", resp.StatusCode())
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        return
    }

    if result.StringOfEquals("status","DRAFT",true) != true {
        fmt.Println("Leaving invoice unchanged: ", *result.StringOf("status"))
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        return
    }

    success = http.HttpNoBody("DELETE","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp)
    if success != true {
        fmt.Println("No HTTP response: ", http.LastErrorText())
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        return
    }

    if (resp.StatusCode() < 200) || (resp.StatusCode() >= 300) {
        deleteError := chilkat.NewJsonObject()
        deleteError.Load(resp.BodyStr())
        fmt.Println("HTTP ", resp.StatusCode(), " ", *deleteError.StringOf("name"), " ", *deleteError.StringOf("details[0].issue"))
        fmt.Println("PayPal debug ID: ", *deleteError.StringOf("debug_id"))
        http.DisposeHttp()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        deleteError.DisposeJsonObject()
        return
    }

    fmt.Println("Draft deleted; HTTP ", resp.StatusCode())

    http.DisposeHttp()
    resp.DisposeHttpResponse()
    errorInfo.DisposeJsonObject()
    result.DisposeJsonObject()
    deleteError.DisposeJsonObject()