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

PayPal: Authorize a buyer-approved order

See more PayPal Examples

Authorize the stored order after the buyer approves it. Save the returned authorization ID and amount for capture or void. An authorization reserves funds; it is not a capture.

PayPal documentation

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Handle hoCrypt
    String sRequestId
    Variant vBody
    Handle hoBody
    Handle hoErrorInfo
    Handle hoResult
    Boolean iParsed
    Integer i
    Integer j
    String sTemp1
    String sTemp2
    String sTemp3
    Integer iTemp1
    Integer iTemp2

    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

    //  Use the approved order created with intent AUTHORIZE.
    Get ComSetUrlVar Of hoHttp "id" "ORDER_ID" To iSuccess
    //  Create and persist this key once per business operation. On a timeout,
    //  retry the same request with its saved key, not a newly generated one.
    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Get ComGenerateUuid Of hoCrypt To sRequestId
    Send ComSetRequestHeader To hoHttp "PayPal-Request-Id" sRequestId
    Send ComSetRequestHeader To hoHttp "Prefer" "return=representation"
    Get Create (RefClass(cComChilkatJsonObject)) To hoBody
    If (Not(IsComObjectCreated(hoBody))) Begin
        Send CreateComObject of hoBody
    End

    Get pvComObject of hoBody to vBody
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" "https://api-m.sandbox.paypal.com/v2/checkout/orders/{$id}/authorize" 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 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 iParsed
    If (iParsed <> True) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Expected a JSON response; HTTP " iTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoResult "id" To sTemp1
    Showln "ID: " sTemp1
    Get ComStringOf Of hoResult "status" To sTemp1
    Showln "Status: " sTemp1
    //  A purchase unit may have no authorizations yet, or more than one.
    Move 0 To i
    While (i < (ComSizeOfArray(hoResult, "purchase_units")))
        Set ComI Of hoResult To i
        Move 0 To j
        While (j < (ComSizeOfArray(hoResult, "purchase_units[i].payments.authorizations")))
            Set ComJ Of hoResult To j
            Get ComStringOf Of hoResult "purchase_units[i].reference_id" To sTemp1
            Get ComStringOf Of hoResult "purchase_units[i].payments.authorizations[j].id" To sTemp2
            Showln "Unit: " sTemp1 "  authorization: " sTemp2
            Get ComStringOf Of hoResult "purchase_units[i].payments.authorizations[j].status" To sTemp1
            Get ComStringOf Of hoResult "purchase_units[i].payments.authorizations[j].amount.value" To sTemp2
            Get ComStringOf Of hoResult "purchase_units[i].payments.authorizations[j].amount.currency_code" To sTemp3
            Showln "  " sTemp1 "  " sTemp2 " " sTemp3
            Move (j + 1) To j
        Loop

        Move (i + 1) To i
    Loop



End_Procedure