get_id(), '_partnercenter_invoice_token', true); if ($token_from_db !== $values['request-token']) { wp_die(__('Invalid token', 'partnercenter-api')); } // Get invoice from DB $invoice_from_db = wc_gzdp_get_order_last_invoice($order->get_id()); // Check if the right invoice id was requested if (intval($invoice_from_db->get_id()) !== intval($values['document-id'])) { wp_die(__('Mismatching invoice number', 'partnercenter-api')); } // Check if file exists if (! file_exists($invoice_from_db->get_pdf_path())) { wp_die(__('File not found', 'partnercenter-api')); } // Send headers to download file header($_SERVER['SERVER_PROTOCOL'].' 200 OK'); header('Cache-Control: public'); // needed for internet explorer header('Content-Type: application/pdf'); header('Content-Transfer-Encoding: Binary'); header('Content-Length:'.filesize($invoice_from_db->get_pdf_path())); header('Content-Disposition: attachment; filename= '.$invoice_from_db->get_filename()); readfile($invoice_from_db->get_pdf_path()); // Stop the rest of the process exit(); } /* * Load right template for custom virtual page. * * @since 1.1.0 */ public function change_template() { /** @var $wp_query WP_Query */ global $wp_query; if (! empty($wp_query->query_vars[self::$endpoint])) { // Set some data $wp_query->is_404 = false; $wp_query->is_feed = false; $wp_query->is_page = true; $wp_query->is_singular = true; $wp_query->is_home = false; $wp_query->is_archive = false; $wp_query->is_category = false; // Collect values to pass on $values = [ 'order-id' => sanitize_title($wp_query->query_vars['order-id']), 'document-id' => sanitize_title($wp_query->query_vars['document-id']), 'request-token' => sanitize_title($wp_query->query_vars['request-token']), ]; self::show_response($values); } } } new PARTNERCENTER_API_Custom_Document_Download_Endpoint_Class;