Creating Hook
-
Hi there.
I apologize if this is a ridiculous question, but being that I am a very novice dev, especially with WP, I’m not sure what to do with “hooks”.
I have added the hook info into the functions.php of the theme, commenting out the IPN values I don’t care about.
I have changed the top of the code to the following:
add_action(‘pbs_ipn_setup’, ‘pbs_ipn’, 10, 1);
function pbs_ipn($posted) {Is this correct, and if so, how do I invoke it to get the values?
Thanks in advance
-
Looks like you’re missing the actual hook name. Which hook are you wanting to build on, or what exactly is your goal with IPN? Give me some of that detail and I’ll be happy to provide a sample of how you’d handle it.
Hi there. Basically, I have a single SKU that I will be selling (software) with a registration form that gets entered into my database after purchase, and then a serial number and download link are assigned to the customer. I was hoping to use IPN to give me the following PayPal fields:
IPN Variables
$payment_type
$payment_date
$payment_status
$payer_email
$payer_id
$receiver_email
$receiver_id
$mc_currency
$mc_fee
$mc_gross
$mc_gross1
$txn_type
$txn_id
$notify_version
$invoice
$verify_sign
$IPN_statusWhich could also be entered into my DB after the purchase has been verified. All the DB stuff for registration is functional, and my email to the customer. I am able to review the IPN info (from sandbox) in the plugin form section, but I’d like to be able to utilize the data in my custom DB so reporting is easy.
Thanks for your help. ??
Ok, for that you should probably just use the payment status hooks. If you want to run some code any time an IPN hits where the payment status is completed then you would use the follow hook within your functions.php file.
<?php add_action('paypal_ipn_for_wordpress_payment_status_completed', 'process_completed_payment_ipn', 10, 1); function process_completed_payment_ipn($posted) { $mc_gross = isset($posted['mc_gross']) ? $posted['mc_gross'] : ''; $protection_eligibility = isset($posted['protection_eligibility']) ? $posted['protection_eligibility'] : ''; $address_status = isset($posted['address_status']) ? $posted['address_status'] : ''; $item_number1 = isset($posted['item_number1']) ? $posted['item_number1'] : ''; $payer_id = isset($posted['payer_id']) ? $posted['payer_id'] : ''; $tax = isset($posted['tax']) ? $posted['tax'] : ''; $address_street = isset($posted['address_street']) ? $posted['address_street'] : ''; $payment_date = isset($posted['payment_date']) ? $posted['payment_date'] : ''; $payment_status = isset($posted['payment_status']) ? $posted['payment_status'] : ''; $charset = isset($posted['charset']) ? $posted['charset'] : ''; $address_zip = isset($posted['address_zip']) ? $posted['address_zip'] : ''; $mc_shipping = isset($posted['mc_shipping']) ? $posted['mc_shipping'] : ''; $mc_handling = isset($posted['mc_handling']) ? $posted['mc_handling'] : ''; $first_name = isset($posted['first_name']) ? $posted['first_name'] : ''; $mc_fee = isset($posted['mc_fee']) ? $posted['mc_fee'] : ''; $address_country_code = isset($posted['address_country_code']) ? $posted['address_country_code'] : ''; $address_name = isset($posted['address_name']) ? $posted['address_name'] : ''; $notify_version = isset($posted['notify_version']) ? $posted['notify_version'] : ''; $payer_status = isset($posted['payer_status']) ? $posted['payer_status'] : ''; $business = isset($posted['business']) ? $posted['business'] : ''; $address_country = isset($posted['address_country']) ? $posted['address_country'] : ''; $num_cart_items = isset($posted['num_cart_items']) ? $posted['num_cart_items'] : ''; $mc_handling1 = isset($posted['mc_handling1']) ? $posted['mc_handling1'] : ''; $address_city = isset($posted['address_city']) ? $posted['address_city'] : ''; $verify_sign = isset($posted['verify_sign']) ? $posted['verify_sign'] : ''; $payer_email = isset($posted['payer_email']) ? $posted['payer_email'] : ''; $mc_shipping1 = isset($posted['mc_shipping1']) ? $posted['mc_shipping1'] : ''; $tax1 = isset($posted['tax1']) ? $posted['tax1'] : ''; $txn_id = isset($posted['txn_id']) ? $posted['txn_id'] : ''; $payment_type = isset($posted['payment_type']) ? $posted['payment_type'] : ''; $last_name = isset($posted['last_name']) ? $posted['last_name'] : ''; $address_state = isset($posted['address_state']) ? $posted['address_state'] : ''; $item_name1 = isset($posted['item_name1']) ? $posted['item_name1'] : ''; $receiver_email = isset($posted['receiver_email']) ? $posted['receiver_email'] : ''; $payment_fee = isset($posted['payment_fee']) ? $posted['payment_fee'] : ''; $quantity1 = isset($posted['quantity1']) ? $posted['quantity1'] : ''; $receiver_id = isset($posted['receiver_id']) ? $posted['receiver_id'] : ''; $txn_type = isset($posted['txn_type']) ? $posted['txn_type'] : ''; $mc_gross_1 = isset($posted['mc_gross_1']) ? $posted['mc_gross_1'] : ''; $mc_currency = isset($posted['mc_currency']) ? $posted['mc_currency'] : ''; $residence_country = isset($posted['residence_country']) ? $posted['residence_country'] : ''; $test_ipn = isset($posted['test_ipn']) ? $posted['test_ipn'] : ''; $receipt_id = isset($posted['receipt_id']) ? $posted['receipt_id'] : ''; $payment_gross = isset($posted['payment_gross']) ? $posted['payment_gross'] : ''; $ipn_track_id = isset($posted['ipn_track_id']) ? $posted['ipn_track_id'] : ''; $IPN_status = isset($posted['IPN_status']) ? $posted['IPN_status'] : ''; $cart_items = isset($posted['cart_items']) ? $posted['cart_items'] : ''; /** Starting from here you can use the data variables above to run DB queries and update your database however you like. */ }
So that function will then run any time a PayPal IPN is sent with a payment status of completed.
Keep in mind that you may want to add some additional hooks to handle different payment status values. For example, if somebody pays with an e-check, the original IPN would come through with a status of pending. If you want to, you could update your database to show the pending status using the paypal_ipn_for_wordpress_payment_status_pending hook instead of paypal_ipn_for_wordpress_payment_status_completed.
So then you could utilize the two hooks together to update your system both when a pending transaction comes through and when the transaction changes to completed, or failed, in which case you would use the paypal_ipn_for_wordpress_payment_status_failed hook.
Does that make sense?
I see. It had not clicked that the actions were already set up. I thought you needed a custom name. That makes more sense. I found the list of all the actions on your site, too, so I will work with it.
Thanks so much for the input.
Cheers
Sorry again.
Now to set it up on the page, is this correct?
paypal_ipn_for_wordpress_payment_status_completed();
Then I should be able to access something like $txn_id?
In theory, I could
$_SESSION[“txn_id”]=$txn_id;
Then access the info on my next page?I think that whatever I’m doing, I’m doing it wrong, since I can’t echo the result. All your help is greatly appreciated.
IPN is something that happens behind-the-scenes. You won’t see anything that you just echo to the page, and the session would be completely different from any user session. It’s just server-to-server communication.
Anything you want to automate via IPN would need to be done within your hook function using the general PHP vars that are available for you. You can update your database, generate email notifications, hit 3rd party web services, etc. It’s not any actual page that anybody would see, though.
That makes sense. I was just testing the “echo” so I could see if I was getting any values. I’m just not sure how to call things (ie. the hook) to be able to access things such as
$mc_fee = isset($posted[‘mc_fee’]) ? $posted[‘mc_fee’] : ”;
$address_country_code = isset($posted[‘address_country_code’]) ? $posted[‘address_country_code’] : ”;
$address_name = isset($posted[‘address_name’]) ? $posted[‘address_name’] : ”;
$notify_version = isset($posted[‘notify_version’]) ? $posted[‘notify_version’] : ”;so I can add them to my MYSQL INSERT statements
Thanks in advance
You don’t “call” anything. The IPN is triggered instantly when the transaction takes place and it automatically pushes that data to your IPN listener.
Any hooks that you have setup at that time would be triggered/called automatically, and within those hooks/functions you would setup your SQL inserts utilizing the data that is prepared for you in the PHP vars already.
I think you’re thinking about this too much. The plugin does all the hard work for you. All you have to do is basically copy what I gave you above and then build your SQL statements under the comment I added explaining that this is where you would build them.
Hi again
To clarify, I have placed the following into the functions.php
add_action(‘paypal_ipn_for_wordpress_payment_status_completed’, ‘process_completed_payment_ipn’, 10, 1);
function process_completed_payment_ipn($posted)
{
$mc_gross = isset($posted[‘mc_gross’]) ? $posted[‘mc_gross’] : ”;
…
}etc
Then, when I successfully complete a transaction and the information ends up in the IPN plugin form. Then, variables such as “$mc_gross” should just be available globally, or only in a specific spot or page?
They would only be available within that function at the time it is triggered. So the payment would happen, the IPN would be triggered, and that would trigger the code in your function. Within that code you would use those variables to create your SQL queries so that when the IPN hits and your code runs the database is updated accordingly.
Get this figured out now?
Actually, I have not had a chance to try it. If I understand correctly, I should setup my SQL insert inside the action in the functions.php. Is this correct?
Yes, and you can use those PHP variables that are prepared for you inside that to build your queries with.
Trying it agin, I must still be missing something. I have added the following under the variables within the action (in functions.php)
$servername = “localhost”;
$username = “user”;
$password = “password”;
$dbname = “dbmame”;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die(“Connection failed: ” . mysqli_connect_error());
}
$updateipn = “INSERT INTO pbs_ipndata (txn_id, txn_status)
VALUES (‘$txn_id’,’$IPN_status’)”;mysqli_query($conn, $updateipn);
With actually connection info, obviously. Nothing seems to go into the database. Thoughts?
Thanks
When working with the WordPress database you should use the $wpdb class. Based on what you provided here this is what I would do.
<?php add_action('paypal_ipn_for_wordpress_payment_status_completed', 'process_completed_payment_ipn', 10, 1); function process_completed_payment_ipn($posted) { $mc_gross = isset($posted['mc_gross']) ? $posted['mc_gross'] : ''; $protection_eligibility = isset($posted['protection_eligibility']) ? $posted['protection_eligibility'] : ''; $address_status = isset($posted['address_status']) ? $posted['address_status'] : ''; $item_number1 = isset($posted['item_number1']) ? $posted['item_number1'] : ''; $payer_id = isset($posted['payer_id']) ? $posted['payer_id'] : ''; $tax = isset($posted['tax']) ? $posted['tax'] : ''; $address_street = isset($posted['address_street']) ? $posted['address_street'] : ''; $payment_date = isset($posted['payment_date']) ? $posted['payment_date'] : ''; $payment_status = isset($posted['payment_status']) ? $posted['payment_status'] : ''; $charset = isset($posted['charset']) ? $posted['charset'] : ''; $address_zip = isset($posted['address_zip']) ? $posted['address_zip'] : ''; $mc_shipping = isset($posted['mc_shipping']) ? $posted['mc_shipping'] : ''; $mc_handling = isset($posted['mc_handling']) ? $posted['mc_handling'] : ''; $first_name = isset($posted['first_name']) ? $posted['first_name'] : ''; $mc_fee = isset($posted['mc_fee']) ? $posted['mc_fee'] : ''; $address_country_code = isset($posted['address_country_code']) ? $posted['address_country_code'] : ''; $address_name = isset($posted['address_name']) ? $posted['address_name'] : ''; $notify_version = isset($posted['notify_version']) ? $posted['notify_version'] : ''; $payer_status = isset($posted['payer_status']) ? $posted['payer_status'] : ''; $business = isset($posted['business']) ? $posted['business'] : ''; $address_country = isset($posted['address_country']) ? $posted['address_country'] : ''; $num_cart_items = isset($posted['num_cart_items']) ? $posted['num_cart_items'] : ''; $mc_handling1 = isset($posted['mc_handling1']) ? $posted['mc_handling1'] : ''; $address_city = isset($posted['address_city']) ? $posted['address_city'] : ''; $verify_sign = isset($posted['verify_sign']) ? $posted['verify_sign'] : ''; $payer_email = isset($posted['payer_email']) ? $posted['payer_email'] : ''; $mc_shipping1 = isset($posted['mc_shipping1']) ? $posted['mc_shipping1'] : ''; $tax1 = isset($posted['tax1']) ? $posted['tax1'] : ''; $txn_id = isset($posted['txn_id']) ? $posted['txn_id'] : ''; $payment_type = isset($posted['payment_type']) ? $posted['payment_type'] : ''; $last_name = isset($posted['last_name']) ? $posted['last_name'] : ''; $address_state = isset($posted['address_state']) ? $posted['address_state'] : ''; $item_name1 = isset($posted['item_name1']) ? $posted['item_name1'] : ''; $receiver_email = isset($posted['receiver_email']) ? $posted['receiver_email'] : ''; $payment_fee = isset($posted['payment_fee']) ? $posted['payment_fee'] : ''; $quantity1 = isset($posted['quantity1']) ? $posted['quantity1'] : ''; $receiver_id = isset($posted['receiver_id']) ? $posted['receiver_id'] : ''; $txn_type = isset($posted['txn_type']) ? $posted['txn_type'] : ''; $mc_gross_1 = isset($posted['mc_gross_1']) ? $posted['mc_gross_1'] : ''; $mc_currency = isset($posted['mc_currency']) ? $posted['mc_currency'] : ''; $residence_country = isset($posted['residence_country']) ? $posted['residence_country'] : ''; $test_ipn = isset($posted['test_ipn']) ? $posted['test_ipn'] : ''; $receipt_id = isset($posted['receipt_id']) ? $posted['receipt_id'] : ''; $payment_gross = isset($posted['payment_gross']) ? $posted['payment_gross'] : ''; $ipn_track_id = isset($posted['ipn_track_id']) ? $posted['ipn_track_id'] : ''; $IPN_status = isset($posted['IPN_status']) ? $posted['IPN_status'] : ''; $cart_items = isset($posted['cart_items']) ? $posted['cart_items'] : ''; /** Starting from here you can use the data variables above to run DB queries and update your database however you like. */ global $wpdb; $data = array( 'txn_id' => $txn_id, 'txn_status' => $payment_status, ); $wpdb->insert('pbs_ipndata',$data); }
Then you just need to make sure the IPN is getting triggered as expected and that the payment status of the transaction you’re testing with is indeed completed.
- The topic ‘Creating Hook’ is closed to new replies.