To send an email, check out wp_mail()
; For example:
$to = '[email protected]';
$subject="The subject";
$body = 'The email body content';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
As for when a confirmation is made, it might be best to customize the return url on the form – see setting the return url on individual transactions. If you can redirect to a custom page, then that is much easier to detect.
You still have to work out how much information about the transaction can you pass in the url that will give you enough information once the url returns. It might be easily accomplished by setting information using the transient API and checking for that key in the success page template.
Here is a page template for testing. You can make a page and set it to this page template in order to test how parts might work:
<?php
/**
* Template Name: PayPal Template Page
*
* @package WordPress
* @subpackage Twenty_Sixteen
* @since Twenty Sixteen 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while( have_posts() ) : the_post();
// check the page action and define nonce ahead of time for validation or transient setting
$action = isset( $_REQUEST[ 'action' ] ) ? $_REQUEST[ 'action' ] : '';
$nonce_action = 'paypal_buynow';
// for testing, let's just send the request back to this page
$submit_url = esc_url( get_the_permalink() );
// if no action is detected, then just display a normal page
if ( empty( $action ) ) {
// redirect back to this page and set the action --- this might be the only real-world link
// for now it's ignored
$redirect_url = esc_url( get_the_permalink() . '?action=success&nonce=" . $nonce );
// create nonce to validate this request
$nonce = wp_create_nonce( $nonce_action );
// store user information we"ll need on success
// should also include transation details...
// or a checkout number we could get later
$current_user = wp_get_current_user();
$user_data = array (
'email' => $current_user->user_email,
'display_name' => $current_user->display_name,
'id' => $current_user->ID,
);
$data = json_encode( $user_data );
// encode into the transient cache -- which won't be around forever
// we'll get it again once we get back to this page
set_transient( $nonce_action . $nonce, $data, 12 * HOUR_IN_SECONDS );
?><h1>Welcome <?php echo $user_data[ 'display_name' ]; ?>!</h1><h2>Buy Stuff Now!</h2>
<form action="<?php echo $submit_url; ?>" method="post" target="_top">
<input name="cmd" type="hidden" value="_s-xclick"/>
<INPUT type="hidden" name="return" value="<?php echo $redirect_url; ?>">
<INPUT type="hidden" name="action" value="success">
<INPUT type="hidden" name="nonce" value="<?php echo $nonce; ?>">
<input type="image" name="submit"
src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif"
alt="PayPal - The safer, easier way to pay online">
</form>
<?php
}
// we do detect an action in the url so let's see if we can turn that into something...
else if ( $action === 'success' ) {
// get the nonce ready and any vars -- expect to fail but hope for success
$nonce = isset( $_REQUEST[ 'nonce' ] ) ? $_REQUEST[ 'nonce' ] : '';
$data = null;
$trans = false;
$transient_key = $nonce_action . $nonce;
// validate the nonce
$is_valid = wp_verify_nonce( $nonce, $nonce_action );
if ( $is_valid ) {
// use the none key for transient info --- this could be store on post meta as well
$trans = get_transient( $transient_key );
if ( $trans ) {
// decode the info -- this was transaction info we recorded earlier
$user_data = json_decode( $trans, true );
}
}
// didn't work... display error
if ( empty( $user_data ) ) {
?><h1>FAILED :(</h1><?php
}
else {
// debug success!!
echo "<pre>";
print_r( $user_data );
echo "</pre>";
?><h1>SUCCESS!!!</h1><?php
// convert the user information into an HTML email
$to = $user_data[ 'email' ];
$subject="You are a success!!!";
ob_start();
?><html>
<head>
</head>
<body>
<h1>Congrats <?php echo $user_data[ 'display_name' ]; ?>!</h1>
<h2>You Just purchased everything we have in stock!</h2>
<p>Trans#:<?php echo $nonce; ?></p>
</body>
</html><?php
$body = ob_get_clean();
$headers = array ( 'Content-Type: text/html; charset=UTF-8' );
// send it off!
$emailed = wp_mail( $to, $subject, $body, $headers );
// display the responce
echo "<h3>Email send to:</h3><pre>";
print_r( array ( $emailed ? 'SUCCESS' : 'FAILED', $emailed, $to, $subject, $body, $headers ) );
echo "</pre>";
// destroy the information to process this request... cause we're done!
// you could also let it timeout... whatever
delete_transient($transient_key);
}
// debug --- just showing data passed in post
echo "<pre>";
print_r( $_POST );
echo "</pre>";
// simple button to clear the current transation
?>
<form action="<?php echo $submit_url; ?>" method="post" target="_top">
<button name="submit">Return</button>
</form>
<?php
}
// End of the loop.
endwhile;
?>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>