Freeside:1.9:Documentation:Developer/FS/SelfService/php/php examples

From Freeside
< Freeside:1.9:Documentation:Developer‎ | FS‎ | SelfService
Revision as of 15:03, 13 July 2010 by Ivan (talk | contribs) (Quick Examples)

Jump to: navigation, search

Quick Examples

freeside.login_example.php

freeside_signup_example.php

Sample Application

This is a small application that demonstrates the flow for a simple renewal.

login.php

Display the login page.

<?php

require('freeside.class.php');
$freeside = new FreesideSelfService();

$login_info = $freeside->login_info();

extract($login_info);

$error = $_GET['error'];
if ( $error ) {
  $username = $_GET['username'];
  $domain   = $_GET['domain'];
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Login</TITLE></HEAD>
<BODY BGCOLOR="#e8e8e8"><FONT SIZE=5>Login</FONT><BR><BR>
<FONT SIZE="+1" COLOR="#ff0000"><?php echo htmlspecialchars($error); ?></FONT>

<FORM ACTION="process_login.php" METHOD=POST>
<INPUT TYPE="hidden" NAME="session" VALUE="login">

<TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=2 CELLPADDING=0>

<TR>
  <TH ALIGN="right">Username </TH>
  <TD>
    <INPUT TYPE="text" NAME="username" VALUE="<?php echo htmlspecialchars($username); ?>"><?php if ( $single_domain ) { echo '@'.$single_domain; } ?>
  </TD>
</TR>

<?php if ( $single_domain ) { ?>

  <INPUT TYPE="hidden" NAME="domain" VALUE="<?php echo $single_domain ?>">

<?php } else { ?>

  <TR>
    <TH ALIGN="right">Domain </TH>
    <TD>
      <INPUT TYPE="text" NAME="domain" VALUE="<?php echo htmlspecialchars($domain); ?>">
    </TD>
  </TR>

<?php } ?>

<TR>
  <TH ALIGN="right">Password </TH>
  <TD>
    <INPUT TYPE="password" NAME="password">
  </TD>
</TR>
<TR>
  <TD COLSPAN=2 ALIGN="center"><INPUT TYPE="submit" VALUE="Login"></TD>
</TR>
</TABLE>
</FORM>

<?php if ( $phone_login ) { ?>

  <B>OR</B><BR><BR>
    
  <FORM ACTION="process_login.php" METHOD=POST>
  <INPUT TYPE="hidden" NAME="session" VALUE="login">
  <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=2 CELLPADDING=0>
  <TR>
    <TH ALIGN="right">Phone number </TH>
    <TD>
      <INPUT TYPE="text" NAME="username" VALUE="<?php echo htmlspecialchars($username) ?>">
    </TD>
  </TR>
  <INPUT TYPE="hidden" NAME="domain" VALUE="svc_phone">
  <TR>
    <TH ALIGN="right">PIN </TH>
    <TD>
      <INPUT TYPE="password" NAME="password">
    </TD>
  </TR>
  <TR>
    <TD COLSPAN=2 ALIGN="center"><INPUT TYPE="submit" VALUE="Login"></TD>
  </TR>
  </TABLE>
  </FORM>

<?php } ?>

</BODY></HTML>

process_login.php

<?php

require('freeside.class.php');
$freeside = new FreesideSelfService();

$response = $freeside->login( array( 
  'username' => strtolower($_POST['username']),
  'domain'   => strtolower($_POST['domain']),
  'password' => strtolower($_POST['password']),
) );

#error_log("[login] received response from freeside: $response");

$error = $response['error'];

if ( $error ) {

  header('Location:login.php?username='. urlencode($username).
                           '&domain='.   urlencode($domain).
                           '&error='.    urlencode($error)
        );
  die();

}

// sucessful login

$session_id = $response['session_id'];

#error_log("[login] logged into freeside with session_id=$session_id");

// now what?  for now, always redirect to the main page.
// eventually, other options?

header("Location:main.php?session_id=$session_id")
#die();

?>

main.php

<?php

require('freeside.class.php');
$freeside = new FreesideSelfService();

$session_id = $_GET['session_id'];

$response = $freeside->customer_info( array(
  'session_id' => $session_id,
) );

$error = $response['error'];

if ( $error ) {
  header('Location:login.php?error='. urlencode($error));
  die();
}

extract($response);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <TITLE>My Account</TITLE>
  </HEAD>
  <BODY>
    <H1>My Account</H1>

      Hello, <?php echo htmlspecialchars($name); ?><BR><BR>

      <?php echo $small_custview; ?>

      <BR>

      <A HREF="order_renew.php?session_id=<?php echo $session_id; ?>">Renew early</A>

  </BODY>
</HTML>

order_renew.php

<?php

require('freeside.class.php');
$freeside = new FreesideSelfService();

$session_id = $_GET['session_id'];

$renew_info = $freeside->renew_info( array(
  'session_id' => $session_id,
) );

$error = $renew_info['error'];

if ( $error ) {
  header('Location:login.php?error='. urlencode($error));
  die();
}

#in the simple case, just deal with the first package
$bill_date         = $renew_info['dates'][0]['bill_date'];
$bill_date_pretty  = $renew_info['dates'][0]['bill_date_pretty'];
$renew_date        = $renew_info['dates'][0]['renew_date'];
$renew_date_pretty = $renew_info['dates'][0]['renew_date_pretty'];
$amount            = $renew_info['dates'][0]['amount'];

$payment_info = $freeside->payment_info( array(
  'session_id' => $session_id,
) );

extract($payment_info);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <TITLE>Renew Early</TITLE>
  </HEAD>
  <BODY>
    <H1>Renew Early</H1>

    <FONT SIZE="+1" COLOR="#ff0000"><?php echo htmlspecialchars($_GET['error']); ?></FONT>

      <FORM NAME="OneTrueForm" METHOD="POST" ACTION="process_payment_order_renew.php" onSubmit="document.OneTrueForm.process.disabled=true">

      <INPUT TYPE="hidden" NAME="date"       VALUE="<?php echo $date; ?>">
      <INPUT TYPE="hidden" NAME="session_id" VALUE="<?php echo $session_id; ?>">
      <INPUT TYPE="hidden" NAME="amount"     VALUE="<?php echo $amount; ?>">

      A payment of $<?php echo $amount; ?> will renew your account through <?php echo $renew_date_pretty; ?>.<BR><BR>

      <TABLE BGCOLOR="#cccccc">
      <TR>
        <TD ALIGN="right">Amount</TD>
        <TD>
          <TABLE><TR><TD BGCOLOR="#ffffff">
            $<?php echo $amount; ?>
          </TD></TR></TABLE>
        </TD>
      </TR>
      <TR>
        <TD ALIGN="right">Card type</TD>
        <TD>
          <SELECT NAME="card_type"><OPTION></OPTION>
            <?php foreach ( array_keys($card_types) as $t ) { ?>
              <OPTION <?php if ($card_type == $card_types[$t] ) { ?> SELECTED <?php } ?>
                      VALUE="<?php echo $card_types[$t]; ?>"
              ><?php echo $t; ?>
            <?php } ?>
          </SELECT>
        </TD>
      </TR>

      <TR>
        <TD ALIGN="right">Card number</TD>
        <TD>
          <TABLE>
            <TR>
              <TD>
                <INPUT TYPE="text" NAME="payinfo" SIZE=20 MAXLENGTH=19 VALUE="<?php echo $payinfo; ?>"> </TD>
              <TD>Exp.</TD>
              <TD>
                <SELECT NAME="month">
                  <?php foreach ( array('01','02','03','04','05','06','07','08','09','10','11','12') as $m) { ?>
                    <OPTION<?php if ($m == $month ) { ?> SELECTED<?php } ?>
                    ><?php echo $m; ?>
                  <?php } ?>
                </SELECT>
              </TD>
              <TD> / </TD>
              <TD>
                <SELECT NAME="year">
                  <?php $lt = localtime(); $y = $lt[5] + 1900;
                        for ($y = $lt[5]+1900; $y < $lt[5] + 1910; $y++ ) { ?>
                    <OPTION<?php if ($y == $year ) { ?> SELECTED<?php } ?>
                    ><?php echo $y; ?>
                  <?php } ?>
                </SELECT>
              </TD>
            </TR>
          </TABLE>
        </TD>
      </TR>
      <?php if ( $withcvv ) { ?>
        <TR>
          <TD ALIGN="right">CVV2 (<A HREF="javascript:myopen('cvv2.html','cvv2','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=480,height=288')">help</A>)</TD>
          <TD><INPUT TYPE="text" NAME="paycvv" VALUE="" SIZE=4 MAXLENGTH=4></TD>
        </TR>
      <?php } ?>
      <TR>
        <TD ALIGN="right">Exact name on card</TD>
        <TD><INPUT TYPE="text" SIZE=32 MAXLENGTH=80 NAME="payname" VALUE="<?php echo $payname; ?>"></TD>
      </TR><TR>
        <TD ALIGN="right">Card billing address</TD>
        <TD>
          <INPUT TYPE="text" SIZE=40 MAXLENGTH=80 NAME="address1" VALUE="<?php echo $address1; ?>">
        </TD>
      </TR><TR>
        <TD ALIGN="right">Address line 2</TD>
        <TD>
          <INPUT TYPE="text" SIZE=40 MAXLENGTH=80 NAME="address2" VALUE="<?php echo $address2; ?>">
        </TD>
      </TR><TR>
        <TD ALIGN="right">City</TD>
        <TD>
          <TABLE>
            <TR>
              <TD>
                <INPUT TYPE="text" NAME="city" SIZE="12" MAXLENGTH=80 VALUE="<?php echo $city; ?>">
              </TD>
              <TD>State</TD>
              <TD>
                <SELECT NAME="state">
                  <?php foreach ( $states as $s ) { ?>
                    <OPTION<?php if ($s == $state) { ?> SELECTED<?php } ?>
                    ><?php echo $s; ?>
                  <?php } ?>
                </SELECT>
              </TD>
              <TD>Zip</TD>
              <TD>
                <INPUT TYPE="text" NAME="zip" SIZE=11 MAXLENGTH=10 VALUE="<?php echo $zip; ?>">
              </TD>
            </TR>
          </TABLE>
        </TD>
      </TR>

      <TR>
        <TD COLSPAN=2>
          <INPUT TYPE="checkbox" CHECKED NAME="save" VALUE="1">
          Remember this information
        </TD>
      </TR><TR>
        <TD COLSPAN=2>
          <INPUT TYPE="checkbox"<?php if ( $payby == 'CARD' ) { ?> CHECKED<?php } ?> NAME="auto" VALUE="1" onClick="if (this.checked) { document.OneTrueForm.save.checked=true; }">
          Charge future payments to this card automatically
        </TD>
      </TR>
      </TABLE>
      <BR>
      <INPUT TYPE="hidden" NAME="paybatch" VALUE="<?php echo $paybatch; ?>">
      <INPUT TYPE="submit" NAME="process" VALUE="Process payment"> <!-- onClick="this.disabled=true"> -->
      </FORM>

  </BODY>
</HTML>

process_payment_order.php

<?php

require('freeside.class.php');
$freeside = new FreesideSelfService();

$response = $freeside->process_payment_order_renew( array( 
    'session_id' => $_POST['session_id'],
    'payby'      => 'CARD',
    'amount'     => $_POST['amount'],
    'payinfo'    => $_POST['payinfo'],
    'paycvv'     => $_POST['paycvv'],
    'month'      => $_POST['month'],
    'year'       => $_POST['year'],
    'payname'    => $_POST['payname'],
    'address1'   => $_POST['address1'],
    'address2'   => $_POST['address2'],
    'city'       => $_POST['city'],
    'state'      => $_POST['state'],
    'zip'        => $_POST['zip'],
    'save'       => $_POST['save'],
    'auto'       => $_POST['auto'],
    'paybatch'   => $_POST['paybatch'],
) );

error_log("[process_payment_order_renew] received response from freeside: $response");

$error = $response['error'];

if ( $error ) {

  error_log("[process_payment_order_renew] response error: $error");

  header('Location:order_renew.php'.
           '?session_id='. urlencode($_POST['session_id']).
           '?error='.      urlencode($error).
           '&payby=CARD'.
           '&amount='.     urlencode($_POST['amount']).
           '&payinfo='.    urlencode($_POST['payinfo']).
           '&paycvv='.     urlencode($_POST['paycvv']).
           '&month='.      urlencode($_POST['month']).
           '&year='.       urlencode($_POST['year']).
           '&payname='.    urlencode($_POST['payname']).
           '&address1='.   urlencode($_POST['address1']).
           '&address2='.   urlencode($_POST['address2']).
           '&city='.       urlencode($_POST['city']).
           '&state='.      urlencode($_POST['state']).
           '&zip='.        urlencode($_POST['zip']).
           '&save='.       urlencode($_POST['save']).
           '&auto='.       urlencode($_POST['auto']).
           '&paybatch='.   urlencode($_POST['paybatch'])
        );
  die();

}

// sucessful renewal.

$session_id = $response['session_id'];

// now what?

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <TITLE>Renew Early</TITLE>
  </HEAD>
  <BODY>
    <H1>Renew Early</H1>

    Renewal processed sucessfully.

  </BODY>
</HTML>