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 have a page with a fulfilled form :
    <form method="post" action="https://myurl.com/">  
      <input type="hidden" name="nospam" value="" />
      <input type="hidden" name="vads_site_id" value="5932111">
      <input type="hidden" name="vads_ctx_mode" value="TEST">
      <input type="hidden" name="vads_trans_id" value="170810203408011">
      <input type="hidden" name="vads_trans_date" value="10/08/17 20:34:08">
      <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">
      <input type="hidden" name="signature" value="[[CalculateSignature]]" /> 
    
      <label>First name</label> 
      <input type="text" name="vads_cust_first_name" value="My first name" readonly />
    
      <label>Last name</label>  
      <input type="text" name="vads_cust_last_name" value="My last name" readonly />
        
      <input type="submit" value="Go" name="go">
    </form>


    I need to :

    1. retrieve all the fields which are begining by vads_,
    2. sort them alphabeticaly,
    3. concatenate them with the sign "+",
    4. make a SHA1 operation on this string
    5. and finally, insert it where there is the snippet call [[CalculateSignature]].


    Nice program didn't it ? smiley

    I have an example of PHP function to doing this kind of stuff, but I don't know how to adapt it to make it work as a snippet.
    Here is the code :
    // Function to calculate signature
    // ------------------------------
    
    // Key certificat
    $key = "1122334455667788";
    
    // $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 ;
    }


    Is someone here who can help me to transform this code to a snippet ?
    If this is the correct way to doing the job of course !

    This question has been answered by multiple community members. See the first response.

      • 3749
      • 24,544 Posts
      I'm not clear on what you're trying to do. If the form values are hidden, they can't be changed by the user, so why not put them in the snippet itself rather than in the form?

      If you put this snippet above the form:

      $string = "INTERACTIVE+TEST+EUR+PAYMENT+10/08/17 20:34:08+"170810203408011+SINGLE+5932111+V2+1122334455667788";
      $signature = sha1($string);
      $modx->setPlaceholder('signature', $signature);
      


      And put this tag in the form:

      [[+signature]]


      The tag should be replaced by the signature.





        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 28173
        • 409 Posts
        In fact this form is the second step of this process :

        • 1st step : the user fulfil a form with is own data, specially an amount for a cotisation. This step is done by FormIt.
        • 2nd step : after validation, the user can verifying is sent data to a new form, on readonly. This step is done by FormItRetriever. Normaly at this step, I could calculate signature, but I didn't success with a Hook.
        • 3rd step : the user validate (a second time) this new "fixed" form and all the data are sent to the bank platform (and it needs the signature !)

        My purpose is to calculate the signature, but I'm not sure how to do this.
        I need to retrieve all vads_ values from the first step... but how and at which step : with a FormIt's Hook ? with a Snippet ?
          • 3749
          • 24,544 Posts
          That's a tough one. I think this might work on the second page (the one with FormItRetriever). IIRC, FormItRetriever sets the data as placeholders, so you should be able to grab it with $modx->getPlaceholders().

          [[!FormItRetriever]]
          
          <form method="post" action="https://myurl.com/">  
            <input type="hidden" name="nospam" value="" />
            <input type="hidden" name="vads_site_id" value="5932111">
            <input type="hidden" name="vads_ctx_mode" value="TEST">
            <input type="hidden" name="vads_trans_id" value="170810203408011">
            <input type="hidden" name="vads_trans_date" value="10/08/17 20:34:08">
            <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">
            <input type="hidden" name="signature" value="[[!CalculateSignature]]" /> 
           
            <label>First name</label> 
            <input type="text" name="vads_cust_first_name" value="My first name" readonly />
           
            <label>Last name</label>  
            <input type="text" name="vads_cust_last_name" value="My last name" readonly />
               
            <input type="submit" value="Go" name="go">
          </form>



          /* CalculateSignature snippet */
          //Initialisation of the variable which contain the string to be encrypted
          $contenu_signature = "" ;
            
              $params = $modx->getPlaceholders();
              // sort the fields by alphabetical order
              ksort($params);
                  foreach ($params as $nom =>$valeur)
              { 
              // Retreive the vads_  fields
                  if (strpos($nom,'vads_') !== false)) { 
            
                      // 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 ;
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
          • discuss.answer
            • 28173
            • 409 Posts
            Thanks Bob for helping me smiley

            I have digged on this way but there is a problem.
            First, there was an extra parenthesis on line 11. Easy to solve.
            But there is worse : the line 5 is blocking the snippet (white page and no error show).
            On the doc, there are talking about $modx->getPlaceholder but not about $modx->getPlaceholders
            • discuss.answer
              • 3749
              • 24,544 Posts
              Sorry, I always forget that that function doesn't exist in the $modx class.

              Try it this way:

              $params = $modx->placeholders;


              If that doesn't work, try this:

              $params = $_POST;
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 28173
                • 409 Posts
                Yes ! your first suggestion is working : I can calculate the signature.

                Thank a lot !

                  • 3749
                  • 24,544 Posts
                  I'm glad I could help. Thanks for reporting back. smiley
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting