<cfinclude template="__Security-Block.cfm">

<!--- Set the function & Fax Service Response Parameters --->
<cfset LOCAL.TheReturnedData                    = "">
<cfset LOCAL.TheValidToken                      = "">
<cfset LOCAL.returnFaxSendResult                = StructNew()>
<cfset LOCAL.returnFaxSendResult.ErrorMessage   = "">
<cfset LOCAL.returnFaxSendResult.ValidFax       = "">
<cfset LOCAL.returnFaxSendResult.FaxProcessTime = "">
<cfset ARGUMENTS.FromFaxNumber = "13235444226" />
<cfset ARGUMENTS.ToFaxNumber = "13235444226" />
<cfset ARGUMENTS.ToName = "Bob Smith" /> 
<cfset ARGUMENTS.user_ID = "0dfaea2a-f711-4057-9a5b-e891046aad7a" />

<cfset request.oAuthAccessToken = CreateObject("component","components.general.oAuth").GetAccessToken(
                OauthServerCallTimeOuts = '5',
                TheClientID             = '9a6592e3-01a0-4c3b-acc9-50f6eaf60441',
                TheClientSecret         = 'lAXMJ2KOjpDmWHrc',
                OauthTokenEndpoint      = 'https://api.securedocex.com/tokens',
                OauthGrantType          =  'client_credentials') >

<!--- ERROR: capture any oAuth service errors --->
<cfif LEN(trim(request.oAuthAccessToken.ErrorMessage)) NEQ 0>
    <cfset LOCAL.returnFaxSendResult.ErrorMessage = request.oAuthAccessToken.ErrorMessage>
</cfif>
<!--- ====================== END: CAPTURING OAUTH SERVICE CALL ===================== ---> 
        
<!--- ====================== BEGIN: Fax SERVICE CALL ===================== ---> 
<!--- if valid access token exists, make Fax service call --->
<cfif LEN(trim(request.oAuthAccessToken.ValidToken)) NEQ 0>
    <cfset LOCAL.AuthorizationEndpoint = "https://api.securedocex.com/faxes">
    <cfscript>
        myPdf = fileReadBinary( expandPath( "/test.pdf" ) );
        request.myPdf = toBase64( myPdf );
        //request.myPdf = toBinary( myPdf );
    </cfscript>

   <cfset stFields_old = 
       {
            "client_data": {
                "billing_code": "This is a test",
                "client_code": "client code",
                "client_id": "client id",
                "client_matter": "client matter",
                "client_name": "client name",
                "client_reference_id": "Client refernce id"
            },
            "destinations": [
                {
                    "fax_number" : "#ARGUMENTS.ToFaxNumber#",
                    "to_name" : "Bob Smith",
                    "to_company": "Acme Widgets Inc."
                }
            ],
            "documents": [
                {
                    "document_content": "#request.myPdf#",
                    "document_type": "PDF"
                }
            ],
           "fax_options" : 
           {
                        "image_resolution" : "FINE",
                        "include_cover_page" : "true",
                        "custom_CallerID" : "18189785900",
                        "header_options": {
                        "line_1":["${CSID}", "${DATE_TIME2}", "${FAX_ID}", "Page: ${CURRENT_PAGE} / ${TOTAL_PAGE}"]
                    },
                "custom_CSID" : "custom csid",
                "cover_page_options" : 
                {
                    "from_name" : "Mary Adams",
                    "subject" : "Information Fax",
                    "message" : "Sample fax",
                    "retry_options":{
                         "non_billable":4,
                         "billable":10,
                         "human_answer":1
                     }
                }
            }
        }
    > 

    <cfset Request.stFields = 
       {        
            "destinations": [
                {
                    "fax_number" : "#ARGUMENTS.ToFaxNumber#",
                    "to_name" : "#ARGUMENTS.ToName#",
                    "to_company": "#ARGUMENTS.ToName#"
                }
            ],
            "documents": [
                {
                    "document_content": "#request.myPdf#",
                    "document_type": "PDF"
                }
            ]
        }
    >

    <!--- Make web service call to the REST service to retrieve the Fax --->
    <cfset LOCAL.RestStartTime  = GetTickCount()>
    <cfhttp 
        url         = "#LOCAL.AuthorizationEndpoint#"
        method      = "POST" 
        timeout     =  "10"
        result      = "httpFaxResponse">
        <cfhttpparam name="Content-Type"  type="HEADER" value="application/pdf">
        <cfhttpparam name="Authorization" type="HEADER" value="#request.oAuthAccessToken.ValidToken#">
        <cfhttpparam name="user-id" type="HEADER" value="#ARGUMENTS.user_ID#">
        <cfhttpparam type="body" value="#serializeJSON(Request.stFields)#">
        <!--- <cfhttpparam type="formfield" name="documents" value="#serializeJSON(documents)#">
        <cfhttpparam type="formfield" name="destinations" value="#serializeJSON(destinations)#"> --->
    </cfhttp>
    <cfset LOCAL.RestEndTime = GetTickCount()>
    <cfset LOCAL.returnFaxSendResult.FaxProcessTime = NumberFormat((LOCAL.RestEndTime - LOCAL.RestStartTime)/1000,'__.___')>

    <cfif httpFaxResponse.responseHeader.Status_Code eq 201>             
        <cftry>
            <!--- set the FAX into the return struct --->
            <cfset LOCAL.Faxresponse ="#deserializeJSON(httpFaxResponse.Filecontent)#" />
            <cfset LOCAL.returnFaxSendResult.fax_id = LOCAL.Faxresponse[1].fax_id>
            <cfset LOCAL.returnFaxSendResult.destination_fax_number = LOCAL.Faxresponse[1].destination_fax_number>
            <cfset LOCAL.returnFaxSendResult.ValidFax       = "Yes">
            <cfoutput>
                   <!---  Fax sent sucessfully  ---><cfdump var="#LOCAL.returnFaxSendResult#"><cfabort>
            </cfoutput>
            <cfcatch>
                 <cfset LOCAL.returnFaxSendResult.ValidFax       = "No">
                <!--- ERROR: add general CFCATCH error to error log statement --->
                <cfset LOCAL.returnFaxSendResult.ErrorMessage = LOCAL.returnFaxSendResult.ErrorMessage & "FAX SEND SERVICE: Error when we get response |CFCATCH MESSAGE: #CFCATCH.Message#|CFCATCH DETAIL: #CFCATCH.Detail#|CFC | Fax Number #ARGUMENTS.ToFaxNumber# | Return code: #httpFaxResponse.responseHeader.Status_Code#">
            </cfcatch>
        </cftry>                
    <cfelse>
            <!--- ERROR: add failed FAX service call to error log statement --->
            <cfset LOCAL.returnFaxSendResult.ValidFax       = "No">
           <cfset LOCAL.returnFaxSendResult.ErrorMessage = LOCAL.returnFaxSendResult.ErrorMessage & "FAX SEND SERVICE: Error when we send fax to the customer |CFCATCH MESSAGE: #CFCATCH.Message#|CFCATCH DETAIL: #CFCATCH.Detail#|CFC | Fax Number: #ARGUMENTS.ToFaxNumber#  | Return code: #httpFaxResponse.responseHeader.Status_Code#">
    </cfif>
   
</cfif>
