DEV Community

Isaac Tonyloi
Isaac Tonyloi

Posted on


I tried using the Daraja API with Springboot


Enter fullscreen mode Exit fullscreen mode
import com.mpesa.Daraja;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class DarajaConfig {

  private String consumerKey = "your-consumer-key";
  private String consumerSecret = "your-consumer-secret";
  private String shortCode = "your-shortcode";
  private String passkey = "your-passkey";
  private String testUsername = "test-username";
  private String testAmount = "1";

  public Daraja daraja() {
    return new Daraja(consumerKey, consumerSecret, shortCode, passkey, testUsername, testAmount);

Enter fullscreen mode Exit fullscreen mode

Initiating a Payment

import com.mpesa.Daraja;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

public class PaymentController {

  private Daraja daraja;

  public String initiatePayment(@RequestParam String phoneNumber) {
    return daraja.initiatePayment(phoneNumber);


Enter fullscreen mode Exit fullscreen mode

Top comments (0)

An Animated Guide to Node.js Event Loop

>> Check out this classic DEV post <<