# Wallet Funding

<figure><img src="https://531890104-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6VMBpmeWY7lXMJNJNFMA%2Fuploads%2Ft3NiLhhqjnVY0gZKwqkf%2Fimage.png?alt=media&#x26;token=dc3ea41b-acc5-4adb-b003-42051a7de899" alt=""><figcaption></figcaption></figure>

## FOR INCOMING TRANSACTIONS/FUNDING&#x20;

An incoming transaction is a kind of transaction where users on your platform receives money between into wallet from other wallets, banks or other countries or a funding activity. This is what we refer to as an incoming transactions.

{% hint style="info" %}
BEFORE INTEGRATION - DO THIS
{% endhint %}

Before you begin any integration you need to have created an incoming URL endpoint where all request between Finswich & your application will take place for interactions and notifications of incoming transactions. See Below for Image reference

<figure><img src="https://531890104-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6VMBpmeWY7lXMJNJNFMA%2Fuploads%2F5ONpdMHu7s3b7jPvKW3B%2FIncoming%20settle.png?alt=media&#x26;token=e5183d87-7a52-40b1-9728-27ee1224b961" alt=""><figcaption><p>Setting up the incoming webhook URL</p></figcaption></figure>

## TRANSACTION FLOW EVENTS

1. User Category:  this is used to ensure that the recipient user has the right policy setup to receive the incoming transaction&#x20;

**Req Body**&#x20;

{% code overflow="wrap" %}

```javascript
{"event":"USER_CATEGORY","data":{"user_reference":"saudiemcode001","mode":"LIVE"}}
```

{% endcode %}

**Sample Code on third party**&#x20;

```javascript
      if (body.event === 'USER_CATEGORY') {

          const user = await this.user_repository.FindById(
            body.data.user_reference,
            [
              '-permissions',
              '-settings',
              '-recovery_keys',
              '-contigency_lock_expires',
              '-contigency_level',
              '-verification_code',
            ],
          );

          return {
            status: 'success',
            message: 'User category sent!',
            data: {
              user_category: body.data.user_reference,
              user,
            },
          };
        }
```

Credit Transactions Events -Once there is a credit to your Finswich wallet, the notifications will be posted to your incoming webhook URL. you can use the data to update your user’s wallet balance on your application.

**Req Body**&#x20;

{% code overflow="wrap" %}

```javascript
{"event":"CREDIT_TRANSACTION","data":{"user_reference":"saudiemcode001","amount"10,"currency":"sar","transaction_reference":"FIN-DXVb1CpC6jnTXkqJ1682607236","quarantine":false,"mode":"LIVE"}}
```

{% endcode %}

```javascript
 if (body.event === 'CREDIT_TRANSACTION') {
          //! consider crediting your wallet here
          const credit_model = {
            txn_reference: body.data.txn_reference,
            user_reference: body.data.user_reference,
            amount: body.data.amount,
            currency: body.data.currency,
          };

          const wallet = await this.wallet_repository.FindByUserId(
            credit_model.user_reference,
          );

          if (!wallet) {
            throw new notFoundException(en['wallet-not-found']);
          }

          await this.finswich_txn_logs_repository.CreateTxnLogs({
            txn_reference: credit_model.txn_reference,
            amount: credit_model.amount,
            currency: credit_model.currency,
            user: credit_model.user_reference,
            wallet_id: wallet.id,
            type: 'incoming',
            status: 'success',
          });

          await this.wallet_repository.IncreaseBalance(
            credit_model.user_reference,
            credit_model.amount,
          );

          await this.wallet_repository.UpdateById(
            wallet.id,
            {},
            { total_incoming: credit_model.amount },
          );

          return {
            status: 'success',
            message: en['user-account-credited-success'],
          };
        } else {
          throw badRequestException(en['user-account-credited-failure']);
        }
```

&#x20;
