Skip to content

Latest commit

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Safaricom Express(USSD Push)

Express initiates online payment on behalf of the customer using USSD Push.

1. Authentication

1.1 Register for a Safaricom Developer Account

For authentication credentials to be used only for the sandbox, Safaricom Developer Account is required. To log in or register, go to https://developer.safaricom.co.ke/. The Organization Information must be provided when applying for new credentials when going live.

1.2 Create an app

Using https://developer.safaricom.co.ke/MyApps, you can create an app and select the Lipa Na M-Pesa and M-Pesa Sandbox check boxes. By creating an app, you can obtain the Consumer Key and Consumer Secret that will be used for Basic Authentication.

Step 1: Get Access Token using Basic Auth

METHODGET
URLSandbox: https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials
Successful Request
curl --location --request GET 'http://localhost:8080/accessToken'
Request Fields
NAMEDESCRIPTIONTYPE
authorizationBasic Authentication over HTTPS, this is a base64 encoded string of your app’s consumer key and consumer secretHeader
grant_typeclient _credentials grant type is supported. Put this under paramsQuery

Successful Response

{"access_token": "ZPRHAAXD1oaZ1gp9EdXFGVfw8Gv6","expires_in": "3599"}
Response Fields
NAMEDESCRIPTIONTYPE
access_tokenAccess token used to access the customer to business payments apiJSON
expires_inToken expiry time in secondsJSON

Step 2: Initiate an express(USSD Push) payment

METHODPOST
URLSandbox: https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest
Successful Request
curl --location --request POST 'http://localhost:8080/stkPush' \
--header 'Content-Type: application/json' \--data-raw '{"Amount": 1,"PhoneNumber": 2547484581XX}'
Request Fields
NAMEDESCRIPTIONTYPE
AmountThe amount transacted normally integer valueInteger
PhoneNumberThe Mobile Number to receive the USSD Pin PromptInteger

Successful Response

{"MerchantRequestID": "68444-143235050-2","CheckoutRequestID": "ws_CO_22112022152650652748458100","ResponseCode": "0","ResponseDescription": "Success. Request accepted for processing","CustomerMessage": "Success. Request accepted for processing"}
Response Fields
NAMEDESCRIPTIONTYPE
MerchantRequestIDThis is a globally unique identifier for any submitted payment requestString
CheckoutRequestIDThis is a globally unique identifier of the processed checkout transaction requestString
ResponseDescriptionAn acknowledgement message from API that gives the status of the request submission usually maps to a specific ResponseCode valueNumeric
ResponseCodeThis is a Numeric status code that indicates the status of the transaction submission. 0 means successful submission and any other means code means an error occurredNumeric
CustomerMessageThis is a message that your system can display to the Customer as an acknowledgment of the payment request submissionString

Data sent to the Callback URL

{"Body": {"stkCallback": {"MerchantRequestID": "68444-143235050-2","CheckoutRequestID": "ws_CO_22112022152650652748458100","ResultCode": 0,"ResultDesc": "The service request is processed successfully.","CallbackMetadata": {"Item": [{"Name": "Amount","Value": 1},{"Name": "MpesaReceiptNumber","Value": "QKM0FXE6RK"},{"Name": "TransactionDate","Value": 20221122152706},{"Name": "PhoneNumber","Value": 2547484581XX}]}}}}

Step 3: Express Query Status

METHODPOST
URLSandbox: https://sandbox.safaricom.co.ke/mpesa/stkpushquery/v1/query
Successful Request
curl --location --request POST 'http://localhost:8080/stkPush/status' \
--header 'Content-Type: application/json' \--data-raw '{"CheckoutRequestID": "ws_CO_22112022164548577748458100"}'
Request Fields
NAMEDESCRIPTIONTYPE
CheckoutRequestIDThis is a globally unique identifier of the processed checkout transaction requestInteger

Successful Response

{"ResponseCode": "0","ResponseDescription": "The service request has been accepted successsfully","MerchantRequestID": "5468-13551780-2","CheckoutRequestID": "ws_CO_22112022164548577748458100","ResultCode": "0","ResultDesc": "The service request is processed successfully."}
Response Fields
NAMEDESCRIPTIONTYPE
ResponseCode0 means successful submissionInteger
ResponseDescriptionAcknowledgment message of successful submission requestString
MerchantRequestIDTimestamp of the transaction with format(YEAR+MONTH+DATE+HOUR+MINUTE+SECOND)Timestamp
CheckoutRequestIDGlobal unique identifier of the processed transaction checkoutString
ResultCode0 means successful transaction by customer, any other means an error occurred.Integer
ResultDescAcknowledgment message describing the transaction statusString

Error Codes

ERROR CODEDESCRIPTION
404.001.03Invalid access token
500.001.1001Transaction already in process

About

Spring Boot + Daraja API external API call

Resources

Stars

1 star

Watchers

1 watching

Forks

Used by

Contributors

Languages