Skip to main content

Receive M-PESA payments

Receive payments from M-PESA users via STK Push.​

POST https://sandbox.kopokopo.com/api/v2/incoming_payments
Content-Type: application/json
Accept: application/json
Authorization: Bearer 0Sn0W6kzNicvoWhDbQcVSKLRUpGjIdlPSEYyrHqrDDoRnQwE7Q
User-Agent: "< product >/< product-version > < comment >"
{
"payment_channel": "M-PESA STK Push",
"till_number": "K000000 || 000000",
"subscriber": {
"first_name": "Joe",
"last_name": "Buyer",
"phone_number": "+254999999999",
"email": "jbuyer@mail.net"
},
"amount": {
"currency": "KES",
"value": 20000
},
"metadata": {
"customer_id": "123456789",
"reference": "123456",
"notes": "Payment for invoice 12345"
},
"_links": {
"callback_url": "https://callback_to_your_app.your_application.com"
}
}

HTTP Request​

POST https://sandbox.kopokopo.com/api/v2/incoming_payments

Request Parameters (JSON)​

ParameterRequiredTypeDescription
payment_channelYesStringThe payment channel to be used eg. M-PESA
till_numberYesStringYour M-PESA till number or online payments account number from the Kopo Kopo dashboard to which the payment will be made.
subscriberYesSubscriberA Subscriber JSON object see below
amountYesAmountAn Amount JSON object containing currency and amount
metadataNoMetadataAn optional JSON object containing a maximum of 5 key value pairs
linksYes_linksA JSON object containing the call back URL where the result of the Incoming Payment will be posted

Subscriber (JSON)​

ParameterRequiredTypeDescription
first_nameNoStringFirst name of the subscriber
last_nameNoStringLast name of the subscriber
phone_numberYesStringThe phone number of the subscriber from which the payment will be made
emailNoStringE-mail address of the subscriber - optional

HTTP Response​

Successful Response​

Upon a successful request a HTTP Status 201 will be returned and the location HTTP Header will contain the URL of the newly created Incoming Payment

HTTP/1.1 201 Created
Location: https://sandbox.kopokopo.com/api/v2/incoming_payments/247b1bd8-f5a0-4b71-a898-f62f67b8ae1c

Error Response​

This will have a HTTP Status code indicating the error and it might have a json that contains the error code and an error message specifying the error that occured. Please note that the error response might not be present depending on the type of error that occured.

If the error json response is present, it will look as follows:

HTTP/1.1 400 Bad Request

{ "error_code": 400, "error_message": "Till number can't be blank" }

Process Incoming Payment Result​

POST https://your-callback-url.com/api/v2/payment_request_result
Content-Type: application/json
User-Agent: < product >/< product-version > < comment >
X-KopoKopo-Signature: 0Sn0W6kzNicvoWhDbQcVSKLRUpGjIdlPSEYyrHqrDDoRnQwE7Q
{
"data": {
"id": "a652f86f-f2aa-4d70-baa2-ccfe4b78f4fc",
"type": "incoming_payment",
"attributes": {
"initiation_time": "2020-10-19T09:24:48.622+03:00",
"status": "Success",
"event": {
"type": "Incoming Payment Request",
"resource": {
"id": "52f86f-f2aa-4d70-baa2-ccfe4b78f4fc",
"reference": "OJJ1MPU40Z",
"origination_time": "2020-10-19T09:24:54+03:00",
"sender_phone_number": "+254999999999",
"amount": "100.0",
"currency": "KES",
"till_number": "K000000",
"system": "Lipa Na M-PESA",
"status": "Received",
"sender_first_name": "Joe",
"sender_middle_name": null,
"sender_last_name": "Buyer"
},
"errors": null
},
"metadata": {
"customer_id": "123456789",
"reference": "123456",
"notes": "Payment for invoice 12345"
},
"_links": {
"callback_url": "https://webhook.site/675d4ef4-0629-481f-83cd-d101f55e4bc8",
"self": "http://sandbox.kopokopo.com/api/v2/incoming_payments/a652f86f-f2aa-4d70-baa2-ccfe4b78f4fc"
}
}
}
}

Unsuccessful Incoming Payment

{
"data": {
"id": "09bc45fc-3b97-4c44-b860-42a7bcbd7480",
"type": "incoming_payment",
"attributes": {
"initiation_time": "2020-10-15T09:45:18.843+03:00",
"status": "Failed",
"event": {
"type": "Incoming Payment Request",
"resource": null,
"errors": ["The initiator information is invalid."]
},
"metadata": {
"customer_id": "123456789",
"reference": "123456",
"notes": "Payment for invoice 12345"
},
"_links": {
"callback_url": "https://webhook.site/675d4ef4-0629-481f-83cd-d101f55e4bc8",
"self": "https://sandbox.kopokopo.com/api/v2/incoming_payments/09bc45fc-3b97-4c44-b860-42a7bcbd7480"
}
}
}
}

After a Incoming Payment is initiated, a Incoming Payment Result will be posted asynchronously to the call back URL specified in the Incoming Payment.

Incoming Payment Result Parameters (JSON)​

Parameters contained in a successful Incoming Payment result;

ParameterRequiredTypeDescription
idYesStringThe ID of the Incoming Payment
typeYesStringThe topic of the request. incoming_payment in this instance.
initiation_timeYesStringThe timestamp of when the Incoming Payment was created.
statusYesStringA status string denoting the status of the Incoming Payment
eventYesJSONA JSON Object encapsulating the event of the request (see below)
metadataNoJSONAn optional JSON objet containing a maximum of 5 key - value pairs that had been sent during the initiation of the request
_linksYesJSONA JSON object containing links to the Incoming Payment and the corresponding Buygoods Transaction resource

Event JSON

ParameterRequiredTypeDescription
typeYesStringThe type of the event (Incoming Payment)
resourceYesJSONThe resource corresponding to the event. In this case this is a Buygoods Transaction
errorsNoStringA string containing information on the error than occured

Resource (Buygoods Transaction) JSON

ParameterRequiredTypeDescription
referenceYesStringThe reference of the transaction
origination_timeYesStringThe transaction timestamp
sender_phone_numberYesStringThe phone number that sent the payment
amountYesFloatThe amount of the transaction
currencyYesStringCurrency
till_numberYesStringThe Online Payments Account till number to which the payment was made
systemYesStringThe mobile money system
sender_first_nameYesStringFirst name of payer
sender_middle_nameNoStringMiddle name of payer
sender_last_nameYesStringLast name of payer
tip

Remember — if you are also subscribed to the buygoods_transaction_received webhook, the Request Payment Result will also be sent to that end point.

Query Incoming Payment Status​

GET https://sandbox.kopokopo.com/api/v2/incoming_payments/d79995cd-0111-e511-80da-0aa34a9b2388
Accept: application/json
Authorization: Bearer 0Sn0W6kzNicvoWhDbQcVSKLRUpGjIdlPSEYyrHqrDDoRnQwE7Q
User-Agent: < product >/< product-version > < comment >
payment_request_url = 'https://sandbox.kopokopo.com/api/v2/incoming_payments/d76265cd-0951-e511-80da-0aa34a9b2388'

# Using K2 Connect - https://github.com/kopokopo/k2-connect-ruby (Recommended)
k2_stk = K2Stk.new("your_access_token")
k2_stk.receive_mpesa_payments(your_input)

# Query specific resource
k2_stk.query_resource(payment_request_url) or k2_stk.query_resource(k2_stk.location_url)

# Query recent transaction
k2_stk.query_status

The request above results the following JSON payload when a Incoming Payment Result exists

{
"data": {
"id": "a652f86f-f2aa-4d70-baa2-ccfe4b78f4fc",
"type": "incoming_payment",
"attributes": {
"initiation_time": "2020-10-19T09:24:48.622+03:00",
"status": "Success",
"event": {
"type": "Incoming Payment Request",
"resource": {
"id": "52f86f-f2aa-4d70-baa2-ccfe4b78f4fc",
"reference": "OJJ1MPU40Z",
"origination_time": "2020-10-19T09:24:54+03:00",
"sender_phone_number": "+254999999999",
"amount": "100.0",
"currency": "KES",
"till_number": "K000000",
"system": "Lipa Na M-PESA",
"status": "Received",
"sender_first_name": "Joe",
"sender_middle_name": null,
"sender_last_name": "Buyer"
},
"errors": null
},
"metadata": {
"customer_id": "123456789",
"reference": "123456",
"notes": "Payment for invoice 12345"
},
"_links": {
"callback_url": "https://webhook.site/675d4ef4-0629-481f-83cd-d101f55e4bc8",
"self": "http://sandbox.kopokopo.com/api/v2/incoming_payments/a652f86f-f2aa-4d70-baa2-ccfe4b78f4fc"
}
}
}
}

JSON structure returned when No corresponding Incoming Payment Result exists

{
"data": {
"id": "e192ac63-8b24-4caf-8637-a2a9b75a14e6",
"type": "incoming_payment",
"attributes": {
"initiation_time": "2020-10-27T11:16:42.364+03:00",
"status": "Pending",
"event": {
"type": "Incoming Payment Request",
"resource": null,
"errors": null
},
"metadata": {
"customer_id": "123456789",
"reference": "123456",
"notes": "Payment for invoice 12345"
},
"_links": {
"callback_url": "https://webhook.site/675d4ef4-0629-481f-83cd-d101f55e4bc8",
"self": "https://sandbox.kopokopo.com/api/v2/incoming_payments/e192ac63-8b24-4caf-8637-a2a9b75a14e6"
}
}
}
}

With an Incoming Payment location url, you can query what the status of the Incoming Payment is. If a corresponding Incoming Payment Result exists, it will be bundled in the payload of the result.

HTTP Request​

GET https://sandbox.kopokopo.com/api/v2/incoming_payments/
Accept: application/json
Authorization: Bearer 0Sn0W6kzNicvoWhDbQcVSKLRUpGjIdlPSEYyrHqrDDoRnQwE7Q
User-Agent: < product >/< product-version > < comment >

Query Parameters​

ParameterRequiredTypeDescription
IDYesStringThe reference / ID of the Incoming Payment to retrieve