Skip to content

Java SDK

The com.sangho:sangho-java SDK supports Java 11+ with a synchronous interface. Compatible with Spring Boot, Micronaut, and any standard Java project.

Installation

Add the dependency via Maven or Gradle. The SDK has no mandatory transitive dependencies beyond the standard JVM.

Initialization

Instantiate the Sangho class with your API key. All methods follow the builder pattern for constructing parameters — ensuring strict compile-time typing.

Dependency injection (Spring Boot)

Declare the Sangho client as a Spring bean by reading the key from @Value(“${sangho.secret-key}”). Inject it into your services via @Autowired or constructor — a single shared instance is sufficient, as the client is thread-safe.

Installation

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

Initialization & first call

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

Error handling

All exceptions inherit from com.sangho.exception.SanghoException. Catch specific subclasses to handle each business case.

Pagination

List endpoints return a SanghoCollection<T> object with methods getData(), getCount(), and hasMore().

Automatic retry

The SDK automatically retries 429 and 5xx errors. Configure via SanghoOptions.builder().setMaxNetworkRetries(3) passed to the constructor.

Error handling

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) {
    // Invalid or expired API key
    System.err.println("Auth error: " + e.getMessage());
} catch (InvalidRequestException e) {
    // Invalid parameter
    System.err.println("Param: " + e.getParam());
    System.err.println("Message: " + e.getMessage());
} catch (RateLimitException e) {
    // Too many requests — retry with 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 results
for (Transaction tx : page.getData()) {
    System.out.println(tx.getId());
}


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