通过PayPal付款后如何发送确认电子邮件?

时间:2016-02-28 作者:bishal neupane

我正在开发博客网站,我使用了自定义贝宝按钮,但现在我想在通过贝宝付款后发送确认电子邮件。

我搜索了IPN的API,但不知道如何与WordPress集成。那么,你能帮我做这个或任何快速解决方案吗?

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top"><input name="cmd" type="hidden" value="_s-xclick" />

enter image description here

1 个回复
最合适的回答,由SO网友:jgraup 整理而成

要发送电子邮件,请签出wp_mail(); 例如:

$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 );
至于何时进行确认,最好在表单上自定义返回url-请参阅setting the return url on individual transactions. 如果可以重定向到自定义页面,则更容易检测。

您仍然需要计算出您可以在url中传递多少关于事务的信息,以便在url返回后为您提供足够的信息。通过使用transient API 检查成功的关键page template.

这是一个用于测试的页面模板。您可以创建一个页面并将其设置为此页面模板,以便测试部件的工作方式:

<?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(); ?>