ok I'am back here we go:
PLEASE BACK UP ALL YOUR FILES SO YOU DON'T SAY I RUINED YOUR MACHFROM!
Step1: Get a paypal account duh!
a1: Turn on form reidrect
a2: Turn on IPN(instant payment notification) located in the menu profile->more
options->Instant Payment Notification Preferences(under selling prefs)
you will need to specify your IPN listener url example
www.yoursite.com/includes/paypal.php (so create a php page for this)
call it whatever you want I use paypal.php it doesn't matter.
a3: Once your bank info is verified you can have your customers skip paypal signup by
"enabling paypal account option" Website Payment Preferences(under selling prefs)
this is located in profile->more options->Website Payment Preferences
Step2: Edit these include files
a1: post-functions.php look for this code
//if 'form review' enabled, send review_id
if(!empty($form_review)) {
$process_result['review_id'] = $record_insert_id;
}
right underneath that put you code something like this is what I use. I created the paypal links by using a paypal URL generator to get me started then learned the variables from paypal
//CUSTOM CODE REDIRECT/////////////////////////////////////////////////
if($form_id == "7") {
//Here I wanted to prepopulate some of the paypal fields from
//they filled out on the machform
//Of coarse just replace the elements_# with your elements
$first_name = $user_input['element_2_1'];
$last_name = $user_input['element_2_2'];
//The phone number is split this way cause that's how paypal reads it
$ibo_phone_a = $user_input['element_4_1'];
$ibo_phone_b = $user_input['element_4_2'];
$ibo_phone_c = $user_input['element_4_3'];
$payer_email = $user_input['element_5'];
//Here I silently pass a session_id that is unique to each payer to
//to a hidden form element
//later in the code I will show you how to achieve this
$custom_session_id = $user_input['element_17'];
switch ($user_input['element_8']) {
//obviously this is where you would put your products depending on what the
//user chooses they will be redirected to the correct paypal url
//once the form has been submitted. The redirect will happen after the
//the success page is viewed not at the review page
//this way they get a "Success you form has been saved redirecting to PP..."
case 1 : $item_name = "apples"; break;
case 2 : $item_name = "monkeys"; break;
case 3 : $item_name = "poo"; break;
case 4 : $item_name = "yuniar"; break;
case 1 : $redirect = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&first_name=".$first_fname."&last_name=".$last_lname."&night_phone_a=".$ibo_phone_a."&night_phone_b=".$ibo_phone_b."&night_phone_c=".$ibo_phone_c."&email=".$ibo_email."&business=bussnessemail@yoursite.com&undefined_quantity=0&item_name=".$team_name."+&amount=180.00&no_shipping=1&return=yoursite.com&cancel_return=http://www.yoursite.com/cancelled.php¤cy_code=USD&bn=PP%2dBuyNowBF&charset=UTF%2d8&custom=".$custom_session_id." target=_self"; break;
case 2 : $redirect = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&first_name=".$first_fname."&last_name=".$last_lname."&night_phone_a=".$ibo_phone_a."&night_phone_b=".$ibo_phone_b."&night_phone_c=".$ibo_phone_c."&email=".$ibo_email."&business=bussnessemail@yoursite.com&undefined_quantity=0&item_name=".$team_name."+&amount=280.00&no_shipping=1&return=yoursite.com&cancel_return=http://www.yoursite.com/cancelled.php¤cy_code=USD&bn=PP%2dBuyNowBF&charset=UTF%2d8&custom=".$custom_session_id." target=_self"; break;
case 3 : $redirect = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&first_name=".$first_fname."&last_name=".$last_lname."&night_phone_a=".$ibo_phone_a."&night_phone_b=".$ibo_phone_b."&night_phone_c=".$ibo_phone_c."&email=".$ibo_email."&business=bussnessemail@yoursite.com&undefined_quantity=0&item_name=".$team_name."+&amount=80.00&no_shipping=1&return=yoursite.com&cancel_return=http://www.yoursite.com/cancelled.php¤cy_code=USD&bn=PP%2dBuyNowBF&charset=UTF%2d8&custom=".$custom_session_id." target=_self"; break;
case 4 : $redirect = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&first_name=".$first_fname."&last_name=".$last_lname."&night_phone_a=".$ibo_phone_a."&night_phone_b=".$ibo_phone_b."&night_phone_c=".$ibo_phone_c."&email=".$ibo_email."&business=bussnessemail@yoursite.com&undefined_quantity=0&item_name=".$team_name."+&amount=100.00&no_shipping=1&return=yoursite.com&cancel_return=http://www.yoursite.com/cancelled.php¤cy_code=USD&bn=PP%2dBuyNowBF&charset=UTF%2d8&custom=".$custom_session_id." target=_self"; break;
}
//custom code
//$process_result['form_redirect'] = $redirect; //you don't need this code
//$_SESSION['form_redirect'] = $process_result['form_redirect'];//not needed
$_SESSION['form_redirect'] = $redirect; //needed
}
/////////////////////////////////////////////////////////////////////////
a2: now go to bottom pf post-functions.php around line 2340 you will see this
//delete all entry from this user in review table
$session_id = session_id();
do_query("DELETE FROM <code>ap_form_{$form_id}_review</code> where id='{$record_id}' or session_id='{$session_id}'");
$commit_result['form_redirect'] = $form_redirect;
Right below that add this
//CUSTOM REDIRECT CODE
//custom code
if (!empty($_SESSION['form_redirect'])) {
//disable the code to redirect using timer
//$commit_result['form_redirect'] = $_SESSION['form_redirect'];
}
return $commit_result;
Step 3: Edit view-functions.php
a1: around line 2267 you will see this
$form_markup = <<<EOT
ABOVE this line put this code
if ($form_id == 7 && !empty($_SESSION['form_redirect'])) {
$custom_redirect_url = '<meta http-equiv="Refresh" content="5;url='.$_SESSION['form_redirect'].'">';
unset($_SESSION['form_redirect']);
}
a2: Now if you wanted the redirects to contain the session_id like I did that way you
could use it as a receipt or a unique indetifier if you were creating a DB to
store all the data that pay pal sends you like i did read this post
http://www.appnitro.com/forums/topic/session_id-retreival?replies=3
Step 4: Setting up an IPN listener this will enable to store user data into your DB
a1: create a table in the your machform DB called lets say payments
a2: create a file called paypal.php
a3: go to paypal and the you will need to specify your IPN listener url example
www.yoursite.com/includes/paypal.php
a4: In you paypal.php file erase all default html and put this between
<body>paste here!</body> tags
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?
include("../machform/config.php");//Establishes varibles used to connect to you machform DB, then you can specify wich table in your inserst command
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
@mysql_select_db(DB_NAME) or die( "Unable to select database");
//add 'cmd' as required to get VERIFIED response
$req = 'cmd=_notify-validate';
//put post into NVP format
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// setup headers for request to paypal
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
//Open socket
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
//Test your IPN with the sandbox account for devs you must create one first
//go to https://developer.paypal.com/
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);// use this when testing is done and you want to go live
if (!$fp)// failed to connect to url
{
//write to file
$fh = fopen("logipn.txt", 'a');//open file and create if does not exist
fwrite($fh, "\r\n/////////////////////////////////////////\r\n HTTP ERROR \r\n");//Just for spacing in log file
fwrite($fh, $errstr);//write data
fclose($fh);//close file
//email
$mail_From = "From: payments@yoursite.com";
$mail_To = $email;
$mail_Subject = "HTTP ERROR";
$mail_Body = $errstr;//error string from fsockopen
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
}
else//successful connect to url
{
fputs ($fp, $header . $req);//send request
while (!feof($fp)) //while not end of file
{
$res = fgets ($fp, 1024);//get response
if (strcmp ($res, "VERIFIED") == 0) //IF ALL PAYPAL IPN VARIABLES ARE VERIFIED PROCEED
{
//PAYPAL IPN VARIABLES
//Payment Information = $_POST[''];
$payment_type = $_POST['payment_type'];
$payment_date = $_POST['payment_date'];
$paymentDateTmp=strtotime($_POST['payment_date']);
$ConvertedPaymentDate=strftime('%Y-%m-%d %H:%M:%S',$paymentDateTmp);
if($_POST['pending_reason'] == ""){
$pending_reason = "none";
}else{
$pending_reason = $_POST['pending_reason'];
}
if($_POST['reason_code'] == ""){
$reason_code = "none";
}else{
$reason_code = $_POST['reason_code'];
}
$payment_status = $_POST['payment_status'];
$receipt_id = $_POST['receipt_ID'];
//Buyer Information
$address_status = $_POST['address_status'];
$payer_status = $_POST['payer_status'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$payer_email = $_POST['payer_email'];
$payer_id = $_POST['payer_id'];
$address_name = $_POST['address_name'];
$address_country = $_POST['address_country'];
$address_country_code = $_POST['address_country_code'];
$address_zip = $_POST['address_zip'];
$address_state = $_POST['address_state'];
$address_city = $_POST['address_city'];
$address_street = $_POST['address_street'];
$night_phone_a = $_POST['night_ phone_a'];
$night_phone_b = $_POST['night_ phone_b'];
$night_phone_c = $_POST['night_ phone_c'];
//Basic information
$business = $_POST['business'];
$receiver_email = $_POST['receiver_email'];
$receiver_id = $_POST['receiver_id'];
$residence_country = $_POST['residence_country'];
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];//Will be used for team name
$quantity = $_POST['quantity'];
$shipping = $_POST['shipping'];
$tax = $_POST['tax'];
//Currency and currrency exchange
$mc_currency = $_POST['mc_currency'];
$mc_fee = $_POST['mc_fee'];
$mc_gross = $_POST['mc_gross']; //Full Payment amount before fee's
$mc_gross_1 = $_POST['mc_gross_1'];
//Transaction fields
$txn_type = $_POST['txn_type'];
$txn_id = $_POST['txn_id'];
$notify_version = $_POST['notify_version'];
//Advanced and custom information
$custom = $_POST['custom']; //Can either equal a session ID or the number 2
if($_POST['resend'] == ""){
$resend = "none";//amount of time that the IPN message was resent
}else{
$resend = $_POST['resend'];
}
//Obviously you need to create a table with these fields number_of_payments,payment_date,element_2_1,element_2_2,element_5,custom_session_id,payment_amt,payment_status,pending_reason,reason_code,item_name,team_name,pp_receipt_id,times_resent
$sql = "INSERT INTO payments (number_of_payments,payment_date,element_2_1,element_2_2,element_5,custom_session_id,payment_amt,payment_status,pending_reason,reason_code,item_name,team_name,pp_receipt_id,times_resent) VALUES ('$number_of_payments', '$ConvertedPaymentDate', '$first_name', '$last_name', '$payer_email', '$custom', '$mc_gross', '$payment_status', '$pending_reason', '$reason_code', '$item_name', '$item_number', '$receipt_id', '$resend')";
$insert = mysql_query($sql);
//write to file
$fh = fopen("logipn.txt", 'a');//open file and create if does not exist
fwrite($fh, "\r\n/////////////////////////////////////////\r\n Verified \r\n");//Just for spacing in log file
fwrite($fh, $req);//write data
fclose($fh);//close file
//email construct
//Email address to send emails:
$to="youremail@gmail.com";//
$from = "payments@yoursite.com";
$subject = "VERIFIED IPN Payment was Successful";
//begin of HTML message
$message = "
<html>
<body bgcolor='#DCEEFC'>
<center>
<h4>Thank you $first_name for Your purchase.</h4> <br>
Below are the details fo your transaction.
<strong>Date of payment</strong> $payment_date new format $ConvertedPaymentDate
<strong>Paypal Reciept#</strong> <span style='color:red;'>$receipt_id</span>
<strong>Item Name:</strong> $item_name
<strong>Payment Amount:</strong> $mc_gross
<h4>Paypal Variable Test</h4>
<li><strong>Date:</strong> $ConvertedPaymentDate</li>
<li><strong>First Name:</strong> $first_name</li>
<li><strong>Last Name:</strong> $last_name</li>
<li><strong>Buyers Email:</strong> $payer_email</li>
<li><strong>SessionID:</strong> $custom</li>
<li><strong>Payment Status:</strong> $payment_status</li>
<li><strong>Pending Reason Code:</strong> $pending_reason</li>
<li><strong>Reason Code:</strong> $reason_code</li>
<li><strong>Product:</strong> $item_name</li>
<li><strong>Team Name:</strong> $item_number</li>
<li><strong>PayPal Receipt ID:</strong> $receipt_id</li>
<li><strong>Times IPN was resent:</strong> $resend</li>
</center>
</body>
</html>";
//end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
//options to send to cc+bcc
//$headers .= "Cc: [email]maa@p-i-s.cXom[/email]";
//$headers .= "Bcc: [email]email@maaking.cXom[/email]";
// now lets send the email.
mail($to, $subject, $message, $headers);
}
else if (strcmp ($res, "INVALID") == 0)
{
//write to file
$fh = fopen("logipn.txt", 'a');//open file and create if does not exist
fwrite($fh, "\r\n/////////////////////////////////////////\r\n Invalid \r\n");//Just for spacing in log file
fwrite($fh, $req);//write data
fclose($fh);//close file
$to="youremail@gmail.com";
$from = "payments@yoursite.net";
$subject = "INVALID IPN therefore no entry was made into DB";
//Error messge when IPN's are not verified
$message2 = "
<html>
<body bgcolor='#DCEEFC'>
<center>
<h4>The IPN could not be verified and therefore was invalid.</h4> <br>
Below are the details.
<strong>PayPal IPN Message</strong> ".$req."
</center>
</body>
</html>";
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
//options to send to cc+bcc
//$headers .= "Cc: [email]maa@p-i-s.cXom[/email]";
//$headers .= "Bcc: [email]email@maaking.cXom[/email]";
// now lets send the email.
mail($to, $subject, $message2, $headers);
}
}
fclose ($fp);//close file pointer
}
?>
</body>
</html>
a5: Now go to https://developer.paypal.com/ and open a free dev account log in and on
the left where you see test tools click it and then click
"Instant Payment Notification (IPN) Simulator." Specify you IPN listener in our
case www.yoursite.com/includes/paypal and choose web accept scroll down
and click send. and if all goes well you will receive and email and the data should
be in the DB as well.
PLEASE NOTE: That this took me almost 3days to accomplish and you have to just carfully work the code I will be here to assist you but please have patients I will help. Just make sure to back all original files. thank you hopes this helps.
Please Yuniar take my code and make it awesome for next build so we can have paypal and be very cool web devs thank :)