Aller au contenu

SDK Java

Le SDK com.sangho:sangho-java supporte Java 11+ avec une interface synchrone. Compatible avec Spring Boot, Micronaut et tout projet Java standard.

Installation

Ajoutez la dépendance via Maven ou Gradle. Le SDK n’a pas de dépendances transitives obligatoires en dehors de la JVM standard.

Initialisation

Instanciez la classe Sangho avec votre clé API. Toutes les méthodes suivent le pattern builder pour la construction des paramètres — garantissant un typage strict à la compilation.

Injection de dépendances (Spring Boot)

Déclarez le client Sangho comme un bean Spring en lisant la clé depuis @Value(”${sangho.secret-key}”). Injectez-le ensuite dans vos services via @Autowired ou constructeur — une seule instance partagée suffit, le client est thread-safe.

Installation

text
<!-- Maven — pom.xml -->
<dependency>
  <groupId>com.sangho</groupId>
  <artifactId>sangho-java</artifactId>
  <version>1.4.0</version>
</dependency>

Initialisation & premier appel

java
import com.sangho.Sangho;
import com.sangho.model.PaymentIntent;
import com.sangho.param.PaymentIntentCreateParams;


Sangho sangho = new Sangho(System.getenv("SANGHO_SECRET_KEY"));


PaymentIntentCreateParams params = PaymentIntentCreateParams.builder()
    .setAmount(5000L)
    .setCurrency("XAF")
    .addPaymentMethodType("mobile_money")
    .putMetadata("order_id", "CMD-001")
    .build();


PaymentIntent intent = sangho.paymentIntents().create(params);
System.out.println(intent.getId());           // pi_xxxxxxxxxxxx
System.out.println(intent.getClientSecret()); // pi_xxx_secret_xxx

Gestion des erreurs

Toutes les exceptions héritent de com.sangho.exception.SanghoException. Attrapez les sous-classes spécifiques pour traiter chaque cas métier.

Pagination

Les endpoints de liste retournent un objet SanghoCollection<T> avec les méthodes getData(), getCount() et hasMore().

Retry automatique

Le SDK retente automatiquement les erreurs 429 et 5xx. Configurez via SanghoOptions.builder().setMaxNetworkRetries(3) passé au constructeur.

Gestion des erreurs

java
import com.sangho.exception.AuthenticationException;
import com.sangho.exception.InvalidRequestException;
import com.sangho.exception.RateLimitException;
import com.sangho.exception.SanghoException;


try {
    PaymentIntent intent = sangho.paymentIntents().create(params);
} catch (AuthenticationException e) {
    // Clé API invalide ou expirée
    System.err.println("Auth error: " + e.getMessage());
} catch (InvalidRequestException e) {
    // Paramètre invalide
    System.err.println("Param: " + e.getParam());
    System.err.println("Message: " + e.getMessage());
} catch (RateLimitException e) {
    // Trop de requêtes — retenter avec backoff
} catch (SanghoException e) {
    System.err.println("Code: " + e.getCode());
    System.err.println("Status: " + e.getStatusCode());
    System.err.println("Message: " + e.getMessage());
}

Pagination

java
import com.sangho.model.Transaction;
import com.sangho.model.SanghoCollection;
import com.sangho.param.TransactionListParams;


TransactionListParams params = TransactionListParams.builder()
    .setStatus("completed")
    .setPage(1L)
    .setPageSize(20L)
    .build();


SanghoCollection<Transaction> page =
    sangho.transactions().list(params);


System.out.println(page.getCount());  // Total de résultats
for (Transaction tx : page.getData()) {
    System.out.println(tx.getId());
}


// Page suivante
if (page.hasMore()) {
    TransactionListParams next = TransactionListParams.builder()
        .setStatus("completed")
        .setPage(2L)
        .setPageSize(20L)
        .build();
    SanghoCollection<Transaction> page2 =
        sangho.transactions().list(next);
}