get_payment_method() && $order->is_paid() && '' !== (string) $order->get_meta( AgentaOS_Gateway::META_TRANSACTION_ID ); } /** * @param WC_Order $order Order. * @return string */ public static function url( WC_Order $order ) { return add_query_arg( array( self::QUERY_VAR => $order->get_id(), 'key' => $order->get_order_key(), ), home_url( '/' ) ); } /** * @param WC_Order $order Order being edited. */ public static function admin_link( $order ) { if ( $order instanceof WC_Order && self::available( $order ) ) { printf( '
', esc_url( self::url( $order ) ), esc_html__( 'AgentaOS invoice (PDF)', 'agentaos-payments' ) ); } } /** * @param WC_Order $order Order shown to the customer. */ public static function customer_link( $order ) { if ( $order instanceof WC_Order && self::available( $order ) ) { printf( '', esc_url( self::url( $order ) ), esc_html__( 'Download invoice (PDF)', 'agentaos-payments' ) ); } } /** * @param array $actions My Account order actions. * @param WC_Order $order Order. * @return array */ public static function order_action( $actions, $order ) { if ( $order instanceof WC_Order && self::available( $order ) ) { $actions[ self::QUERY_VAR ] = array( 'url' => self::url( $order ), 'name' => __( 'Invoice', 'agentaos-payments' ), ); } return $actions; } public static function download() { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- the order key is the access check, as on WooCommerce's order confirmation page. if ( empty( $_GET[ self::QUERY_VAR ] ) ) { return; } $order = wc_get_order( absint( $_GET[ self::QUERY_VAR ] ) ); $key = isset( $_GET['key'] ) ? sanitize_text_field( wp_unslash( $_GET['key'] ) ) : ''; // phpcs:enable if ( ! $order || ! hash_equals( $order->get_order_key(), $key ) || ! self::available( $order ) ) { wp_die( esc_html__( 'Invoice not found.', 'agentaos-payments' ), '', array( 'response' => 404 ) ); } $gateways = WC()->payment_gateways()->payment_gateways(); $gateway = $gateways['agentaos']; try { $pdf = $gateway->api()->invoice_receipt( self::invoice_id( $order, $gateway ) ); } catch ( Exception $e ) { $gateway->log( 'Invoice for order ' . $order->get_id() . ' not available: ' . $e->getMessage() ); wp_die( esc_html__( 'The invoice is not ready yet. Please try again in a few minutes.', 'agentaos-payments' ), '', array( 'response' => 503 ) ); } nocache_headers(); header( 'Content-Type: application/pdf' ); header( 'Content-Disposition: inline; filename="invoice-' . absint( $order->get_id() ) . '.pdf"' ); header( 'X-Robots-Tag: noindex' ); echo $pdf; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- PDF bytes from AgentaOS. exit; } /** * @param WC_Order $order Order. * @param AgentaOS_Gateway $gateway Gateway. * @return string The paid invoice of the order's checkout, remembered after the first lookup. * @throws Exception When AgentaOS has no paid invoice for the checkout yet. */ private static function invoice_id( WC_Order $order, AgentaOS_Gateway $gateway ) { $invoice_id = (string) $order->get_meta( self::META_INVOICE_ID ); if ( '' !== $invoice_id ) { return $invoice_id; } $checkout = $gateway->api()->get_checkout( (string) $order->get_meta( AgentaOS_Gateway::META_SESSION_ID ) ); foreach ( isset( $checkout['invoices'] ) && is_array( $checkout['invoices'] ) ? $checkout['invoices'] : array() as $invoice ) { if ( isset( $invoice['id'], $invoice['status'] ) && 'paid' === $invoice['status'] ) { $invoice_id = sanitize_text_field( (string) $invoice['id'] ); $order->update_meta_data( self::META_INVOICE_ID, $invoice_id ); $order->save(); return $invoice_id; } } throw new Exception( 'no paid invoice yet' ); } }