Jump to content

Send Confirmation email only on successful transaction?


classic

Recommended Posts

I am new to PHP but have completed a successful integration with a payment gateway for testing/learning purposes. What I want to now accomplish is for the script to send me a confirmation email only when there is a successful transaction. I have the code completed for the email to send and that is already functioning as well, however, it sends me an email every time there is a transaction submitted, regardless if it actually goes through and gets approval from the gateway.I was thinking of utilizing an if / else statement but I am totally stumped on this one.I appreciate any direction or help you guys have.

Link to comment
Share on other sites

Your paymeny provider will send a status code with the rest of the result information and you can, as you said, use an if statement to only send the e-mail if it's there. For example, WorldPay / Business Gateway returns the variable $_POST["transStatus"] which is either Y, N or C (for cancelled). I branch depending on which status is sent.

Link to comment
Share on other sites

Thanks for the info. Since I am fairly new, I am trying to figure out the proper syntax to call the email to send after the if statement. The e-mail code is below:

// Receiving variables@$ip= $_SERVER['REMOTE_ADDR'];@$TransType = addslashes($_POST['TransType']);@$NameOnCard = addslashes($_POST['NameOnCard']);@$Amount = addslashes($_POST['Amount']);@$Street = addslashes($_POST['Street']);@$Zip = addslashes($_POST['Zip']);//Sending Email to form owner$header = "From: email@emailgoeshere.com\n"  . "Reply-To: email@emailgoeshere.com\n";$subject = "Donation Received!";$email_to = "email@emailtogoeshere.com";$message = "Visitor's IP: $ip\n". "NameOnCard: $NameOnCard\n". "Amount: $Amount\n". "Street: $Street\n". "Zip: $Zip\n"@mail($email_to, $subject ,$message ,$header );?>

I pieced the above together from several examples and it seems to work okay as I am getting the emails. Would the added if statement look something like this:

if ($response_message=="Approved")// Receiving variables@$ip= $_SERVER['REMOTE_ADDR'];@$TransType = addslashes($_POST['TransType']);@$NameOnCard = addslashes($_POST['NameOnCard']);@$Amount = addslashes($_POST['Amount']);@$Street = addslashes($_POST['Street']);@$Zip = addslashes($_POST['Zip']);//Sending Email to form owner$header = "From: email@emailgoeshere.com\n"  . "Reply-To: email@emailgoeshere.com\n";$subject = "Donation Received!";$email_to = "email@emailtogoeshere.com";$message = "Visitor's IP: $ip\n". "NameOnCard: $NameOnCard\n". "Amount: $Amount\n". "Street: $Street\n". "Zip: $Zip\n"@mail($email_to, $subject ,$message ,$header );?>

Link to comment
Share on other sites

Yes, but you want to wrap everything that you want to happen in braces { }Visitor IP can be useful, but it can also be misleading if someone uses a subnet mask or if the visitor is from an office type environment and sharing their internet access - this can lead to false IP addresses or the masked 255.255.0.0 (I'm not sure about the new longer IP address equivalent).

Link to comment
Share on other sites

Thanks a bunch for the help. I am assuming the {} are after the if statement (not including)? so:

if ($response_message=="Approved"){// Receiving variables@$ip= $_SERVER['REMOTE_ADDR'];@$TransType = addslashes($_POST['TransType']);@$NameOnCard = addslashes($_POST['NameOnCard']);@$Amount = addslashes($_POST['Amount']);@$Street = addslashes($_POST['Street']);@$Zip = addslashes($_POST['Zip']);//Sending Email to form owner$header = "From: email@emailgoeshere.com\n"  . "Reply-To: email@emailgoeshere.com\n";$subject = "Donation Received!";$email_to = "email@emailtogoeshere.com";$message = "Visitor's IP: $ip\n". "NameOnCard: $NameOnCard\n". "Amount: $Amount\n". "Street: $Street\n". "Zip: $Zip\n"@mail($email_to, $subject ,$message ,$header );}?>

Link to comment
Share on other sites

Yup - only add the else clause if you want the script to do something...else. However, you almost always do - for example, if the transaction failed, you want to tell your user that and perhaps keep their item(s) in their basket instead of clearing them (which is what most customers expect to happen in the event of a successful transaction).It's worth noting that you should avoid the need for silencing errors from variable assignment - you should handle all possible errors you can foresee. If any part of variable assignment fails, that portion of the script should gracefully handle the failure.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...