#!/usr/local/bin/perl
#
# Program name:  cyber.pl
# Author: Todd Hassan
# Creation date:  7/2/97
# Last modified: 7/2/97
# Description:  This module allows Hassan Consulting's Shopping Cart
# to work with CyberCash.
# Version 1.01
#
# Version history
#
# 1.01 fixed comments field

# CyberCash mail receipt

sub cyber_mail_list
{
  require 5.001;

  # BSD systems

  #  use CCLib qw(SetServer sendmserver);

  # non-BSD systems

  use CCLibNN qw(SetServer sendmserver);

  %payment = ( 'host' => "$cyber_host",
             'port' => "$cyber_port",
             'secret' => "$cyber_secret");
  &SetServer(%payment);

  &read_list;

  for ($i=1; $i<=$num_lines; $i++)
  {
    $item_no = $ITEMS{$i};
    &lookup_item($item_no); 
  }

  $item_total = 0;
  $subtotal = 0;

  # for NT only

  $mail_file = "$temp_location/shop$$.txt";

  if ($POST{bstreet} eq "")
  {
    $POST{bstreet} = $POST{street};
    $POST{bcity} = $POST{city};
    $POST{bstate} = $POST{state};
    $POST{bzip} = $POST{zip};
    $POST{bcountry} = $POST{country};
  }

  #  Do some checks on the input

  &check_email_form;

  if ($num_lines < 1)
  {
    $output = "Error: Nothing in your shopping basket."
  }

  $item_total = 0;
  $subtotal = 0;
  $mail1 = "";

  if ($NT)
  {
    $mail = "$mail_location $mail_file -t $admin -f $admin -s \"$admin_subject\"";
    if ($admin1 ne "")
    {    
      $mail1 = "$mail_location $mail_file -t $admin1 -f $admin -s \"$admin_subject\"";
    }
    if (!(open(MAIL, ">$mail_file")))
    {
      &html_header;
      print("Error opening mail file: $mail_file<P>\n");
      exit(1);
    }
  }
  else
  {
    $mail = "$mail_location -oi -t";
    open(MAIL, "| $mail") || &sys_error("$prog: Can't open pipe to sendmail: $!");
    if ($admin1 ne "")
    { 
      print MAIL ("To: $admin, $admin1\r\n");
    }
    else
    { 
      print MAIL ("To: $admin\r\n");
    }
    print MAIL "X-Mailer: Hassan Consulting Shopping Cart \r\n";
    print MAIL "Mime-Version: 1.0\r\n";
    print MAIL "Content-Type: text/plain; charset=$char_set\r\n";
    print MAIL "From: $admin\r\n";
    print MAIL "Reply-to: $POST{email}\r\n";
    print MAIL "Subject: $admin_subject\r\n\r\n";
  }
  print MAIL ("   Name: $POST{name}\n");
  print MAIL ("  Email: $POST{email}\n");
  print MAIL ("Shipping Address: $POST{street}\n         $POST{city} $POST{state} $POST{zip} $POST{country}\n");
  if ($POST{bstreet} ne "")
  {
    print MAIL ("Billing Address: $POST{bstreet}\n         $POST{bcity} $POST{bstate} $POST{bzip} $POST{bcountry}\n");
  }
  print MAIL ("Phone: $POST{phone}\n");
  if ($POST{company} ne "")
  {
    $info_buffer .= "Company Name: $POST{company}\r\n";
  }
  if ($POST{comments} ne "")
  {
    $info_buffer .= "Comments: $POST{comments}\r\n";
  }
  print MAIL ("Order:\n");
  print MAIL ("-----------------------------------------------------------------\n");

  if ($num_lines > 0)
  {
    for ($i=1; $i<=$num_lines; $i++)
    {
      $item_no = $ITEMS{$i};
      $color = $COLORS{$i};
      $size = $SIZES{$i};
      &lookup_item($item_no);   
      if (($color eq "")&&($size eq ""))
      {   
        printf MAIL ("%s %s  %-45s %10s\n", $NUM{$i}, $product_num, $product_name, $product_price);
      }
      else
      {
        printf MAIL ("%s %s  %-45s %10s\n", $NUM{$i}, $product_num, $product_name, $product_price);
        print MAIL ("Color: $color  Size: $size\n");
      }
      $subtotal += $product_price * $NUM{$i};
      $item_total += $NUM{$i};
    }

    $subtotal = &round($subtotal);
    $temp = $subtotal;
    $temp1 = $item_total;
    printf MAIL ("\nTotal # items: %-29s Subtotal  $email_currency_symbol%9s\n", $temp1, $temp);

    $shipping = &calc_shipping($subtotal);

    if ($shipping_label{1} ne "")
    {
      printf MAIL ("                                             Shipping  $email_currency_symbol%9s\n",$shipping);
      printf MAIL ("    ( $shipping_label{$shipby} )\n");
    }

#----------------------------

    if ($POST{bstate} ne "")
    {
	$state = $POST{bstate};
    }
    else
    {
 	$state = $POST{state};
    }  

    if (("\U$state\E" eq $STATE)&&($TAX))
    {
      if ($CALC_TAX_ON_SHIPPING eq 'N')
      {
        $tax = ($subtotal) * $tax_rate;
      }
      else
      {
        $tax = ($subtotal + $shipping) * $tax_rate;
      }
      $tax = &round($tax);
      printf MAIL ("                                                     Tax  $email_currency_symbol%8s\r\n", $tax);
      $total = &round($tax + $subtotal+ $shipping);
    }
    else
    {
      $total = &round($subtotal + $shipping);
    }
    $total = &round($total);
    printf MAIL ("                                                Total  $email_currency_symbol%9s\n", $total);
  }     

  if ($POST{comments} ne "")
  {
    print MAIL ("Comments: $POST{comments}\r\n");
  }

  # start cybercash stuff here

  $POST{ccard} =~ s/\D//g;
  $amount = "usd $total";

  %result = &sendmserver('mauthonly', 
                       'Order-ID', $FORM{SID},
		       'Amount', $amount,
		       'Card-Number', $POST{ccard},
		       'Card-Name', $POST{ccname},
		       'Card-Address', $POST{bstreet},
		       'Card-City', $POST{bcity},
		       'Card-State', $POST{bstate},
		       'Card-Zip', $POST{bzip},
		       'Card-Country', $POST{bcountry},
		       'Card-Exp', $POST{ccexpiry});

  if ($result{MStatus} ne 'success')
  {
   &html_header;
    print MAIL ("This order failed the CyberCash processing with an error message of $result{MErrMsg}\n"); 
    print ("This order failed the CyberCash processing with an error message of $result{MErrMsg}\n"); 
    close(MAIL);
    exit;
  }

  print MAIL ("\n\nThis order was approved and entered into your CyberCash system.\nOrder ID: $FORM{SID}\nAVS Code: $result{'avs-code'}\n");
  print MAIL ("Action Code: $result{'action-code'}\n");
  print MAIL ("Authorization Code: $result{'auth-code'}\n");
  print MAIL ("Auxilary Message: $result{'aux-msg'}\n");
  close (MAIL) || &sys_error("$prog: Error closing pipe to sendmail: $!");

  if ($NT)
  {
    `$mail`;
    if ($mail1 ne "")
    {
      `$mail1`;
    }
  }


# send receipt to customer  

  if ($NT)
  {
    $mail = "$mail_location $mail_file -t $POST{email} -f $admin -s \"$customer_subject\"";

    if (!(open(MAIL, ">$mail_file")))
    {
      &html_header();
      print("Error opening mail file: $mail_file<P>\n");
      exit(1);
    }
  }
  else
  {
    open(MAIL, "| $mail") || &sys_error("$prog: Can't open pipe to sendmail: $!");
    print MAIL ("To: $POST{email}\n");
    print MAIL "X-Mailer: Hassan Consulting Shopping Cart\r\n";
    print MAIL "Mime-Version: 1.0\r\n";
    print MAIL "Content-Type: text/plain; charset=$char_set\r\n";
    print MAIL ("From: $admin\n");
    print MAIL ("Reply-To: $admin\n");    
    print MAIL ("Subject: $customer_subject\n\n");
  }

  print MAIL ("   Name: $POST{name}\n");
  print MAIL ("  Email: $POST{email}\n");


  print MAIL ("Shipping Address: $POST{street}\n         $POST{city} $POST{state} $POST{zip} $POST{country}\n");
  if ($POST{bstreet} ne "")
  {
    print MAIL ("Billing Address: $POST{bstreet}\n         $POST{bcity} $POST{bstate} $POST{bzip} $POST{bcountry}\n");
  }
  print MAIL ("Phone: $POST{phone}\n\n");
  print MAIL ("Order:\n");
  print MAIL ("-----------------------------------------------------------------\n");
  if ($num_lines > 0)
  {
    for ($i=1; $i<=$num_lines; $i++)
    {
      $item_no = $ITEMS{$i};
      $color = $COLORS{$i};
      $size = $SIZES{$i};
      &lookup_item($item_no);   
      if (($color eq "")&&($size eq ""))
      {   
        printf MAIL ("%s %-52s %10s\n", $NUM{$i}, $product_name, $product_price);
      }
      else
      {
        printf MAIL ("%s %-52s %10s\n", $NUM{$i}, $product_name, $product_price);
        print MAIL ("Color: $color  Size: $size\n");
      }
    }

    $temp = &round($subtotal);
    $temp1 = $item_total;
    printf MAIL ("\nTotal # items: %-29s Subtotal  $email_currency_symbol%9s\n", $temp1, $temp);
    if ($shipping_label{1} ne "")
    {
      printf MAIL ("                                             Shipping  $email_currency_symbol%9s\n",$shipping);
      printf MAIL ("    ( $shipping_label{$shipby} )\n");
    }

    if ($POST{bstate} ne "")
    {
	$state = $POST{bstate};
    }
    else
    {
 	$state = $POST{state};
    }  
      if ("\U$state\E" eq $STATE)
      {
       printf MAIL ("                                                  Tax  $email_currency_symbol%9s\n", $tax);
      }
      printf MAIL ("                                                Total  $email_currency_symbol%9s\n", $total);
  }
  if ($POST{comments} ne "")
  {
    print MAIL ("Comments: $POST{comments}\r\n");
  }

  close (MAIL) || &sys_error("$prog: Error closing pipe to sendmail: $!");


  if ($NT)
  {
    `$mail`;
    @delete_list = ("$mail_file");
    unlink(@delete_list);
  }
}

1;








