JavaScript for E-Commerce

Back | Contents | Next
Please note that this tutorial was first published in the year 2000, and is based on work conducted between 1997 and 2000. While we trust that it still constitutes useful tutorial material, it should not necessarily be construed as to impart best practice in the year 2017. For more details please see this blog post.

Order Finalization - sendorder.cgi

The final stage of order finalization is sending the order to the merchant. This will be done via email by the following Perl script:

Source


#!/usr/bin/perl -T

# Use the CGI.pm module
use CGI qw/:standard/;

# Remove environment variables
delete $ENV{'PATH'};
delete $ENV{'BASH_ENV'};

# Location of sendmail program
$mailprog = '/usr/lib/sendmail';

# To (merchant), From (customer) and Subject
$to = '"Tim Serong" <tim@wirejunkie.com>';
$from = '"' . param('firstname') . ' ' . param('surname') . '" <' . param('email') . '>';
$subject = 'Order from FOMS';

# Message body consists of Items Ordered...
$body  = "\r\n----- Items Ordered -----\r\n\r\n";
$total = 0;
for($i=1; ; $i++) {
	my($s) = param('item' . $i . '_id');
	if(!$s) { last; }
	$body .= "Name: " . param('item' . $i . '_name') . "\r\n";
	$body .= "Product Type: " . param('item' . $i . '_productType') . "\r\n";
	$body .= "ID: " . param('item' . $i . '_id') . "\r\n";
	$body .= 'Price: $' . param('item' . $i . '_price') . "\r\n\r\n";
	$total += param('item' . $i . '_price');
}

# ...Total Price...
$body .= "\r\n----- Total -----\r\n\r\n";
$body .= 'Total: $' . sprintf("%2.2f", $total) . "\r\n";

# ...Shipping Details...
$body .= "\r\n----- Shipping Details -----\r\n\r\n";
$body .= "Name: ";
if(param('salutation') == 1) { $body .= "Mr "; }
elsif(param('salutation') == 2) { $body .= "Mrs "; }
elsif(param('salutation') == 3) { $body .= "Miss "; }
elsif(param('salutation') == 4) { $body .= "Ms "; }
$body .= param('firstname') . ' ' . param('surname') . "\r\n";
$body .= "Address: " . param('address') . "\r\n";
$body .= "Suburb: " . param('suburb') . "\r\n";
$body .= "State: " . param('state') . "\r\n";
$body .= "Postcode: " . param('postcode') . "\r\n";
$body .= "Country: " . param('country') . "\r\n";
$body .= "Phone: " . param('phone') . "\r\n";
$body .= "Fax: " . param('fax') . "\r\n";

# ...and Billing Details.
$body .= "\r\n----- Billing Details -----\r\n\r\n";
if(param('cardtype')) {
	$body .= "Credit Card Type: ";
	if(param('cardtype') == 1) { $body .= "Visa"; }
	elsif(param('cardtype') == 2) { $body .= "MasterCard"; }
	elsif(param('cardtype') == 3) { $body .= "BankCard"; }
	elsif(param('cardtype') == 4) { $body .= "AMEX"; }
	$body .= "\r\n";
	$body .= "Card Number: " . param('cardnumber') . "\r\n";
	$body .= "Expires: " . param('cardexpires') . "\r\n";
}
$body .= "Payment Method: ";
if(param('paymenttype') == 1) { $body .= "Use payment details as above"; }
elsif(param('paymenttype') == 2) { $body .= "I will phone you with payment details"; }
elsif(param('paymenttype') == 3) { $body .= "I will fax you payment details"; }
elsif(param('paymenttype') == 4) { $body .= "I will send a cheque/money order"; }
elsif(param('paymenttype') == 5) { $body .= "Cash on Delivery (COD)"; }
$body .= "\r\n";

# Send the email
&sendmail($to, $from, $subject, $body);

# Say "thank you"
&print_html("Thank you for your order.", "FOMS Order Form");


#----------
# Subs...
#----------

sub sendmail {
	my($to, $from, $subject, $body) = @_;

	open (MAIL, "|$mailprog -t") || &error("Can't open $mailprog!");
	print MAIL "To: $to\r\n";
	print MAIL "From: $from\r\n";
	print MAIL "Subject: $subject\r\n";
	print MAIL "\r\n$body\r\n";
	close MAIL;
}

sub error {
	my($msg) = @_;
	&print_html("Error!", $msg);
}

sub print_html {
	my($body, $title) = @_;
	print "Content-type: text/html\n\n";

	print "<html>\n";
	if($title) {
		print "<head>\n";
		print "<title>$title</title>\n";
		print "</head>\n";
	}
	print "<body>\n";
	print "<center>\n";
	if($title) {
		print "<h1>$title</h1>\n";
	}
	print $body;
	print "<form><input type=\"button\" value=\"Back to Catalogue\" onClick=\"window.close();\"></form>\n";
	print "</center>\n";
	print "</body>\n";
	print "</html>\n";
}
			

Description

This script loops through the itemn_* form fields, concatenating each one to a string. It then appends shipping and billing information and sends it all to the merchant as an email, which ends up looking something like this:


----- Items Ordered -----

Name: Penyon - Some People
Product Type: CDs
ID: 1
Price: 10.00

Name: Yes Minister Volume One
Product Type: Tapes
ID: 19
Price: 30.00


----- Total -----

Total: $40

----- Shipping Details -----

Name: Mr Fish
Address: 52 Salmon Road
Suburb: Guppy
State: Trout
Postcode: 1234
Country: Aquaraland
Phone: 1234-5678
Fax: 1234-5679

----- Billing Details -----

Credit Card Type: Visa
Card Number: 4242424242424242
Expires: 10/01
Payment Method: Use payment details as above

			

The script uses the CGI.pm module for simplicity. You will of course need to change the values $to (the merchant's email address) and $subject ('Order from FOMS') on lines fourteen and sixteen. The only other things that may need to be modified depending on the server being used are the location of the Perl binary on line one, and the location of the sendmail binary on line eleven.

This script has not been tested on Windows NT, but judging by the crop of sendmail-for-NT programs out there it should run without too much difficulty if the sendmail location is set correctly. The other thing that may need changing is the email address format, because some versions of sendmail for NT don't understand addresses in the form "Real Name" <email@ddress>.


Back | Contents | Next

This tutorial has been translated to Serbo-Croatian language by Jovana Milutinovich from Webhostinggeeks.com.

This tutorial has been translated to Slovak by Juraj Rehorovsky from Coupofy team.

If you find this tutorial useful and want to show your apprectiation, a small donation is most welcome, either in Bitcoins to 19QpWDmZPmTqawcr8VsXcbdc4Znq1ecrza, or via PayPal.