$payment booking_id, gateway, amount, currency, transaction_id, note, user_id, refund_of. */ public function create( array $payment ): int { $this->db->insert( $this->table, array( 'booking_id' => (int) $payment['booking_id'], 'gateway' => (string) $payment['gateway'], 'amount_minor' => (int) $payment['amount'], 'currency' => (string) $payment['currency'], 'transaction_id' => (string) ( $payment['transaction_id'] ?? '' ), 'refund_of' => (int) ( $payment['refund_of'] ?? 0 ), 'note' => mb_substr( (string) ( $payment['note'] ?? '' ), 0, 255 ), 'user_id' => (int) ( $payment['user_id'] ?? 0 ), 'created_at' => self::now(), ) ); return (int) $this->db->insert_id; } public function find( int $id ): ?array { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $row = $this->db->get_row( $this->db->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id ), ARRAY_A ); return $row ? self::hydrate( $row ) : null; } /** * A booking's payments, oldest first. * * @param int $booking_id Booking. * @return array> */ public function for_booking( int $booking_id ): array { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT * FROM {$this->table} WHERE booking_id = %d ORDER BY id", $booking_id ), ARRAY_A ); return array_map( array( self::class, 'hydrate' ), $rows ); } public function total_paid( int $booking_id ): int { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery return (int) $this->db->get_var( $this->db->prepare( "SELECT COALESCE(SUM(amount_minor), 0) FROM {$this->table} WHERE booking_id = %d", $booking_id ) ); } /** * Whether a provider transaction was already recorded, so webhooks and * returns cannot count one payment twice. * * @param string $gateway Method ID. * @param string $transaction_id Provider ID. */ public function has_transaction( string $gateway, string $transaction_id ): bool { if ( '' === $transaction_id ) { return false; } // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery return null !== $this->db->get_var( $this->db->prepare( "SELECT id FROM {$this->table} WHERE gateway = %s AND transaction_id = %s AND amount_minor > 0 LIMIT 1", $gateway, $transaction_id ) ); } /** * Amount already refunded of a payment. * * @param int $payment_id Payment. */ public function refunded( int $payment_id ): int { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery return - (int) $this->db->get_var( $this->db->prepare( "SELECT COALESCE(SUM(amount_minor), 0) FROM {$this->table} WHERE refund_of = %d", $payment_id ) ); } /** * Payments in a window, for reports and exports. * * @param int $from Start. * @param int $to End. * @return array> */ public function between( int $from, int $to ): array { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT * FROM {$this->table} WHERE created_at >= %s AND created_at < %s ORDER BY created_at LIMIT 20000", self::dt( $from ), self::dt( $to ) ), ARRAY_A ); return array_map( array( self::class, 'hydrate' ), $rows ); } /** * Cast a row. * * @param array $row Row. * @return array */ private static function hydrate( array $row ): array { return array( 'id' => (int) $row['id'], 'booking_id' => (int) $row['booking_id'], 'gateway' => (string) $row['gateway'], 'amount' => (int) $row['amount_minor'], 'currency' => (string) $row['currency'], 'transaction_id' => (string) $row['transaction_id'], 'refund_of' => (int) $row['refund_of'], 'note' => (string) $row['note'], 'user_id' => (int) $row['user_id'], 'created_at' => self::ts( $row['created_at'] ), ); } }