> For the complete documentation index, see [llms.txt](https://docs.kkiapay.me/v1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kkiapay.me/v1/plugin-et-sdk/frameworks/integration-de-kkiapay-dans-une-application-next.js.md).

# Intégration de KKiaPay dans une application next.js

### Installation

Veuillez exécuter la commande suivante :

```javascript
npm install kkiapay-react
```

### Importation&#x20;

Vous pouvez maintenant importer la dépendance KKiaPay dans votre page :

```javascript
'use client'

import { useKKiaPay } from 'kkiapay-react';
import { useEffect } from 'react';
```

### Usage

Définissez une fonction qui vous permettra d'afficher la fenêtre de paiement KKiaPay :&#x20;

```javascript
function Home() {
  const { openKkiapayWidget } = useKKiaPay();

  function open() {
    openKkiapayWidget({
      amount: 4000,
      api_key: "xxxxxxxxxxxxxxxxxx",
      sandbox: true,
      email: "randomgail@gmail.com",
      phone: "97000000",
    });
  }

  return <button onClick={open}>click me</button>;
}
```

{% hint style="danger" %}
**Important !**&#x20;

Afin d'éviter toute fraude, procédez à la vérification côté serveur de l'opération de transaction. Pour cela, apprenez-en plus sur les [SDKs Serveurs](/v1/plugin-et-sdk/admin-sdks-server-side.md).
{% endhint %}

Vous pouvez souscrire aux événements liés à l'opération de paiement avec la fonction **addKkiapayListener.** Pour être notifié du statut réussi d'une opération de paiement, vous pouvez procéder comme suit :

```javascript
function Home() {
  const { openKkiapayWidget, addKkiapayListener } = useKKiaPay();

  // ..... others components options
  function successHandler(response) {
    console.log(response);
  }
  
  function failureHandler(error) {
    console.log(error);
  }

  useEffect(() => {
    addKkiapayListener('success', successHandler)
    addKkiapayListener('failed', failureHandler)
  }, [addKkiapayListener]);

  // ..... others components options
}
```

Parce qu'elle est ajoutée dans le hook useEffect, la fonction addKkiapayListener sera appelée à chaque fois que votre composant sera rendu. Si le composant est rendu n fois, la fonction sera enrégistrée n fois.

Afin d’eviter les fuites de mémoire, nous aurons besoin de supprimer le callback lorsque le composant est détruit :&#x20;

```javascript
function Home() {
  const { 
    openKkiapayWidget,
    addKkiapayListener,
    removeKkiapayListener
  } = useKKiaPay();

  // ..... others components options
  function successHandler(response) {
    console.log(response);
  }
    
  function failureHandler(error) {
    console.log(error);
  }

  useEffect(() => {
    addKkiapayListener('success',successHandler)
    addKkiapayListener('failed', failureHandler)
    
    return () => {
      removeKkiapayListener('success',successHandler)
      removeKkiapayListener('failed', failureHandler)
    };
  }, [addKkiapayListener,removeKkiapayListener]);
  // ..... others components options
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.kkiapay.me/v1/plugin-et-sdk/frameworks/integration-de-kkiapay-dans-une-application-next.js.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
