Simple php paypal ipn for wordpress - php

This is my first time trying to integrate paypal into a wordpress site - so I might need a bit of hand holding. I'm building a custom plugin for a client where the admin creates booking forms for events and assigns them to users. The user can then log in and see their assigned booking forms and fill them out. Once filled out, the user can then pay the price of their booking.
Let say the user has filled out their booking form and is now ready to pay for their booking, they click on "pay" and are taken to a payment page (www.yoursite.com/deposit/?booking-id=xxx) which is essentially just a message and a pay button and hidden form fields for paypal. Here is the php for that page:
<?php
$paypal_url='https://www.sandbox.paypal.com/cgi-bin'; //
$paypal_id='malik#thedistractionsband.co.uk'; // Business email ID
$booking_form_id = get_query_var( 'booking-id' );
$current_user = wp_get_current_user();
?>
<?php
// The Query
$bookings_query = new WP_Query(
array(
'post_type' => 'bookings',
'p' => $booking_form_id
)
);
// The Loop
if ( $bookings_query->have_posts() ) {
while ( $bookings_query->have_posts() ) {
$bookings_query->the_post();
?>
<h2>Pay Deposit</h2>
<p>Hello <?php echo $current_user->display_name; ?> blah blah blah</p>
// Paypal form
<form action="<?php echo $paypal_url; ?>" method="post" name="frmPayPal1">
<?php
$total_amount = get_post_meta( $bookings_query->post->ID, 'wedding_price', true );
$deposit_amount = $total_amount*0.2;
?>
<input type="hidden" name="business" value="<?php echo $paypal_id; ?>">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="item_name" value="<?php echo get_post_meta( $bookings_query->post->ID, 'wedding_name', true ); ?> - 20% Deposit">
<input type="hidden" name="item_number" value="DISTR<?php echo $booking_form_id; ?>">
<input type="hidden" name="credits" value="510">
<input type="hidden" name="userid" value="<?php echo $current_user->ID; ?>">
<input type="hidden" name="amount" value="<?php echo $deposit_amount; ?>">
<input type="hidden" name="cpp_header_image" value="http://www.thedistractionsband.co.uk/files/2015/08/LOGO-1.1-1024x304.png">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="handling" value="0">
<input type="hidden" name="cancel_return" value="<?php echo get_site_url()."/payment-cancel/"; ?>">
<input type="hidden" name="return" value="<?php echo get_site_url()."/my-bookings/"; ?>">
<input name="notify_url" value="<?php echo DISTRACTIONS_LOGIN_PLUGIN_URL ?>includes/payments/paypal-ipn.php" type="hidden">
<input type="submit" border="0" name="submit" value="Pay Now" alt="PayPal - The safer, easier way to pay online!">
<div class="cards"><i class="fa fa-cc-amex"></i> <i class="fa fa-cc-mastercard"></i> <i class="fa fa-cc-visa"></i> <i class="fa fa-credit-card"></i> <i class="fa fa-cc-paypal"></i></div>
</form>
<?php
}
/* Restore original Post Data */
wp_reset_postdata();
} else {
echo '<p>No bookings found</p>';
}
?>
This all works fine, and actually payments go through fine. I'm now trying to work on the paypal-ipn.php file which is where I want to create communications with paypal and add an entry to the post_meta database table when payment is successful.
So far I don't have much, but here is my paypal-ipn.php
<?php
global $wpdb;
update_post_meta(52, 'deposit_paid', 1);
?>
I'm hoping that this would update deposit_paid to '1' (for post_id 52 - which I will dynamically change to the booking that has been paid for....once I've got this working).
Currently it does nothing, Am I doing something wrong here?

You have a few options here - the easiest (as its just a php page) is to add the following:
// Make wordpress functions available - so we can write to the db etc
require_once realpath('/wp-load.php');
With the correct relative path to the wp-load.php file (its in the root of WordPress).
You need this to enable the functions and get update_post_meta(); to work etc...
When developing you should have:
define('WP_DEBUG', true);
Set up in your wp-config.php as this will alert you to problems.
Option #2 is to make a function and hook the into a WordPress hook when the user submits the form. This is more complex and I feel not what you are after.

Related

Paypal (Website Payments Standard )with core php not getting the Correct response on call back file

Following is my form that I am using to send information to paypal.
<?php
$paypalURL = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; //Test PayPal API URL
//$paypalID = 'mmarmy.buisness#gmail.com'; //Business Email mfarhan7333-facilitator#gmail.com
$paypalID = 'mfarhan7333-facilitator#gmail.com';
$successURL = 'http://dev.mmarmy.com/success_callback.php'; //Test PayPal API URL
$cancelURL = 'http://dev.mmarmy.com/fail_callback.php'; //Test PayPal API URL
//echo $_GET['ID'];
?>
<form action="<?php echo $paypalURL; ?>" id="dateForm" method="post">
<!-- Identify your business so that you can collect the payments. -->
<input type="hidden" name="business" value="<?php echo $paypalID; ?>">
<!-- Specify a Buy Now button. -->
<input type="hidden" name="cmd" value="_xclick">
<!-- Specify details about the item that buyers will purchase. -->
<input type="hidden" name="item_name" value="1000 credits">
<input type="hidden" name="item_number" value="3">
<input type="hidden" name="amount" value="10">
<input type="hidden" name="currency_code" value="USD">
<!-- Specify URLs -->
<input type='hidden' name='cancel_return' value='<?php echo $cancelURL; ?>'>
<input type='hidden' name='return' value='<?php echo $successURL; ?>'>
<!-- Display the payment button. -->
<input type="image" name="submit" border="0"
src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
</form>
<script type="text/javascript">
// document.getElementById('dateForm').submit(); // SUBMIT FORM
</script>
Below is my success call back url file code that is success_callback.php`
<?php
session_start();
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
?>
Only $_REQUEST array is returning data that is following
Array
(
[_ga] => GA1.2.1379896017.1501588349
[__utma] => 61791929.1379896017.1501588349.1505416341.1505461692.73
[__utmz] => 61791929.1501588349.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
[fbm_600425873678785] => base_domain=.mmarmy.com
[_gid] => GA1.2.1895192165.1505321082
[PHPSESSID] => a7ce9090dcf325e8db9f72acb8d57f65
[__utmb] => 61791929.20.10.1505461692
[__utmc] => 61791929
)
I am unable to get following information on success call back url. Can anybody help me if I am missing something.
echo $itemNo = $_REQUEST['item_number'];
echo $itemTransaction = $_REQUEST['tx']; // Paypal transaction ID
echo $itemPrice = $_REQUEST['amt']; // Paypal received amount
echo $itemCurrency = $_REQUEST['cc']; // Paypal received currency type

Generating paypal buttons for other users dynamically

I am planning on letting people list and sell their items on my site with payment/transaction processing done via paypal. To do this, i plan on letting my users dynamically generate a paypal button of their own when they are uploading their items(using my upload form).
I know that i can generate paypal buttons for my own account simply by logging into my paypal account. However, how would i go about doing this for other people ( if possible), whose accounts i will not have access to, and how would i make it secure?
Inputs would be appreciated!
P.S Im coding the site in php
If I understand the question correctly, you want to have your users customize a PayPal button to their own needs, right?
If so, what you should do is store the user's email address and the image button they want to use in a database (like MySQL) and then retrieve it and print the HTML.
Here's an example:
$query = mysql_query("SELECT * FROM `users`, `items` WHERE users.id = items.user_id") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
?>
<form name="_xclick" action="https://www.paypal.com/us/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="<?php echo $row['email']; ?>">
<input type="hidden" name="currency_code" value="<?php echo $row['currency']; ?>">
<input type="hidden" name="item_name" value="<?php echo $row['item_name']; ?>">
<input type="hidden" name="amount" value="<?php echo $row['price']; ?>">
<input type="image" src="<?php echo $row['button_image']; ?>" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
<?php } ?>
Here is some very brief documentation from PayPal on it.

Auto-Increment Form Input Names With $_SESSION

I want to use PHP $_SESSION to access user form-inputted data. The flow looks like this:
STEP 1
User fills out form.
<form method="post" action="review.php">
<input type="text" name="length">
<input type="text" name="width">
<input typ="text" name="quantity">
<input type="submit" value="submit">
</form>
STEP 2
Form data is submitted and 'review.php' converts from POST to SESSION. User reviews form details and adds item to cart.
STEP 3
Item is added to PayPal Shopping Cart. User decides he wants to add another item so user clicks on "continue shopping" and returns to STEP 1.
STEP 1
User fills out form details. Except this time, I want the input name to auto-increment.
For example:
<input type="text" name="length2">
<input type="text" name="width2">
<input type="text" name="quantity2">
And I want it to auto increment however many number of times the user adds new items to the cart.
<input type="text" name="length3">
<input type="text" name="length4">
<input type="text" name="etc">
Usual flow continues... STEP 2 ---> STEP 3
After successful payment in STEP 3, user will be redirected to a new page.
STEP 4(newPage)
All of the purchased items' details will be displayed on this page in order and with the same group. For example, (length, width, quantity) then (length2, width2, quantity2) and so on and so forth. They will each be displayed in different forms.
Those forms will also include <input type="file"> so that the user can upload a file for each form. Each form represents a purchased item.
Once the user uploads files to each corresponding form, they will be submitted to a database.
QUESTION
How can I make the input names auto increment preferably with PHP,
and of course whilst using $_SESSION? I would like the name to
return to its original name after the user's session. So let's say
that same user wanted to order the next day and he ordered 5 items,
the input names would span to 5 (length, length2,..., length5).
How can I display all of the user inputted data in each respective
form(see STEP 4)?
I know I have to do all of the back-end stuff on PHP($_SESSION, forms, etc) but if someone has a jQuery solution for the auto increment and showing all of the data in the end I'm all for it.
Here's What I Was Working With
Tried to use this to display the data with the incremented names but couldn't figure out a solution.
<?php
session_start();
?>
<form>
<?php
$size = $_SESSION['size'];
$variables = array("$size");
$i = 1;
foreach ($variables as $var ) {
$name = "txt".$i;
echo "<input type='text' name='".$name."' value='".$var."' />";
$i++;
}
?>
</form>
Some of you might bring up PayPal IPN. I created an IPN listener as well but it doesn't get called via sandbox. A user on here posted that his IPN listener didn't work on sandbox either but when he went live via paypal.com it did work. Could be the case with mine too, but I want to go ahead and start selling so I need a different approach. Even if my IPN listener does work via paypal.com, I can't risk going live with it without being 100% sure. So if I can use this method for now until I can confirm that my listener works I will do it.
As always I am very grateful for your guys' help.
I actually figured this out a while back. Here's the code I am using:
<?php
if(isset($_POST['submit'])){
$_SESSION['invoice'] = $_POST['invoice'];
$invoice_no = $_SESSION['invoice'];
$decal = array(
'length' => $_POST['os0'],
'width' => $_POST['os1'],
'color' => $_POST['os2'],
'quantity' => $_POST['os3'],
'price' => $_POST['price'],
'submit' => $_POST['submit']
);
$_SESSION['order'][] = $decal;
$i = 0;
}
if(isset($_SESSION['order'])){
foreach($_SESSION['order'] as $sav) {
$i++;
?>
<input type="hidden" name="on0_<?php echo $i; ?>" value="Length">
<div class="decalName">
<label>Custom Decal <?php echo $i; ?></label>
</div>
<div class="label-input">
<label>Length</label><input type="text" name="os0_<?php echo $i; ?>" id="length" size="2" class="num" value="<?php echo $sav['length']; ?>" readonly>
</div>
<input type="hidden" name="on1_<?php echo $i; ?>" value="Width">
<div class="label-input"><label>Width</label><input type="text" name="os1_<?php echo $i; ?>" id="width" size="2" class="num" value="<?php echo $sav['width']; ?>" readonly>
</div>
<input type="hidden" name="on2_<?php echo $i; ?>" value="Color">
<div class="label-input">
<label>Color</label><input type="text" name="os2_<?php echo $i; ?>" value="<?php echo $sav['color']; ?>" readonly>
</div>
<div class="label-input">
<label>Quantity</label><input type="text" name="os3_<?php echo $i; ?>" size="2" class="num" id="quantity" value="<?php echo $sav['quantity']; ?>" readonly>
</div>
<input type="hidden" name="on3_<?php echo $i; ?>" value="quantity">
<input type="hidden" class="num" value="0.20">
<div class="label-input">
<label>Price $:</label><input type="text" class="tot" name="price" value="<?php echo $sav['price']; ?>" readonly>
</div><!--end label-input-->
<?php
}
?>

WordPress Modification. 2 Search Widgets, searching different content

I modified the Searchform.php to:
'search_id' => 'id', 'form_action' => ( 'http://local.amleo.com/newps/pulldata.php'
For the first Search widget, to go to a custom PHP page that displays results for something else.
The next Search Widget, I'd like to search the category "AML Hot Topics". Not sure how I can do that. Any ideas??
So you can visualize: http://i.imgur.com/HSd9EEZ.png
The 1st Search is the one I modified the Searchform.php for. The 2nd is the one I'm not sure about.
I'm no super-duper PHP wizard by any means, but I can follow directions pretty decently.
You don't need two target for search, instead you can do it using something like this
One form for newps
<form method="get" id="searchform" action="<?php echo esc_url( home_url() ); ?>">
<input type="text" value="<?php echo esc_attr( get_search_query() ); ?>" name="s" id="s" />
<input type="hidden" value="newps" name="key" />
<input type="submit" id="searchsubmit" value="<?php esc_attr_e('Search','Aggregate'); ?>" />
</form>
Another for AML
<form method="get" id="searchform" action="<?php echo esc_url( home_url() ); ?>">
<input type="text" value="<?php echo esc_attr( get_search_query() ); ?>" name="s" id="s" />
<input type="hidden" value="aml" name="key" />
<input type="submit" id="searchsubmit" value="<?php esc_attr_e('Search','Aggregate'); ?>" />
</form>
Create a search.php file in your theme's root folder, something like this
get_header();
// if you get `key` then it will be your custom search
// otherwise, default search will be performed
if(!empty($_GET['key'])) {
$key = $_GET['key']; // it will be either 'newps' or 'aml'
$search = $_GET['s'];
// modify the query using your $key and $search param
query_posts(...);
}
if (have_posts()) :
while(have_posts()): the_post();
// the_title() and more...
endwhile;
endif;
// reset the query if modified
if(!empty($key)) wp_reset_query();
get_sidebar();
get_footer();

how to submit data to my database when the paypal buynow button is clicked from my cart

hi there Iam creating a Paypal IPN and i have a pay now button in my cart page which is of type image. Im used to using type submit. i was just wondering how i would write to my database on the submition of this pay now button. what i mean is i want to write to the database as this button is clicked but im not sure exactly how to do it? here is my code for sending my data to paypal.
<?php
if(isset($_SESSION['username']) && isset($_SESSION['itemnames']))
{
$checkoutbutton .='<form action="https://www.sandbox.paypal.com/cgibin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="xxxxxx">
<input type="hidden" name="upload" value="1">';
?> <ul2>
<?php
for ($i = 0;$i <count($_SESSION['itemnames']);$i++)
{ ?>
<li>
<?php
$x = $i + 1;
echo "<h3>";
echo $_SESSION['itemnames'][$i];
echo "</h3>";
echo "<b>Cost per Item: €" .$_SESSION['price'][$i] ."</br> Number of Items: " . $_SESSION['quantity'][$i] ."</b>";
$checkoutbutton .='<input type="hidden" name="item_name_'.$x.'" value="'. $_SESSION['itemnames'][$i] .'">
<input type="hidden" name="amount_'.$x.'" value="' .$_SESSION['price'][$i] . '">
<input type="hidden" name="quantity_'.$x.'" value="' .$_SESSION['quantity'][$i] . '">';
?>
</li>
<?php } $checkoutbutton .='<input type="hidden" name="notify_url" value="http://ipnscript.php">
<input type="hidden" name="return" value="http://ranscomplete.php">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="return to store">
<input type="hidden" name="cancel_return" value="http://www.theislandapp.com/transcancelled.php">
<input type="hidden" name="currency_code" value="EUR">
<input type="image" src="x-click-but5.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">';
?>
</ul2>
<?php }
else {?>
<h2> No Items in cart </h2>
<?php } ?> </br></br>
You'll need to add a step before heading to PayPal. Basically your cart would POST to another script on your site, which would log whatever info you need, and then you can loop through your cart and send things over to PayPal via a GET string on the URL. I believe the variables are the same, plus you can even get their name and address info to store on your side beforehand.
You could always use ajax(jquery) to call a page in the background, it doesn't even have to display any data.
$.post("save.php", { name: "John", item: "143" } );
http://api.jquery.com/jQuery.post/
By giving your buy button an id, and using click function would make this possible.
<input id="yebeclicked" type="image" src="x-click-but5.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
<script type="text/javascript">
$("#yebeclicked").click(function() {
$.post("save.php", { name: "John", item: "143" } );
});
</script>
Cheers. ;)

Categories