Paypal IPN hook not working - php

I have created a plugin to send email to user after their donation using paypal. But problem is that user not receiving any email. Please kindly check my code below. I am also using plugin called "PayPal IPN for WordPress" to get information of donation.
<?php
/*
Plugin Name: PayPal IPN Send Email
Plugin URI: http://www.trustedwebsolution.com/
Description: Hook tester for PayPal IPN for WordPress plugin.
Author: Jhishu Singha
Version: 1.0.0
*/
add_action('paypal_ipn_for_wordpress_payment_status_processed', 'paypal_ipn_for_wordpress_payment_email', 10, 1);
function paypal_ipn_for_wordpress_payment_email($posted) {
// Parse data from IPN $posted array
$dir = plugin_dir_url( __FILE__ );
$item_name = isset($posted['item_name']) ? $posted['item_name'] : '';
$payment_gross = isset($posted['payment_gross']) ? $posted['payment_gross'] : '';
$first_name = isset($posted['first_name']) ? $posted['first_name'] : '';
$last_name = isset($posted['last_name']) ? $posted['last_name'] : '';
$payer_email = isset($posted['payer_email']) ? $posted['payer_email'] : '';
$subscr_date = isset($posted['subscr_date']) ? $posted['subscr_date'] : '';
/**
* At this point you can use the data to generate email notifications,
* update your local database, hit 3rd party web services, or anything
* else you might want to automate based on this type of IPN.
*/
if($item_name == 'ATF Donation') {
$to = $payer_email;
$subject = 'Email from Adaptive Training Foundation';
$message = 'Thanks for donation';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $message, $headers );
} else {
// No email
}
}
?>
I am trying to solve it for few days. But no luck. Thanks in advance for help.

I think you should use a different hook. Instead of paypal_ipn_for_wordpress_payment_status_processed use paypal_ipn_for_wordpress_payment_status_completed and see if that helps.
Also, for testing purposes, I would add an email to yourself in the else section of your statement so that you'll get something if the code falls there and you won't think the IPN simply never ran.

Related

WordPress: I need to send users a notification when their role is changed, my code snippet doesn't work

I'm building a members style site using WordPress. When a user registers their default role is "subscriber" once we manually approve their account we change the user role to "private_event_member", we need to send the user an email to tell them we have changed their role. I found the following code snippet and added it to the functions.php file
function user_role_update( $user_id, $new_role ) {
$site_url = get_bloginfo('wpurl');
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Role changed: ".$site_url."";
$message = "Hello " .$user_info->display_name . " your role has changed on ".$site_url.", congratulations you are now an " . $new_role;
wp_mail($to, $subject, $message);
}
add_action( 'set_user_role', 'user_role_update', 10, 2);
This failed to send the email as expected. So to be sure I decided to install a plugin called WP Mail Log and then also WP Mail SMTP, I configured the Sendblue SMTP option. I've tested this and all other emails like for example user registration notifications and new orders are being sent and recorded successfully in the logs, these are being received. The above mentioned code however seems to do nothing.
This seems to be a widely used piece of code that should work so can anyone explain to me why this snippet behaves differently from other mail on the server? It doesn't even appear in the sending logs so as far as I can see it's not doing anything at all. Has the set_user_role action I'm hooking into changed? What could be the cause?
Any help much appreciate!
You should use profile_update hook for this. Read more here - https://developer.wordpress.org/reference/hooks/profile_update/
function notify_user_on_role_change($user_id,$old_user_data,$userdata) {
// Getting role before update
foreach($old_user_data->roles as $role):
$old_role = $role;
endforeach;
// error_log(print_r($userdata,true)); // debug
//If we change role send email
if($old_role != $userdata['role']):
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Profile Updated";
$message = "Hello, your user role is changed to ".$userdata['role']."";
wp_mail( $to, $subject, $message);
endif;
}
add_action('profile_update','notify_user_on_role_change',10,3);
Send notification only if you change to specific user role
function notify_user_on_role_change($user_id,$old_user_data,$userdata) {
// Getting role before update
foreach($old_user_data->roles as $role):
$old_role = $role;
endforeach;
// error_log(print_r($old_role,true)); // debug
//If we change role send email
if($old_role != 'private_event_member' && $userdata['role'] == 'private_event_member'):
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Profile Updated";
$message = "Hello, your user role is changed to ".$userdata['role']."";
wp_mail( $to, $subject, $message);
endif;
}
add_action('profile_update','notify_user_on_role_change',10,3);

Wordpress is executing URL in code when called via wp_mail()

I have written a custom plugin (that creates a custom post type) and allows any user to submit a new post from a form on my website. To prevent bots, I have setup an e-mail confirmation code which they must click, where this changes the post status from Draft to Published.
Unfortunately the wp_mail() code shown below seems to be executing this confirmation URL automatically. As soon as the post is submitted, it is set to Draft until it reaches this code, and then it automatically publishes.
Removing this block however makes everything work as expected. Does anyone have any idea as to the reason and how to fix it?
$confirm_url = site_url(). '/verification?id=' . $post_id . '&hash=' . $hash;
// Send a verification e-mail to the user to confirm publication
$subject = 'Please confirm your Slicer Profile submission';
$body = $confirm_url;
wp_mail( $profile_email, $subject, $body );
This has been resolved, wanted to share the solution for anyone else that may stumble into this themselves. The site_url() was stored in its own variable and the forward slash in the URL string was not properly escaped, which seemed to have been causing the issue.
This has now been updated to the following and works perfect.
$site_url = site_url();
$confirm_url = $site_url. '\/verification?id=' . $post_id . '&hash=' . $hash;
// Send a verification e-mail to the user to confirm publication
$subject = 'Please confirm your Slicer Profile submission';
$body = $confirm_url;
wp_mail( $profile_email, $subject, $body );

How to hook inside a function in wordpress

I got a plugin (kind of sign up form), that offers developers some actions/hooks to add their own stuff. Inside the plugin the function is called like this:
// Allow devs to hook in
do_action( 'after_record_action', $result, $data, $format );
I guess that $data is an array storing the form data. After a visitor uses the signup form I want to send a mail containing $data using wp_mail()
How can I execute the following script using after_record_action? Do I need to add this inside my functions.php?
// get data from $data[] array
$data['email'] = $email;
$data['key'] = $key;
// use $data to create a personalized mail
$to = $email;
$subject = "Wordpress Test";
$content = "Hi, this us your key:" . $key . "Enjoy using it!";
// send mail using wp_mail
$status = wp_mail($to, $subject, $content);
I appreciate any help in combining these, as I am not too experienced using php.
Add following code into your currently active theme's functions.php file:
add_action('after_record_action', 'marian_rick_custom_action', 10, 3);
function marian_rick_custom_action ($result, $data, $format){
// get data from $data[] array
$email = $data['email'];
$key = $data['key'];
// use $data to create a personalized mail
$to = $email;
$subject = "Wordpress Test";
$content = "Hi, this us your key:" . $key . "Enjoy using it!";
// send mail using wp_mail
$status = wp_mail($to, $subject, $content);
}
if you are interested, how all this really works, check the official docs here

woocommerce_created_customer action not working at all

I am writing plugin which sends coupon code for every X spent at shop and this feature works pretty well but I have troubles with sending coupon to a new registered user.
Sending mail function (XYZ here on purpose):
function sendFirstVoucher($to_email, $discount, $name, $coupon){
$headers = 'From: XYZ' . "";
$subject = 'Your ' . $discount . '% discount voucher for XYZ';
$message = 'Hi . ' . $name . 'and thank you for registering with XYZ! \r\n\r\n' .
"blah blah blah " . $coupon . " when you check out on our website, you can apply the code in either the cart page or check out page. \r\n\r\n" .
"From now on when you make purchases you will also receive additional discount codes for every $100 spent. More information can be found at our website (link to membership program page). \r\n" .
"If you have any questions please feel free to contact us at XYZ \r\n\r\n" .
"Kind Regards, \r\nThe XYZ";
wp_mail($to_email, $subject, $message, $headers);
}
And in the main plugin file:
function send_coupon_to_freshly_registered_user($customer_id, $new_customer_data, $password_generated) {
$coupon_code = generateCoupon(5);
$user_email = $new_customer_data["user_email"];
$user_login = $new_customer_data["user_login"];
sendFirstVoucher($user_email, "5", $user_login, $coupon_code);
}
finally at the nearly end of file there is:
add_action('woocommerce_created_customer', 'send_coupon_to_freshly_registered_user',10,3);
But once I am creating user via dashboard or register myself via wp-login page (because site is in maintenace mode) it's not sending any email nor creating coupon (it works perfectly for similar function for X voucher per 100$)
Have anyone got idea how to acomplish this?
Kind regards,
Tom
Ok, I have achieved this by using core wp functions:
add_action('user_register', 'send_coupon_to_freshly_registered_user',10,1);
function send_coupon_to_freshly_registered_user($user_id) {
$user = get_user_by('id',$user_id); //new line
$user_login = stripslashes($user->user_login); //changed line
$user_email = stripslashes($user->user_email); //changed line
$coupon_code = generateCoupon(5);
sendFirstVoucher($user_email, "5", $user_login, $coupon_code);
}
I hope it will help someone. Maybe it's not a real answer but the one that works for me.
Regards,
Tom

How to pass data from textboxes to PayPal using CodeIgniter

I am trying to make a PayPal transaction in my page. I have the following fields defined:
firstname
lastname
address
email
amount
When I click to proceed to payment it redirects my browser to PayPal correctly. Afterwards I am able to log in using the test account and make a test payment. The problem is that when I call print_r to display the return_url to check the data that has been passed, I noticed that my input textbox wasn't there.
Here is the code that posts to PayPal:
$firstname = $this->input->post('firstname');
$lastname = $this->input->post('lastname');
$address = $this->input->post('address');
$email = $this->input->post('email');
$config['business'] = 'test#test.com';
$config['return'] = 'http://localhost/test/payment/return_details/';
$config['cancel_return'] = 'http://localhost/test/payment/create-job-listing/';
$config['production'] = FALSE; //Its false by default and will use sandbox
$config["first_name"] = 'asdasd';
$config["last_name"] = 'asasas';
$config['address1'] = 'new york';
$this->load->library('paypal',$config);
$this->paypal->add('Amount',0.1);
$this->paypal->pay(); //Proccess the payment
Can someone help me figure this out?
In your return_url put the code:
<?php
echo "<pre>";
$data = file_get_contents('php://input'); //this fetches the raw input stream
$info = explode('&',$data);
echo "<pre>";
print_r($info);
?>
//Information sent by paypal is in the input stream and this code fetches the input!!

Categories