meta( $order, '_cray_reference' ); $row = $reference ? $this->row( $reference ) : array(); $col = static function ( $k ) use ( $row ) { return isset( $row[ $k ] ) ? $row[ $k ] : ''; }; $txn = $this->first( array( $this->meta( $order, '_cray_transaction_reference' ), $col( 'provider_reference' ), ) ); // _cray_reference is the merchant-generated customer reference, never the transaction reference. if ( $txn && $reference && $txn === $reference ) { $txn = ''; } $data = $this->set( $data, 'transaction_reference', $txn ); if ( $reference ) { $data['gateway_reference'] = $reference; } $channel = $this->first( array( $this->meta( $order, '_cray_channel' ), $col( 'type' ) ) ); $data = $this->set( $data, 'channel', $channel ); $event = strtolower( $this->meta( $order, '_cray_event' ) ); $ch = strtolower( $channel ); $account_last4 = $this->meta( $order, '_cray_sender_account_last4' ); $is_bank = false !== strpos( $event, 'payin' ) || preg_match( '/bank|transfer|virtual|payin|ussd/', $ch ) || '' !== $account_last4; $is_card = false !== strpos( $ch, 'card' ) || false !== strpos( $event, 'card' ); if ( $is_card ) { $data['payment_type'] = 'card'; $data = $this->set( $data, 'card_masked', $this->meta( $order, '_cray_card_masked' ) ); } elseif ( $is_bank ) { $data['payment_type'] = 'bank'; $data = $this->set( $data, 'bank_name', $this->first( array( $this->meta( $order, '_cray_sender_bank_name' ), $this->meta( $order, '_cray_sender_bank_code' ) ) ) ); $data = $this->set( $data, 'account_last4', $account_last4 ); $data = $this->set( $data, 'account_name', $this->meta( $order, '_cray_sender_name' ) ); $data = $this->set( $data, 'bank_reference', $this->meta( $order, '_cray_transaction_reference' ) ); } $paid_at = (int) $this->meta( $order, '_cray_paid_at' ); if ( $paid_at > 0 ) { $data['paid_at'] = $paid_at; } elseif ( empty( $data['paid_at'] ) && $col( 'provider_created' ) ) { $ts = strtotime( $col( 'provider_created' ) . ' UTC' ); $data['paid_at'] = $ts ? $ts : 0; } $amount = $this->first( array( $this->meta( $order, '_cray_amount_paid' ), $col( 'amount' ) ) ); if ( is_numeric( $amount ) ) { $data['amount_paid'] = (float) $amount; } $data = $this->set( $data, 'currency', strtoupper( (string) $col( 'currency' ) ) ); $fee = $this->first( array( $this->meta( $order, '_cray_charge' ), $col( 'charge' ) ) ); $net = $this->first( array( $this->meta( $order, '_cray_net_amount' ), $col( 'net_amount' ) ) ); if ( is_numeric( $fee ) ) { $data['fee'] = (float) $fee; } if ( is_numeric( $net ) ) { $data['net_amount'] = (float) $net; } $env = $this->meta( $order, '_cray_environment' ); if ( $env ) { $data['environment'] = false !== stripos( $env, 'live' ) ? 'live' : 'test'; } $data = $this->extra( $data, __( 'Gateway status', 'acrislip-order-receipts' ), $this->first( array( $this->meta( $order, '_cray_status' ), $col( 'status' ) ) ) ); $data = $this->extra( $data, __( 'Gateway event', 'acrislip-order-receipts' ), $this->meta( $order, '_cray_event' ) ); $data = $this->extra( $data, __( 'Sender bank code', 'acrislip-order-receipts' ), $this->meta( $order, '_cray_sender_bank_code' ) ); $data = $this->extra( $data, __( 'RRN', 'acrislip-order-receipts' ), (string) $col( 'rrn' ) ); $data = $this->extra( $data, __( 'STAN', 'acrislip-order-receipts' ), (string) $col( 'stan' ) ); $verified = (int) $this->meta( $order, '_cray_verified_at' ); if ( $verified ) { $data = $this->extra( $data, __( 'Verified with gateway', 'acrislip-order-receipts' ), wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $verified ) ); } return $data; } /** * Cray transactions row, if the table exists. * * @param string $reference Reference. * @return array */ private function row( $reference ) { global $wpdb; $table = $wpdb->prefix . 'cray_wcs_transactions'; $found = wp_cache_get( 'vprwc_cray_table' ); if ( false === $found ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery $found = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ) === $table ? 'yes' : 'no'; wp_cache_set( 'vprwc_cray_table', $found ); } if ( 'yes' !== $found ) { return array(); } $table = esc_sql( $table ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE reference = %s", $reference ), ARRAY_A ); return is_array( $row ) ? $row : array(); } }