We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22574
    • 181 Posts
    Not sure if this is a stupid question.

    But how do I change one of modx’s pages to run as a .php page?

    I am integrating a payment gateway by eway.com.au and it requires the checkout page to have a php script above the <HTML>
    <HEAD> call. So I think it needs to be a .php page as calling a snippet doesn’t work. And if I just past the script into the template it just comes out as text.

    Anyone?

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      No reason why a snippet shouldn’t work. Their PHP code may need a bit of tweaking (for example a snippet needs to return whatever it’s generating), but I’ve never run across a PHP script that couldn’t work as a snippet.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 22574
        • 181 Posts
        Here is an example of eways code.
        The simple way would be to make the cart link go to this page.
        But I want to use a modx page just incase the template design changes etc...

        <?php
        require_once('EwayPaymentLive.php');
        if (isset($_POST['btnProcess'])){
        
          $txtFirstName = $_POST['txtFirstName'];
          $txtLastName = $_POST['txtLastName'];
          $txtEmail = $_POST['txtEmail'];
          $txtAddress = $_POST['txtAddress'];
          $txtPostcode = $_POST['txtPostcode'];
          $txtTxnNumber = $_POST['txtTxnNumber'];
          $txtInvDesc = $_POST['txtInvDesc'];
          $txtInvRef = $_POST['txtInvRef'];
          $txtOption1 = $_POST['txtOption1'];
          $txtOption2 = $_POST['txtOption2'];
          $txtOption3 = $_POST['txtOption3'];
          $txtCCNumber = $_POST['txtCCNumber'];
          $ddlExpiryMonth = $_POST['ddlExpiryMonth'];
          $ddlExpiryYear = $_POST['ddlExpiryYear'];
          $txtCCName = $_POST['txtCCName'];
          $txtAmount = $_POST['txtAmount'];
          
          // Set the payment details
          $eway = new EwayPaymentLive($eWAY_CustomerID, $eWAY_PaymentMethod, $eWAY_UseLive);
        
          $eway->setTransactionData("TotalAmount", $txtAmount); //mandatory field
          $eway->setTransactionData("CustomerFirstName", $txtFirstName);
          $eway->setTransactionData("CustomerLastName", $txtLastName);
          $eway->setTransactionData("CustomerEmail", $txtEmail);
          $eway->setTransactionData("CustomerAddress", $txtAddress);
          $eway->setTransactionData("CustomerPostcode", $txtPostcode);
          $eway->setTransactionData("CustomerInvoiceDescription", $txtInvDesc);
          $eway->setTransactionData("CustomerInvoiceRef", $txtInvRef);
          $eway->setTransactionData("CardHoldersName", $txtCCName); //mandatory field
          $eway->setTransactionData("CardNumber", $txtCCNumber); //mandatory field
          $eway->setTransactionData("CardExpiryMonth", $ddlExpiryMonth); //mandatory field
          $eway->setTransactionData("CardExpiryYear", $ddlExpiryYear); //mandatory field
          $eway->setTransactionData("TrxnNumber", "");
          $eway->setTransactionData("Option1", $txtOption1);
          $eway->setTransactionData("Option2", $txtOption2);
          $eway->setTransactionData("Option3", $txtOption3);
          
          $eway->setCurlPreferences(CURLOPT_SSL_VERIFYPEER, 0); // Require for Windows hosting
        
          // Send the transaction
          $ewayResponseFields = $eway->doPayment();
        
          if(strtolower($ewayResponseFields["EWAYTRXNSTATUS"])=="false")
          {
              print "Transaction Error: " . $ewayResponseFields["EWAYTRXNERROR"] . "<br>\n";
              foreach($ewayResponseFields as $key => $value)
                  print "\n<br>\$ewayResponseFields[\"$key\"] = $value";
          }
          else if(strtolower($ewayResponseFields["EWAYTRXNSTATUS"])=="true")
          {
               // payment succesfully sent to gateway
               // Payment succeeded get values returned
               $lblResult = " Result: " . $ewayResponseFields["EWAYTRXNSTATUS"] . "<br>";
               $lblResult .= " AuthCode: " . $ewayResponseFields["EWAYAUTHCODE"] . "<br>";
               $lblResult .= " Error: " . $ewayResponseFields["EWAYTRXNERROR"] . "<br>";
               $lblResult .= " eWAYInvoiceRef: " . $ewayResponseFields["EWAYTRXNREFERENCE"] . "<br>";
               $lblResult .= " Amount: " . $ewayResponseFields["EWAYRETURNAMOUNT"] . "<br>";
               $lblResult .= " Txn Number: " . $ewayResponseFields["EWAYTRXNNUMBER"] . "<br>";
               $lblResult .= " Option1: " . $ewayResponseFields["EWAYOPTION1"] . "<br>";
               $lblResult .= " Option2: " . $ewayResponseFields["EWAYOPTION2"] . "<br>";
               $lblResult .= " Option3: " . $ewayResponseFields["EWAYOPTION3"] . "<br>";
               echo $lblResult;
          }
          else
          {
               // invalid response recieved from server.
               $lblResult =  "Error: An invalid response was recieved from the payment gateway.";
               echo $lblResult;
          }
        
        }
        else {
        ?>
        <HTML>
        <HEAD><title>eWAY PHP Example</title></HEAD>
        <body>
        <form id="Form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" ENCTYPE="multipart/form-data">
        
        <div id="pnlBeforeProcess" style="height:328px;width:488px;">  * Fields in Red are required 
        <TABLE id=Table1 style="WIDTH: 352px; HEIGHT: 264px" cellSpacing=0 cellPadding=3 
        width=352 border=0>
          <TR>
            <TD bgColor=#000000 colSpan=2><FONT color=#ffffff><STRONG>Process 
              Transaction</STRONG></FONT></TD></TR>
          <TR>
            <TD bgColor=gainsboro>
        <span id="Label1">First Name:</span></TD>
            <TD bgColor=gainsboro>
        <input name="txtFirstName" type="text" id="txtFirstName" /></TD></TR>
          <TR>
            <TD bgColor=gainsboro>
        <span id="Label5">Last Name:</span></TD>
            <TD bgColor=gainsboro>
        <input name="txtLastName" type="text" id="txtLastName" /></TD></TR>
          <TR>
            <TD bgColor=gainsboro>
        <span id="Label7">Email Address:</span></TD>
            <TD bgColor=gainsboro>
        <input name="txtEmail" type="text" id="txtEmail" /></TD></TR>
          <TR>
            <TD bgColor=gainsboro>
        <span id="Label11">Address:</span></TD>
            <TD bgColor=gainsboro>
        <input name="txtAddress" type="text" id="txtAddress" /></TD></TR>
          <TR>
            <TD bgColor=gainsboro>
        <span id="Label12">Postcode:</span></TD>
            <TD bgColor=gainsboro>
        <input name="txtPostcode" type="text" id="txtPostcode" /></TD></TR>
          <TR>
            <TD bgColor=gainsboro>
        <span id="Label8">Invoice Description:</span></TD>
            <TD bgColor=gainsboro>
        <input name="txtInvDesc" type="text" id="txtInvDesc" /></TD></TR>
          <TR>
            <TD bgColor=gainsboro>
        <span id="Label9">Invoice Reference:</span></TD>
            <TD bgColor=gainsboro>
        <input name="txtInvRef" type="text" id="txtInvRef" /></TD></TR>
          <TR>
            <TD bgColor=gainsboro>
        <span id="Label13">Transaction Number:</span></TD>
            <TD bgColor=gainsboro>
        <input name="txtTxnNumber" type="text" id="txtTxnNumber" /></TD></TR>
          <TR>
            <TD bgColor=red>
        <span id="Label10">Card Holders Name:</span></TD>
            <TD 
          bgColor=red>
        <input name="txtCCName" type="text" id="txtCCName" /></TD></TR>
          <TR>
            <TD bgColor=red>
        <span id="Label2">Card Number:</span></TD>
            <TD bgColor=red>
        <input name="txtCCNumber" type="text" maxlength="17" id="txtCCNumber" /></TD></TR>
          <TR>
            <TD bgColor=red>
        <span id="Label3">Card Expiry:</span></TD>
            <TD bgColor=red>
        <select name="ddlExpiryMonth" id="ddlExpiryMonth">
        		<option value="01">01</option>
        		<option value="02">02</option>
        		<option value="03">03</option>
        		<option value="04">04</option>
        		<option value="05">05</option>
        		<option value="06">06</option>
        		<option value="07">07</option>
        		<option value="08">08</option>
        		<option value="09">09</option>
        		<option value="10">10</option>
        		<option value="11">11</option>
        		<option value="12">12</option>
        	</select>
        <select name="ddlExpiryYear" id="ddlExpiryYear">
        		<option value="04">04</option>
        		<option value="05">05</option>
        		<option value="06">06</option>
        		<option value="07">07</option>
        		<option value="08">08</option>
        		<option value="09">09</option>
        		<option value="10">10</option>
        		<option value="11">11</option>
        		<option value="12">12</option>
        	</select></TD></TR>
          <TR>
            <TD style="HEIGHT: 24px" bgColor=red>
        <span id="Label4">Card Type:</span></TD>
            <TD style="HEIGHT: 24px" bgColor=red>
        <select name="ddlCardType" id="ddlCardType">
        		<option value="VISA">VISA</option>
        		<option value="MASTERCARD">MASTERCARD</option>
        		<option value="AMEX">AMEX</option>
        	</select></TD></TR>
          <TR>
            <TD bgColor=red>
        <span id="Label6">Total Amount:</span></TD>
            <TD bgColor=red>
        <input name="txtAmount" type="text" id="txtAmount" style="width:64px;" /></TD></TR>
          <TR>
            <TD bgColor=gainsboro colSpan=2>
        <input type="submit" name="btnProcess" value="Process Transaction" id="btnProcess" /></TD></TR></TABLE>
        <BR>
        </form>
        </body>
        </HTML>
        <?php }?>
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Ok, what you have is a POST processing script that displays a form if POST is not set (form has not been submitted). This is a common way of dealing with forms in MODx snippets.

          You can use your MODx resource for this with some modification. The last "else" clause that displays the page with the form if the POST is not set can be pared down to just output the form, and the snippet placed where the page’s content should be. Instead of using inline PHP, put the form HTML code into a variable and return it in the snippet code (although snippets can use inline PHP if you really want to). I would fiddle with the form to get rid of the inline styling as well; that should always be handled in a CSS file. The HTML code of the form doesn’t matter; all that matters is that you have all the form fields and that the form fields have the same names. Your action would be [~[*id*]~] (or its Revo equivalent)

          <?php>
          ...
          } else {
          $output ='
          <form id="Form1" method="post" action="[~[*id*]~]" enctype="multipart/form-data">
          ...
          </form>
          ';
          return $output;
          }
          ?>
           
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 22574
            • 181 Posts
            Thanks mate!