cancel
Showing results for 
Search instead for 
Did you mean: 

Create form without a pre-defined X_Amount Using SIM & PHP

I'm trying to create a form that simply captures a "Childs Name" and allows for a donation amount of any amount they enter. So therefore there is no pre-defined or "set" amount. 

 

I started with usiung SIM authentication method and the simple example code on SIM overview tab and it works however if the amount isn't set in file before then I get error (99).

 

I found this code and it will allow me to use form where client can insert childs name and any amount they want in 'x_amount' field. Code below. 

 

As of right now my unbderstanding is that this file serves form to be filled out then processes itself then upon submission auto submits to Authorize url via a little javascript. RIGHT NOW file shows form then once I submit it I get blank white screen.

 

<?php

//
// [Fixup 1] Site Identity Constants
//

require_once 'anet_php_sdk/AuthorizeNet.php'; // Include the SDK you downloaded in Step 2

//$SiteOwnerName = "site-owner-name";
//$PageTitle = "page-title";
$ReturnHomePage = "http://localhost:8888/samples";

$LiveLoginId = "********";
$LiveTransactionKey = "***********";

$LiveGatewayUrl = "https://secure.authorize.net/gateway/transact.dll";

$loginid = $LiveLoginId;
$x_tran_key = $LiveTransactionKey;
$gateway_url = $LiveGatewayUrl;
//
// Other Global Vars
//
$doredirect = false;

// did form submit??
if (isset($HTTP_POST_VARS["formSubmitted"])){
    $strFirstName = $HTTP_POST_VARS["x_first_name"];
    $strLastName = $HTTP_POST_VARS["x_last_name"];
    //
    // [Fixup 5] Make Description Here
    //
    $strDescription = "[ApplyTo:". trim($HTTP_POST_VARS["ApplyTo"])."] + [Country:" . trim($HTTP_POST_VARS["ForCountry"])."]";
    $amount = $HTTP_POST_VARS["x_amount"];
    if (substr($amount, 0,1) == "$") {
    $amount = substr($amount,1);
    }
    if (is_numeric($amount))
    {
        $doredirect=true;
    } else {
        $msg = "Please enter only a numeric amount.";
    }
}
?>

<?
if ($doredirect == false)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>- Donations</title>
    <meta name="description" content="<? echo $SiteOwnerName ?>">
    <meta name="resource-type" content="document">
    <meta name="revisit-after" content="14 days">
    <meta name="classification" content="consumer">
    <meta name="keywords" content="<? echo $SiteOwnerName ?>">
    <meta name="robots" content="All">
    <meta name="distribution" content="global">
    <meta name="rating" content="general">
    <meta name="copyright" content="2008">
    <meta name="web author" content="ITDevWorks, LLC: wwww.itdevworks.com">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
    <!--
    fieldset
    {
        float: left;
        clear: left;
        width: 400px;
        margin: 0 0 0 0;
        padding: 0em 1em 1em 0;
        border: none;
    }
    
    fieldset legend
    {
        font-size: 1em;
        margin-right: 0;
        padding: 0;
        font-family: Corbel, Geneva, Arial, Helvetica, sans-serif;
        font-size: 1.5em;
        font-weight: 600;
    }
    fieldset ol
    {
        padding: 0;
        border-top: solid 1px black;
        list-style: none;
        margin-left: 10px;
        margin-top: 2px;
    }
    
    fieldset li
    {
        padding: 0 0 0 0;
        float: left;
        clear: left;
        margin: 4px 10px 0 0;
        width: 100%;
        vertical-align: top;
    }
    fieldset label
    {
        float: left;
        font-size: 1em;
        width: 60%;
        margin-top: 3px;
        text-align: left;
    }
    
    fieldset input
    {
        width: 95%;
    }
    
    fieldset select
    {
        width: 38%;
    }
    
    fieldset input.btn-submit
    {
        margin-top: 10px;
        text-align: center;
        width:250px;
    }
    
    -->
    </style>

    <script language="JavaScript">
    <!--
    function IsNumeric(sText)
    
    {
        var ValidChars = "0123456789.";
        var IsNumber=true;
        var Char;
        
        for (i = 0; i < sText.length && IsNumber == true; i++) {
            Char = sText.charAt(i); 
            if (ValidChars.indexOf(Char) == -1) {
                IsNumber = false;
            }
        }
        if (IsNumber==false) {
            alert('Please enter only a number.');
            document.getElementById("formsub").innerHTML = "";
        }
        else {
            document.getElementById("formsub").innerHTML = 
            "<input type=\"submit\" class=\"btn-submit\" value=\"Click " +
            "here for secure payment form\" /><input type=\"hidden\" " +
            "name=\"formSubmitted\" value=\"1\"/>";     
        }
    }
    
    // -->
    </script>
</head>
<html>
<body>
<p style="font-weight: bold; font-family:Arial, Helvetica, Sans-Serif; font-size: 14px;">
    <? echo "$SiteOwnerName -  $PageTitle" ?>
</p>
<? if ($msg<>"") {echo "<p color=red>$msg</p>";} ?>                         
<form method="POST" action="donation-real.php">
    <div style="text-align:left; font-family: Verdana; font-size:.8em;">
        <fieldset>
        <legend>
        Donation Form
        </legend>
        <ol>
            <li>
                <label for="x_first_name">
                *First name:
                </label>
                <input type="text" name="x_first_name" size="30"/>
            </li>
            <li>
                <label for="x_last_name">
                *Last name:
                </label>
                <input type="text" name="x_last_name"/>
            </li>
            <li>
                <label for="x_amount" >
                *Donation Amount: $
                </label>
                <input type="text" name="x_amount"
                    onblur="IsNumeric(this.value);"/>
            </li>
            <li>
<?
                //
                // [Fixup 4]. Change these controls as per your requirements
                // 
?>
                
                <label for="ApplyTo">
                *How to apply your donation:
                </label>
                <select name="ApplyTo" id="ApplyTo">
                    <option value="As needed" selected>As needed</option>
                    <option value="A good cause">A good cause</option>
                    <option value="Good cause 2">Good cause 2</option>
                    <option value="Good cause 3">Good cause 3</option>
                    <option value="And so on">And so on...</option>
                </select>
            </li>
            <li id="formsub">
                <input type="submit" class="btn-submit"
                    value="Click here for secure payment form" />
                <input type="hidden" name="formSubmitted" value="1"/>
            </li>
        </ol>
        </fieldset>
    </div>
</form>
<? 
} else {// DO REDIRECT
    srand(time());
    $sequence = rand(1, 1000);
    // Insert the form elements required for SIM by calling InsertFP
    $ret = getFP($loginid, $x_tran_key, $amount, $sequence);
    $fields = array(  
        'x_fp_sequence'=>$sequence,  
        'x_fp_timestamp'=>$ret[1],  
        'x_fp_hash'=>$ret[0],  
        'x_description'=>$strDescription,  
        'x_login'=>$loginid,  
        'x_show_form'=>"PAYMENT_FORM",  
        'x_first_name'=>$strFirstName,  
        'x_last_name'=>$strLastName,  
        'x_amount'=>$amount
    );  
    echo "<html><head></head><body>";
    echo "<form name='myform' action='https://secure.authorize.net/gateway/transact.dll' method='post'>";
    foreach ($fields as $key => $value) {
        print "<input type='hidden' name='".$key."' value=\"".$value."\">";
        $fields_string .= "$key=$value";
    }
    echo "</form>";
?>
<script language="javascript" type="text/javascript">
document.myform.submit();
</script>
<?
}
?>
</body>
</html>

 I need to get this file working or I need help pointing me in right direction.

 

Please tell me if I left anything out.

chasepeysen
Member
1 REPLY 1

Well, the basic idea is that the form you fill out can't generate the fingerprint, since it doesn't know what the amount is, but you -can- submit to a second form that then uses that bit of Javascript at the bottom to auto-submit. The code in general is rather messy and I'd need to rewrite half of it to make sure it's working properly. What's your skill level in PHP and HTML and where did you get this code? The code sample here would seem to be fairly simple to build a form around if you know what you're doing:

https://developer.authorize.net/integration/fifteenminutes/#hosted

TJPride
Expert