This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

PayPal Integration YEAH!!!


  1. drumaboy46
    Member

    Ok I have successfully made this work using various example on the forum. Basically the user after filling the form and choosing a pay amount on submission gets redirected to the paypal website for payment works great.

    This is my only hope and question:

    Is there a way to only save the form in the DB once they have actually completed their payment on paypal so for example.

    Step1: They fill out form and choose payment option $100
    Step2: some how when they submit the submission is cached (or something like temporally saved)
    Step3: When they are done with paypal then they will be redirected to my custom success.php page (which I will only make accessible after they pay) which will prompt machform to save the form in the DB.

    I need this part because now when people fill the form it goes through but I have no way of knowing other then going to paypal and check my account if they paid...I have an idea how I could accomplish this but need a few pointers.

    OR

    What I think is better is that the confirmation code simply gets passed from paypal to sucess.php and the date and confirmation number are inserted into fields pymt_date and conf_num I will create them on the ap_form_# table for that user. Just thought of this idea sorry about above example if too complicated. :)

    Posted 13 years ago #
  2. webmaster
    Member

    Looking for same thing...

    Posted 13 years ago #
  3. drumaboy46
    Member

    hello? Is there anyone that can help webmaster and me?!

    Posted 13 years ago #
  4. webmaster
    Member

    With PayPal IPN there should be something that can be done... let's wait for Yuniar reply.

    I must add that I do not yet own the product and I'm currently looking around for a product that will let us add on our website a form with complete validation and a PayPal button at the bottom to pay membership and if payment is successful will complete the subscription by filling a database.

    Will MachForm let us do that?

    Posted 13 years ago #
  5. Willy
    Member

    You can configure your form so that after completing the form the user is taken to a new url where you can have your payment conditions and options.

    Posted 13 years ago #
  6. webmaster
    Member

    Thanks Willy. I see no problem filling the form then sending user to a new page only to pay but after PayPal is successful (IPN return a success code?) will it write down all the data to the database?

    I don't want to have a DB entry where someone left when it was time to pay. I want to DB to fill only if PayPal confirmed the payment.

    Posted 13 years ago #
  7. drumaboy46
    Member

    Willy please read first post I already figured that part out. As for webmaster Machform is perfect for what you want. Validation is there but more fine tuned validation can be added by you. I currently have a fully functional form the only thing I am asking per my original post is the ability to save the form in the DB after a payment is made or update a field with payment info in the DB.

    Here is my temporary solution yet untested but I know will work.

    Step 1: Form is filled out. Payment option will be a drop down for example user chooses $100.

    Step 2: Onsubmit user is given a chance to review submission. Once they click continue they will be taken to my custom paypal page to pay the corresponding amount.

    Step 3: After payment is made they will be redirected to my success.php page were I will have code that updates a field in the DB called "payment status"(this is where code for the PayIPN will come into play looks VERY promising.)

    I need the third step because since Machform can't use multi-users I have to create my own page where my admins can see who has paid, which payments are pending etc....

    Webmaster I will be working on this thru the night and I think I can do it. Just waiting on Yuniar or Redityo for some expert advice but I think we may finally have something more refined here. thanks webmaster for the hint with IPN. Will update tonight or tomrr :)

    Posted 13 years ago #
  8. Willy
    Member

    Excuse me for not being clear drumaboy46,

    My answer was only for webmaster as I already understood that what you are looking for is more complex. And interesting, hopefully you'll manage to work it all out.

    Posted 13 years ago #
  9. webmaster
    Member

    46, it seems it will do what I want when you figure out how to do what we both are looking for. :-)

    From what I understand the BD is filled as soon as form is completed... and like me you would like an additional field that will toggle to TRUE if IPN reply with success.

    Posted 13 years ago #
  10. drumaboy46
    Member

    My apologies Willy :0...no harm intended.

    webmaster I will let you know the details after some testing :) I guess I'll have to buy something from my self to see if it will actually works. I know my wife will wonder what all the $1 charges on our account are for :)

    Posted 13 years ago #
  11. webmaster
    Member

    Well... Yuniar came but did not reply. Just like I never received a reply to a message sent to customer.service@appnitro.com 3 days ago.

    Posted 13 years ago #
  12. drumaboy46
    Member

    This was a little more difficult then expected. Was able to use my form, redirect to paypal with certain fields populated, make payment. And with IPN was able to receive a custom email message and also store transaction details in my DB...phew. But some much coding for me because I have a longer work flow for what I want. Also had to learn pal IPN structure. But it can be done now I am trying to create a review page and pass custom variables to and from paypal. And yes what happened to Yunair seems like everyone wants his time don't worry he always responds. This is a feature that everyone wants.

    Posted 13 years ago #
  13. ryanseo
    Member

    drumaboy46, can you show me how you did this step by step by chance? Much appreciated, thanks.

    Posted 13 years ago #
  14. drumaboy46
    Member

    don't worry ryanseo I will respond doing some heavy coding for now but I will try to make a rather a large post tomrr :) step by step as your said. But for now everyone should read up on paypal IPN. www.paypal.com/ipn

    Posted 13 years ago #
  15. drumaboy46
    Member

    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&currency_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&currency_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&currency_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&currency_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 :)

    Posted 13 years ago #
  16. mig2000
    Member

    Has anyone else successfully implemented the above? I would like to do this as I see it to be a similar cases in many application. Thanks for the great work and sharing.

    Posted 13 years ago #
  17. mig2000
    Member

    I have the following case. I wonder if this can work in my case:

    Please see the link below. This form requires the user to fill out the form and then the by clicking the Submit, the system takes them to PayPal to pay. How can I have it to make sure they pay before I actually let Macform to add the record to the database or send a copy or the email to the users? The way it is today, the user can create the record and NOT pay the payment. Please advice.

    http://www.dsaom.org/contactus/annualwalk/walk2010.html

    Posted 13 years ago #
  18. Trowar
    Member

    I use a payment system on my machform. I have every entry generate a unique reference. This is passed through paypal using the 'custom' pass-through varibale.

    When PayPal cofirms the payment through IPN it sends me this reference. My IPN page then uses the reference to locate the customer's record and populates a field to say they have paid.

    Posted 13 years ago #
  19. mig2000
    Member

    That sounds Great. Can you share with me the step by step in what I need to do to get that accomplished? In advance I thank you for your help.

    Posted 13 years ago #
  20. Trowar
    Member

    I'll try to figure out the easierst way. Does you site use the above code? If so I'll try ro write something that integrates into that.

    Posted 13 years ago #

RSS feed for this topic

Reply »