We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28173
    • 409 Posts
    Hi !

    I'm trying to read values sent by a form, inside a Hook to process calculation, but I think I don't doing the thing on the right way...

    Here my FormIt call with the html code for the form :
    [[!FormIt?
       &submitVar=`valid`
       &hooks=`spam,CalculSignature,redirect`
       &redirectTo=`407`
       &store=`1`
       &validate=`nospam:blank,
          vads_cust_last_name:required,
          vads_cust_email:email:required
          
    ]]
    [[!+fi.validation_error_message:notempty=`<p>[[!+fi.validation_error_message]]</p>`]]
    <form method="post" action="[[~[[*id]]]]">  
      <input type="hidden" name="nospam" value="" />
      <input type="hidden" name="vads_site_id" value="593211111">
      <input type="hidden" name="vads_ctx_mode" value="TEST">
      <input type="hidden" name="vads_currency" value="EUR">
      <input type="hidden" name="vads_action_mode" value="INTERACTIVE">
      <input type="hidden" name="vads_page_action" value="PAYMENT">
      <input type="hidden" name="vads_version" value="V2">
      <input type="hidden" name="vads_payment_config" value="SINGLE">
    
          <label>Name</label>  
          <input type="text" name="vads_cust_last_name" value="">
    
          <label>Postal address</label> 
          <input type="text" name="vads_cust_address" value="">
    
          <label>ZIP code</label> 
          <input type="text" name="vads_cust_zip" value="">
    
          <label>City</label> 
          <input type="text" name="vads_cust_city" value="">
    
          <label>Country</label> 
          <input type="text" name="vads_cust_state" value="">
    
          <label>e-mail adress</label> 
          <input type="text" name="vads_cust_email" value="">
    
          <label>Phone</label> 
          <input type="text" name="vads_cust_phone" value="">
    
        <input class="button button-flat-primary" type="submit" value="Valid" name="valid">
    </form>


    After form validation, I need to read all values of the vads_* fields on a function, to calculate a "signature".
    I have a PHP code (not writen by myself!) to doing this :
    // Function to calculate signature
    // $params : array which contain the fields to send by the form
    function getSignature($params){
    
    //Initialisation of the variable which contain the string to be encrypted
    $contenu_signature = "" ;
    
        // sort the fields by alphabetical order
        ksort($params);
            foreach ($params as $nom =>$valeur)
        { 
        // Retreive the vads_  fields
            if (substr($nom,0,5)=='vads_') { 
    
                // Concatenation with "+" separator
                $contenu_signature .= $valeur."+";
            }
        }
    
    // Adding the certificat on the end
    $contenu_signature .= $key;
    
    // SHA-1 algorythm application
    $signature = sha1($contenu_signature);
    return $signature ;
    }


    I have tried to transform this piece of code on a hook/snippet "CalculSignature" to works with FormIt :
    // Function to calculate signature
    
    // We retreive the array of the sent values
    $params = $hook->getValues();
    
    // $params : array which contain the fields to send by the form
    function getSignature($params){
    
    //Initialisation of the variable which contain the string to be encrypted
    $contenu_signature = "" ;
    
        // sort the fields by alphabetical order
        ksort($params);
            foreach ($params as $nom =>$valeur)
        { 
        // Retreive the vads_  fields
            if (substr($nom,0,5)=='vads_') { 
                // Concatenation with "+" separator
                $contenu_signature .= $valeur."+";
            }
        }
    // Adding the certificat on the end
    $contenu_signature .= $key;
    
    // SHA-1 algorythm application
    $signature = sha1($contenu_signature);
    
    // We set the calculated signature on the placeholder [[+signature_calculated]] to be retreive on next step by FormItRetriever
    $hook->setValue('signature_calculated', $signature);
    
    
    }
    
    // We are ending the Hook
    return true;
    


    But my hook doesn't works : after sending the form, the placeholder [[+signature_calculated]] is empty and I don't understand why...

    What I'm doing wrong ? [ed. note: Spheerys last edited this post 6 years, 8 months ago.]
      • 38850
      • 110 Posts
      I may be missing some of your code, but inside your snippet you are using a function:

      function getSignature($params){



      Is this function actually called from somewhere? If you don't call the function, it's not going to run.

      Your code would have to call that function and look more like this:

      $params = $hook->getValues();
      function getSignature($params) {
           $contenu_signature = "" ;
           ksort($params);
          foreach ($params as $nom => $valeur) { 
              if (substr($nom,0,5) == 'vads_') {
                  $contenu_signature .= $valeur . "+";
              }
          }
          $contenu_signature .= $key;
          $signature = sha1($contenu_signature);
          return $signature;
      }
      $hook->setValue('signature_calculated', getSignature($params)); 
      return true;
      


      Also inside this function you are referencing a variable "$key". Is this being set properly in a way that it can be used here?
        • 28173
        • 409 Posts
        You're right, I didn't call the function nowhere...
        The variable $key is a string like 1122334455667788 and it should be concatenated on the end of $contenu_signature separated by a "+"
        Finaly, the $contenu_signature will look like :
        INTERACTIVE+1524+TEST+978+PAYMENT+SINGLE+12345678+20090501193530+654321+V2+1122334455667788


        And this is this line which should be encrypted by a SHA1 algorythm, stored on [[+signature_calculated]] placeholder.

        And you are also right about the $key variable : it is not defined nowhere on the example code.
        On my comprehension, I simply could add this line and this $key value will be solved :
        $key = "1122334455667788";


        So my hook code will look like :
        $params = $hook->getValues();
        $key = "1122334455667788";
        function getSignature($params) {
            $contenu_signature = "" ;
            ksort($params);
            foreach ($params as $nom => $valeur) { 
                if (substr($nom,0,5) == 'vads_') {
                    $contenu_signature .= $valeur . "+";
                }
            }
            $contenu_signature .= $key;
            $signature = sha1($contenu_signature);
            return $signature;
        }
        $hook->setValue('signature_calculated', getSignature($params)); 
        return true;

        But it's still not working : my placeholder is still empty.
          • 28173
          • 409 Posts
          I have a clue... In fact, any placeholder is empty !

          I followed the "Setting Values" part of this documentation : https://docs.modx.com/extras/revo/formit/formit.tutorials-and-examples/formit.examples.custom-hook#FormIt.Examples.CustomHook-SettingValues

          I have simplified my hook like this :
          <?php
          $datestamp = date('Y-m-d H:i:s');
          $hook->setValue('datestamp_submitted', $datestamp);
          return true;


          In my case, I'm not sending a mail after validate the form : I'm using RedirectTo FormIt's parameter to print all sent data on a new resource (with FormItRetriever's help).
          And it's on this new resource I want to print my calculated value. On the precedent example, I put the followed code :
          Date Submitted: [[+datestamp_submitted]]

          And the placeholder is also blank !

          So maybe my function is good now (with your help vigilante wink ), but I'm doing something wrong to retrieve this placeholder value !

          How can I do this ?
            • 38850
            • 110 Posts
            Update your function one more time to pass in the $key value as well to the function:

            $params = $hook->getValues();
            $key = "1122334455667788";
            function getSignature($params, $key) {
                $contenu_signature = "" ;
                ksort($params);
                foreach ($params as $nom => $valeur) { 
                    if (substr($nom,0,5) == 'vads_') {
                        $contenu_signature .= $valeur . "+";
                    }
                }
                $contenu_signature .= $key;
                $signature = sha1($contenu_signature);
                return $signature;
            }
            $hook->setValue('signature_calculated', getSignature($params, $key)); 
            return true;
            


            As for why the placeholder isn't working, we might need more help.

            I think "setValue" is meant as a way to include additional fields into a form. But since your form is already submitted, I don't know if the new value is present in the set of submitted data. It could be that things are happening in the wrong order.
            Someone else may have to confirm this idea.

            The documentation says
            Note that using the setValues() method here will make the corresponding placeholders available to your email chunk; the effect of manually setting values is similar to adding hidden fields to your form.
            But they did this not through the `&hooks` but rather through `&preHooks`.

            You have to use a preHook to add more data (like hidden fields) to the form BEFORE submission. But if you are calculating some data AFTER the form submits and want it included on the redirect page, I'm not exactly sure the way to do that. You might have to do your calculations from a snippet on the redirected page rather than as a hook in the form.

            For example, your form submits as normal, then it goes to the redirect page (where the form placeholders are available). From there you call a snippet and pass in the variables to get your calculated data.
              • 28173
              • 409 Posts
              Quote from: vigilante at Aug 10, 2017, 07:08 PM

              For example, your form submits as normal, then it goes to the redirect page (where the form placeholders are available). From there you call a snippet and pass in the variables to get your calculated data.
              Yes that's exactly what I want !
              I will try to build the snippet by myself and I will come back if needed smiley