db->prepare( 'SELECT * FROM %i WHERE `id` = %d LIMIT 1', $this->table( 'transaction_intents' ), $id ); $row = $this->db->get_row( $sql, ARRAY_A ); return is_array( $row ) ? $this->hydrate( $row ) : null; } /** * {@inheritDoc} * * @param Uuid $uuid Parameter. */ public function find_by_uuid( Uuid $uuid ): ?TransactionIntent { $sql = $this->db->prepare( 'SELECT * FROM %i WHERE `uuid` = %s LIMIT 1', $this->table( 'transaction_intents' ), $uuid->to_string() ); $row = $this->db->get_row( $sql, ARRAY_A ); return is_array( $row ) ? $this->hydrate( $row ) : null; } /** * {@inheritDoc} * * @param int $order_intent_id Parameter. */ public function find_by_order_intent_id( int $order_intent_id ): array { $sql = $this->db->prepare( 'SELECT * FROM %i WHERE `order_intent_id` = %d ORDER BY `id` ASC', $this->table( 'transaction_intents' ), $order_intent_id ); $rows = $this->db->get_results( $sql, ARRAY_A ); if ( ! is_array( $rows ) ) { return []; } $out = []; foreach ( $rows as $row ) { if ( is_array( $row ) ) { $out[] = $this->hydrate( $row ); } } return $out; } /** * {@inheritDoc} * * @param string $key Parameter. */ public function find_by_idempotency_key( string $key ): ?TransactionIntent { $sql = $this->db->prepare( 'SELECT * FROM %i WHERE `idempotency_key` = %s LIMIT 1', $this->table( 'transaction_intents' ), $key ); $row = $this->db->get_row( $sql, ARRAY_A ); return is_array( $row ) ? $this->hydrate( $row ) : null; } /** * {@inheritDoc} * * @param string $gateway Parameter. * @param string $gateway_intent_id Parameter. */ public function find_by_gateway_intent_id( string $gateway, string $gateway_intent_id ): ?TransactionIntent { $sql = $this->db->prepare( 'SELECT * FROM %i WHERE `gateway` = %s AND `gateway_intent_id` = %s LIMIT 1', $this->table( 'transaction_intents' ), $gateway, $gateway_intent_id ); $row = $this->db->get_row( $sql, ARRAY_A ); return is_array( $row ) ? $this->hydrate( $row ) : null; } /** * {@inheritDoc} * * @param TransactionIntent $intent Parameter. */ public function save( TransactionIntent $intent ): void { $data = [ 'uuid' => $intent->uuid()->to_string(), 'order_intent_id' => $intent->order_intent_id(), 'gateway' => $intent->gateway(), 'gateway_intent_id' => $intent->gateway_intent_id(), 'amount_minor' => $intent->amount()->amount_minor(), 'currency' => $intent->amount()->currency(), 'payment_type' => $intent->payment_type()->value, 'status' => $intent->status()->value, 'idempotency_key' => $intent->idempotency_key(), 'client_secret_hash' => $intent->client_secret_hash(), 'failure_code' => $intent->failure_code(), 'failure_message' => $intent->failure_message(), 'created_at' => $this->format_datetime( $intent->created_at() ), 'updated_at' => $this->format_datetime( $intent->updated_at() ), ]; if ( null === $intent->id() ) { $this->db->insert( $this->table( 'transaction_intents' ), $data ); $intent->assign_id( $this->last_insert_id() ); return; } $this->db->update( $this->table( 'transaction_intents' ), $data, [ 'id' => $intent->id() ] ); } /** * {@inheritDoc} * * @param int $order_intent_id Parameter. */ public function delete_for_order_intent( int $order_intent_id ): void { $sql = $this->db->prepare( 'DELETE FROM %i WHERE `order_intent_id` = %d', $this->table( 'transaction_intents' ), $order_intent_id ); $this->query_prepared( $sql ); } /** * Map a database row to an entity. * * @param array $row Database row. */ private function hydrate( array $row ): TransactionIntent { return new TransactionIntent( (int) $row['id'], new Uuid( (string) $row['uuid'] ), (int) $row['order_intent_id'], (string) $row['gateway'], null !== $row['gateway_intent_id'] ? (string) $row['gateway_intent_id'] : null, new Money( (int) $row['amount_minor'], (string) $row['currency'] ), TransactionPaymentType::from( (string) $row['payment_type'] ), TransactionIntentStatus::from( (string) $row['status'] ), null !== $row['idempotency_key'] ? (string) $row['idempotency_key'] : null, null !== $row['client_secret_hash'] ? (string) $row['client_secret_hash'] : null, null !== $row['failure_code'] ? (string) $row['failure_code'] : null, null !== $row['failure_message'] ? (string) $row['failure_message'] : null, $this->parse_datetime( (string) $row['created_at'] ), $this->parse_datetime( (string) $row['updated_at'] ), ); } }