__( 'Transaction reference', 'acrislip-order-receipts' ), 'value' => $refs['transaction_reference'], ); } if ( $refs['customer_reference'] ) { $fields['vprwc_customer_reference'] = array( 'label' => __( 'Customer reference', 'acrislip-order-receipts' ), 'value' => $refs['customer_reference'], ); } if ( $refs['payment_customer_reference'] ) { $fields['vprwc_payment_customer_reference'] = array( 'label' => __( 'Payment customer reference', 'acrislip-order-receipts' ), 'value' => $refs['payment_customer_reference'], ); } return $fields; } /** * Write a customer receipt or invoice PDF to a protected temporary folder. * * @param WC_Order $order Order. * @param string $document receipt|invoice. * @return string Path, or '' on failure. */ public static function write_temp( $order, $document = VPRWC_Receipt::DOCUMENT_RECEIPT ) { try { $receipt = VPRWC_Receipt::build( $order, VPRWC_Receipt::VARIANT_CUSTOMER, $document ); $bytes = VPRWC_Renderer::pdf( $receipt ); } catch ( \Throwable $e ) { if ( function_exists( 'wc_get_logger' ) ) { wc_get_logger()->error( 'PDF failed for order ' . $order->get_id() . ': ' . $e->getMessage(), array( 'source' => 'acrislip-order-receipts' ) ); } return ''; } $uploads = wp_get_upload_dir(); $dir = trailingslashit( $uploads['basedir'] ) . 'vprwc-temp/' . wp_generate_password( 20, false ) . '/'; if ( ! wp_mkdir_p( $dir ) ) { return ''; } $root = trailingslashit( $uploads['basedir'] ) . 'vprwc-temp/'; if ( ! file_exists( $root . 'index.php' ) ) { file_put_contents( $root . 'index.php', "get_billing_email(); if ( ! is_email( $to ) || ! VPRWC_Receipt::is_paid( $order ) ) { return false; } $file = self::write_temp( $order ); if ( ! $file ) { return false; } $number = VPRWC_Numbering::get( $order ); $shop = VPRWC_Receipt::shop(); /* translators: 1: receipt number 2: shop name */ $subject = sprintf( __( 'Your Payment Receipt %1$s from %2$s', 'acrislip-order-receipts' ), $number, $shop['legal_name'] ? $shop['legal_name'] : $shop['name'] ); $heading = __( 'Payment Receipt', 'acrislip-order-receipts' ); $name = $order->get_billing_first_name(); $body = '
' . esc_html( $name /* translators: %s: customer first name */ ? sprintf( __( 'Hi %s,', 'acrislip-order-receipts' ), $name ) : __( 'Hello,', 'acrislip-order-receipts' ) ) . '
'; $body .= '' . esc_html( sprintf( /* translators: 1: receipt number 2: order number */ __( 'Thank you for your payment. Your receipt %1$s for order #%2$s is attached as a PDF.', 'acrislip-order-receipts' ), $number, $order->get_order_number() ) ) . '
'; $body .= self::shop_contact_html(); $body .= self::references_html( $order ); $mailer = WC()->mailer(); $message = $mailer->wrap_message( $heading, $body ); $sent = (bool) $mailer->send( $to, $subject, $message, "Content-Type: text/html\r\n", array( $file ) ); if ( $sent ) { /* translators: %s: receipt number */ $order->add_order_note( sprintf( __( 'Payment receipt %s emailed to the customer.', 'acrislip-order-receipts' ), $number ) ); } return $sent; } /** * Email the invoice to the order's billing email. * * @param WC_Order $order Order. * @return bool */ public static function send_invoice( $order ) { $to = $order->get_billing_email(); if ( ! is_email( $to ) ) { return false; } $file = self::write_temp( $order, VPRWC_Receipt::DOCUMENT_INVOICE ); if ( ! $file ) { return false; } $number = VPRWC_Numbering::get_invoice( $order ); $shop = VPRWC_Receipt::shop(); /* translators: 1: invoice number 2: shop name */ $subject = sprintf( __( 'Your invoice %1$s from %2$s', 'acrislip-order-receipts' ), $number, $shop['legal_name'] ? $shop['legal_name'] : $shop['name'] ); $heading = __( 'Invoice', 'acrislip-order-receipts' ); $name = $order->get_billing_first_name(); $body = '' . esc_html( $name /* translators: %s: customer first name */ ? sprintf( __( 'Hi %s,', 'acrislip-order-receipts' ), $name ) : __( 'Hello,', 'acrislip-order-receipts' ) ) . '
'; $body .= '' . esc_html( sprintf( /* translators: 1: invoice number 2: order number */ __( 'Your invoice %1$s for order #%2$s is attached as a PDF.', 'acrislip-order-receipts' ), $number, $order->get_order_number() ) ) . '
'; $body .= self::shop_contact_html(); $body .= self::references_html( $order ); $mailer = WC()->mailer(); $message = $mailer->wrap_message( $heading, $body ); $sent = (bool) $mailer->send( $to, $subject, $message, "Content-Type: text/html\r\n", array( $file ) ); if ( $sent ) { /* translators: %s: invoice number */ $order->add_order_note( sprintf( __( 'Invoice %s emailed to the customer.', 'acrislip-order-receipts' ), $number ) ); } return $sent; } /** * Shop letterhead for receipt and invoice emails (plugin fields, with WooCommerce fallbacks). * * @return string */ private static function shop_contact_html() { $shop = VPRWC_Receipt::shop(); $name = ! empty( $shop['legal_name'] ) ? $shop['legal_name'] : $shop['name']; $html = '';
$html .= '' . esc_html( $name ) . '';
foreach ( (array) $shop['address_lines'] as $line ) {
$html .= '
' . esc_html( $line );
}
if ( ! empty( $shop['phone'] ) ) {
$html .= '
' . esc_html( $shop['phone'] );
}
if ( ! empty( $shop['email'] ) ) {
$html .= '
' . esc_html( $shop['email'] );
}
return $html . '
| ' . esc_html__( 'Payment details', 'acrislip-order-receipts' ) . ' | |
| ' . esc_html( $label ) . ' | '; $html .= '' . esc_html( $value ) . ' |