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

PayPal: Remove an external payment recorded on the wrong invoice

See more PayPal Examples

Use the saved external payment record ID. Verify that it belongs to the selected invoice, then remove the bookkeeping entry. The bank transfer itself is unaffected. Record it on the correct invoice as a separate step.

PayPal documentation

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 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.
    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkAuthToken(http, "SANDBOX_ACCESS_TOKEN")
    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::ckSetUrlVar(http,"id","INVOICE_ID")

    success = CkHttp::ckHttpNoBody(http,"GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp)
    If success <> 1
        Debug "No HTTP response: " + CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    If (CkHttpResponse::ckStatusCode(resp) < 200) OR (CkHttpResponse::ckStatusCode(resp) >= 300)
        errorInfo.i = CkJsonObject::ckCreate()
        If errorInfo.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        CkJsonObject::ckLoad(errorInfo,CkHttpResponse::ckBodyStr(resp))
        Debug "HTTP " + Str(CkHttpResponse::ckStatusCode(resp)) + " " + CkJsonObject::ckStringOf(errorInfo,"name") + " " + CkJsonObject::ckStringOf(errorInfo,"details[0].issue")
        Debug "PayPal debug ID: " + CkJsonObject::ckStringOf(errorInfo,"debug_id")
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(errorInfo)
        ProcedureReturn
    EndIf

    result.i = CkJsonObject::ckCreate()
    If result.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    parsedResult.i = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
    If parsedResult <> 1
        Debug "Expected a JSON response; HTTP " + Str(CkHttpResponse::ckStatusCode(resp))
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(errorInfo)
        CkJsonObject::ckDispose(result)
        ProcedureReturn
    EndIf

    paymentId.s = "EXTERNAL_PAYMENT_ID"
    matched.i = 0
    i.i = 0
    While (i < CkJsonObject::ckSizeOfArray(result,"payments.transactions"))
        CkJsonObject::setCkI(result, i)
        If (CkJsonObject::ckStringOfEquals(result,"payments.transactions[i].payment_id",paymentId,1) = 1) AND (CkJsonObject::ckStringOfEquals(result,"payments.transactions[i].type","EXTERNAL",1) = 1)
            matched = 1
        EndIf

        i = i + 1
    Wend
    If matched <> 1
        Debug "This invoice has no matching external payment record."
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(errorInfo)
        CkJsonObject::ckDispose(result)
        ProcedureReturn
    EndIf

    CkHttp::ckSetUrlVar(http,"paymentId",paymentId)
    success = CkHttp::ckHttpNoBody(http,"DELETE","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments/{$paymentId}",resp)
    If success <> 1
        Debug "No HTTP response: " + CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(errorInfo)
        CkJsonObject::ckDispose(result)
        ProcedureReturn
    EndIf

    If (CkHttpResponse::ckStatusCode(resp) < 200) OR (CkHttpResponse::ckStatusCode(resp) >= 300)
        removeError.i = CkJsonObject::ckCreate()
        If removeError.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        CkJsonObject::ckLoad(removeError,CkHttpResponse::ckBodyStr(resp))
        Debug "HTTP " + Str(CkHttpResponse::ckStatusCode(resp)) + " " + CkJsonObject::ckStringOf(removeError,"name") + " " + CkJsonObject::ckStringOf(removeError,"details[0].issue")
        Debug "PayPal debug ID: " + CkJsonObject::ckStringOf(removeError,"debug_id")
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(errorInfo)
        CkJsonObject::ckDispose(result)
        CkJsonObject::ckDispose(removeError)
        ProcedureReturn
    EndIf

    Debug "External payment entry removed."


    CkHttp::ckDispose(http)
    CkHttpResponse::ckDispose(resp)
    CkJsonObject::ckDispose(errorInfo)
    CkJsonObject::ckDispose(result)
    CkJsonObject::ckDispose(removeError)


    ProcedureReturn
EndProcedure