-
Verben konjugieren - mit Coniuno spielend leicht gelernt
Verben konjugieren - mit Coniuno spielend leicht gelernt
Verben konjugieren mit Coniuno
Verben konjugieren in Deutsch, Niederländisch, Englisch, Französisch, Italienisch, Spanisch, Portugiesisch und Latein
Verben konjugieren in Deutsch, Niederländisch, Englisch, Französisch, Italienisch, Spanisch, Portugiesisch und Latein
-


PayPal – Automatische eMail Kaufbestätigung an den Kunden mit IPN


IPN – Instant Payment Notification


Zurück zur Übersicht

VIII. Vollständiger Code:


Im Nachfolgenden ist der komplette Code des Tutorials aufgeführt, inklusive einer Beispiel-password.php Datei.
Ihr könnt den Code gerne für eure eigene Webseite verwenden und nach euren Wünschen anpassen.
Wenn ihr noch weitere Hürden genommen habt oder Tricks rausgefunden habt, warum IPN Notifications nicht funktioniert haben, wäre Feedback super, da werden sich bestimmt andere Entwickler auch drüber freuen.

Hinweis:

Der IPN Listener sucht nach der Password Datei (password.php) in dem Verzeichnis, in welchem er selber gespeichert ist, siehe im Code:

require_once("password.php"); // database and mail configuration

Wenn ihr mit dem IPN Listener live geht und in das Internet ladet, stellt bitte sicher, dass diese Datei ausserhalb eurer Web-Domain gespeichert ist.
siehe hierzu auch: Wo speichert man seine Passwörter



password.php: Die Datei zum Speichern eurer MySQL Datenbank Zugangsdaten und des Passworts zum Senden von eMails über eure Webseite:

<?php $mySqlServer = ""; // your provider's mySql Server (e.g. Strato: rdbms) $mySqlUser = ""; // your User Account on the server $mySqlPass = ""; // your Password for the server $mySqlDB = ""; // the database that holds the user table with your customer's emails $mailPass = ""; // password to send mails with your account on <meineSeite> ?>


ipn-listener.php: der IPN Listener des Tutorials
<?php // IPN Listener example code // by Helmut Bischoff, 2016 // feel free to use for your own webpages // function to write the log data (same directory as the IPN Listener) function funcWriteDebug ($arDbg) { $strDbg = implode("\n", $arDbg); // store debug information in one string $inc = 0; // store as ipn1.log, ipn2.log... do { $inc++; } while (file_exists('ipn'.$inc.'.log')); file_put_contents ('ipn'.$inc.'.log', $strDbg); // store debug info return true; } // ----------------------------------------------------------------- // main program require_once("password.php"); // database and mail configuration require_once("Mail.php"); // standard PHP mail handler DEFINE("USE_SANDBOX", 1); // set to 1 for Sandbox and Simulator DEFINE("DEBUG", 1); // set to 1 to write Debug Log File if (DEBUG) { // create an array to store debug info $arDbg = array(''); array_push($arDbg, '>> IPN Message <<'); } // IPN message received, add validation command and return to PayPal $req = 'cmd=_notify-validate'; foreach ($_POST as $key => $value) { $value = urlencode(stripslashes($value)); $req .= "&" . $key . "=" . $value; if (DEBUG) array_push($arDbg, $key.'='.$value); // debug log data } // Define URL for "live system" or Sandbox / Simulator testing if (USE_SANDBOX) { $paypal_url = "ipnpb.sandbox.paypal.com"; $port = "443"; } else { $paypal_url = "ipnpb.paypal.com"; $port = "443"; } // try to open the socket to PayPal (identical for sandbox / simulator and live) $fp = fsockopen ('ssl://'.$paypal_url, $port, $errno, $errstr, 30); // if socket cannot be opened, request PayPal to resend the IPN if (!$fp) { header( "HTTP/1.1 400 Bad Request" ); // that's why you shouldn't send 200 OK if (DEBUG) { array_push($arDbg, 'socket open failed'); funcWriteDebug($arDbg); } return false; } // send the IPN message back to PayPal $paypal_path = "/cgi-bin/webscr"; fputs($fp, "POST " . $paypal_path . " HTTP/1.1\r\n"); fputs($fp, "Host: " . $paypal_url . ":".$port."\r\n"); fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-Length: " . strlen($req) . "\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $req); // check for VERIFIED response if (DEBUG) array_push($arDbg, 'PayPal Validation result:'); $valid = false; // init: result is INVALID while(!feof($fp)) // PayPal sends VERIFIED or INVALID { $res = fgets ($fp, 1024); if (strcmp(strtoupper(trim($res)), "VERIFIED") == 0) $valid = true; if (DEBUG) array_push($arDbg, 'result = '.$res); } fclose ($fp); // close the socket to PayPal if (!$valid) { // IPN verification failed, ignore message if (DEBUG) { array_push($arDbg, 'IPN INVALID'); funcWriteDebug($arDbg); } return false; // optional: implement error notification } // to yourself (e.g. send email) else { if (DEBUG) { array_push($arDbg, 'IPN VERIFIED'); } } // ------------------------------------------------------------------------- // now we are done with PayPal... // here comes the private code for successful IPN notification handling // e.g. enter user data in database, and eventually send eMail to the // new customer // read all interesting data from the POST data received in the IPN notification // at the beginning of the IPN Listener // if not included, default the variables to '-' $buy_email = isset($_POST['payer_email']) ? $_POST['payer_email'] : '-'; $buy_firstname = isset($_POST['first_name']) ? $_POST['first_name'] : '-'; $buy_lastname = isset($_POST['last_name']) ? $_POST['last_name'] : '-'; $buy_paytype = isset($_POST['payment_type']) ? $_POST['payment_type'] : '-'; $buy_paystatus = isset($_POST['payment_status']) ? $_POST['payment_status'] : '-'; $buy_mcgross = isset($_POST['mc_gross']) ? $_POST['mc_gross'] : '-'; $buy_mccurrency = isset($_POST['mc_currency']) ? $_POST['mc_currency'] : '-'; $buy_itemnumber = isset($_POST['item_number']) ? $_POST['item_number'] : '-'; // enter new customer's email address in a database $errLevel = 0; // init: before db activities // connect to mySql server $link = mysqli_connect($mySqlServer, $mySqlUser, $mySqlPass); if ($link) $errLevel = 1; // if successful, raise level // connect to database if (($errLevel == 1) && (mysqli_select_db($link, $mySqlDB))) $errLevel = 2; // check if buyer is already registered if ($errLevel == 2) { if ($gUser = mysqli_query($link, "SELECT * FROM users WHERE email='".$buy_email."'")) { $errLevel = 3; } } if ($errLevel == 3) { // check if 0 or 1 entry $verify = mysqli_num_rows($gUser); // if 0 entries, then add if ($verify == 0) $errLevel = 4; // new user to database } if ($errLevel == 4) { // user not registered yet if (mysqli_query($link, "INSERT INTO users (email) VALUES ('".$buy_email."')")) { $errLevel = 5; // add user to database } } if ($errLevel >= 1) mysqli_close($link); // above level 0, close connection // evaluate error level if ($errLevel == 3) { // user already registered $errText = 'user already registered'; } else if ($errLevel == 5) { // new user successfully registered $errText = 'new user registered'; } else // else, something went wrong { $errText = 'ErrLevel: '.$errLevel.' Error: '; switch ($errLevel) // write debug information { case 0: $errText .= 'couldn\'t connect to MySQL Server' . mysql_error(); break; case 1: $errText .= 'couldn\'t set default database' . mysql_error(); break; case 2: $errText .= 'couldn\'t fetch data from database' . mysql_error(); break; case 4: $errText .= 'couldn\'t insert user to database' . mysql_error(); break; default: break; } // log the error, then terminate the program // optional: error handling, e.g. send mail to yourself if (DEBUG) { array_push($arDbg, 'Database error: '.$errText); funcWriteDebug($arDbg); } return false; } // send email to the new customer $mimeVer = '1.0'; $contType = 'text/html; charset=UTF-8'; // send mail in HTML format and UTF-8 $from = 'Your Webpage <support@your_webpage.de>'; $to = '<'.$buy_email.'>'; $subject = 'Kaufbestätigung'; $body = ' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <div style="font-family:Verdana; font-size:10pt"> Sehr geehrte(r) Frau/Herr '.$buyLastName.',<br> <br> vielen Dank, dass Sie unser Produkt gekauft haben.<br> … <br> <br> vielen Dank und viele Grüße<br> Name <br> <br> </div> </body> </html> '; // end of mail body $headers['From'] = $from; $headers['To'] = $to; $headers['Subject'] = $subject; $headers['MIME-Version'] = $mimeVer; $headers['Content-type'] = $contType; $params['sendmail_path'] = '/usr/lib/sendmail'; $mail_object =& Mail::factory('sendmail', $params); $mail_object->send($to, $headers, $body); if (PEAR::isError($mail)) { if (DEBUG) { array_push($arDbg, 'Mail could not be send, error: '.$mail->getMessage()); funcWriteDebug($arDbg); } return false; } // write the log file, and terminate if (DEBUG) { array_push($arDbg, 'IPN Listener done, terminating program'); funcWriteDebug($arDbg); } ?>





Support:
Webmaster:
support@coniuno.de
webmaster@coniuno.de
Copyright © Helmut Bischoff 2005-2018. All rights reserved
 
Copyright H.Bischoff 2005-2018. All rights reserved