array( 'Cray', '#5b3df5' ), 'paystack' => array( 'Paystack', '#0ba4db' ), 'flutterwave' => array( 'Flutterwave', '#f5a623' ), 'dodo' => array( 'Dodo Payments', '#111827' ), 'stripe' => array( 'Stripe', '#635bff' ), 'paypal' => array( 'PayPal', '#003087' ), 'bacs' => array( 'Bank transfer', '#0f766e' ), 'cheque' => array( 'Cheque', '#475569' ), 'cod' => array( 'Cash on delivery', '#475569' ), 'other' => array( '', '#64748b' ), ); } /** * Provider key for a payment method id. * * @param string $method Payment method id. * @return string */ public static function provider_key( $method ) { $method = strtolower( (string) $method ); foreach ( array( 'cray', 'paystack', 'flutterwave', 'dodo', 'stripe', 'paypal', 'ppcp' ) as $needle ) { if ( false !== strpos( $method, $needle ) ) { return 'ppcp' === $needle ? 'paypal' : $needle; } } if ( in_array( $method, array( 'bacs', 'cheque', 'cod' ), true ) ) { return $method; } return 'other'; } /** * Display status for an order's payment. * * @param WC_Order $order Order. * @return string paid|refunded|partially_refunded|pending|on-hold|failed|cancelled */ public static function status( $order ) { if ( $order->has_status( 'refunded' ) ) { return 'refunded'; } if ( $order->has_status( array( 'failed' ) ) ) { return 'failed'; } if ( $order->has_status( array( 'cancelled' ) ) ) { return 'cancelled'; } if ( VPRWC_Receipt::is_paid( $order ) ) { return (float) $order->get_total_refunded() > 0 ? 'partially_refunded' : 'paid'; } return $order->has_status( 'on-hold' ) ? 'on-hold' : 'pending'; } /** * Status labels. * * @return array */ public static function status_labels() { return array( 'paid' => __( 'Paid', 'acrislip-order-receipts' ), 'partially_refunded' => __( 'Partly refunded', 'acrislip-order-receipts' ), 'refunded' => __( 'Refunded', 'acrislip-order-receipts' ), 'pending' => __( 'Pending', 'acrislip-order-receipts' ), 'on-hold' => __( 'On hold', 'acrislip-order-receipts' ), 'failed' => __( 'Failed', 'acrislip-order-receipts' ), 'cancelled' => __( 'Cancelled', 'acrislip-order-receipts' ), ); } /** * Meta keys to read when resolving a transaction reference for display. * Prefer provider-specific transaction keys over merchant lookup keys. * * @return string[] */ public static function transaction_reference_meta_keys() { return (array) apply_filters( 'vprwc_transaction_reference_meta_keys', array( '_vprwc_transaction_reference', '_vprwc_search_reference', '_cray_transaction_reference', '_vantpay_reference', '_paystack_txn_ref', '_flw_payment_txn_ref', '_stripe_intent_id', '_vprwc_gateway_reference', ) ); } /** * Resolve the transaction reference the same way everywhere: Payment box, PDFs, emails and customer views. * Adapters first, then the WooCommerce transaction id, then known order meta, then order notes. * `_cray_reference` (the merchant lookup key sent to Cray) is never used as the transaction reference. * * @param WC_Order $order Order. * @param array $pay Optional payment data from VPRWC_Payment_Data::get(). * @return string */ public static function transaction_reference( $order, array $pay = array() ) { if ( ! $pay ) { $pay = VPRWC_Payment_Data::get( $order ); } $candidates = array( isset( $pay['transaction_reference'] ) ? $pay['transaction_reference'] : '', $order->get_transaction_id(), ); foreach ( self::transaction_reference_meta_keys() as $key ) { $v = $order->get_meta( $key, true ); if ( is_scalar( $v ) && '' !== trim( (string) $v ) ) { $candidates[] = trim( (string) $v ); } } $is_cray = $order && 0 === strpos( (string) $order->get_payment_method(), 'cray' ); if ( ! $is_cray ) { $candidates[] = self::reference_from_notes( $order ); } $merchant = $order ? trim( (string) $order->get_meta( '_cray_reference', true ) ) : ''; foreach ( $candidates as $c ) { if ( ! is_scalar( $c ) ) { continue; } $c = trim( (string) $c ); if ( '' === $c ) { continue; } if ( $merchant && 0 === strcasecmp( $c, $merchant ) ) { continue; } return $c; } return ''; } /** * Find a reference in the order notes (for gateways that only write notes). * * @param WC_Order $order Order. * @return string */ public static function reference_from_notes( $order ) { $notes = wc_get_order_notes( array( 'order_id' => $order->get_id(), 'type' => 'internal', 'limit' => 30, ) ); foreach ( (array) $notes as $note ) { $text = wp_strip_all_tags( (string) $note->content ); if ( preg_match( '/\b(?:transaction\s+)?(?:reference|ref|txn|transaction id)\b\s*[:#]?\s*([A-Za-z0-9][A-Za-z0-9_\-\.\/]{5,})/i', $text, $m ) ) { return rtrim( $m[1], '.' ); } } return ''; } /** * Build the summary. * * @param WC_Order $order Order. * @param bool $admin Include fees, net and gateway internals. * @return array */ public static function summary( $order, $admin = false ) { $pay = VPRWC_Payment_Data::get( $order ); $method = (string) $order->get_payment_method(); $key = self::provider_key( $method ); $prov = self::providers(); $name = $prov[ $key ][0] ? $prov[ $key ][0] : (string) $order->get_payment_method_title(); $currency = $pay['currency'] ? $pay['currency'] : $order->get_currency(); $txn = self::transaction_reference( $order, $pay ); $gw_ref = (string) $pay['gateway_reference']; if ( $gw_ref === $txn ) { $gw_ref = ''; } $status = self::status( $order ); $labels = self::status_labels(); $refs = VPRWC_Receipt::references( $order ); $out = array( 'order_id' => $order->get_id(), 'order_number' => (string) $order->get_order_number(), 'provider' => $key, 'provider_name' => '' !== $name ? $name : __( 'Other', 'acrislip-order-receipts' ), 'provider_color' => $prov[ $key ][1], 'method_title' => (string) $order->get_payment_method_title(), 'status' => $status, 'status_label' => $labels[ $status ], 'transaction_reference' => $txn, 'customer_reference' => $refs['customer_reference'], 'payment_customer_reference' => $refs['payment_customer_reference'], 'provider_reference' => $gw_ref, 'session_reference' => (string) $pay['session_reference'], 'payment_type' => (string) $pay['payment_type'], 'channel' => (string) $pay['channel'], 'card' => VPRWC_Payment_Data::card_display( $pay ), 'bank_name' => (string) $pay['bank_name'], 'account_masked' => $pay['account_last4'] ? '**** ' . $pay['account_last4'] : '', 'account_name' => (string) $pay['account_name'], 'bank_reference' => (string) $pay['bank_reference'], 'amount' => VPRWC_Receipt::money( $pay['amount_paid'] > 0 ? $pay['amount_paid'] : $order->get_total(), $currency ), 'total_raw' => (float) $order->get_total(), 'currency' => $currency, 'paid_at' => $pay['paid_at'] ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), (int) $pay['paid_at'] ) : '', 'paid_ts' => (int) $pay['paid_at'], 'created' => $order->get_date_created() ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $order->get_date_created()->getTimestamp() ) : '', 'environment' => (string) $pay['environment'], 'customer_name' => trim( $order->get_formatted_billing_full_name() ), 'customer_email' => (string) $order->get_billing_email(), ); if ( $admin ) { $fee_cur = $pay['fee_currency'] ? $pay['fee_currency'] : $currency; $out['fee'] = null !== $pay['fee'] ? VPRWC_Receipt::money( $pay['fee'], $fee_cur ) : ''; $out['net'] = null !== $pay['net_amount'] ? VPRWC_Receipt::money( $pay['net_amount'], $fee_cur ) : ''; $out['refunds'] = (float) $order->get_total_refunded() > 0 ? VPRWC_Receipt::money( $order->get_total_refunded(), $order->get_currency() ) : ''; } return $out; } /** * Cache the reference in searchable meta when it came from a fallback (notes, adapter tables). * * @param WC_Order $order Order. * @param string $ref Reference. */ public static function remember_reference( $order, $ref ) { if ( '' !== $ref && $order->get_meta( '_vprwc_search_reference' ) !== $ref ) { $order->update_meta_data( '_vprwc_search_reference', sanitize_text_field( $ref ) ); $order->save_meta_data(); } } }