• Hi All,

    I am creating a template file for the thank you page using do_shortcode and [accept_stripe_payment_checkout]. This all works fine for the standard output, however I need to retrieve the transaction ID as I need it to add some more data about the user to the database.

    Is it possible to add the ‘transaction_id’ to the shortcode as a parameter instead of using {transaction_id} in the body text? Alternatively is there a way to retrieve an array of the data outputted from your shortcode method?

    Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • That’s not the best place to grab the payment information as its built just to display the checkout result to the user.
    The right place to hook and get checkout data is
    do_action( 'asp_stripe_payment_completed', $data, $data['charge'] );

    If you hook in this location and dump $data, you should find all the information you need.

    Thread Starter biffa

    (@biffa)

    Hi, thanks for the quick response.

    I’ve added this to the funtions.php file:

    add_action('asp_stripe_payment_completed', 'asp_after_txn_callback', 10 ,2);
    function asp_after_txn_callback ($post_data, $charge)
    {
        //Do stuff
        //$charge is the Stripe charge object.
        //print_r($post_data);//Lets see what info is in this array.
    }

    And added the code you suggested above to my template file. However I’m getting several errors:
    Notice: Undefined variable: data
    Notice: Trying to access array offset on value of type null

    add_action('asp_stripe_payment_completed', 'asp_after_txn_callback');
    function asp_after_txn_callback ($data)
    {
       echo "<pre>";
    var_dump($data);
    echo "</pre>";
    }
    • This reply was modified 3 years, 7 months ago by Ovidiu.
    Thread Starter biffa

    (@biffa)

    ok that makes sense. However, var_dump($data[‘charge’]) works but var_dump($data[‘txn_id’]) returns NULL

    Where exactly are you checking? If its front-end, its possible it doesn’t pause on your variables. Use a PHP IDE to debug the code.
    The ID you’re looking for should be returned within $data[‘charge’][‘id’] and within $data[‘txn_id’]

    Thread Starter biffa

    (@biffa)

    ID should be returned but isn’t. This is the code I have.

    In functions/fields.php :

    add_action('asp_stripe_payment_completed', 'asp_after_txn_callback');
    function asp_after_txn_callback ($data)
    { 
    var_dump($data['charge']['id']); 
    }

    In my template file stripe_success.php
    do_action( ‘asp_stripe_payment_completed’, $data, $data[‘charge’] );

    This is the errors that are returned:

    Notice: Undefined variable: data in /home/hp3-linc1-nfs2-z/400/506400/user/htdocs/wp-content/themes/champs/stripe_success.php on line 22
    
    Notice: Undefined variable: data in /home/hp3-linc1-nfs2-z/400/506400/user/htdocs/wp-content/themes/champs/stripe_success.php on line 22
    
    Notice: Trying to access array offset on value of type null in /home/hp3-linc1-nfs2-z/400/506400/user/htdocs/wp-content/themes/champs/stripe_success.php on line 22
    
    Notice: Trying to access array offset on value of type null in /home/hp3-linc1-nfs2-z/400/506400/user/htdocs/wp-content/themes/champs/functions/fields.php on line 1431
    
    Notice: Trying to access array offset on value of type null in /home/hp3-linc1-nfs2-z/400/506400/user/htdocs/wp-content/themes/champs/functions/fields.php on line 1431
    NULL

    If I remove [‘id’] it works and returns the full array from Stripe. But I need the transaction from that array.

    Thread Starter biffa

    (@biffa)

    Basically what I’m trying to do is check if the payment went through and returned as true. That way I can update the database with the details of the user so I know they have paid. I was hoping to do this by adding the transaction ID to the user meta. If this is not the best way to do this, please let me know a better way.

    Thanks.

    The do_action call is an existing hook inside the plugin right after payment is confirmed. That’s where you’re hooking using your own add_action call to grab the payment data. You should read about WordPress actions and filters and how to use them to get/modify the data.

    Thread Starter biffa

    (@biffa)

    I’m not having a problem getting the $data array, the problem is getting the transaction ID from that array.

    There’s no issue getting the transaction ID, the code is fine except the do_action call which shouldn’t be included anywhere in your code as that one should be and is included within our plugin.

    Put this into stripe-success.php or or theme’s functions.php and you should see the id (exit line stops the script execution so you can see the output since you’re not using a PHP debugger).

    add_action('asp_stripe_payment_completed', 'asp_after_txn_callback');
    function asp_after_txn_callback ($data) {
    var_dump($data['txn_id']);
    exit();
    }
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Using short code in template file’ is closed to new replies.