Simple php paypal ipn for wordpress

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:

Read More
<?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?

Related posts

1 comment

  1. 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.

Comments are closed.