# Documentation

FIFA Connect ID .NET SDK, v6.1

# 1. Introduction

The primary responsibility of the FIFA Connect ID Service (FCI) is to generate a unique FIFA_ID for stakeholders involved in football, futsal, and beach soccer. Registration Systems in various Member Associations integrate with FCI to register persons, organisations and facilities in order to obtain FIFA_IDs for these entities.

Integration can be achieved in two ways:

- [Recommended] Via the SDK, a library that encapsulates the REST API and simplifies access to the service.
- Directly, by utilizing the REST API of FCI.

This documentation serves as a reference for developers seeking to integrate Registration Systems with FCI using the SDK.

## 1.1 Chapters overview

Chapter 1 provides introductory information on the classes within the SDK, as well as guidance on dates and thread safety.

Chapter 2 covers the basics of integration with FCI, serving as a starting point for further actions.

The following three chapters discuss the management of specific entity types:

- Chapter 3 describes how to register, update, or merge a Person, with particular attention to identifying national and international duplicates.
- Chapter 4 discusses all aspects of Organisation registration, update, and merge.
- Chapter 5 demonstrates how to register or update a Facility.

Chapter 6 provides information on integration with the Transfer Matching System (TMS), enabling the transmission of data concerning players' first professional registrations, transfers, and committed payments.

Chapter 7 focuses on subscribing to automatic notifications for changes made to entities registered in FCI.

## 1.2 Clarification on Class Names

Class names, following the FIFA Data Standard, cannot be modified.

## 1.3 Unexpected Setters

Some SDK classes that represent read-only data provided by the server side (e.g., `PersonDuplicateType`) contain setter methods. These are included to allow the client to simulate possible responses from the service, which can be useful for testing.

## 1.4 Date and Time

All SDK methods require date and time in the UTC time zone for consistency across different time zones. If the date/time is not in UTC, an Exception is thrown.

Birth dates should not be affected by time zone changes. For example, both persons born in Chile and Japan on 1.1.2012 should be registered as born on 1.1.2012 0:00 UTC.

> Note: Birth dates returned from the SDK should represent a date without a time zone (time zone should be disregarded when comparing dates). For example, a birth date of 1.1.2012 0:00 [UTC-3] represents the same birth date as 1.1.2012 0:00 [UTC+9].

## 1.5 Thread Safety

Any instance members are not guaranteed to be thread safe. In particular, all methods on `FifaConnectServiceBusClient` cannot be called from multiple threads simultaneously. If needed, a new instance of `FifaConnectServiceBusClient` should be created per thread.

# 2. Setup

## 2.1 .NET version

The SDK is compatible with .NET Standard 2.0.

## 2.2 Installation

The SDK NuGet has to be installed in your project. Refer to the instructions for how to do that.

## 2.3 Using Registration Facade

The `RegistrationFacade` class simplifies the usage of `FifaConnectIdClient` and `FifaConnectServiceBusClient` in order to seamlessly start executing actions in FCI.

This chapter goes through needed dependencies when bootstrapping the `RegistrationFacade` instance.

### 2.3.1 Connect ID environment

Instance of type `ConnectIdEnvironment` provides the information about the service SDK will make request to. It is recommended to use one of environments defined as static properties of the class:

    ConnectIdEnvironment connectIdEnvironment = ConnectIdEnvironment.Beta;

Available pre-defined environments are:

Environment | Environment code
--- | ---
Beta | beta
PreProduction | prep
Production | prod

### 2.3.2 Client credentials

In order to authenticate SDK to the service you have to provide a set of client credentials. Create instance of `ClientCredentials` using client ID and secret key gained during given environment onboarding process.

    ClientCredentials clientCredentials = new ClientCredentials("clientId", "secretKey");

### 2.3.3 Private key storage

In order for the SDK to encrypt outgoing and decrypt incoming data, a pair of public and private certificates must be generated.

Instructions on how to generate and upload certificates can be found in fifa-connectservicebus-certificates-generation.html file in root directory of SDK's *.zip package. Once this is done, the next step is to provide the generated private key to the client.

The easiest way to do this is by using the `PrivateKeyMemoryStorage` class. To create an instance of this class, you need to provide `X509Certificate2` object(s) as its constructor parameter(s). Each `X509Certificate2` object consists of the key from the generated file and an optional password. Please take a look at the following example:

    const string pathToCertificate = @"C:\Encrypt\certificate.pfx"; // Example path to the certificate file

    X509Certificate2 certificate = new X509Certificate2(pathToCertificate, "password", X509KeyStorageFlags.DefaultKeySet);

    PrivateKeyMemoryStorage privateKeyStore = new PrivateKeyMemoryStorage(certificate);

In the example above, we used the `PrivateKeyMemoryStorage` class, which implements the `IPrivateKeyStorage` interface. However, any implementation of `IPrivateKeyStorage` could be used here - we do not restrict using custom code at this point.

### 2.3.4 Logger

Each SDK client method uses an instance `Fifa.ConnectServiceBus.Sdk.Logging.ILogger` in order to log any errors that might happen during the application runtime. There is no default Logger implementation provided, as different parties could want a different way of storing logs.

    public interface ILogger
    {
      /// <summary>
      /// Writes the diagnostic message and exception at the debug level.
      /// </summary>
      void Debug(string message, params object[] args);

      /// <summary>
      /// Writes the diagnostic message and exception at the error level.
      /// </summary>
      void Error(string message, params object[] args);

      /// <summary>
      /// Writes the diagnostic message and exception at the error level.
      /// </summary>
      void Error(Exception exception, string message, params object[] args);
    }

A frequent mistake when implementing the `ILogger` is ignoring the `args` parameter when formatting the message. Example of the `void Debug(string message, params object[] args)` might be:

    Console.WriteLine(message, args);

When implementing `void Error(Exception exception, string message, params object[] args)` please make sure to handle the Exception properly, i.e. include the inner exceptions (if any) and the stack trace. The message itself might not be enough.

### 2.3.5 Registration Facade settings

#### 2.3.5.1 XSD Schemas Directory Resolver

The SDK validates input against the XSD schema you can find in the `Resources` directory in the SDK package. If the SDK fails to locate it, it won't be able to validate anything because the construction of `FifaConnectIdClient` will fail with an error message indicating that the file `scenarios.xsd` could not be found.

By default, the SDK assumes that the `Resources` directory is located next to the executing assembly. This works out of the box, as long as the `Resources` directory is copied to app's output directory at build/publish time.

> Optional: While not a recommended approach, if your setup requires it, you can instantiate `XsdSchemasDirectoryResolver` using a constructor accepting an argument pointing to a directory where `scenarios.xsd` is located, or implement your own `IDirectoryResolver`. The resolver then needs to be passed into constructors of classes requiring it as the last parameter; for example:

    XsdSchemasDirectoryResolver xsdSchemasDirectoryResolver = new XsdSchemasDirectoryResolver(@"C:\custom\directory\where\scenarios.xsd\is\located");

#### 2.3.5.2 Person Details Cache

Using `IPersonDetailsCache` allows all Registration Systems to exchange persons’ details via Service Bus in order to find international duplicates and ensure that the same person will not be registered twice.

If your infrastructure for the application using SDK consists of multiple servers or your listener and registration facade are running in separate processes, then additional configuration is required.

The most important thing to consider when choosing the way you want to host your listener is that it requires a shared storage between all instances of the SDK. Thus, when hosting the listener in the cloud (e.g. as Azure WebJob) or when having multiple instances of Registration System (and in effect: multiple instances of the SDK), each instance must access the same shared state.

The request has to be matched with response. In order for this correlation to work, an ID of the request message (so called Correlation Id) needs to be stored and later retrieved by our code.

> For example, when sending the requests like: `RegisterPersonAndWaitForDetailsInCaseOfDuplicates` or `GetDuplicates` the Correlation Id is created for them. Once the information is back from another Registration System it also contains the same Correlation Id - this way the listener knows which response should be matched with the request.

SDK defines the `IPersonDetailsCache` interface, which must be implemented by the client of the SDK and passed into `RegistrationFacadeSettings`.

    public interface IPersonDetailsCache
    {
        /// <summary>
        /// Puts person details into storage mechanism.
        /// </summary>
        void Put(PersonDetails personDetails);

        /// <summary>
        /// Get person details with given correlation id from storage mechanism.
        /// </summary>
        PersonDetails GetByCorrelationId(string correlationId);

        /// <summary>
        /// Deletes the entries from storage mechanism that are older than given date.
        /// </summary>
        void DeleteOlderThan(TimeSpan duration);
    }

Examples of implementation of `IPersonDetailsCache` are:

- SQL database - shared state is stored in SQL table (MySQL, SQL Server, Oracle)
- cache server - shared state is stored in cache server (e.g. Redis)
- shared disk - shared state is stored as a set of files which are stored on a shared drive (e.g. using Samba protocol)

> FALLBACK: Only when `RegistrationFacade` and listener are running under the same application SDK uses `PersonDetailsMemoryCache` instance correctly. This default implementation of the interface `IPersonDetailsCache` puts XML that is created in the response for Person details request to the memory cache.

#### 2.3.5.3 Hashed Person Processor

When using local person identifier for Idempotency, you can change its hashing method. This can be done by implementing `IHashedPersonProcessor` interface. Provided implementation needs to be passed to `RegistrationFacadeSettings` class when creating registration facade.

    public interface IHashedPersonProcessor
    {
        /// <summary>
        /// Process provided hashed person type
        /// </summary>
        HashedPersonType Process(HashedPersonType hashedPersonType);
    }

> FALLBACK: SHA-512 hashing is used by default.

#### 2.3.5.4 Actor Resolver

You can provide information about the current user interacting with the Registration System. This can be accomplished by providing implementation of `IActorResolver` interface.

    public interface IActorResolver
    {
        /// <summary>
        /// Returns name or identifier of actor
        /// </summary>
        string Resolve();
    }

Such actor resolver could be much more advanced than `SimpleActorResolver` provided as a default implementation. In case of web application, actor could be fetched from current web request context.

> FALLBACK: `SimpleActorResolver` with empty actor identifier is used by default.

### 2.3.6 Create Registration Facade

Once dependencies are ready you can create `RegistrationFacade` object:

    RegistrationFacadeSettings registrationFacadeSettings = new RegistrationFacadeSettings()
    {
        PersonDetailsCache = personDetailsCache,                // 2.3.5.2 Person Details Cache
        HashedPersonProcessor = hashedPersonProcessor,          // 2.3.5.3 Hashed Person Processor
        ActorResolver = actorResolver,                          // 2.3.5.4 Actor Resolver
        XsdFilesDirectoryResolver = xsdFilesDirectoryResolver   // Optional, 2.3.5.1 XSD Schemas Directory Resolver
    };

    RegistrationFacade registrationFacade = new RegistrationFacade(
        connectIdEnvironment,        // 2.3.1 Connect ID environment
        clientCredentials,           // 2.3.2 Client credentials
        privateKeyStorage,           // 2.3.3 Private key storage
        logger,                      // 2.3.4 Logger
        registrationFacadeSettings); // 2.3.5 Registration Facade settings

## 2.4 Configure listener

After creating facade, you can set up the background listener, which will listen and process new messages received from Connect Service Bus. This is the key element that every Registration System needs to implement.

### 2.4.1 Person Details Provider

When `BackgroundListener` receives a message with a request for person's details it will call a method `GetPersonDetails` on `PersonDetailsProvider` passing FIFA ID of a person.

Therefore you have to provide implementation of `IPersonDetailsProvider` that makes a lookup (e.g. in your database) and returns a valid `PersonLocal` XML from FIFA Data Standard.

Chapter 3.1 contains a guide on `PersonLocal` creation and serialization.

If for any reason record for a requested person is not found or cannot be returned, `IPersonDetailsProvider` implementation should return `null` or empty string.

    public interface IPersonDetailsProvider
    {
        /// <summary>
        /// Gets detailed information about person with given request.
        /// </summary>
        string GetPersonDetails(PersonDetailsRequest request);
    }

XML below is an example of data that should be returned by `PersonDetailsProvider`

    <?xml version="1.0" encoding="UTF-8"?>
    <PersonLocal xmlns="http://fifa.com/fc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xmime="http://www.w3.org/2005/05/xmlmime" PersonFIFAId="105C3Z1" LocalFirstName="ΑΓΓΕΛΗΣ" LocalLastName="ΧΑΡΑΛΑΜΠΟΥΣ" LocalLanguage="gre" LocalCountry="CY" LocalSystemMAId="1234567" InternationalFirstName="ANGELIS" InternationalLastName="CHARALAMBOUS" Gender="male" DateOfBirth="1989-05-31" PlaceOfBirth="Larnaca" RegionOfBirth="Larnaca" CountryOfBirth="CY" Nationality="CY">
        <NationalIdentifier Identifier="GHKJKLD123132132" NationalIdentifierNature="PassportNumber" Country="CY" DateFrom="2010-08-13" DateTo="2020-08-13"/>
        <PlayerRegistration PersonFIFAId="105C3Z1" Status="active" OrganisationFIFAId="105C40I" RegistrationValidFrom="2003-04-11" RegistrationValidTo="2009-06-15" Discipline="Football" Level="amateur" RegistrationNature="Registration"/>
        <PlayerRegistration PersonFIFAId="105C3Z1" Status="active" OrganisationFIFAId="105C40I" RegistrationValidFrom="2009-06-16" Discipline="Football" Level="pro" RegistrationNature="Registration"/>
        <TeamOfficialRegistration PersonFIFAId="105C3Z1" Status="active" OrganisationFIFAId="105C40I" RegistrationValidFrom="2012-08-13" RegistrationValidTo="2014-08-13" Discipline="Futsal" TeamOfficialRole="Coach"/>
    </PersonLocal>

### 2.4.2 Message handlers

You can provide additional message handlers in order to set up additional Service Bus Listener capabilities. Each message handler implements `IMessageHandler` interface.

    public interface IMessageHandler
    {
        /// <summary>
        /// Checks if handler can handle message of a given message type
        /// </summary>
        /// <param name="messageType">message type of the message to handle</param>
        /// <returns>true if message type is supported by handler, otherwise false</returns>
        bool CanHandle(string messageType);

        /// <summary>
        /// Runs internal handler logic
        /// </summary>
        /// <param name="messageType">message type of the message</param>
        /// <param name="message">content of the message</param>
        void Handle(string messageType, Message message);
    }

SDK contain few predefined message handlers:

- Custom PersonLocal message handlers
- TMS message handlers
- Notifications handlers

Further chapters will cover their usage, by assuming all of them are added to `messageHandlers` collection, before creating the listener.

    List<IMessageHandler> messageHandlers = new List<IMessageHandler>();

### 2.4.3 Clean job

When configuring the listener, an implementation of `ICacheCleanJob` can be provided, that will run alongside the listener and take care of the removal of old messages.

    public interface ICacheCleanJob
    {
        /// <summary>
        /// Starts a job responsible for removal of old messages in the storage
        /// </summary>
        /// <param name="token">Token used for shutting the job down</param>
        /// <returns>Job's task</returns>
        Task RunAsync(CancellationToken token);
    }

You can use the default implementation provided by the SDK (`PersonDetailsCacheCleanJob`) or create your own.

    IPersonDetailsCache personDetailsCache = registrationFacadeSettings.PersonDetailsCache; // 2.3.5.2 Person Details Cache
    PersonDetailsCacheCleanJobSettings cleanJobSettings = new PersonDetailsCacheCleanJobSettings(personDetailsCache);

    PersonDetailsCacheCleanJob cleanJob = new PersonDetailsCacheCleanJob(cleanJobSettings); /* or YourOwnCleanJob */

### 2.4.4 Create and run listener

In order to create `BackgroundListener` instance use previously bootstrapped `RegistrationFacade`:

    BackgroundListener listener = registrationFacade.ConfigureListener(
        personDetailsProvider,  // 2.4.1 Person Details Provider
        messageHandlers,        // 2.4.2 Message handlers
        cleanJob);              // 2.4.3 Clean job

To start listening for messages and handling them run the following method:

    CancellationToken cancellationToken = CancellationToken.None; // CancellationToken of choice can be provided
    await listener.RunAsync(cancellationToken);

> IMPORTANT NOTE: Only one listener shall be running at all times.

### 2.4.5 [Optional] Subscribe to other events from Connect Service Bus

Additionally an instance of the `BackgroundListener` class exposes the following events:

- `PersonDetailsRequestReceived`
- `PersonDetailsReceived`
- `GenericMessageReceived`
- `ServiceBusExceptionEvent`

These events are automatically raised by the listener. You can use them to register your own event handlers and provide additional logic for handling received messages.

    listener.PersonDetailsReceived += details =>
    {
        // do something with those details
    };

    listener.GenericMessageReceived += message =>
    {
        byte[] bytes = message.Content;
        string actionName = message.Action;
    };

### 2.4.6 Retries and dead-letter queue

If a message received from Connect Service Bus is processed properly by all registered handlers (no exceptions are thrown), then the message is marked as processed and deleted from the Connect Service Bus queue.

In case any handler throws an exception, a message will not be deleted from the Connect Service Bus but will be scheduled for later processing. `BackgroundListener` will retry to process this message.

After a series of unsuccessful handles, a message will be moved to a dead messages queue, which will be monitored by FIFA Connect ID Helpdesk.

### 2.4.7 [Optional] Best practices for hosting Listener as Azure WebJob

- How to use an SSL certificate in your application code in Azure App Service
- Use settings.job file with { "is_in_place": true} setting
- In Application Settings set `Always On` setting to `On`
- If you use Entity Framework remember to Initialize EF Context after Listener restart
- Remember about `IPersonDetailsCache` implementation from 2.3.5.2 Person Details Cache point in documentation

## 2.5 Using FIFA Connect ID Client

### 2.5.1 Get client via Registration Facade

Note that in most cases you would need to use Registration Facade instance, not the client. It is, however possible to get FIFA Connect ID Client out of facade as shown in below example.

    IFifaConnectIdClient fifaConnectIdClient = registrationFacade.ConnectIdClient;   // 2.3 Using Registration Facade

### 2.5.2 Create instance

You can also create a standalone `FifaConnectIdClient` by injecting its dependencies into public constructor:

    FifaConnectIdClient fifaConnectIdClient = new FifaConnectIdClient(
        connectIdEnvironment, // 2.3.1 Connect ID environment
        clientCredentials,    // 2.3.2 Client credentials
        logger);              // 2.3.4 Logger

# 3. Person

The first section 3.1 guides through working with PersonLocal, introducing XML serialization and validation. These are particularly useful in `IPersonDetailsProvider`, when responding the other organisation with person details via Service Bus.

Current version of the SDK supports two approaches to operations on Person entity:

- using Registration Facade to register/update/find a person and search for duplicates at the same time. Due to personal data protection regulation, providing details of persons from other Registration Systems requires communication with Hashing Service and Connect Service Bus. Registration Facade hides this complexity behind a single method.
- using FIFA Connect ID Client for other scenarios (including force registration or registrations management).

Two following sections describe implementation steps which are necessary to cover both scenarios:

- Chapter 3.2 - Person registration/update/search together with a duplicate check
- Chapter 3.3 - operations on Person entity not requiring a duplicate check

The last section 3.4 covers rarely used custom message type person details sharing via Service Bus.

## 3.1 Working with PersonLocal

### 3.1.1 Create PersonLocal object

Below example shows how to create `PersonLocal` object with required and optional fields:

    // required fields
    PersonLocal personLocal = new PersonLocal(
        "ΧΑΡΑΛΑΜΠΟΥΣ", Gender.Male, ISO3166CountryCode.CY,
        new DateTime(1990, 11, 11), ISO316613CountryCode.CY, "Larnaca",
        ISO6392LanguageCode.Gre, ISO3166CountryCode.CY, "105C3Z1")
    {
        // optional fields
        LocalFirstName = "ΑΓΓΕΛΗΣ",
        LocalSystemMAId = "1234567",
        InternationalFirstName = "ANGELIS",
        InternationalLastName = "CHARALAMBOUS",
        RegionOfBirth = "Larnaca",
        SecondNationality = ISO3166CountryCode.GR,
        PopularName = "Angelis Angeli"
    };

`PersonLocal` created this way contains general information of the person whose details will be sent to other Registration System. Another business context are registrations. Each registration type can be included in XML file by adding new elements to lists by following methods:

- `AddPlayerRegistration()`
- `AddTeamOfficialRegistration()`
- `AddMatchOfficialRegistration()`
- `AddOrganisationOfficialRegistration()`

As all types are very similar to each other (as all extend `Registration` class), let's focus on how to set up `PlayerRegistration` and link it with `PersonLocal` object:

    // PersonDetailsRequest is available in GetPersonDetails() method of IPersonDetailsProvider interface
    PersonDetailsRequest request = new PersonDetailsRequest("replyTo", "fifaId", "correlationId", null);
    string personFifaId = request.UniqueFifaId;
    string clubOrMemberAssociationId = "105C40I";

    // creating an instance of the object
    PersonLocal personLocal = new PersonLocal("ΧΑΡΑΛΑΜΠΟΥΣ", Gender.Male, ISO3166CountryCode.CY,
        new DateTime(1990, 11, 11), ISO316613CountryCode.CY, "Larnaca", ISO6392LanguageCode.Gre,
        ISO3166CountryCode.CY, personFifaId);

    // create an instance of player registration
    PlayerRegistration playerRegistration = new PlayerRegistration(
        personFifaId, Status.Active, clubOrMemberAssociationId, new DateTime(2003, 4, 11),
        RegistrationLevel.Amateur, Discipline.Football, PlayerRegistrationNature.Registration)
    {
        // optional fields
        RegistrationValidTo = new DateTime(2006, 6, 15),
        ClubTrainingCategory = ClubTrainingCategory.Category3
    };

    // include player registration in PersonLocal
    personLocal.AddPlayerRegistration(playerRegistration);

Another important field, which can be set in `PersonLocal` in similar way as registrations, is `Photo`. This property itself is not required, but when using it, a mandatory instance of `PictureEmbedded` class needs to be passed as a constructor argument. SDK gives two possibilities when preparing `PictureEmbedded` object: using byte[] array (`new PictureEmbedded(byte[] image)`) or using path to local file (`new PictureEmbedded(string localPath)`). Below example shows what's the approach here:

    // creating an instance of the object
    PersonLocal personLocal = new PersonLocal("ΧΑΡΑΛΑΜΠΟΥΣ", Gender.Male, ISO3166CountryCode.CY,
        new DateTime(1990, 11, 11), ISO316613CountryCode.CY, "Larnaca", ISO6392LanguageCode.Gre,
        ISO3166CountryCode.CY, "105C3Z1");

    // creating and instance of the picture by absolute path
    string absolutePathToPhoto = @"C:\image.png";
    PictureEmbedded picture = new PictureEmbedded(absolutePathToPhoto);

    // include photo in PersonLocal
    personLocal.Photo = new Photo(picture);

SDK supports following image formats: jpg, png, gif, tif, bmp. Please mind that the maximum size of a person details message sent through Service Bus is approximately 10MB. You might want to ensure that the size of the photo does not make the entire PersonLocal object exceed that limit. A reasonable boundary for a PNG file is 8MB.

`NationalIdentifier` and `LocalPersonName` are both optional in `PersonLocal` scope and there is no difference in rules with setting their properties comparing to other fields. See example of `NationalIdentifier` below:

    // creating an instance of the object
    PersonLocal personLocal = new PersonLocal("ΧΑΡΑΛΑΜΠΟΥΣ", Gender.Male, ISO3166CountryCode.CY,
        new DateTime(1990, 11, 11), ISO316613CountryCode.CY, "Larnaca", ISO6392LanguageCode.Gre,
        ISO3166CountryCode.CY, "105C3Z1");

    // create an instance of player registration
    NationalIdentifier nationalIdentifier = new NationalIdentifier("9876543210", NationalIdentifierNature.PassportNumber, ISO3166CountryCode.CY)
    {
        // optional fields
        Description = "NationalIdentifier test description",
        DateFrom = new DateTime(2003, 4, 11),
        DateTo = new DateTime(2004, 6, 1)
    };

    // include national identifier in PersonLocal
    personLocal.AddNationalIdentifier(nationalIdentifier);

### 3.1.2 Serialize XML document

SDK doesn't restrict any method of generating string with XML content. Recommended and by far the easiest method, however, is using `PersonLocalXmlSerializer` class, that we provided in SDK.

    PersonLocalXmlSerializer personLocalXmlSerializer = new PersonLocalXmlSerializer(xsdSchemasDirectoryResolver);   // Optional, 2.3.5.1 XSD Schemas Directory Resolver

> IMPORTANT NOTE: The SDK does XML validation using an XSD schema. To learn more about correct setup, please refer to part 2.3.5.1 XSD Schemas Directory Resolver.

After `PersonLocal` object is created, you need to pass it to `Serialize()` method of `PersonLocalXmlSerializer` service. Under the hood, this method creates XML string and executes validation against FIFA Data Standard. In case validation fails, `XmlSerializationException` will be thrown giving a message with all possible issues that were found during validation.

    // creating an instance of the object
    PersonLocal personLocal = new PersonLocal("ΧΑΡΑΛΑΜΠΟΥΣ", Gender.Male, ISO3166CountryCode.CY,
        new DateTime(1990, 11, 11), ISO316613CountryCode.CY, "Larnaca", ISO6392LanguageCode.Gre,
        ISO3166CountryCode.CY, "105C3Z1");

    string xmlString = null;
    try
    {
        xmlString = personLocalXmlSerializer.Serialize(personLocal);
    }
    catch (XmlSerializationException e)
    {
        IEnumerable<XmlValidationError> validationErrors = e.XmlValidationErrors;
        // handle serialization exception
    }

As all of this happens in background listener, you should implement a proper error handling when catching the exception, so to get notified in case when XML response was not generated. Note that in case of exception thrown, Registration System that was asking for person details, won't receive a correct answer, so it's important to keep data structure valid and try to limit number of exceptions.

### 3.1.3 Deserialize XML with person details

When running operations that could possibly return person’s duplicates (such as registering persons, updating persons, etc.), methods will return responses with list of `PersonDuplicateWithDetails`. `PersonDetails` that have been received here contains an XML content, which is available in `XmlData` property. At this point application can store all details in local database (or handle them in a different way - depending on a need), so that it is possible to compare person being processed with their potential duplicates.

Optionally, after receiving XML string from `XmlData`, you may parse it into object. This can be done with a custom implementation, however the suggested solution here is to use `PersonLocalXmlSerializer` class (provided by our SDK) and its `Deserialize()` method returning `PersonLocal` object. Take a look at the following example:

    try
    {
        PersonLocal personLocal = personLocalXmlSerializer.Deserialize(personDuplicateWithDetails.PersonDetails.XmlData);
    }
    catch (XmlDeserializationException ex)
    {
        string xmlContent = ex.Xml;
        Exception innerException = ex.InnerException;
    }

### 3.1.4 [Optional] XML validation

As `PersonLocalXmlSerializer` includes XML validation, there is no need of running additional validation over it. But, in case of internal unit tests or when instead of our helper tool, custom solution produces XML content, it is possible to check whether output is compliant with FIFA Data Standard. To achieve that, use `XsdSchemaValidator` as on following example:

    // invalid xml string
    string xmlString = "xmlString";
    XsdSchemaValidator xsdSchemaValidator = new XsdSchemaValidator(xsdSchemasDirectoryResolver);   // Optional, 2.3.5.1 XSD Schemas Directory Resolver
    XmlValidationResult validationResult = xsdSchemaValidator.Validate(xmlString);

    if (!validationResult.IsValid)
    {
        // handle validation errors
        List<XmlValidationError> validationErrors = validationResult.ValidationFailures;
    }

## 3.2 Working with Registration Facade

The `RegistrationFacade` class covers full scenario of registering a person in FIFA Connect ID. If potential duplicates are found, Registration Facade will send requests for person details to other Registration System using Connect Service Bus and wait for their response (with timeout). The same logic needs to apply when one tries to update Person details or search for duplicates but does not intend to register a person even if no duplicates are found.

### 3.2.1 Register new person

First create a person you want to register:

    string organisationId = "id";

    // setup player name(s)
    List<PersonName> personNames = new List<PersonName>
    {
        new PersonName("John", "Doe")
    };

    // setup birthdate
    DateTime dateOfBirth = new DateTime(1990, 10, 12);
    PersonType person = new PersonType
    {
        DateOfBirth = dateOfBirth,
        Gender = "female"
    };

    // setup registrations
    PlayerRegistrationType registration = new PlayerRegistrationType
    {
        OrganisationFIFAId = organisationId,
        Status = "active",
        Level = "Pro",
        Discipline = "Football",
        RegistrationNature = "Registration"
    };

    person.PlayerRegistrations = new List<PlayerRegistrationType>
    {
        registration
    };

    // finally create PersonData
    PersonData personData = new PersonData(personNames, person);

Then try to register a person using Registration Facade. The code snippet below shows an example handling (writing to console) of both scenarios, i.e. successful registration and potential duplicates found.

    /* A record identifier in your database or any other key that is unique for a person.
    This data will be stored hashed and cannot be decrypted.
    For further information please consult section 3.2.1.1 Idempotency */
    string personLocalId = "";
    TimeSpan timeout = TimeSpan.FromSeconds(10);

    RegistrationResult result = await registrationFacade.RegisterPersonAndWaitForDetailsInCaseOfDuplicates(personData, timeout, personLocalId);

    if (result.IsSuccessful)
    {
        Console.WriteLine($"Person successfully registered with unique FIFA ID: {result.UniqueFifaId}.");
    }
    else
    {
        Console.WriteLine("Duplicates found.");

        foreach (PersonDuplicateWithDetails duplicateWithDetails in result.PersonDuplicateWithDetails)
        {
            string idOfDuplicatedPerson = duplicateWithDetails.Duplicate.PersonFifaId;
            double proximityScore = duplicateWithDetails.Duplicate.ProximityScore;
            string dataReceived = duplicateWithDetails.PersonDetails?.XmlData ?? "<detailed person data not received from other Registration System - timeout>";

            Console.WriteLine($"FIFA ID: {idOfDuplicatedPerson}, score: {proximityScore}, FIFA Data XML: {dataReceived}");
        }
    }

Service Bus listener must be up and running to receive person details. There may be cases where a person has no registrations, but still appears on the list of duplicates as FIFA Data Standard does not require any registrations to construct a valid object. Connect ID stores computed hashes, but has no way of knowing which Registration System to ask for the details, so as a result null XML will be returned. In addition to that, for every potential duplicate there will be a hash score computed, ranging from 0 (no resemblance) to 1 - exact match. Your system should be prepared to handle duplicates, considering both the possibility of receiving incomplete data and the value of the hash score.

##### 3.2.1.1 Idempotency

Idempotency ensures that the same record is saved only once even when multiple registration requests are sent. To ensure that the same record is not registered in the system twice we recommend to pass person local identifier when registering person (Person local id can be a record identifier in your database or any other key that is unique for a person). Local identifier will be useful in case of retries on the client side (e.g. due to network issues or occasional client time-outs). It is sent and stored on the server in a hashed form (SHA-512 is used by default by SDK) and cannot be decrypted.

If you choose not to take advantage of the idempotency mechanism, you should expect that in rare situations FIFA_ID will be created on/by the server but you will not receive it on the client. Should this happen, the next time you try to register the same person, you will receive a duplicate pointing to your Registration System, but without any details (as your listener will not be able to provide details for the person which FIFA_ID you have not received).

The only thing that you need to do to start using idempotency mechanism is to pass additional parameter (local person identifier) to `ForceRegisterPerson` or `RegisterPersonAndWaitForDetailsInCaseOfDuplicates` methods on `RegistrationFacade`. Based on the method that is used the system behaves as follows:

    string personLocalIdentifier = "Person Local Id";
    RegistrationResult result = await registrationFacade.RegisterPersonAndWaitForDetailsInCaseOfDuplicates(personData, TimeSpan.FromSeconds(10), personLocalIdentifier);

In the example above, the `RegisterPersonAndWaitForDetailsInCaseOfDuplicates` will check if there is no other registration with the same person local identifier. If the registration with the same person local identifier exists, it will be returned as a result. There will be no second registration nor the update of an existing one. If there is no registration with the same person local identifier, the method will run the default logic of searching for duplicates and registering a new person. Person local identifier will be stored on the server in the hashed form for the newly created record.

Please note, that the use of the `ForceRegisterPerson` does not change the behaviour, i.e. a new person will not be created if the same person local identifier is used.

#### 3.2.1.2 Handling multiple names (local, international and maiden/birth names)

SDK provides support for registering a person with multiple names. Two scenarios will usually require such a feature:

- registering a person for which local spelling differs from international spelling (e.g. from a non-Latin alphabet country)
- registering a person with both last name and maiden name

Support for such a scenario depends on the way person registration is used.

Before registering a person, you need to create a list of pairs of names (`List<PersonName>`) . The following example shows the most sophisticated scenario when a single person has both: local name, international name and maiden name provided. Although two pairs of names will be used in minority of cases and three pairs hardly ever, the example is instructive.

    string internationalFirstName = "Olga";
    string internationalLastName = "Terekhova";
    string localFirstName = "Ольга";
    string localLastName = "Терехова";
    string localMaidenName = "Бессолова";

    PersonName personInternationalName = new PersonName(internationalFirstName, internationalLastName);
    PersonName personLocalName = new PersonName(localFirstName, localLastName);
    PersonName personMaidenName = new PersonName(localFirstName, localMaidenName);

    // setup player name(s)
    List<PersonName> personNames = new List<PersonName>();

    personNames.Add(personInternationalName);
    personNames.Add(personLocalName);
    personNames.Add(personMaidenName);

#### 3.2.1.3 Troubleshooting

Please keep in mind that, especially in the Beta environment, some of the listeners of other Registration Systems might not be working. The method waits a specified period of time for the details to arrive and returns without them afterwards.

If you are certain that both your and the other Registration System's listener are up and running and you still do not receive person details, make sure that a valid implementation of `IPersonDetailsCache` is used. More on the subject can be found in the chapter on Person details cache.

Detailed explanation of handling aforementioned issues can be found under this FAQ article.

### 3.2.2 Update person

First create two persons and register them:

    string organisationId = "id";

    // setup player name(s)
    List<PersonName> person1Names = new List<PersonName>
    {
        new PersonName("John", "Doe")
    };
    List<PersonName> person2Names = new List<PersonName>
    {
        new PersonName("John", "Kowalsky")
    };

    // setup birthdate
    DateTime dateOfBirth = new DateTime(1990, 10, 12);
    PersonType person = new PersonType
    {
        DateOfBirth = dateOfBirth,
        Gender = "male"
    };

    // setup registrations
    PlayerRegistrationType registration = new PlayerRegistrationType
    {
        OrganisationFIFAId = organisationId,
        Status = "active",
        Level = "Pro",
        Discipline = "Football",
        RegistrationNature = "Registration"
    };

    person.PlayerRegistrations = new List<PlayerRegistrationType>
    {
        registration
    };

    // finally create PersonData
    PersonData person1Data = new PersonData(person1Names, person);
    PersonData person2Data = new PersonData(person2Names, person);

    //register two different players
    registrationFacade.ForceRegisterPerson(person1Data);
    registrationFacade.ForceRegisterPerson(person2Data);

Then try to update second person using Registration Facade with names of first person. The code snippet below shows an example handling (writing to console) of both scenarios, i.e. successful update and potential duplicates found. Please note that this operation will throw an `InvalidClientDataException` if you lack the permissions to perform it (the player has active registrations, none of which belongs to your Registration System).

    UpdateResult result = await registrationFacade.UpdatePersonAndWaitForDetailsInCaseOfDuplicates(personId, TimeSpan.FromSeconds(10), names, dateOfBirth, gender);

    if (result.IsSuccessful)
    {
        Console.WriteLine($"Person updated successfully.");
    }
    else
    {
        Console.WriteLine("Duplicates found.");

        foreach (PersonDuplicateWithDetails duplicateWithDetails in result.PersonDuplicateWithDetails)
        {
            string idOfDuplicatedPerson = duplicateWithDetails.Duplicate.PersonFifaId;
            double proximityScore = duplicateWithDetails.Duplicate.ProximityScore;
            string dataReceived = duplicateWithDetails.PersonDetails?.XmlData ?? "<detailed person data not received from other Registration System - timeout>";

            Console.WriteLine($"FIFA ID: {idOfDuplicatedPerson}, score: {proximityScore}, FIFA Data XML: {dataReceived}");
        }
    }

### 3.2.3 [Optional] Get person duplicates (without registering)

If you need to get list of duplicates for a person, method `GetDuplicates` can be used. This methods looks for duplicates in Connect ID service. If any duplicate is found requests for person details are sent to relevant Registration Systems. It is possible to look for duplicates only within specific organisations by providing `organisationsIds` argument.

    IReadOnlyCollection<PersonDuplicateWithDetails> foundDuplicates = await registrationFacade.GetDuplicates(personData, TimeSpan.FromSeconds(10));

    if (foundDuplicates.Count > 0)
    {
        Console.WriteLine("Duplicates found.");

        foreach (PersonDuplicateWithDetails duplicateWithDetails in foundDuplicates)
        {
            string idOfDuplicatedPerson = duplicateWithDetails.Duplicate.PersonFifaId;
            double proximityScore = duplicateWithDetails.Duplicate.ProximityScore;
            string dataReceived = duplicateWithDetails.PersonDetails?.XmlData ?? "<detailed person data not received from other Registration System - timeout>";

            Console.WriteLine($"FIFA ID: {idOfDuplicatedPerson}, score: {proximityScore}, FIFA Data XML: {dataReceived}");
        }
    }

#### 3.2.3.1 [Optional] Get person duplicates by FIFA identifier

To get the list of duplicates for a registered person, use method `GetDuplicatesExceptTrustedKnownDuplicates`. To ignore duplicates marked as known duplicate by trusted organisations, please provide optional `trustedOrganisationsIds` argument.

    const string personFifaId = "105C40T";
    DuplicatesOfRegisteredPerson foundDuplicates = await registrationFacade.GetDuplicatesExceptTrustedKnownDuplicates(personFifaId, TimeSpan.FromSeconds(10));

### 3.2.4 Known limitations of Registration Facade

#### Available operations

Registration Facade covers scenario of person registration/update/search, but for other functionalities FIFA Connect ID Client described in the next chapter must be used.

In particular FIFA Connect ID Client must be used for:

- force registration of a person
- getting person data from FIFA Connect ID Directory
- all operations on organisation and facility

#### Concurrent requests

Currently there is no request throttling in API, but it's recommended not to exceed 10 concurrent connections. Too many requests to API may result in requests time outs.

In case you plan to do a batch import of person records, please contact support team so that the infrastructure can be scaled up accordingly.

## 3.3 Working with FIFA Connect ID Client

This chapter discusses all operations on a Person entity, e.g. Registration, Update, Merge, etc. It is different from chapter 3.2 Working with Registration Facade in that operations described here do not involve a duplicate check. A developer who wants to perform a duplicate check via Service Bus should either refer to chapter 3.2 or implement communication via Service Bus by themselves.

### 3.3.1 Registering a person

In order to register a person in FIFA Connect ID service, provide a `PersonData` object. Here is an example, that registers a person:

    PersonName[] names = new[]
    {
        new PersonName("ΑΓΓΕΛΗΣ", "ΧΑΡΑΛΑΜΠΟΥΣ"), // local name
        new PersonName("ANGELIS", "CHARALAMBOUS") // international name
    };

    PersonType person = new PersonType
    {
        DateOfBirth = new DateTime(1980, 10, 10),
        // fill up registrations
    };

    PersonData personData = new PersonData(names, person);

    /* A record identifier in your database or any other key that is unique for a person.
    This data will be stored hashed and cannot be decrypted.
    For further information please consult section 3.2.1.1 Idempotency */
    string personLocalId = "";

    try
    {
        string uniqueFifaId = await fifaConnectIdClient.RegisterPersonAsync(personData, personLocalId);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaDuplicatedPersonFoundException ex)
    {
        // possible duplicates were found
        IList<PersonDuplicateType> duplicates = ex.GetDuplicatesResponseType.Duplicates;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some other error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

#### 3.3.1.1 Person bulk registration

In order to register persons in parallel you can use Task Parallel Library. Please note that in order to optimize costs of test environment we keep it small-scale. Let us know upfront if you plan to test bulk registration, so we can scale up the environment. Additionally we recommend not exceeding 20 concurrent registrations to avoid performance degradation of the service. Take a look at the following example:

    LocalDatabase database = new LocalDatabase();
    IQueryable<Player> players = database.Players.Where(x => x.FifaId == null);

    players
        .AsParallel()
        .WithDegreeOfParallelism(10)
        .ForAll(player =>
        {
            PersonData personData = CreatePerson(player);
            string fifaId = fifaConnectIdClient.RegisterPersonAsync(personData).Result;

            database.UpdateFifaId(player.Id, fifaId);
        });

### 3.3.2 Force registration

To force the registration process (proceed even if potential duplicates are found):

    /* A record identifier in your database or any other key that is unique for a person.
    This data will be stored hashed and cannot be decrypted.
    For further information please consult section 3.2.1.1 Idempotency */
    string personLocalId = "";

    try
    {
        string uniqueFifaId = await fifaConnectIdClient.ForceRegisterPersonAsync(personData, personLocalId);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some other error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

### 3.3.3 Getting person data

This method will throw `FifaPersonMergedException` when you try to get data for a person that has been previously merged (refer to section 3.3.4 Merging/unmerging persons). The exception will contain data about requested person together with primary person id. To get the information associated with the specified FIFA ID such as a date of birth or list of person's registrations:

    const string uniqueFifaId = "BVGE8T6";

    try
    {
        PersonType person = await fifaConnectIdClient.GetPersonAsync(uniqueFifaId);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find the person
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (FifaEntityDeletedException ex)
    {
        // person has been deleted
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (FifaPersonMergedException ex)
    {
        // requested person has been merged
        PersonType person = ex.RequestedEntity;
        string primaryPersonId = ex.PrimaryEntityId;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

### 3.3.4 Merging/unmerging persons

To merge two persons into a single one use `MergePersonsAsync` method available on `FifaConnectIdClient`. As a result, the primary person data will be kept. No more operations should be executed on the secondary person. Attempt to get secondary person's data will result in 301 error code. Please refer to section 3.3.3 Getting person data. This operation can end with conflict error under specific conditions. It can happen when previous merge/unmerge operation is not yet completed on the same persons. As a result `FifaConflictException` will be thrown and client should retry operation later.

Here is an example how to merge persons:

    const string primaryPersonId = "";
    const string secondaryPersonId = "";

    try
    {
        PersonType person = await fifaConnectIdClient.MergePersonsAsync(primaryPersonId, secondaryPersonId);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaConflictException ex)
    {
        // a conflicting operation is currently in progress
        ConflictResponseType details = ex.Response;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find one of specified persons
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

In order to revert merge operation call `UnmergePersonsAsync` method with the same arguments.

#### 3.3.4.1 Getting merged secondary FIFA IDs for person

To retrieve all merged secondary FIFA IDs for a specific person, use the `GetMergedSecondaryFifaIdsForPerson` method provided by the `FifaConnectIdClient`.

Note that only persons directly merged into the provided FIFA ID will be considered (those that can be unmerged).

    const string personFifaId = "";

    try
    {
        IReadOnlyCollection<string> personSecondaryFifaIds = await fifaConnectIdClient.GetMergedSecondaryFifaIdsForPerson(personFifaId);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find one of specified persons
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (FifaEntityDeletedException ex)
    {
        // person has been deleted
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

### 3.3.5 Getting duplicates

To get a list of people a person may be duplicated with:

    try
    {
        IEnumerable<PersonDuplicateType> foundDuplicates = await fifaConnectIdClient.GetDuplicatesAsync(personData);

        foreach (PersonDuplicateType duplicate in foundDuplicates)
        {
            string idOfDuplicatedPerson = duplicate.PersonFifaId;
            double proximityScore = duplicate.ProximityScore;
            DateTime registrationDate = duplicate.PersonRegistrationContext.RegistrationDate;
        }
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

This method is expected to be used rarely, e.g. in cases when Registration System chooses to do a duplicate check without the registration.

It is possible to look for duplicates only within specific organisations by providing `organisationsIds` argument.

#### 3.3.5.1 Getting duplicates by FIFA identifier

To get a list of people a registered person may be duplicated with:

    const string personFifaId = "105C408";

    try
    {
        GetDuplicatesOfRegisteredPersonResponseType response = await fifaConnectIdClient.GetDuplicatesExceptTrustedKnownDuplicates(personFifaId);

        foreach (PersonDuplicateType duplicate in response.Duplicates)
        {
            string idOfDuplicatedPerson = duplicate.PersonFifaId;
            double proximityScore = duplicate.ProximityScore;
            DateTime registrationDate = duplicate.PersonRegistrationContext.RegistrationDate;
        }
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

To ignore known duplicates registered by specific organisations, please provide optional `trustedOrganisationsIds` argument.

### 3.3.6 Add/Update registrations

It is possible to update an existing person record (provided that we know the person's FIFA ID) by adding or editing its registrations. In order to do that, use the class `AddRegistrationsRequestType` or `UpdateRegistrationsRequestType` (depending on the operation) and fill information about the registrations.

Note, that each person always contains four collections of registrations:

- player registrations
- match official registrations
- team official registrations
- organisation official registrations

In the examples below, only player registrations are used - the other 3 collections remain empty (they are not set).

> IMPORTANT NOTE: Operation that results in Member Association (OrganisationFifaId) being changed for a given registration constitutes a transfer in Connect ID. Only one transfer at the same time is allowed. If you want to perform multiple transfers, please call `AddRegistrations` or `UpdateRegistrations` methods separately for each transfer.

#### 3.3.6.1 Add registrations

Note that in the example below a registration is added in a National Association and not in a Club. This is not a coincidence - FIFA Connect ID Service, although allowed to store Clubs in general, is not allowed to store persons' assignments to Clubs. Server will return an error should the Client try to register a person within a Club.

    const string personId = "105C408";
    const string organisationId = "105KYBB";

    PlayerRegistrationType exampleRegistration = new PlayerRegistrationType()
    {
        Level = "Pro",
        Discipline = "Football",
        RegistrationNature = "Registration",
        OrganisationFIFAId = organisationId,
        Status = "Active"
    };

    PersonRegistrationsType personRegistrations = new PersonRegistrationsType();
    personRegistrations.PlayerRegistrations = new List<PlayerRegistrationType>();
    personRegistrations.PlayerRegistrations.Add(exampleRegistration);

    AddRegistrationsRequestType request = new AddRegistrationsRequestType(personId, personRegistrations);

    try
    {
        PersonType personType = await fifaConnectIdClient.AddRegistrations(request, false);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find the person
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

#### 3.3.6.2 Update registrations

In order to execute a request we need to know the registrations we want to update and their new values. Note that the order of registrations is important. Operation will try to change the first item in "old" list with the first item in the "new" list, etc.

"Old" list should contain only those registrations, which you want to update. For example, when person has four registrations and you want to update only two of them, you just need to provide these two.

    const string personFifaId = "105C408";
    const string organisationId = "105KYBB";

    PersonType person = await fifaConnectIdClient.GetPersonAsync(personFifaId);

    PersonRegistrationsType oldPersonRegistrations = new PersonRegistrationsType();
    oldPersonRegistrations.PlayerRegistrations = person.PlayerRegistrations;

    PlayerRegistrationType newPlayerRegistration = new PlayerRegistrationType()
    {
        Level = "Pro",
        Discipline = "Football",
        RegistrationNature = "Registration",
        Status = "Active",
        OrganisationFIFAId = organisationId
    };

    PersonRegistrationsType newPersonRegistrations = new PersonRegistrationsType();
    newPersonRegistrations.PlayerRegistrations.Add(newPlayerRegistration);

    UpdateRegistrationsRequestType request = new UpdateRegistrationsRequestType(personFifaId,
        oldPersonRegistrations,
        newPersonRegistrations
    );

    try
    {
        PersonType personType = await fifaConnectIdClient.UpdateRegistrations(request, false);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid or the Registration System lacks permissions to perform the operation
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find the person
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

Please note that each time an operation succeed, you will receive an object of class `PersonType` containing the current status of a person with all their registrations. This feature may be used for troubleshooting but also to synchronize the local data with a central DB (ID Directory).

Also note that errors are not thrown if you perform an operation which would result in the object not changing its state. For example, if you want to add a registration for a Football Player, Amateur in France, and such a registration already exists, error will not be thrown and the operation will succeed.

#### 3.3.6.3 Remove registrations

We do not provide SDK method for removing registrations. Correct operation here would be updating registration by setting its status to inactive (deactivation). If you are certain that a registration should be removed (e.g. it has been added by mistake), please contact our Helpdesk.

### 3.3.7 Updating a person

Note, that when updating the name of a person, you need to specify all pairs of names. For example if you originally registered a person with both local and international name, you need to specify both pairs even if you are changing only one name. Connect ID is only able to replace an existing set of names with a new set. The reason for this is personal data protection, which allows the service to store hashes but not names.

To update a person associated with the specified FIFA ID:

    List<PersonName> names = new List<PersonName>();
    names.Add(new PersonName("first name", "last name"));

    try
    {
        PersonType updatedPerson = await fifaConnectIdClient.UpdatePersonAsync("105C40T", names, new DateTime(1995, 01, 01), "male");
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (FifaDuplicatedPersonFoundException ex)
    {
        IList<PersonDuplicateType> duplicates = ex.GetDuplicatesResponseType.Duplicates;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid or the Registration System lacks permission to perform the operation
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find the person
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

### 3.3.8 Adding registrations handled by external system (SystemId)

By default, all registrations are stored in a single national registration system. It is however possible to connect additional system to FIFA Connect ID services. As an example we may consider the [main] national registration system being responsible for Amateur players only and an additional, external system – for Professionals. In such a setup it is important that Connect ID is able to properly route requests for player details sent by other Registration Systems via Service Bus.

In order for this routing to work correctly, an external system, for each of registrations it manages (professional ones), must provide its unique identifier (System ID).

System ID can be set by using `SystemId` property. See below example:

    const string organisationId = "105C8LX";
    const string systemId = "105C8LX_SystemName";

    PlayerRegistrationType newPlayerRegistration = new PlayerRegistrationType()
    {
        Level = "Pro",
        Discipline = "Football",
        RegistrationNature = "Registration",
        Status = "Active",
        OrganisationFIFAId = organisationId,
        SystemId = systemId // The system identifier is created and passed along with the access credentials.
    };

There is no need of setting System ID if registration is added by a default system. In this case the `<null>` value should be used.

### 3.3.9 Bulk deactivating registrations

It is possible to bulk deactivate a set of given registrations using `BulkDeactivateRegistrations` method, which is available in `FifaConnectIdClient` class. As an input method takes a list of person registrations that will be deactivated. Only listed registrations will be deactivated - all other registrations that exist in the context of a given person will be left unchanged. Each registration needs to represent the current state (including status), otherwise server won't recognize it and will return error message that registration doesn't exist. One deactivation request can have up to 500 registrations in total (player, match official, team official and organisation official). Below you can find an example of bulk deactivation usage:

    //create a request based on a list of persons (e.g. taken from your database)
    List<PersonDeactivateType> personsToDeactivate = new List<PersonDeactivateType>();
    foreach (Person person in persons)
    {
        string personId = person.PersonId;
        List<PlayerRegistrationType> playerRegistrations = person.PlayerRegistrations;
        List<TeamOfficialRegistrationType> teamOfficialRegistrations = person.TeamOfficialRegistrations;

        //You can take a subset of registrations that should be deactivated
        List<PlayerRegistrationType> playerRegistrationsToDeactivate = new List<PlayerRegistrationType>
        {
            playerRegistrations[0],
            playerRegistrations[2]
        };

        List<TeamOfficialRegistrationType> teamOfficialRegistrationToDeactivate = new List<TeamOfficialRegistrationType>
        {
            teamOfficialRegistrations[3]
        };

        PersonDeactivateType personToDeactivate = new PersonDeactivateType(person.PersonId,
            new PersonRegistrationsType(
                playerRegistrationsToDeactivate,
                // deactivate all Match Official Registrations
                person.MatchOfficialRegistrations,
                teamOfficialRegistrationToDeactivate,
                //No organisation official registrations will be deactivated
                new List<OrganisationOfficialRegistrationType>()));

        personsToDeactivate.Add(personToDeactivate);
    }

    try
    {
        BulkDeactivateRegistrationsResponseType response = await fifaConnectIdClient.BulkDeactivateRegistrations(new BulkDeactivateRegistrationsRequestType(personsToDeactivate));

        // Get persons for whom at least one registration has failed during deactivation
        List<BulkDeactivateRegistrationsResult> failedRegistrations = response.Results.Where(result => result.Success == false).ToList();
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

`BulkDeactivateRegistrations` method returns `BulkDeactivateRegistrationsResponseType`. It has property `Results`, which is an array of `BulkDeactivateRegistrationsResult`. Each instance of `BulkDeactivateRegistrationsResult` contains information about a status of specific person oriented operation.

Possible properties are: `PersonId`, `Success`, `Exception`. In case deactivation fails for given person, `Success` will be set as `false` and additional information will be provided in `Exception`.

Please note that bulk deactivation is a heavy operation that affects server resources. Before running it please

- make sure that you notified Support Team (support.id@fifa.org), so we could scale up the system
- implement a retry logic in case of timeouts as bulk deactivation operation is idempotent

### 3.3.10 Data holders

Every Registration System that has or used to have a certain person's registration is known to be their DATA HOLDER. If Connect ID Service is aware of all data holders of a given FIFA_ID, it is able to properly build electronic player's passport and send notifications about data changes.

However, in some scenarios newly integrated Registration System may not be automatically registered as a data holder of a person. This will happen e.g. if they try to register an inactive player, they find a duplicate (because that player is active elsewhere) and simply store the FIFA_ID of a duplicate in their Registration System. Connect ID Service will not be aware that Registration System just became a data holder of that person.

Therefore, an additional mechanism of data holding declaration had been introduced. Registration System can now claim to be a data holder of a given FIFA_ID and be included in the data holders list. To claim that your Registration System is person's data holder, `FifaConnectIdClient.RegisterAsDataHolderOfPerson` method should be called.

    const string personFifaId = "105C408";

    try
    {
        await fifaConnectIdClient.RegisterAsDataHolderOfPerson(personFifaId);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaPersonMergedException ex)
    {
        // requested person has been merged
        PersonType person = ex.RequestedEntity;
        string primaryPersonId = ex.PrimaryEntityId;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find the person
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (FifaEntityDeletedException ex)
    {
        // person has been deleted
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

The method is idempotent i.e. can be called multiple times without any negative side-effects. It is important to note that the method only enqueues your registration request. The actual processing happens in the background.

The implications are twofold: 1. The call will return as soon as the request is accepted. 2. It may take some time for the system to process the request

In order to confirm that your request has been processed, you can call another method `FifaConnectIdClient.GetDataHoldersOfPerson(string personFifaId)` which returns the list of systems who are Data Holders of the given person FIFA_ID.

After successful processing, the list should contain your Registration System's ID. However, it is not recommended to call it after every call to `RegisterAsDataHolderOfPerson`.

## 3.4 Sharing Person's details with any system over the Service Bus

You can share the details of any person with another system over the Service Bus. It is important for you to agree on the message type that will be used both by the sender and the recipient.

### 3.4.1 Sending PersonLocal object

You can send a PersonLocal object to any system using an instance of `PersonLocalSender`. Please note that the `messageType` argument has be to agreed with the receiving system so that they can register a handler for it. You can find more information on the subject in our portal.

    XsdSchemasDirectoryResolver xsdFilesResolver = new XsdSchemasDirectoryResolver(@"C:\custom\directory\where\scenarios.xsd\is\located"); // Optional, 2.3.5.1 XSD Schemas Directory Resolver
    PersonLocalSender personLocalSender = new PersonLocalSender(registrationFacade.ServiceBusClient, xsdFilesResolver);

    string recipient = "105C3ZB_CSRS";
    string messageType = "expected-message-type";
    PersonLocal personLocal = new PersonLocal("ΧΑΡΑΛΑΜΠΟΥΣ", Gender.Male, ISO3166CountryCode.CY,
        new DateTime(1990, 11, 11), ISO316613CountryCode.CY, "Larnaca", ISO6392LanguageCode.Gre,
        ISO3166CountryCode.CY, "105C3Z1");

    try
    {
        await personLocalSender.SendAsync(personLocal, recipient, messageType);
    }
    catch (XmlSerializationException ex)
    {
        // handle serialization exception
    }
    catch (ValidationException ex)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (QueueNotFoundException ex)
    {
        // invalid recipient
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

### 3.4.2 Receiving PersonLocal object

In order to receive Person's details sent by another system over the Service Bus (according to section 3.4.1) you need to register an extra message handler for the agreed message type. Our SDK will take care of validation and deserialization of the message.

All you need to provide is an implementation of your own handler that will be invoked when the PersonLocal object is received.

    using Fifa.ConnectId.Sdk.Core.XmlParser.DataStandard;
    using Fifa.ConnectId.Sdk.MessageHandlers;

    public class PersonLocalReceivedHandler : IPersonLocalReceivedHandler
    {
        public void Handle(PersonLocal personLocal, string sender)
        {
            // TODO: implement handle() method.
        }
    }

When configuring the Listener add an extra message handler:

    PersonLocalReceivedHandler personLocalReceivedHandler = new PersonLocalReceivedHandler();
    string messageType = "expected-message-type";
    PersonLocalMessageHandler personLocalMessageHandler = new PersonLocalMessageHandler(personLocalReceivedHandler, messageType, xsdFilesResolver); // xsdFilesResolver is optional, 2.3.5.1 XSD Schemas Directory Resolver

    // add new message handlers to the collection of existing ones
    messageHandlers.Add(personLocalMessageHandler);

# 4.Organisation

All operations that refer to Organisations are performed using the `FifaConnectIdClient`.

## 4.1 Registering an organisation

To register a new organisation:

    OrganisationLocalType organisation = new OrganisationLocalType
    {
        Status = "active",
        OrganisationNature = "WorldFederation",
        // fill up all required data according to FIFA Data Standard
    };

    try
    {
        string uniqueFifaId = await fifaConnectIdClient.RegisterOrganisationAsync(organisation);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaDuplicatedOrganisationFoundException ex)
    {
        // possible duplicates were found
        IList<OrganisationLocalType> duplicates = ex.GetOrganisationDuplicatesResponse.Duplicates;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some other error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

If duplicated organisation is found then `FifaDuplicatedOrganisationFoundException` will be thrown. In order to resolve conflict and register new organisation use optional `force` parameter as presented below:

    bool force = true;
    string organisationFifaId = await fifaConnectIdClient.RegisterOrganisationAsync(organisation, force);

## 4.2 Getting organisation data

To get the organisation data associated with the specified FIFA ID use `GetOrganisationAsync` method.

    const string uniqueFifaId = "BVGE8TG";

    try
    {
        OrganisationLocalType organisation = await fifaConnectIdClient.GetOrganisationAsync(uniqueFifaId);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find the organisation
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (FifaOrganisationMergedException ex)
    {
        // requested organisation has been merged and is inactive
        OrganisationLocalType organisation = ex.RequestedEntity;
        string primaryOrganisationId = ex.PrimaryEntityId;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

This method will throw `FifaOrganisationMergedException` when you try to get data for an organisation that has been previously merged and deactivated (refer to section 4.3 Merging/unmerging organisations). The exception will contain data about requested organisation together with primary organisation id.

## 4.3 Merging/unmerging organisations

To merge two organisations into a single one use `MergeOrganisationsAsync` method available on `FifaConnectIdClient`. As a result of this operation, secondary organisation status will be set to `inactive`. This affects getting secondary organisation data in the future. Please refer to section 4.2 Getting organisation data.

This operation will only work for clubs. In addition you can only merge those that belong to the same Member Association that you are a member of.

    const string primaryOrganisationId = "";
    const string secondaryOrganisationId = "";

    try
    {
        OrganisationLocalType organisation = await fifaConnectIdClient.MergeOrganisationsAsync(primaryOrganisationId, secondaryOrganisationId);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaConflictException ex)
    {
        // a conflicting operation is currently in progress
        ConflictResponseType details = ex.Response;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find one of specified organisations
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

In order to revert merge operation call `UnmergeOrganisationsAsync` method with the same arguments.

## 4.4 Updating an organisation

To update the organisation data use `UpdateOrganisationAsync` method.

    const string organisationId = "BVGE8TG";

    try
    {
        OrganisationLocalType organisation = await fifaConnectIdClient.GetOrganisationAsync(organisationId);

        // modify data
        organisation.Status = "inactive";
        organisation.Email = "some@other.email";

        OrganisationLocalType updatedOrganisation = await fifaConnectIdClient.UpdateOrganisationAsync(organisation);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find the organisation
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (FifaDuplicatedOrganisationFoundException ex)
    {
        // possible duplicates were found
        IList<OrganisationLocalType> duplicates = ex.GetOrganisationDuplicatesResponse.Duplicates;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

If duplicated organisation is found then `FifaDuplicatedOrganisationFoundException` will be thrown. In order to resolve conflict and update organisation use optional `force` parameter.

## 4.5 Searching for an organisation

`FindOrganisations` method takes `FindOrganisationsRequestType` as a parameter that aggregates search criteria. It's possible to search by international and local names, parent organisation, organisation nature, address and last modification date. This method takes into consideration date and time when making a query using `ModifiedFromDateTime` and `ModifiedToDateTime`.

    FindOrganisationsRequestType request = new FindOrganisationsRequestType()
    {
        InternationalName = "Barcelona",
        ModifiedFromDateTime = DateTime.UtcNow.AddDays(-5),
        ModifiedToDateTime = DateTime.UtcNow,
        OfficialAddress = new SearchAddressType
        {
            Country = "ES",
            Town = "Barcelona"
        },
        NumberOfResults = 5
    };

    try
    {
        FindOrganisationsResponseType response = await fifaConnectIdClient.FindOrganisations(request);
        IList<FoundOrganisationLocalType> foundOrganisations = response.Organisations;
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }

## 4.6 Get organisation duplicates

To get a list of duplicates for an organisation use `GetOrganisationDuplicatesAsync` method. This method looks for similar organisations already registered in FIFA Connect ID Service.

    OrganisationLocalType organisation = new OrganisationLocalType
    {
        Status = "active",
        InternationalName = "Organisation name",
        OrganisationNature = "Club",
        // fill up all required data according to FIFA Data Standard
    };

    try
    {
        IEnumerable<OrganisationLocalType> foundDuplicates = await fifaConnectIdClient.GetOrganisationDuplicatesAsync(organisation);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

## 4.7 Get National Associations onboarded status

To get a list with onboarded statuses (has any persons or suborganisations registered) for specific National Associations use `GetNationalAssociationsOnboardedStatus` method. It takes `NationalAssociationsOnboardedRequestType` as a parameter which should contain list of strings with National Associations FIFA IDs.

    NationalAssociationsOnboardedRequestType request = new NationalAssociationsOnboardedRequestType
    {
        NationalAssociationsFifaIds = new List<string>
        {
            "BVGE8TG",
            "AHAR8SV"
        }
    };

    try
    {
        NationalAssociationsOnboardedResponseType response = await fifaConnectIdClient.GetNationalAssociationsOnboardedStatus(request);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find the organisation
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

To get a list with onboarded statuses for all National Associations use `GetAllNationalAssociationsOnboardedStatus` method.

    try
    {
        NationalAssociationsOnboardedResponseType response = await fifaConnectIdClient.GetAllNationalAssociationsOnboardedStatus();
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

## 4.8 Get Member Association of a child organisation (e.g. a club)

Some Member Associations may have a multi-level structure of children organisations, e.g. teams, clubs, schools, regions, etc. In many cases, however, there is a need to find out to which Member Association a given organisation (e.g. a club) belongs to.

To get FIFA ID and short name of that Member Association use `GetMemberAssociationForFifaId` method with the organisation's FIFA ID as input.

Note, that in order to retrieve the full name of the Member Association, call `GetOrganisationAsync` passing FIFA ID of the Member Association.

    const string clubFifaId = "BVGE8TG";

    try
    {
        MemberAssociationResponseType memberAssociation = await fifaConnectIdClient.GetMemberAssociationForFifaId(clubFifaId);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find the memberAssociation
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // provided organisation id is not valid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

# 5. Facility

All operations that refer to Facilities are performed using the `FifaConnectIdClient`.

## 5.1 Registering a facility

To register a new facility:

    FacilityLocalType facility = new FacilityLocalType()
    {
        Status = "active",
        InternationalName = "Wembley Stadium",
        OrganisationFIFAId = "BVGE8TG"
        // fill up all required data according to FIFA Data Standard
    };

    try
    {
        string uniqueFifaId = await fifaConnectIdClient.RegisterFacilityAsync(facility);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaDuplicatedFacilityFoundException ex)
    {
        // possible duplicates were found
        IList<FacilityLocalType> duplicates = ex.GetFacilityDuplicatesResponse.Duplicates;
    }
    catch (FifaConnectIdException ex)
    {
        // some other error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

If duplicated facility is found then `FifaDuplicatedFacilityFoundException` will be thrown. In order to resolve conflict and register new facility use optional `force` parameter.

## 5.2 Getting facility data

To get the facility data associated with the specified FIFA ID:

    const string uniqueFifaId = "105C40T";

    try
    {
        FacilityLocalType facility = await fifaConnectIdClient.GetFacilityAsync(uniqueFifaId);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find the facility
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

## 5.3 Updating a facility

To update the facility data use `UpdateFacilityAsync` method.

    const string facilityId = "105C40T";

    try
    {
        FacilityLocalType facility = await fifaConnectIdClient.GetFacilityAsync(facilityId);

        // set the data to update
        facility.Email = "some@other.email";
        facility.Status = "inactive";

        FacilityLocalType updatedFacility = await fifaConnectIdClient.UpdateFacilityAsync(facility);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (FifaEntityNotFoundException ex)
    {
        // could not find the facility
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }
    catch (FifaDuplicatedFacilityFoundException ex)
    {
        // possible duplicates were found
        IList<FacilityLocalType> duplicates = ex.GetFacilityDuplicatesResponse.Duplicates;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

If duplicated facility is found then `FifaDuplicatedFacilityFoundException` will be thrown. In order to resolve conflict and update facility use optional `force` parameter.

## 5.4 Searching for a facility

To search for a facility use `FindFacilities` method. It takes `FindFacilitiesRequestType` as a parameter that aggregates search criteria. It's possible to search by international and local names, parent facility, organisation, and address.

    FindFacilitiesRequestType request = new FindFacilitiesRequestType
    {
        InternationalName = "Camp Nou",
        ModifiedFromDateTime = DateTime.UtcNow.AddDays(-5),
        ModifiedToDateTime = DateTime.UtcNow,
        OfficialAddress = new SearchAddressType
        {
            Country = "ES",
            Town = "Barcelona"
        },
        NumberOfResults = 5
    };

    try
    {
        FindFacilitiesResponseType response = await fifaConnectIdClient.FindFacilities(request);
        IList<FoundFacilityLocalType> foundFacilities = response.Facilities;
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }

## 5.5 Get facility duplicates

To get a list of duplicates for a facility use `GetFacilityDuplicatesAsync` method. This method looks for similar facilities already registered in FIFA Connect ID service.

    FacilityLocalType facility = new FacilityLocalType
    {
        Status = "active",
        InternationalName = "Facility name",
        // fill up all required data according to FIFA Data Standard
    };

    try
    {
        IEnumerable<FacilityLocalType> foundDuplicates = await fifaConnectIdClient.GetFacilityDuplicatesAsync(facility);
    }
    catch (ValidationException)
    {
        // provided data did not pass validation
    }
    catch (AuthenticationException ex)
    {
        // invalid client credentials
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (UnauthorizedException ex)
    {
        // unauthorized
        IHttpOperationResponse details = ex.HttpOperationResponse;
    }
    catch (InvalidClientDataException ex)
    {
        // data sent to the service was invalid
        BadRequestResponseType details = ex.BadRequestResponse;
    }
    catch (TooManyRequestsException ex)
    {
        // call rate limit was exceeded
        int retryAfterInSeconds = ex.RetryAfter;
    }
    catch (FifaConnectIdException ex)
    {
        // some error occurred, see the details
        IHttpOperationResponse<object> operationResponse = ex.HttpOperationResponse;
    }

# 6. [Optional] Transfer Matching System (TMS) / Clearing House integration

Transfer Matching System (TMS) is an online system facilitating international transfers of players. The FIFA Clearing House (FCH) is an entity acting as an intermediary for the payment of Training Rewards, i.e. Solidarity Contribution and Training Compensation.

Any Registration System who wants to satisfy the FCH requirements, needs to implement support for "FirstProRegistration", "ProofOfPayment" and "DomesticTransferDeclaration" messages. These messages are sent to TMS who further interacts with the FCH.

Please note, that this chapter focuses on the technical implementation only and does not give details about business scenarios that different methods should be used in. For the latter, check an article on our support portal.

## 6.1. Using TMS facade

### 6.1.1 TMS responses cache

When running the listener in a separate process, you need to provide a cache for incoming confirmation messages. Cache serves as shared storage for TMS responses received via Connect Service Bus. `TmsFacade` then utilizes it to check the executing operation status.

Instead of using the provided SDK's `CorrelationIdBasedInMemoryCache<MessageReceived>`, it is recommended that users establish their own storage solution for storing and retrieving incoming TMS responses.

Below are the interfaces that need to be implemented:

- `ICorrelationIdBasedItemProvider<MessageReceived>` - Used by the TMS facade to get records from cache by id. If there is no entry associated with the given id, `null` value should be returned
- `ICorrelationIdBasedItemContainer<MessageReceived>` - Used by the TMS message handlers factory to put records into the cache

The simplest and most recommended method is to create one cache class that manages all types of TMS responses:

    public class TmsResponsesCache : ICorrelationIdBasedItemContainer<MessageReceived>, ICorrelationIdBasedItemProvider<MessageReceived>
    {
        public void Put(string correlationId, MessageReceived itemToCache)
        {
            throw new NotImplementedException();
        }

        public MessageReceived GetByCorrelationId(string correlationId)
        {
            throw new NotImplementedException();
        }
    }

### 6.1.2 TMS message handlers

The TMS Facade relies on the registration facade to listen for incoming responses from TMS. It utilizes the same listener that receives personal details during person registration.

Furthermore, it is essential to pass the same instance of the responses cache class into the TMS message handlers as TMS facade. Otherwise, the facade will never be able to confirm the status of TMS operation.

In order to hook TMS facade into the existing listener infrastructure, you add new message handlers to the current listener:

    IEnumerable<IMessageHandler> tmsMessageHandlers = TmsFacadeHandlersFactory.CreateMessageHandlers(responsesCache);

    messageHandlers.AddRange(tmsMessageHandlers);

### 6.1.3 Create TMS facade

In order to integrate with TMS, you need to instantiate a dedicated facade object that will serve as an entry point for all TMS-related operations.

    const int pollingIntervalInMilliseconds = 50;
    ICachePollingResponseAwaitersFactory responseAwaiterFactory = new CachePollingResponseAwaitersFactory(pollingIntervalInMilliseconds);

    TmsFacade tmsFacade = new TmsFacade(
        registrationFacade.ServiceBusClient,    // 2.3 Using Registration Facade
        logger,                                 // 2.3.4 Logger
        responseAwaiterFactory,
        tmsResponsesCache,                      // 6.1.1 TMS responses cache
        xsdFilesDirectoryResolver);             // Optional, 2.3.5.1 XSD Schemas Directory Resolver

## 6.2 Add person to TMS

In order to add a person to TMS, you need to build a `PersonLocal` object and pass it to `AddPlayer` method. `PersonLocal` type is compliant with FIFA Data Standard.

    PersonLocal personLocal = new PersonLocal(
        "ΧΑΡΑΛΑΜΠΟΥΣ", Gender.Male, ISO3166CountryCode.CY, new DateTime(1990, 11, 11), ISO316613CountryCode.CY,
        "Larnaca", ISO6392LanguageCode.Gre, ISO3166CountryCode.CY, "1XDZR79"
    );

    try
    {
        string correlationId = await tmsFacade.AddPlayer(personLocal);
    }
    catch (TmsOperationException ex)
    {
        // issue occurred during TMS operation
        Exception details = ex.InnerException;
    }

Notice that in this scenario you don't wait for TMS to answer. `correlationId` does not prove that the message has been picked up by TMS, only that it has been successfully sent. Although `correlationId` is not required for any further processing, we recommend that you log it.

## 6.3 Update person in TMS

In order to update a person to TMS, you need to build a `PersonLocal` object and pass it to `UpdatePlayer` method. `PersonLocal` type is compliant with FIFA Data Standard.

    PersonLocal personLocal = new PersonLocal(
        "ΧΑΡΑΛΑΜΠΟΥΣ", Gender.Male, ISO3166CountryCode.CY, new DateTime(1990, 11, 11), ISO316613CountryCode.CY,
        "Larnaca", ISO6392LanguageCode.Gre, ISO3166CountryCode.CY, "1XDZR79"
    );

    try
    {
        string correlationId = await tmsFacade.UpdatePlayer(personLocal);
    }
    catch (TmsOperationException ex)
    {
        // issue occurred during TMS operation
        Exception details = ex.InnerException;
    }

Notice that in this scenario you don't wait for TMS to answer. `correlationId` does not prove that the message has been picked up by TMS, only that it has been successfully sent. Although `correlationId` is not required for any further processing, we recommend that you log it.

## 6.4 Send Domestic Transfer Declaration

TMS facade sends the declaration to the Service Bus. TMS is expected to read the declaration and send a Domestic Transfer Confirmation back with a TMS Transfer ID.

Once the confirmation is received you have to store the `tmsTransferId`. It will be required for sending Proof Of Payment message later on.

    //create person local
    string personFifaId = "1XDZR79";
    PersonLocal personLocal = new PersonLocal(
        "ΧΑΡΑΛΑΜΠΟΥΣ", Gender.Male, ISO3166CountryCode.CY, new DateTime(1990, 11, 11), ISO316613CountryCode.CY,
        "Larnaca", ISO6392LanguageCode.Gre, ISO3166CountryCode.CY, personFifaId
    );

    //add player registration
    personLocal.AddPlayerRegistration(new PlayerRegistration(
        personFifaId,
        Status.Active,
        "103C3AB",
        new DateTime(2020, 10, 10),
        RegistrationLevel.Pro,
        Discipline.Football,
        PlayerRegistrationNature.Registration));

    //create declaration
    Club engagingClub = new Club("ARZI7RL", "Q8ZZ8RT", "Engaging Club International Name");
    Club releasingClub = new Club("HNIUE01", "SW8FJ2P", "Releasing Club International Name");
    RegistrationLevel playerLevelInEngagingClub = RegistrationLevel.Pro;
    RegistrationLevel playerLevelInReleasingClub = RegistrationLevel.Amateur;
    string messageId = "Information uniquely identifying this particular declaration";
    SellOnFee sellOnFee = SellOnFee.No; // if no information is available, set to SellOnFee.NotAvailable
    DomesticTransferDeclaration declaration = new DomesticTransferDeclaration(messageId, personLocal, engagingClub, releasingClub,
        new DateTime(2027, 11, 11), playerLevelInEngagingClub, playerLevelInReleasingClub, TransferNature.Permanent, sellOnFee);

    //add conditional transfer fees
    Club recipientClub = new Club("HNIUE01", "SW8FJ2P", "Club name");

    TransferFeeDetails conditionalTransferFeeDetails = new TransferFeeDetails(156, ISO4217CurrencyCode.AED, new DateTime(2020, 1, 1), recipientClub);
    declaration.AddConditionalTransferFee(new ConditionalTransferFee(conditionalTransferFeeDetails)
    {
        Condition = "specify condition"
    });

    //add fixed transfer fees

    //add release buyout transfer fees

    //populate the declaration with data

    try
    {
        long tmsTransferId = await tmsFacade.DeclareDomesticTransferAndWaitForTransferId(declaration, TimeSpan.FromSeconds(60));
    }
    catch (TmsOperationException operationException)
    {
        // Errors when trying to send message to TMS. Check inner exception for details
        string details = operationException.Message;
        Exception innerException = operationException.InnerException;
    }
    catch (DeclarationRejectedException declarationRejectedException)
    {
        // TMS rejected the declaration. See the reason for details:
        string rejectionReason = declarationRejectedException.Reason;
    }
    catch (ResponseNotReceivedException ex)
    {
        // Possible reasons:
        // 1. TMS has not responded within the timeout (consider increasing the timeout)
        // 2. responsesCache is not populated by the listener (see section 6.1.1)
        string details = ex.Message;
    }
    catch (Exception ex)
    {
        // Something else went wrong
        string details = ex.Message;
    }

The facade waits for a given period of time for the response. Should the response not arrive, ResponseNotReceivedException exception is thrown.

Should TMS consider the request invalid, `DeclarationRejectedException` will be thrown with a message from TMS providing the reason of the rejection.

## 6.5 Send Proof of Payment

In order to send a Proof of Payment to TMS, you need to build a `ProofOfPayment` object and pass it to `ProvePaymentAndWaitForPaymentId` method. Please mind that all properties are obligatory.

TMS facade sends the proof to the Service Bus. TMS is expected to read the proof and send a Proof Of Payment Confirmation back with a TMS Payment ID.

As of May 2024 there is no requirement to store `tmsPaymentId`. We recommend you log it as a confirmation that the message has been sent & received successfully.

    PaymentDetails paymentDetails = new PaymentDetails(156, ISO4217CurrencyCode.AED, new DateTime(2020, 1, 1));
    Club club = new Club("105KYBB", "105C6FI", "Organisation name");
    string messageId = "Information uniquely identifying this particular payment, e.g. bank transfer id";

    ProofOfPayment proofOfPayment = new ProofOfPayment(messageId, PaymentType.FixedTransferFee, 1234, paymentDetails, club);

    try
    {
        long tmsPaymentId = await tmsFacade.ProvePaymentAndWaitForPaymentId(proofOfPayment, TimeSpan.FromSeconds(60));
    }
    catch (TmsOperationException operationException)
    {
        // Errors when trying to send message to TMS. Check inner exception for details
        string details = operationException.Message;
        Exception innerException = operationException.InnerException;
    }
    catch (DeclarationRejectedException declarationRejectedException)
    {
        // TMS rejected the declaration. See the reason for details:
        string rejectionReason = declarationRejectedException.Reason;
    }
    catch (ResponseNotReceivedException ex)
    {
        // Possible reasons:
        // 1. TMS has not responded within the timeout (consider increasing the timeout)
        // 2. responsesCache is not populated by the listener (see section 6.1.1)
        string errorDetails = ex.Message;
    }
    catch (Exception ex)
    {
        // Something else went wrong
        string errorDetails = ex.Message;
    }

The facade waits for a given period of time for the response. Should the response not arrive, `ResponseNotReceivedException` exception is thrown.

Should TMS consider the request invalid, `DeclarationRejectedException` will be thrown with a message from TMS providing the reason of the rejection.

## 6.6 Send First Pro Registration

TMS facade sends the registration to the Service Bus. TMS is expected to read the registration and send a First Pro Registration Confirmation back with a TMS First Registration ID.

As of May 2024 there is no requirement to store `tmsFirstRegistrationId`. We recommend you log it as a confirmation that the message has been sent & received successfully.

    PersonLocal personLocal = new PersonLocal(
        "ΧΑΡΑΛΑΜΠΟΥΣ", Gender.Male, ISO3166CountryCode.CY, new DateTime(1990, 11, 11), ISO316613CountryCode.CY,
        "Larnaca", ISO6392LanguageCode.Gre, ISO3166CountryCode.CY, "1XDZR79"
    );
    PlayerRegistration proRegistration = new PlayerRegistration(
        "1XDZR79", Status.Active, "105C6FI", new DateTime(2021, 03, 31),
        RegistrationLevel.Pro, Discipline.Football, PlayerRegistrationNature.Registration
    );
    personLocal.AddPlayerRegistration(proRegistration);

    Club club = new Club("105KYBB", "105C6FI", "Organisation name");
    string messageId = "Information uniquely identifying this particular registration";
    FirstProRegistration firstProRegistration = new FirstProRegistration(messageId, personLocal, club, ClubStatus.Active, true);
    try
    {
        long tmsFirstRegistrationId = await tmsFacade.DeclareFirstProRegistrationAndWaitForRegistrationId(firstProRegistration, TimeSpan.FromSeconds(60));
    }
    catch (TmsOperationException operationException)
    {
        // Errors when trying to send message to TMS. Check inner exception for details
        string details = operationException.Message;
        Exception innerException = operationException.InnerException;
    }
    catch (DeclarationRejectedException declarationRejectedException)
    {
        // TMS rejected the declaration. See the reason for details:
        string rejectionReason = declarationRejectedException.Reason;
    }
    catch (ResponseNotReceivedException ex)
    {
        // Possible reasons:
        // 1. TMS has not responded within the timeout (consider increasing the timeout)
        // 2. responsesCache is not populated by the listener (see section 6.1.1)
        string details = ex.Message;
    }
    catch (Exception ex)
    {
        // Something else went wrong
        string details = ex.Message;
    }

The facade waits for a given period of time for the response. Should the response not arrive, `ResponseNotReceivedException` exception is thrown.

Should TMS consider the request invalid, `DeclarationRejectedException` will be thrown with a message from TMS providing the reason of the rejection.

# 7. [Optional] Automatic notifications

Certain business operations in FIFA Connect ID trigger notifications. For each notification type a subscription can be created, so that when a notification is triggered, then a message is sent out.

Two message types are supported: email & service bus. For the complete business documentation, list of notification types and methods to subscribe, please refer to Connect ID Notifications article.

In order to subscribe for a particular notification type, please get in touch with support.

Service bus messages are particularly interesting in the context of the SDK as it enables receiving them in an automated way. Under the hood, a service bus notification message is an XML document exchanged via the service bus infrastructure. The SDK handles the XML deserialization & validation and exposes an extension point you can hook into in order to process the message.

In order to use notification messages in your system, you need to implement an interface defined by the SDK and configure the listener to handle the messages.

## 7.1 Notifications message handlers

In order to take advantage of notifications, additional message handlers must be added when configuring the listener. Custom business logic can be invoked by implementing a notification handler interface and passing it as a constructor parameter to the corresponding message handler.

A stub implementation of a custom notification message handler:

    public class CustomPersonMergedNotificationHandler : IPersonMergedNotificationHandler
    {
        public void Execute(PersonMergedNotification personMergedNotification)
        {
            throw new NotImplementedException();
        }
    }

To start handling the incoming notifications you need to add message handlers into current listener:

    List<IMessageHandler> notificationHandlers = new List<IMessageHandler>
    {
        //list of notification message handlers
        new PersonMergedMessageHandler(new CustomPersonMergedNotificationHandler()),
        new PersonNameChangedMessageHandler(new CustomPersonNameChangedNotificationHandler()),
        new DateOfBirthChangedMessageHandler(new CustomDateOfBirthChangedNotificationHandler()),
        new GenderChangedMessageHandler(new CustomGenderChangedNotificationHandler())
    };

    messageHandlers.AddRange(notificationHandlers);

## 7.2 PersonMergedNotification

This notification is created when a person ("secondary") is merged into another person ("primary"). The secondary person FIFA ID is no longer active.

With this notification message you can automate the process of updating your data. If you store any data for the secondary person, after the merge, you should replace the secondary person FIFA ID with the primary person FIFA ID in your Registration System.

### 7.2.1 Contents

The message contains the following information:

- Primary Person FIFA ID - Primary FIFA ID from the respective merge operation
- Secondary Person FIFA ID - Secondary FIFA ID from the respective merge operation
- Actor Name - Who performed the merge operation
- Operation Timestamp - When the merge operation was performed

### 7.2.2 Handler interface

SDK exposes `IPersonMergedNotificationHandler` interface which can be used to process a notification, e.g. update the FIFA ID in an internal Registration System's database.

## 7.3 PersonNameChangedNotification

This notification is created when a change of a person name is made using update. If you store any data for the person, after the change, you might want to update a record in your DB.

Due to the fact that ConnectId does not store persons' names, there is no information on what was the name before and after a change. To get new data of the modified record you can contact the Registration System that triggered the notification.

### 7.3.1 Contents

The message contains the following information:

- PersonFIFAId - Id of a person that is a subject to the change
- Actor Name - Who performed the name change
- Operation Timestamp - When the name change was performed

### 7.3.2 Handler interface

SDK exposes `IPersonNameChangedNotificationHandler` interface which can be used to process a notification.

## 7.4 GenderChangedNotification

This notification is created when a change of a person gender is made using update. If you store any data for the person, after the change, you might want to update a record in your DB.

### 7.4.1 Contents

The message contains the following information:

- PersonFIFAId - Id of a person that is a subject to the change
- Before - Gender before the change
- After - Gender after the change
- Actor Name - Who performed the gender change
- Operation Timestamp - When the gender change was performed

### 7.4.2 Handler interface

SDK exposes `IGenderChangedNotificationHandler` interface which can be used to process a notification, e.g. update the person data in an internal Registration System's database.

## 7.5 DateOfBirthChangedNotification

This notification is created when a change of a person date of birth is made using update. If you store any data for the person, after the change, you might want to update a record in your DB.

### 7.5.1 Contents

The message contains the following information:

- PersonFIFAId - Id of a person that is a subject to the change
- Before - Date of birth before the change
- After - Date of birth after the change
- Actor Name - Who performed the date of birth change
- Operation Timestamp - When the date of birth change was performed

### 7.5.2 Handler interface

SDK exposes `IDateOfBirthChangedNotificationHandler` interface which can be used to process a notification, e.g. update the person data in an internal Registration System's database.