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

PayPal: Get a payable invoice link for your own email

See more PayPal Examples

Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.

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
    Handle hoNow
    Handle hoStamp
    Variant vIssue
    Handle hoIssue
    Boolean iValidDate
    Variant vBody
    Handle hoBody
    Handle hoSendError
Error    Handle hoInvoiceError
    Handle hoInvoice
    Boolean iParsedInvoice
    String sTemp1
    String sTemp2
    Integer iTemp1
    Integer iTemp2
    Boolean bTemp1
    Boolean bTemp2
    Boolean bTemp3
    Boolean bTemp4

    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

    Get ComStringOfEquals Of hoResult "status" "DRAFT" True To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Select an unsent draft."
        Procedure_Return
    End

    //  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
    Get Create (RefClass(cComCkDateTime)) To hoNow
    If (Not(IsComObjectCreated(hoNow))) Begin
        Send CreateComObject of hoNow
    End
    Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
    Get Create (RefClass(cComChilkatStringBuilder)) To hoStamp
    If (Not(IsComObjectCreated(hoStamp))) Begin
        Send CreateComObject of hoStamp
    End
    Get ComStringOf Of hoResult "detail.invoice_date" To sTemp1
    Get ComAppend Of hoStamp sTemp1 To iSuccess
    Get ComAppend Of hoStamp "T00:00:00Z" To iSuccess
    Get Create (RefClass(cComCkDateTime)) To hoIssue
    If (Not(IsComObjectCreated(hoIssue))) Begin
        Send CreateComObject of hoIssue
    End
    Get ComGetAsString Of hoStamp To sTemp1
    Get ComSetFromTimestamp Of hoIssue sTemp1 To iValidDate
    Get ComDiffSeconds Of hoNow     Get pvComObject of hoIssue to vIssue
vIssue To iTemp1
    If ((iValidDate <> True) Or (iTemp1 < 0)) Begin
        Showln "Select a draft with an issue date of today or earlier."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoBody
    If (Not(IsComObjectCreated(hoBody))) Begin
        Send CreateComObject of hoBody
    End
    Get ComUpdateBool Of hoBody "send_to_recipient" False To iSuccess
    Get ComUpdateBool Of hoBody "send_to_invoicer" False To iSuccess
    Get pvComObject of hoBody to vBody
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send" vBody "application/json" 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 hoSendError
        If (Not(IsComObjectCreated(hoSendError))) Begin
            Send CreateComObject of hoSendError
        End
        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoSendError sTemp1 To iSuccess
        Get ComStatusCode Of hoResp To iTemp1
        Get ComStringOf Of hoSendError "name" To sTemp1
        Get ComStringOf Of hoSendError "details[0].issue" To sTemp2
        Showln "HTTP " iTemp1 " " sTemp1 " " sTemp2
        Get ComStringOf Of hoSendError "debug_id" To sTemp1
        Showln "PayPal debug ID: " sTemp1
        Procedure_Return
    End

    //  Read back the invoice: acceptance of the send alone does not guarantee it is payable.
    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 hoInvoiceError
        If (Not(IsComObjectCreated(hoInvoiceError))) Begin
            Send CreateComObject of hoInvoiceError
        End
        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoInvoiceError sTemp1 To iSuccess
        Get ComStatusCode Of hoResp To iTemp1
        Get ComStringOf Of hoInvoiceError "name" To sTemp1
        Get ComStringOf Of hoInvoiceError "details[0].issue" To sTemp2
        Showln "HTTP " iTemp1 " " sTemp1 " " sTemp2
        Get ComStringOf Of hoInvoiceError "debug_id" To sTemp1
        Showln "PayPal debug ID: " sTemp1
        Procedure_Return
    End

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

    Get ComStringOfEquals Of hoInvoice "status" "SENT" True To bTemp1
    Get ComStringOfEquals Of hoInvoice "status" "UNPAID" True To bTemp2
    Get ComStringOfEquals Of hoInvoice "status" "SHARED" True To bTemp3
    Get ComHasMember Of hoInvoice "detail.metadata.recipient_view_url" To bTemp4
    If (((bTemp1 = True) Or (bTemp2 = True) Or (bTemp3 = True)) And (bTemp4 = True)) Begin
        Get ComStringOf Of hoInvoice "detail.metadata.recipient_view_url" To sTemp1
        Showln "Customer payment page: " sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoInvoice "status" To sTemp1
    Showln "Invoice is " sTemp1 ". Retrieve it again when payable."


End_Procedure