How does this function get the id of the most recent post?

prepare_payment() is calling wp_get_recent_posts() which returns an array of post by default. Only one post is being returned in this case, due to the $numberposts parameter being set to 1.

$recent_posts holds the array of posts returned by wp_get_recent_posts(). The following line is setting $thePostID to the ID of the first post in the $recent_posts array (arrays are zero indexed):

$thePostID = $recent_posts[0]['ID'];

Finally, the ID is echo’d using the line echo $thePostID;

Edit: Here is an example of the array returned by wp_get_recent_posts(), which itself is a wrapper for get_posts().`

Array
(
    [0] => Array
        (
            [ID] => 418
            [post_author] => 2
            [post_date] => 2025-01-01 00:00:00
            [post_date_gmt] => 2025-01-01 00:00:00
            [post_content] => This post is scheduled to be published in the future.

It should not be displayed by the theme.
            [post_title] => Scheduled
            [post_excerpt] => 
            [post_status] => future
            [comment_status] => open
            [ping_status] => closed
            [post_password] => 
            [post_name] => scheduled
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2016-04-11 04:28:22
            [post_modified_gmt] => 2016-04-11 04:28:22
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://wpthemetestdata.wordpress.com/?p=418
            [menu_order] => 0
            [post_type] => post
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

)

You can replicate this by adding a simple debugging statement to prepare_payment():

function prepare_payment() {
  $recent_posts = wp_get_recent_posts( array( 'numberposts' => '1' ) );

    // Temporary debugging statement
    print_r( $recent_posts );

  $thePostID = $recent_posts[0]['ID'];
  echo $thePostID;
}

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)