has_status( $blocked ) && ( $order->has_status( array( 'processing', 'completed' ) ) || null !== $order->get_date_paid() ); // Cash on delivery is only collected once the order is completed. if ( $paid && 'cod' === $order->get_payment_method() && ! $order->has_status( 'completed' ) ) { $paid = false; } if ( $paid && ! self::payment_confirmed( $order ) ) { $paid = false; } return (bool) apply_filters( 'vprwc_order_is_paid', $paid, $order ); } /** * Payment methods where an admin marking the order paid is trusted * (no gateway confirmation required). Default: bacs, cheque, cod. * * @return string[] */ public static function trusted_manual_methods() { $methods = array( 'bacs', 'cheque', 'cod' ); /** * Trusted manual payment methods. * * @param string[] $methods Method ids. */ $methods = apply_filters( 'vprwc_trusted_manual_payment_methods', $methods ); return array_values( array_filter( array_map( 'strval', (array) $methods ) ) ); } /** * Whether the payment gateway (or a trusted manual method) confirmed payment. * A manual status change alone is not enough for Cray and similar gateways. * * @param WC_Order $order Order. * @return bool */ public static function payment_confirmed( $order ) { if ( ! $order instanceof WC_Order || $order instanceof WC_Order_Refund ) { return false; } $method = (string) $order->get_payment_method(); if ( in_array( $method, self::trusted_manual_methods(), true ) ) { return true; } if ( 'cray_payments' === $method ) { if ( 'failed' === strtolower( (string) $order->get_meta( '_cray_payment_status', true ) ) ) { return false; } $verified = (string) $order->get_meta( '_cray_payment_verified', true ); if ( '1' === $verified || 'yes' === strtolower( $verified ) ) { return true; } $txn = trim( (string) $order->get_meta( '_cray_transaction_reference', true ) ); $merchant = trim( (string) $order->get_meta( '_cray_reference', true ) ); return '' !== $txn && $txn !== $merchant; } // Other gateways: a recorded payment date or transaction id plus a paid status. return ( null !== $order->get_date_paid() || '' !== (string) $order->get_transaction_id() ); } /** * Format money as plain text. * * @param float $amount Amount. * @param string $currency Currency. * @return string */ public static function money( $amount, $currency ) { return html_entity_decode( wp_strip_all_tags( wc_price( (float) $amount, array( 'currency' => $currency ) ) ), ENT_QUOTES, 'UTF-8' ); } /** * Split WooCommerce's formatted address HTML into plain lines. * * @param string $html Address HTML. * @return string[] */ public static function address_lines( $html ) { $parts = preg_split( '//i', (string) $html ); $lines = array(); foreach ( $parts as $p ) { $p = trim( html_entity_decode( wp_strip_all_tags( $p ), ENT_QUOTES, 'UTF-8' ) ); if ( '' !== $p ) { $lines[] = $p; } } return $lines; } /** * Split a plain-text, plugin-owned address into lines. * * @param string $text Address text. * @return string[] */ public static function text_address_lines( $text ) { $lines = array(); foreach ( preg_split( '/\r\n|\r|\n/', (string) $text ) as $p ) { $p = trim( $p ); if ( '' !== $p ) { $lines[] = $p; } } return $lines; } /** * Date and time in the site timezone. * * @param int $timestamp Unix time. * @return string */ public static function datetime( $timestamp ) { if ( ! $timestamp ) { return ''; } return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) . ' T', (int) $timestamp ); } /** * WooCommerce customer number, or "Guest" for guest orders. * * @param WC_Order $order Order. * @return string */ public static function customer_reference( $order ) { $cid = (int) $order->get_customer_id(); $base = $cid > 0 ? (string) $cid : __( 'Guest', 'acrislip-order-receipts' ); $cray = $order ? trim( (string) $order->get_meta( '_cray_reference', true ) ) : ''; if ( '' === $cray ) { return $base; } return sprintf( /* translators: 1: store customer number or Guest, 2: merchant reference sent to Cray at initiate */ __( '%1$s ยท Cray ref %2$s', 'acrislip-order-receipts' ), $base, $cray ); } /** * Transaction and customer references for documents, emails and the Payment box. * * @param WC_Order $order Order. * @param array $pay Optional payment data from VPRWC_Payment_Data::get(). * @return array{customer_reference:string,payment_customer_reference:string,transaction_reference:string} */ public static function references( $order, array $pay = array() ) { if ( ! $pay ) { $pay = VPRWC_Payment_Data::get( $order ); } return array( 'customer_reference' => self::customer_reference( $order ), 'payment_customer_reference' => (string) $pay['customer_reference'], 'transaction_reference' => VPRWC_Overview::transaction_reference( $order, $pay ), ); } /** * Logo as a data URI (local files only, so no remote fetching is needed). * * @return string */ public static function logo_data_uri() { $url = (string) VPRWC_Settings::get( 'vprwc_logo' ); $id = $url ? attachment_url_to_postid( $url ) : (int) get_theme_mod( 'custom_logo' ); $file = $id ? get_attached_file( $id ) : ''; if ( ! $file && $url ) { $uploads = wp_get_upload_dir(); if ( 0 === strpos( $url, $uploads['baseurl'] ) ) { $file = $uploads['basedir'] . substr( $url, strlen( $uploads['baseurl'] ) ); } } $file = apply_filters( 'vprwc_logo_file', $file ); if ( ! $file || ! is_readable( $file ) || filesize( $file ) > 2 * MB_IN_BYTES ) { return ''; } $type = wp_check_filetype( $file ); if ( ! in_array( $type['type'], array( 'image/png', 'image/jpeg', 'image/gif' ), true ) ) { return ''; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $bytes = file_get_contents( $file ); return false === $bytes ? '' : 'data:' . $type['type'] . ';base64,' . base64_encode( $bytes ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode } /** * Shop details from WooCommerce settings. * * @return array */ public static function shop() { $c = WC()->countries; $address = $c->get_formatted_address( array( 'address_1' => $c->get_base_address(), 'address_2' => $c->get_base_address_2(), 'city' => $c->get_base_city(), 'state' => $c->get_base_state(), 'postcode' => $c->get_base_postcode(), 'country' => $c->get_base_country(), ) ); $name = get_option( 'woocommerce_email_from_name' ); $shop = $name ? wp_specialchars_decode( (string) $name, ENT_QUOTES ) : wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); $legal = trim( (string) VPRWC_Settings::get( 'vprwc_legal_name' ) ); $custom = trim( (string) VPRWC_Settings::get( 'vprwc_business_address' ) ); $email = sanitize_email( (string) VPRWC_Settings::get( 'vprwc_business_email' ) ); if ( ! is_email( $email ) ) { $email = (string) get_option( 'woocommerce_email_from_address' ); } return apply_filters( 'vprwc_shop_details', array( 'name' => $shop, 'legal_name' => '' !== $legal ? wp_specialchars_decode( $legal, ENT_QUOTES ) : $shop, 'tax_number' => trim( (string) VPRWC_Settings::get( 'vprwc_tax_number' ) ), 'address_lines' => '' !== $custom ? self::text_address_lines( $custom ) : self::address_lines( $address ), 'email' => $email, 'phone' => trim( (string) VPRWC_Settings::get( 'vprwc_business_phone' ) ), 'website' => home_url( '/' ), ) ); } /** * Build the receipt or invoice data array. * * @param WC_Order $order Order. * @param string $variant customer|compliance. * @param string $document receipt|invoice. * @return array */ public static function build( $order, $variant = self::VARIANT_CUSTOMER, $document = self::DOCUMENT_RECEIPT ) { $document = self::DOCUMENT_INVOICE === $document ? self::DOCUMENT_INVOICE : self::DOCUMENT_RECEIPT; $invoice = self::DOCUMENT_INVOICE === $document; $compliance = self::VARIANT_COMPLIANCE === $variant; if ( $invoice || ! apply_filters( 'vprwc_allow_compliance_copy', false, $order ) ) { $compliance = false; } $currency = $order->get_currency(); $pay = VPRWC_Payment_Data::get( $order ); $refs = self::references( $order, $pay ); $paid = self::is_paid( $order ); $number = $invoice ? VPRWC_Numbering::get_or_assign_invoice( $order ) : ( $paid ? VPRWC_Numbering::get_or_assign( $order ) : VPRWC_Numbering::get( $order ) ); $other_no = $invoice ? VPRWC_Numbering::get( $order ) : VPRWC_Numbering::get_invoice( $order ); $show = array(); foreach ( array( 'logo', 'shop_address', 'billing', 'shipping', 'items', 'payment', 'card', 'bank', 'channel', 'gateway_ref', 'qr', 'customer_note' ) as $k ) { $show[ $k ] = VPRWC_Settings::on( 'vprwc_show_' . $k ); } if ( $compliance ) { // The compliance copy always carries the full payment record. $show['payment'] = true; $show['card'] = true; $show['bank'] = true; $show['channel'] = true; $show['gateway_ref'] = true; } $items = array(); foreach ( $order->get_items() as $item ) { $meta = wc_display_item_meta( $item, array( 'echo' => false, 'before' => '', 'after' => '', 'separator' => ', ', 'autop' => false, ) ); $product = is_callable( array( $item, 'get_product' ) ) ? $item->get_product() : null; $items[] = array( 'name' => wp_specialchars_decode( $item->get_name(), ENT_QUOTES ), 'sku' => ( $product && $product->get_sku() ) ? $product->get_sku() : '', 'meta' => trim( html_entity_decode( wp_strip_all_tags( (string) $meta ), ENT_QUOTES, 'UTF-8' ) ), 'qty' => (string) $item->get_quantity(), 'total' => self::money( (float) $order->get_line_subtotal( $item, $order->get_prices_include_tax(), true ), $currency ), ); } $totals = array(); $totals[] = array( __( 'Subtotal', 'acrislip-order-receipts' ), self::money( $order->get_subtotal(), $currency ) ); if ( (float) $order->get_total_discount() > 0 ) { $totals[] = array( __( 'Discount', 'acrislip-order-receipts' ), '-' . self::money( $order->get_total_discount(), $currency ) ); } if ( $order->get_shipping_method() || (float) $order->get_shipping_total() > 0 ) { $totals[] = array( __( 'Shipping', 'acrislip-order-receipts' ), self::money( $order->get_shipping_total(), $currency ) ); } foreach ( $order->get_fees() as $fee ) { $totals[] = array( wp_specialchars_decode( $fee->get_name(), ENT_QUOTES ), self::money( $fee->get_total(), $currency ) ); } if ( wc_tax_enabled() ) { foreach ( $order->get_tax_totals() as $tax ) { $totals[] = array( wp_specialchars_decode( $tax->label, ENT_QUOTES ), self::money( $tax->amount, $currency ) ); } } $totals[] = array( __( 'Order total', 'acrislip-order-receipts' ), self::money( $order->get_total(), $currency ) ); $pay_currency = $pay['currency'] ? $pay['currency'] : $currency; $type_labels = array( 'card' => __( 'Card', 'acrislip-order-receipts' ), 'bank' => __( 'Bank transfer', 'acrislip-order-receipts' ), 'wallet' => __( 'Wallet', 'acrislip-order-receipts' ), 'other' => __( 'Other', 'acrislip-order-receipts' ), ); $payment = array( array( __( 'Payment method', 'acrislip-order-receipts' ), $pay['method_title'] ), ); if ( $pay['payment_type'] && isset( $type_labels[ $pay['payment_type'] ] ) ) { $payment[] = array( __( 'Paid with', 'acrislip-order-receipts' ), $type_labels[ $pay['payment_type'] ] ); } if ( $show['channel'] && $pay['channel'] ) { $payment[] = array( __( 'Channel', 'acrislip-order-receipts' ), $pay['channel'] ); } if ( $refs['transaction_reference'] ) { $payment[] = array( __( 'Transaction reference', 'acrislip-order-receipts' ), $refs['transaction_reference'], 'mono' ); } if ( $refs['customer_reference'] ) { $payment[] = array( __( 'Customer reference', 'acrislip-order-receipts' ), $refs['customer_reference'], 'mono' ); } if ( $refs['payment_customer_reference'] ) { $payment[] = array( __( 'Payment customer reference', 'acrislip-order-receipts' ), $refs['payment_customer_reference'], 'mono' ); } if ( $show['gateway_ref'] && $pay['gateway_reference'] ) { $payment[] = array( __( 'Payment reference', 'acrislip-order-receipts' ), $pay['gateway_reference'], 'mono' ); } if ( $show['gateway_ref'] && $pay['session_reference'] ) { $payment[] = array( __( 'Checkout session', 'acrislip-order-receipts' ), $pay['session_reference'], 'mono' ); } $payment[] = array( __( 'Currency', 'acrislip-order-receipts' ), $pay_currency ); if ( $paid ) { $payment[] = array( __( 'Date paid', 'acrislip-order-receipts' ), self::datetime( $pay['paid_at'] ) ); $payment[] = array( __( 'Status', 'acrislip-order-receipts' ), $pay['payment_status'] ); } else { $payment[] = array( __( 'Status', 'acrislip-order-receipts' ), __( 'Due', 'acrislip-order-receipts' ) ); } if ( 'test' === $pay['environment'] ) { $payment[] = array( __( 'Mode', 'acrislip-order-receipts' ), __( 'Test payment, no real money moved', 'acrislip-order-receipts' ) ); } // A separate block for how the money was paid: card or bank transfer. $method_block = array( 'type' => '', 'title' => '', 'rows' => array(), ); if ( 'card' === $pay['payment_type'] && $show['card'] ) { $card = VPRWC_Payment_Data::card_display( $pay ); if ( '' !== $card ) { $method_block = array( 'type' => 'card', 'title' => __( 'Card payment', 'acrislip-order-receipts' ), 'rows' => array( array( __( 'Card', 'acrislip-order-receipts' ), $card ) ), ); } } elseif ( 'bank' === $pay['payment_type'] && $show['bank'] ) { $rows = array(); if ( $pay['bank_name'] ) { $rows[] = array( __( 'Bank', 'acrislip-order-receipts' ), $pay['bank_name'] ); } if ( $pay['account_last4'] ) { $rows[] = array( __( 'Account', 'acrislip-order-receipts' ), '**** ' . $pay['account_last4'] ); } if ( $pay['account_name'] ) { $rows[] = array( __( 'Account name', 'acrislip-order-receipts' ), $pay['account_name'] ); } if ( $pay['bank_reference'] ) { $rows[] = array( __( 'Transfer reference', 'acrislip-order-receipts' ), $pay['bank_reference'] ); } if ( $rows ) { $method_block = array( 'type' => 'bank', 'title' => __( 'Bank transfer', 'acrislip-order-receipts' ), 'rows' => $rows, ); } } $compliance_rows = array(); if ( $compliance ) { if ( null !== $pay['fee'] ) { $compliance_rows[] = array( __( 'Gateway fee', 'acrislip-order-receipts' ), self::money( $pay['fee'], $pay['fee_currency'] ? $pay['fee_currency'] : $pay_currency ) ); } if ( null !== $pay['net_amount'] ) { $compliance_rows[] = array( __( 'Net settled to merchant', 'acrislip-order-receipts' ), self::money( $pay['net_amount'], $pay['fee_currency'] ? $pay['fee_currency'] : $pay_currency ) ); } if ( $pay['environment'] ) { $compliance_rows[] = array( __( 'Environment', 'acrislip-order-receipts' ), $pay['environment'] ); } $compliance_rows[] = array( __( 'Payment method id', 'acrislip-order-receipts' ), (string) $order->get_payment_method() ); $compliance_rows[] = array( __( 'Order id', 'acrislip-order-receipts' ), (string) $order->get_id() ); $compliance_rows[] = array( __( 'Order status', 'acrislip-order-receipts' ), wc_get_order_status_name( $order->get_status() ) ); $compliance_rows[] = array( __( 'Customer IP', 'acrislip-order-receipts' ), (string) $order->get_customer_ip_address() ); foreach ( (array) $pay['extra'] as $label => $value ) { $compliance_rows[] = array( (string) $label, (string) $value ); } $compliance_rows = array_values( array_filter( $compliance_rows, static function ( $row ) { return '' !== $row[1]; } ) ); } $refunded = (float) $order->get_total_refunded(); $name = trim( $order->get_formatted_billing_full_name() ); $shipping = $order->get_formatted_shipping_address(); if ( $refunded >= (float) $order->get_total() && $refunded > 0 ) { $status_key = 'refunded'; $status_label = __( 'Refunded', 'acrislip-order-receipts' ); } elseif ( $paid ) { $status_key = 'paid'; $status_label = __( 'Paid', 'acrislip-order-receipts' ); } else { $status_key = 'due'; $status_label = __( 'Due', 'acrislip-order-receipts' ); } $data = array( 'document' => $document, 'document_title' => $invoice ? __( 'Invoice', 'acrislip-order-receipts' ) : __( 'Payment Receipt', 'acrislip-order-receipts' ), 'document_number' => $number, 'variant' => $compliance ? self::VARIANT_COMPLIANCE : self::VARIANT_CUSTOMER, 'layout' => self::normalize_layout( (string) VPRWC_Settings::get( 'vprwc_layout' ) ), 'paper' => (string) VPRWC_Settings::get( 'vprwc_paper' ), 'brand_color' => (string) VPRWC_Settings::get( 'vprwc_brand_color' ), 'logo_src' => $show['logo'] ? self::logo_data_uri() : '', 'shop' => self::shop(), 'receipt_number' => $invoice ? $other_no : $number, 'invoice_number' => $invoice ? $number : $other_no, 'order_number' => (string) $order->get_order_number(), 'order_date' => self::datetime( $order->get_date_created() ? $order->get_date_created()->getTimestamp() : 0 ), 'issued' => self::datetime( time() ), 'paid_at' => $paid ? self::datetime( $pay['paid_at'] ) : '', 'customer' => array( 'name' => wp_specialchars_decode( $name ? $name : $order->get_billing_company(), ENT_QUOTES ), 'email' => (string) $order->get_billing_email(), 'phone' => (string) $order->get_billing_phone(), ), 'customer_reference' => $refs['customer_reference'], 'payment_customer_reference' => $refs['payment_customer_reference'], 'transaction_reference' => $refs['transaction_reference'], 'billing_lines' => self::address_lines( $order->get_formatted_billing_address() ), 'shipping_lines' => $shipping ? self::address_lines( $shipping ) : array(), 'shipping_method' => wp_specialchars_decode( (string) $order->get_shipping_method(), ENT_QUOTES ), 'customer_note' => (string) $order->get_customer_note(), 'items' => $items, 'totals' => $totals, 'amount_due' => self::money( $order->get_total(), $currency ), 'amount_paid' => $paid ? self::money( $pay['amount_paid'], $pay_currency ) : '', 'refunded' => $refunded > 0 ? self::money( $refunded, $currency ) : '', 'status_key' => $status_key, 'status_label' => $status_label, 'payment' => $payment, 'payment_panel' => self::payment_panel( $order, $pay, $refs, $invoice, $paid, $pay_currency ), 'payment_type' => $pay['payment_type'], 'method_block' => $method_block, 'compliance' => $compliance_rows, 'show' => $show, 'verify_url' => '', 'qr_src' => '', 'footer_note' => (string) VPRWC_Settings::get( 'vprwc_footer_note' ), 'terms' => (string) VPRWC_Settings::get( 'vprwc_terms' ), ); return apply_filters( 'vprwc_receipt_data', $data, $order, $variant ); } /** * Map stored layout ids, including the former "compact" option, to a template style. * * @param string $layout Layout. * @return string classic|modern|minimal */ public static function normalize_layout( $layout ) { if ( 'compact' === $layout ) { $layout = 'minimal'; } return in_array( $layout, array( 'classic', 'modern', 'minimal' ), true ) ? $layout : 'classic'; } /** * Rows for the highlighted payment-details panel. * * @param WC_Order $order Order. * @param array $pay Payment data. * @param array $refs References. * @param bool $invoice Whether this is an invoice. * @param bool $paid Whether the order is paid. * @param string $pay_currency Currency. * @return array */ private static function payment_panel( $order, array $pay, array $refs, $invoice, $paid, $pay_currency ) { $panel = array(); if ( $refs['transaction_reference'] ) { $panel[] = array( __( 'Transaction reference', 'acrislip-order-receipts' ), $refs['transaction_reference'], 'mono' ); } if ( $refs['customer_reference'] ) { $panel[] = array( __( 'Customer reference', 'acrislip-order-receipts' ), $refs['customer_reference'], 'mono' ); } if ( $refs['payment_customer_reference'] ) { $panel[] = array( __( 'Payment customer reference', 'acrislip-order-receipts' ), $refs['payment_customer_reference'], 'mono' ); } if ( $pay['method_title'] ) { $panel[] = array( __( 'Payment method', 'acrislip-order-receipts' ), $pay['method_title'] ); } $instrument = VPRWC_Payment_Data::card_display( $pay ); if ( '' === $instrument && ( $pay['bank_name'] || $pay['account_last4'] ) ) { $instrument = trim( $pay['bank_name'] . ( $pay['account_last4'] ? ' **** ' . $pay['account_last4'] : '' ) ); } if ( '' !== $instrument ) { $panel[] = array( __( 'Paid with', 'acrislip-order-receipts' ), $instrument ); } if ( $paid && ! empty( $pay['paid_at'] ) ) { $panel[] = array( __( 'Date paid', 'acrislip-order-receipts' ), self::datetime( $pay['paid_at'] ) ); } $panel[] = array( $invoice ? __( 'Amount due', 'acrislip-order-receipts' ) : __( 'Amount paid', 'acrislip-order-receipts' ), $invoice ? self::money( $order->get_total(), $order->get_currency() ) : self::money( $pay['amount_paid'], $pay_currency ), ); $panel[] = array( __( 'Currency', 'acrislip-order-receipts' ), $pay_currency ); return $panel; } }