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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Handle hoErrorInfo
    Handle hoResult
    Boolean iParsedResult
    String sPaymentId
    Boolean iMatched
    Integer i
    Handle hoRemoveError
    String sTemp1
    String sTemp2
    Integer iTemp1
    Integer iTemp2
    Boolean bTemp1
    Boolean bTemp2

    Move False To iSuccess

    //  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.
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComAuthToken Of hoHttp To "SANDBOX_ACCESS_TOKEN"
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End

    Get ComSetUrlVar Of hoHttp "id" "INVOICE_ID" To iSuccess

    Get pvComObject of hoResp to vResp
    Get ComHttpNoBody Of hoHttp "GET" "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}" vResp To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln "No HTTP response: " sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Get ComStatusCode Of hoResp To iTemp2
    If ((iTemp1 < 200) Or (iTemp2 >= 300)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoErrorInfo
        If (Not(IsComObjectCreated(hoErrorInfo))) Begin
            Send CreateComObject of hoErrorInfo
        End
        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoErrorInfo sTemp1 To iSuccess
        Get ComStatusCode Of hoResp To iTemp1
        Get ComStringOf Of hoErrorInfo "name" To sTemp1
        Get ComStringOf Of hoErrorInfo "details[0].issue" To sTemp2
        Showln "HTTP " iTemp1 " " sTemp1 " " sTemp2
        Get ComStringOf Of hoErrorInfo "debug_id" To sTemp1
        Showln "PayPal debug ID: " sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoResult
    If (Not(IsComObjectCreated(hoResult))) Begin
        Send CreateComObject of hoResult
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoResult sTemp1 To iParsedResult
    If (iParsedResult <> True) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Expected a JSON response; HTTP " iTemp1
        Procedure_Return
    End

    Move "EXTERNAL_PAYMENT_ID" To sPaymentId
    Move False To iMatched
    Move 0 To i
    While (i < (ComSizeOfArray(hoResult, "payments.transactions")))
        Set ComI Of hoResult To i
        Get ComStringOfEquals Of hoResult "payments.transactions[i].payment_id" sPaymentId True To bTemp1
        Get ComStringOfEquals Of hoResult "payments.transactions[i].type" "EXTERNAL" True To bTemp2
        If ((bTemp1 = True) And (bTemp2 = True)) Begin
            Move True To iMatched
        End

        Move (i + 1) To i
    Loop

    If (iMatched <> True) Begin
        Showln "This invoice has no matching external payment record."
        Procedure_Return
    End

    Get ComSetUrlVar Of hoHttp "paymentId" sPaymentId To iSuccess
    Get pvComObject of hoResp to vResp
    Get ComHttpNoBody Of hoHttp "DELETE" "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments/{$paymentId}" vResp To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln "No HTTP response: " sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Get ComStatusCode Of hoResp To iTemp2
    If ((iTemp1 < 200) Or (iTemp2 >= 300)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoRemoveError
        If (Not(IsComObjectCreated(hoRemoveError))) Begin
            Send CreateComObject of hoRemoveError
        End
        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoRemoveError sTemp1 To iSuccess
        Get ComStatusCode Of hoResp To iTemp1
        Get ComStringOf Of hoRemoveError "name" To sTemp1
        Get ComStringOf Of hoRemoveError "details[0].issue" To sTemp2
        Showln "HTTP " iTemp1 " " sTemp1 " " sTemp2
        Get ComStringOf Of hoRemoveError "debug_id" To sTemp1
        Showln "PayPal debug ID: " sTemp1
        Procedure_Return
    End

    Showln "External payment entry removed."


End_Procedure