Jump to content


Photo

Need Help with .CGI Script


  • This topic is locked This topic is locked
10 replies to this topic

#1 DiveBoss

DiveBoss

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 27 March 2003 - 11:40 AM

First of all I'm new at scripting languages. However, I think I understand most of what I'm reading. My question is: Should these scripts work?
When I try and test I get:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@lonestardivers.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.


Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. Scripting prvided by:
#########################################################
# F R E E S O F T W A R E L I C E N S E #
#########################################################
# #
# This PayPal Tools & Interfaces Software #
# is Copyright© of David W. Van Abel, 2003. #
# #
# This program is free software; you can redistribute #
# it and/or modify it under the terms of the GNU #
# General Public License as published by the Free #
# Software Foundation; either version 2 of the License, #
# or (at your option) any later version. #
# #
# This program is distributed in the hope that it will #
# be useful, but WITHOUT ANY WARRANTY; without even the #
# implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
# See the GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General #
# Public License along with this program; if not, write #
# to the Free Software Foundation, Inc., 59 Temple #
# Place, Suite 330, Boston, MA 02111-1307 USA #
# FULL LICENSE IS FOUND AT #
# http://opensource.or...nses/index.html #
# #
# David W. Van Abel - Author #
# 2131 S. Allison Court #
# Lakewood, CO USA 80227 #
# 303-249-3855 (V) #
# dave@vanabel.com #
# http://vanabel.com #
# http://perlsources.com #
# http://ipnhosting.com #
#########################################################
# E N D O F L I C E N S E #
#########################################################
NO SUPPORT PROVIDED BY THE AUTHOR!!

******** Start ****************
IPNX.CGI
-----
# Version Notes #

#####################################
# WARNING !! NO USER SETTINGS BELOW #
#####################################

use strict;
use vars qw($scriptname $version $ver_date
$root_dir $write_data_files $data_root_dir
$data_dir $history_dir $logfile_dir
$data_file_end $email_admin $print_admin_detail
$mail_program $do_mail $domain $email_subject
);

$scriptname = "ipnx.cgi";
$version = "1.00";
$ver_date = "02-14-03";

use CGI;
use MODULES::Ipnx; # Application Package

use vars qw($query $ua $req $res @pairs $pair $count
$receiver_email $txn_id $payer_email $page %hash_data
$var $key $val $msg $data_file $to_email $chk_it
$return_status $pending_status $file_txn_id $error
$no_txt_file $dmsg $receiver_email $pending_reason
$payer_email $payment_type $trans_date $a $send_back
$b $c $history_file $etype $atrans_date $std_date
);

# File for setting application variables
require "MODULES/Ipnx_Config.pl";


$page = new CGI;
$error = 0;
$msg = "";

# READ POST #

read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query .= '&cmd=_notify-validate';

# POST BACK #

use LWP::UserAgent;
$ua = new LWP::UserAgent;
$req = new HTTP::Request 'POST','https://www.paypal.com/cgi-bin/webscr';
$req->content_type('application/x-www-form-urlencoded');
$req->content($query);
$res = $ua->request($req);

foreach $var ($page->param) {
$hash_data{$var} = $page->param($var);
}

$a = 0;
$trans_date = MODULES::Ipnx::Generic_Date_Stamp($a);
$hash_data{'transaction_date'} = $trans_date;
$std_date = MODULES::Ipnx::reverse_format_dates($trans_date);
$hash_data{'standard_date'} = $std_date;
$return_status = uc($hash_data{'payment_status'});
$pending_status = uc($hash_data{'pending_reason'});
$file_txn_id = ($hash_data{'txn_id'});
$receiver_email = $hash_data{'receiver_email'};
$receiver_email =~ s/\s//g;
$payment_type = uc($hash_data{'payment_type'});

# CHECK CONTROL #

if ($receiver_email ne $email_admin) {
$msg = "Paypal has sent a transaction that does not belong to $domain. Email name received = $receiver_email";
$a = 1;
$atrans_date = MODULES::Ipnx::Generic_Date_Stamp($a);
if (!$file_txn_id) {$file_txn_id = "NonePassed"}
if ($write_data_files) {
$data_file = "$root_dir/$logfile_dir/ERROR-" . "$atrans_date" . "_" . "$file_txn_id" . $data_file_end;
$chk_it = MODULES::Ipnx::open_file_for_write($data_file);
if (!$chk_it) {
$dmsg = "Can not open ERROR data file = $data_file";
$no_txt_file = 1;
}
if (!$no_txt_file) {
$_ = MODULES::Ipnx::write_submitted_data($data_file, %hash_data);
}
}
print "content-type: text/plain\n\nOK\n"; # this is what the sample code contained
$send_back = 1;
$error = 1;
}

# WRITE DATA #

if ((!$error) && ($write_data_files)) {
$data_file = "$data_root_dir/$data_dir/$file_txn_id" . $data_file_end;
$chk_it = MODULES::Ipnx::append_open_file_for_write($data_file);
if (!$chk_it) {
$dmsg = "Can not open data file = $data_file";
$no_txt_file = 1;
}
if (!$no_txt_file) {
$error = MODULES::Ipnx::write_submitted_data($file_txn_id, $data_file, %hash_data);
}
}

# WRITE HISTORY #

if ((!$error) && ($write_data_files)) {
$a = 0;
$_ = MODULES::Ipnx::Generic_Date_Stamp($a);
$history_file = substr($_,0,4);
$history_file .= substr($_,5,2);
$history_file .= "\.txt";
$data_file = "$data_root_dir/$history_dir/$history_file";
$chk_it = MODULES::Ipnx::append_open_file_for_write($data_file);
if (!$chk_it) {
$dmsg = "Can not open history data file = $data_file";
$no_txt_file = 1;
}
if ($chk_it) {
$error = MODULES::Ipnx::write_history_data($file_txn_id, $data_file, %hash_data);
}
}

# EVERYTHING OK #

if (!$error) {
print "content-type: text/plain\n\nOK\n"; # this is what the sample code contained
$send_back = 1;
}

# SEND EMAILS #

if (!$error) {
if ($do_mail) {
$to_email = $hash_data{'payer_email'};
$etype = 0;
if ($return_status ne 'COMPLETED') {
$msg = "Your order at Paypal / $domain has been received.\n";
$msg .= "Completion is pending due to $hash_data{'pending_reason'}.\n";
$chk_it = MODULES::Ipnx::send_email($to_email, $email_admin, $mail_program, $email_subject, $msg, $return_status, $etype, 0, %hash_data);
if (!$chk_it) {
$msg = "Order email not sent. It failed. Manual sending required. To = $to_email, Transaction ID = $file_txn_id";
$error = 1;
}
}
if ($return_status eq 'COMPLETED') {
$msg = "Your order at Paypal / $domain has been completed.\n\n";
$chk_it = MODULES::Ipnx::send_email($to_email, $email_admin, $mail_program, $email_subject, $msg, $return_status, $etype, 0, %hash_data);

if (!$chk_it) {
$msg = "Order email not sent. It failed. Manual sending required. To = $to_email, Transaction ID = $file_txn_id";
$error = 1;
}
}
$etype = 1;
if (!$error) {
if ($return_status eq 'COMPLETED') {
$a = "ADMIN $return_status - $domain / Paypal Sale Completed";
$msg = "Order from $hash_data{'payer_email'}, transaction no = $hash_data{'txn_id'} is $return_status.\n\n";
}
if ($return_status ne 'COMPLETED') {
$a = "ADMIN $return_status - $pending_status - $domain / Paypal Sale";
$msg = "Order from $hash_data{'payer_email'}, transaction no = $hash_data{'txn_id'} is $return_status due to reason = $pending_status.\n\n";
}
$chk_it = MODULES::Ipnx::send_email($email_admin, $email_admin, $mail_program, $a, $msg, $return_status, $etype, $print_admin_detail, %hash_data);
if (!$chk_it) {
$msg = "Admin email not sent. It failed. Transaction ID = $file_txn_id";
$error = 1;
}
}
}
}
if ($do_mail) {
$etype = 1;
if ($error) {
$email_subject = "ADMIN ERROR MESSAGE - $domain / Paypal";
$msg = $dmsg;
if ($print_admin_detail) {$print_admin_detail =0}
$chk_it = MODULES::Ipnx::send_email($email_admin, $email_admin, $mail_program, $email_subject, $msg, $return_status, $etype, $print_admin_detail, %hash_data);
}
}
if (!$send_back) {
print "content-type: text/plain\n\nOK\n";
}

# DONE #
******************
----------------
IPNX_CONFIG.PL
--------------------
# Filename = Ipnx_Config.pl
# Version = 1.00
# Date = 02-14-03

# VARIABLE SETTING & DEFINITIONS #

# $root_dir
# ---------
# Actual directory script will run from
# Start path with leading slash & never end with a slash

$root_dir = "/hostx.webserver.com/~mysite/scgi-bin/ipnx";
# NOTE: data for $root_dir changed for privacy reasons
# $write_data_files
# NOTE: I have set all required folders/files to 755 rwxr-xr-x
# -----------------
# Write data files, set to 1
# DO NOT write data files, set to 0

$write_data_files = 1;

# $data_root_dir
# --------------
# Parent directory for data files (data & history)
# If located under $root_dir, set = $root_dir,
# else to desired location.
# Please note, these directories REQUIRE read-write
# for the WEB SERVER. If you encounter errors saving files,
# check this condition
# These files can be under cgi-bin or htdocs areas

$data_root_dir = "/hostx.webserver.com/~mysite/scgi-bin/ipnx";
# NOTE: data for $data_root_dir changed for privacy reasons but = $root_dir

# $data_dir
# ---------
# Directory for data files, under the $root_dir

$data_dir = "data";

# $history_dir
# ------------
# Directory for history files, under the $root_dir

$history_dir = "history";

# $logfile_dir
# ------------
# Directory for log files, under the $root_dir
# If you move data & history to htdocs, also move this
# directory to the same area.

$logfile_dir = "logs";

# $data_file_end
# --------------
# Ending extension for data & history files
# Make sure it has a dot in front of it

$data_file_end = ".txt";

# $email_admin
# ------------
# Email address of application admin
# Leave in single quotes to avoid problem with @ sign

$email_admin = 'diveboss@lonestardivers.com';

# $print_admin_detail
# -------------------
# To print order detail in admin email, set this value
# to "1", else set to "0" to ignore printing it.

$print_admin_detail = 1;

# $mail_program
# -------------
# Location of sendmail

$mail_program = "/usr/sbin/sendmail -t ";

# $do_mail
# --------
# 1 => Send Email, 0 => Do not send email
# FOR TESTING PURPOSES, YOU MAY ELECT TO TURN EMAIL NOTICES
# ON or OFF. IN THIS CASE, SEE VARIABLE $do_mail= 1
# BELOW (IT HAS INSTRUCTIONS ON THE LINE.)

$do_mail= 1;

# $domain
# -------
# Domain or business name related to this application
# This value is used in the next line

$domain = "LoneStarDivers.com";

# $email_subject
# --------------
# Standard Email subject

$email_subject = "Sales Receipt Email from - $domain";

# End of standard applications Configuration File #


DiveBoss

#2 paypal_pb

paypal_pb

    Advanced Member

  • Members
  • 2,960 posts

Posted 27 March 2003 - 02:41 PM

That's a general script error. Likely:
- syntax error
- missing dependency (possibly MODULES::Ipnx in this case)
- file corruption (if you edit on Windows but serve on Unix/Linux, make sure to save with Unix line feeds in your text editor)


Patrick Breitenbach
PayPal, Inc.
Dev Net: https://www.paypal.com/pdn

#3 DiveBoss

DiveBoss

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 27 March 2003 - 06:29 PM

Patrick,
I am using DzSoft Perl Editor to edit and save the scripts. I originally uploaded through the FrontPage 'Publish' function, buth then after having problems I deleted the files from my website and using FTP uploaded the files again. When viewing the files via my webservers control panel it shows me that it is a perl script file in ascii text; for the file inpx.cgi. When viewing the inpx_config.pl is shows the .pl files to be ascii text with CR/LF. These were also edited and save using DzSoft Perl Editor.


<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">Originally posted by paypal_pb
[br]That's a general script error. Likely:
- syntax error
- missing dependency (possibly MODULES::Ipnx in this case)<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">

Patrick, could you interpet MODULES::Inpx?

I know MODULES is a sub-folder under IPNX. The folder structure is:
/public_html/scgi-bin/inpx/MODULES. With the script file inpx.cgi location in the /public_html/scgi-bin/inpx and the 2 .pl files (inpx_config.pl and inpx.pm) in the /public_html/scgi-bin/inpx/MODULES

<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">Originally posted by paypal_pb{br]

- file corruption (if you edit on Windows but serve on Unix/Linux, make sure to save with Unix line feeds in your text editor)


Patrick Breitenbach
PayPal, Inc.
Dev Net: https://www.paypal.com/pdn
<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">[br]
First error:[br]
$receiver_email = $hash_data{'receiver_email'};
$receiver_email =~ s/\s//g; - this returns an error and appears to be a duplicate of the preceding line with different data for the string $receiver_email. I commented this line out. Can you tell me if it's required?
 
The 2nd error:[br]
if ($receiver_email ne $email_admin) {
[br] errors with uninitialized value in string ne
[br] I didn't think ne was a string; however I also do not know what it is. I thought it meant 'not equal' based on the balance of the context. Remember I'm still learning. 
The 3rd error:[br]
$msg = "Paypal has sent a transaction that does not belong to $domain. Email name received = $receiver_email";[br]
Here I get an uninitialed value in concatenation (.) or string. Can you help with any of these? 


Thank You!
Kerry Vogt aka DiveBoss


DiveBoss

#4 paypal_pb

paypal_pb

    Advanced Member

  • Members
  • 2,960 posts

Posted 28 March 2003 - 01:46 PM

This is beyond my knowledge. You might try contacting the developer.

Patrick Breitenbach
PayPal, Inc.
Dev Net: https://www.paypal.com/pdn

#5 DSD_David

DSD_David

    Junior Member

  • Members
  • 125 posts

Posted 29 March 2003 - 08:03 AM

Hello,

Well, I can maybe offer some help.

You're third error:

------------------
$msg = "Paypal has sent a transaction that does not belong to $domain. Email name received = $receiver_email";

Here I get an uninitialed value in concatenation (.) or string.
------------------

I think that the period in the sentence is throwing you off. It is usualy used to combine two strings together:

$A = 'A';
$B = 'B';
$C = $A . $B;
print $C;

Would give you: AB

Try escaping it out by putting a back slash (\) directly before it.

Your first error:
This command take in the string from $receiver_email and removes all white space characters. Not sure why it is throwing an error.

Your second error:
Yes, you are correct, ne means 'not equal' and it is used to check if two strings of characters are 'not equal'. Again though, I don't see why this is an error. And you can probably take it out for testing because all it seems to do is make sure that this program is recieving money meant for you.

Hope that helps a little.
-David

#6 DiveBoss

DiveBoss

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 29 March 2003 - 04:35 PM

<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">Originally posted by DSD_David
[br]Hello,

Well, I can maybe offer some help.

You're third error:

------------------
$msg = "Paypal has sent a transaction that does not belong to $domain. Email name received = $receiver_email";

Here I get an uninitialed value in concatenation (.) or string.
------------------

I think that the period in the sentence is throwing you off. It is usualy used to combine two strings together:

$A = 'A';
$B = 'B';
$C = $A . $B;
print $C;

Would give you: AB

Try escaping it out by putting a back slash (\) directly before it.

Your first error:
This command take in the string from $receiver_email and removes all white space characters. Not sure why it is throwing an error.

Your second error:
Yes, you are correct, ne means 'not equal' and it is used to check if two strings of characters are 'not equal'. Again though, I don't see why this is an error. And you can probably take it out for testing because all it seems to do is make sure that this program is recieving money meant for you.

Hope that helps a little.
-David
<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">

David,
Thanks for the input. It also explains why when I inadvertently removed a few . (periods) one feature quit working. I compared the original text to the modified text and found I had deleted the period on 2 lines and it affected the program.

I tried the \. entry in that line but the error persisted. I then broke the line down from:

$msg = "Paypal has sent a transaction that does not belong to $domain. Email name received = $receiver_email";

To:

$msg = "Paypal has sent a transaction that does not belong to " . "$domain" . ". " . "Email name received = " . "$receiver_email";

the new line passed. I thought the "." quoted period should work, however it didn't the way it was first used.

Anyway, here is where I stand. I have ipnx.cgi installed and the proper folders and support files it requires and/or uses. I have used Dave VanAbel's test site for IPN, on Perslsources.com to test the program. When I execute a test, I get an OK back. I get a file put into the "data" directory and a file put into the "history" directory. Based on how the .cgi script is written my history file is named yyyymm.txt and appends each entry. The data file is based on the txn_id.txt and each one it creates is new. (except if I do not pick a new txn_id then this files appends. There is also a "log" directory where error logs are placed. During testing at the free test site, I did not receive any error logs. I also got the appropriate emails for buyer and merchant.

When I do a test buy through PayPal, I get an error log and no data or history logs. Checking the code this seems correct if there are errors. However, I do not know why it's erroring. Well I kind of understand. Looking at the error log there are 35 pairs of data seperated by !!. The pairs of data are messed up. Some pairs are missing their mates, most just have the wrong mate.
Example
payer_email=
Completed=Payer_email
Weatherford=business (Weatherford should be address_city)
unverified=mc_currency
USA-mc_gross

and so on.... The more I program the more I understand, but with no books on PERL programming, I've had to just used my knowledge of basic, html and a few other programming languages to cipher this code.

What part of the code determines how to mate up what is received from PayPal and then sends it backs correctly?

This is how I read this:
===============================================================
This section initializes the variables
===============================================================
use strict;
use vars qw($scriptname $version $ver_date
$root_dir $write_data_files $data_root_dir
$data_dir $history_dir $logfile_dir
$data_file_end $email_admin $print_admin_detail
$mail_program $do_mail $domain $email_subject
);

$scriptname = "ipnx.cgi";
$version = "1.00";
$ver_date = "02-14-03";
======================================================================here is were it says where it will find it's application modules
======================================================================
use CGI;
use MODULES::Ipnx; # Application Package

======================================================================
More variables are initialzed
======================================================================

use vars qw($query $ua $req $res @pairs $pair $count $error $page $email_admin $var $key $val $msg $data_file $to_email $chk_it $no_txt_file $dmsg $business $receiver_email $item_name $item_number $quantity $invoice $custom $memo $tax $option_name1 option_selection1 $option_name2 $option_selection2 $num_cart_items $return_status $pending_status $trans_date %hash_data $std_date $mc_gross $mc_fee $mc_currency $settle_amount $settle_currency $exchange_rate $file_txn_id $txn_id $txn_type $for_auction $first_name $last_name $address_name $address_street $address_city $address_state $address_zip $address_country $address_status $payer_email $payer_id
$payer_status $payment_type $notify_version $verify_sign payment_gross
$payment_fee $a $b $c $send_back $pending_reason $history_file $etype $atrans_date
);

======================================================================
# File for setting application variables
======================================================================
require "MODULES/Ipnx_Config.pl";

$page = new CGI;
$error = 0;
$msg = "";

======================================================================
# READ POST from PayPal and add 'cmd'# - stated in the code, but do these 2 lines read the entire data file sent from paypal? if so, how does it parse the data?
======================================================================

read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query .= '&cmd=_notify-validate';

======================================================================
# POST BACK to PayPal# - also stated in the code, but again how many of these lines do the work? What in the code guarantees that what was received will be sent back correctly?
======================================================================

use LWP::UserAgent;
$ua = new LWP::UserAgent;
$req = new HTTP::Request 'POST','https://www.paypal.com/cgi-bin/webscr';
$req->content_type('application/x-www-form-urlencoded');
$req->content($query);
$res = $ua->request($req);

foreach $var ($page->param) {
$hash_data{$var} = $page->param($var);
}

$a = 0;
==== this area sets or defines the variables ==================
==== the first 3 lines use an application module caled inpx.pm ====

$trans_date = MODULES::Ipnx::Generic_Date_Stamp($a);
$hash_data{'transaction_date'} = $trans_date;
$std_date = MODULES::Ipnx::reverse_format_dates($trans_date);
$business = ($hash_data{'business'});
$receiver_email = ($hash_data{'receiver_email'});
#$receiver_email =~ s/\s//g;
$item_name = ($hash_data{'item_name'});
$item_number = ($hash_data{'item_number'});
$quantity = ($hash_data{'quantity'});
$invoice = ($hash_data{'quantuty'});
$custom = ($hash_data{'custom'});
$memo = ($hash_data{'memo'});
$tax = ($hash_data{'tax'});
$option_name1 = ($hash_data{'option_name1'});
$option_selection1 = ($hash_data{'option_selection1'}); $option_name2 = ($hash_data{'option_name2'});
$option_selection2 = ($hash_data{'option_selection2'});
$num_cart_items = ($hash_data{'num_cart_items'});
$return_status = uc($hash_data{'payment_status'});
$pending_status = uc($hash_data{'pending_reason'});
#$payment_date = ($hash_data{'payment_date'});
$mc_gross = ($hash_data{'mc_gross'});
$mc_fee = ($hash_data{'mc_fee'});
$mc_currency = ($hash_data{'mc_currency'});
$settle_amount = ($hash_data{'settle_amount'});
$settle_currency = ($hash_data{'settle_currency'});
$exchange_rate = ($hash_data{'exchange_rate'});
$file_txn_id = ($hash_data{'txn_id'});
$txn_type = ($hash_data{'txn_type'});
$for_auction = ($hash_data{'for_auction'});
$first_name = ($hash_data{'first_name'});
$last_name = ($hash_data{'last_name'});
$address_name = ($hash_data{'address_name'});
$address_street = ($hash_data{'address_street'});
$address_city = ($hash_data{'address_city'});
$address_state = ($hash_data{'address_state'});
$address_zip = ($hash_data{'address_zip'});
$address_country = ($hash_data{'address_country'});
$address_status = ($hash_data{'address_status'});
$payer_email = ($hash_data{'payer_email'});
$payer_id = ($hash_data{'payer_id'});
$payer_status = ($hash_data{'payer_status'});
$payment_type = uc($hash_data{'payment_type'});
$notify_version = ($hash_data{'notify_version'});
$verify_sign = ($hash_data{'verify_sign'});
$payment_gross = ($hash_data{'payment_gross'});
$payment_fee = ($hash_data{'payment_fee'});


# CHECK CONTROL #

======================================================================
While reading the next section, I might have found my problem. I will have to make some changes on paypal to verify. My primary email address is not the one payments are send to, I can change that and make it my primary. I'll get back with you.

if ($receiver_email ne $email_admin) {
$msg = "Paypal has sent a transaction that does not belong to" . "$domain" . ". " . "Email name received = " . "$receiver_email"; $a = 1;
$atrans_date = MODULES::Ipnx::Generic_Date_Stamp($a);
if (!$file_txn_id) {$file_txn_id = "NonePassed"}
if ($write_data_files) {
$data_file = "$root_dir/$logfile_dir/ERROR-" . "$atrans_date" . "_" . "$file_txn_id" . $data_file_end;
$chk_it = MODULES::Ipnx::open_file_for_write($data_file);
if (!$chk_it) {
$dmsg = "Can not open ERROR data file = $data_file";
$no_txt_file = 1;
}
if (!$no_txt_file) {
$_ = MODULES::Ipnx::write_submitted_data($data_file, %hash_data);
}
}
print "content-type: text/plain\n\nOK\n"; # this is what the sample code contained
$send_back = 1;
$error = 1;
}

# WRITE DATA #

if ((!$error) && ($write_data_files)) {
$data_file = "$data_root_dir/$data_dir/$file_txn_id" . $data_file_end;
$chk_it = MODULES::Ipnx::append_open_file_for_write($data_file);
if (!$chk_it) {
$dmsg = "Can not open data file = $data_file";
$no_txt_file = 1;
}
if (!$no_txt_file) {
$error = MODULES::Ipnx::write_submitted_data($file_txn_id, $data_file, %hash_data);
}
}

# WRITE HISTORY #

if ((!$error) && ($write_data_files)) {
$a = 0;
$_ = MODULES::Ipnx::Generic_Date_Stamp($a);
$history_file = substr($_,0,4);
$history_file .= substr($_,5,2);
$history_file .= "\.txt";
$data_file = "$data_root_dir/$history_dir/$history_file";
$chk_it = MODULES::Ipnx::append_open_file_for_write($data_file);
if (!$chk_it) {
$dmsg = "Can not open history data file = $data_file";
$no_txt_file = 1;
}
if ($chk_it) {
$error = MODULES::Ipnx::write_history_data($file_txn_id, $data_file, %hash_data);
}
}

# EVERYTHING OK #

if (!$error) {
print "content-type: text/plain\n\nOK\n"; # this is what the sample code contained
$send_back = 1;
}

# SEND EMAILS #

if (!$error) {
if ($do_mail) {
$to_email = $hash_data{'payer_email'};
$etype = 0;
if ($return_status ne 'COMPLETED') {
$msg = "Your order at Paypal / $domain has been received.\n";
$msg .= "Completion is pending due to $hash_data{'pending_reason'}.\n";
$chk_it = MODULES::Ipnx::send_email($to_email, $email_admin, $mail_program, $email_subject, $msg, $return_status, $etype, 0, %hash_data);
if (!$chk_it) {
$msg = "Order email not sent. It failed. Manual sending required. To = $to_email, Transaction ID = $file_txn_id";
$error = 1;
}
}
if ($return_status eq 'COMPLETED') {
$msg = "Your order at Paypal / $domain has been completed.\n\n";
$chk_it = MODULES::Ipnx::send_email($to_email, $email_admin, $mail_program, $email_subject, $msg, $return_status, $etype, 0, %hash_data);

if (!$chk_it) {
$msg = "Order email not sent. It failed. Manual sending required. To = $to_email, Transaction ID = $file_txn_id";
$error = 1;
}
}
$etype = 1;
if (!$error) {
if ($return_status eq 'COMPLETED') {
$a = "ADMIN $return_status - $domain / Paypal Sale Completed";
$msg = "Order from $hash_data{'payer_email'}, transaction no = $hash_data{'txn_id'} is $return_status.\n\n";
}
if ($return_status ne 'COMPLETED') {
$a = "ADMIN $return_status - $pending_status - $domain / Paypal Sale";
$msg = "Order from $hash_data{'payer_email'}, transaction no = $hash_data{'txn_id'} is $return_status due to reason = $pending_status.\n\n";
}
$chk_it = MODULES::Ipnx::send_email($email_admin, $email_admin, $mail_program, $a, $msg, $return_status, $etype, $print_admin_detail, %hash_data);
if (!$chk_it) {
$msg = "Admin email not sent. It failed. Transaction ID = $file_txn_id";
$error = 1;
}
}
}
}
if ($do_mail) {
$etype = 1;
if ($error) {
$email_subject = "ADMIN ERROR MESSAGE - $domain / Paypal";
$msg = $dmsg;
if ($print_admin_detail) {$print_admin_detail =0}
$chk_it = MODULES::Ipnx::send_email($email_admin, $email_admin, $mail_program, $email_subject, $msg, $return_status, $etype, $print_admin_detail, %hash_data);
}
}
if (!$send_back) {
print "content-type: text/plain\n\nOK\n";
}


DiveBoss

#7 DSD_David

DSD_David

    Junior Member

  • Members
  • 125 posts

Posted 29 March 2003 - 09:43 PM

Hey,

You have the line:

#$payment_date = ($hash_data{'payment_date'});

commented out, which may be throwing your order off.
Also, check into the line:

$invoice = ($hash_data{'quantuty'});

Something fishy with that statement.

Next...You wrote:

-------------------------
# READ POST from PayPal and add 'cmd'# - stated in the code, but do these 2 lines read the entire data file sent from paypal? if so, how does it parse the data?
======================================================================

read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query .= '&cmd=_notify-validate';
-------------------------

Yes, this does read in all the information from the post from PayPal, and then adds in the cmd value. That's it. There is no parsing here.

the aprsing takes place, I believe with:
foreach $var ($page->param) {
$hash_data{$var} = $page->param($var);
}

This is done with the CGI module, which unfortunetly, I have very little prcatice with.


If none of my other suggested changes works, you can try this, although I don't think it should make much of a difference.
Try commenting out those three lines and add this:

# split posted variables into pairs
@pairs = split(/&/, $query);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$hash_data{$name} = $value;
}


-David

#8 DiveBoss

DiveBoss

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 02 April 2003 - 10:59 AM

David,
Thanks for replying. I corrected 'quantuty', hell I can't type even though I've been using a keyboard for 30 years. I figured the rest, the way you did, about the parsing, etc.
My biggest problem was cured when I finally figured out that my return html screen that is called was trying to be posted to from PayPal and they provided code that changed the POST to GET. The script is working with many new changes including better mail messages to both myself and the buyer. I'll keep working on the script until I'm happy, but at least it now works.
I do have a question.
If PayPal POSTS to my HTML 'return' page, how can I accept that post and make the 'return html page' a printable receipt?
I would have to change the current layout which is just a text html screen that states the transaction was completed. I could layout a nice Invoice Style Screen that allowed for me to populate name, address, ext, and purchase information such as Item Name, Price, quantity, tax, s&h, total.
I would guess that there is html code similar to the perl code that would accept that POST from PayPal. Do you know how I can do this?



<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">Originally posted by DSD_David
[br]Hey,

You have the line:

#$payment_date = ($hash_data{'payment_date'});

commented out, which may be throwing your order off.
Also, check into the line:

$invoice = ($hash_data{'quantuty'});

Something fishy with that statement.

Next...You wrote:

-------------------------
# READ POST from PayPal and add 'cmd'# - stated in the code, but do these 2 lines read the entire data file sent from paypal? if so, how does it parse the data?
======================================================================

read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query .= '&cmd=_notify-validate';
-------------------------

Yes, this does read in all the information from the post from PayPal, and then adds in the cmd value. That's it. There is no parsing here.

the aprsing takes place, I believe with:
foreach $var ($page->param) {
$hash_data{$var} = $page->param($var);
}

This is done with the CGI module, which unfortunetly, I have very little prcatice with.


If none of my other suggested changes works, you can try this, although I don't think it should make much of a difference.
Try commenting out those three lines and add this:

# split posted variables into pairs
@pairs = split(/&/, $query);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$hash_data{$name} = $value;
}


-David
<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">

DiveBoss

#9 paypal_pb

paypal_pb

    Advanced Member

  • Members
  • 2,960 posts

Posted 02 April 2003 - 11:51 AM

The POST to the "return" URL is identical to the IPN. You can use the same or a similar script.

Patrick Breitenbach
PayPal, Inc.
Dev Net: https://www.paypal.com/pdn

#10 DiveBoss

DiveBoss

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 02 April 2003 - 02:03 PM

Patrick,
Are you saying if I put the PERL Script code:

# READ POST from PayPal and add 'cmd'#
read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query .= '&cmd=_notify-validate';

in an HTML file that it will accept the post from PayPal?

Of course, I would not need the line:
$query .= '&cmd=_notify-validate';

Then if I add the code:

foreach $var ($page->param) {
$hash_data{$var} = $page->param($var);
}

$business = ($hash_data{'business'});
$receiver_email = ($hash_data{'receiver_email'});
$item_name = ($hash_data{'item_name'});
$item_number = ($hash_data{'item_number'});
$quantity = ($hash_data{'quantity'});
$invoice = ($hash_data{'quantity'});
$custom = ($hash_data{'custom'});
$memo = ($hash_data{'memo'});
$tax = ($hash_data{'tax'});
$option_name1 = ($hash_data{'option_name1'});
$option_selection1 = ($hash_data{'option_selection1'});
$option_name2 = ($hash_data{'option_name2'});
$option_selection2 = ($hash_data{'option_selection2'});
$num_cart_items = ($hash_data{'num_cart_items'});
$return_status = uc($hash_data{'payment_status'}); #uc($hash
$pending_status = uc($hash_data{'pending_reason'}); #uc($hash
$payment_date = ($hash_data{'payment_date'});
$mc_gross = ($hash_data{'mc_gross'});
$mc_fee = ($hash_data{'mc_fee'});
$mc_currency = uc($hash_data{'mc_currency'});
$settle_amount = ($hash_data{'settle_amount'});
$settle_currency = ($hash_data{'settle_currency'});
$exchange_rate = ($hash_data{'exchange_rate'});
$file_txn_id = ($hash_data{'txn_id'});
$txn_type = uc($hash_data{'txn_type'});
$for_auction = ($hash_data{'for_auction'});
$first_name = ($hash_data{'first_name'});
$last_name = ($hash_data{'last_name'});
$address_name = ($hash_data{'address_name'});
$address_street = ($hash_data{'address_street'});
$address_city = ($hash_data{'address_city'});
$address_state = ($hash_data{'address_state'});
$address_zip = ($hash_data{'address_zip'});
$address_country = ($hash_data{'address_country'});
$address_status = uc($hash_data{'address_status'});
$payer_email = ($hash_data{'payer_email'});
$payer_id = ($hash_data{'payer_id'});
$payer_status = uc($hash_data{'payer_status'});
$payment_type = uc($hash_data{'payment_type'}); #uc(hash
$notify_version = ($hash_data{'notify_version'});
$verify_sign = ($hash_data{'verify_sign'});
$payment_gross = ($hash_data{'payment_gross'});
$payment_fee = ($hash_data{'payment_fee'});

I would then be able to place the variables in my invoice table to create a populated invoice? I understand that what I have shown here would basically work, however not being schooled in html, I would think some of the commands would be different. I'm looking for enough information as shown in the code above to make this work. Can anyone provide that information? Thanks for taking the time to help.

One more thing, do varaibles have to be initialized in html and if so what is the code arrangement for that?


<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">Originally posted by paypal_pb
[br]The POST to the "return" URL is identical to the IPN. You can use the same or a similar script.

Patrick Breitenbach
PayPal, Inc.
Dev Net: https://www.paypal.com/pdn
<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">

DiveBoss

#11 DSD_David

DSD_David

    Junior Member

  • Members
  • 125 posts

Posted 05 April 2003 - 07:18 AM

Hey,

HTML and PERL are totaly different.
You cannot put PERL coding (except Server-Side Includes) into HTML.
Perl script ahve to be put in a *.pl or *.cgi file on their own, and your server has to be able to run PERL scripts.

-David




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users