Handle payments
Following a request to accept or make payments, a response from the server is received.
This response contains details about the transaction you just initiated. Either it is the reasons for which we could not initiate a transaction (there is therefore an error) or it is the data on the initiated transaction.
In all cases, the response of a transaction must be handled.
Handling a payment request
Below is an example of payment initiation and response received in return.
- NodeJs
- Laravel
- PHP
const momo = require("@chipdeals/momo-api");momo.setApiKey("test_FOdigzgSopV8GZggZa89");momo .collect() .amount(100) .currency("XOF") .from("22951010200") .firstName("Iyam") .lastName("EVERICH") .create() .onSuccess((paymentData) => handlePyamentSuccess(paymentData)) .onError((paymentData) => handlePaymentFails(paymentData));function handlePyamentSuccess(paymentData) { console.log("payment succeeded"); console.log("payment data: ", paymentData);}function handlePyamentSuccess(paymentData) { console.log("payment failed");}
dev@Chipdeals:~/Chipdeals/projects/nodejs-doc$ node collection.js2022-11-13 08:03:32:527 Collection request2022-11-13 08:03:36:126 TEST [202] Transaction is pending2022-11-13 08:03:39:287 TEST [201] Data in validation2022-11-13 08:03:46:138 TEST [203] Data are validated, server is working2022-11-13 08:03:51:125 TEST [204] Waiting for ussd push validation2022-11-13 08:03:54:898 TEST [200] Successfully processed transaction2022-11-13 08:03:54:898 TEST [OK] 100 XOF received form customerpayment succeededpayment data: { reference: 'f1d87806-72c0-4a46-8560-89c3fb6f7070', senderPhoneNumber: '22951010200', senderOperator: 'MTN', senderFirstName: 'Iyam', senderLastName: 'EVERICH', currency: 'XOF', amount: 100, status: 'success', statusMessage: 'Successfully processed transaction', startTimestampInSecond: 1668326614, endTimestampInSecond: 0, senderCountryCode: 'BJ', originalCurrency: 'XOF', statusMessageCode: 200, originalAmount: 100, transactionType: 'payment'}
<?phpuse Illuminate\Support\Facades\Route;Route::get('/collect', function () { $momo = new \Chipdeals\MomoApi\Momo(); $momo->setApiKey("test_FOdigzgSopV8GZggZa89"); $transaction = $momo ->collect() ->amount(100) ->currency("XOF") ->from("22990630401") ->firstName("Iyam") ->lastName("EVERICH") ->webhookUrl("https://webhook.site/e73cfd43-9703-4759-bf17-28d172f3b6fd") ->create(); $status = $transaction->getStatus(); if ($status == "pending") { echo "transaction started and is pending <br/>"; print_r($transaction->getArray()); } else { echo "transaction failed"; }});
transaction started and is pending
Array
(
[reference] => aca59aa9-830b-4bf7-bb0a-74cd438130ee
[phoneNumber] => 22990630401
[currency] => XOF
[operator] => MTN
[firstName] => Iyam
[lastName] => EVERICH
[originalCurrency] => XOF
[originalAmount] => 100
[amount] => 100
[status] => pending
[statusMessage] => Transaction is pending
[statusCode] => 202
[startTimestampInSecond] => 1668327832
[endTimestampInSecond] => 0
[isCollection] => true
)
<?phprequire_once("path/to/chipdeals-mobile-money-api.php");$momo = new Momo();$momo->setApiKey("test_FOdigzgSopV8GZggZa89");$transaction = $momo ->collect() ->amount(100) ->currency("XOF") ->from("22990630401") ->firstName("Iyam") ->lastName("EVERICH") ->webhookUrl("https://webhook.site/e73cfd43-9703-4759-bf17-28d172f3b6fd") ->create();$status = $transaction->getStatus();if ($status == "pending") { echo "transaction started and is pending <br/>"; print_r($transaction->getArray());} else { echo "transaction failed";}
transaction started and is pending
Array
(
[reference] => aca59aa9-830b-4bf7-bb0a-74cd438130ee
[phoneNumber] => 22990630401
[currency] => XOF
[operator] => MTN
[firstName] => Iyam
[lastName] => EVERICH
[originalCurrency] => XOF
[originalAmount] => 100
[amount] => 100
[status] => pending
[statusMessage] => Transaction is pending
[statusCode] => 202
[startTimestampInSecond] => 1668327832
[endTimestampInSecond] => 0
[isCollection] => true
)
Details of the Response object to a payment
The response format is the same as the one presented in the introduction
If the request is validated, a payment
property that replaces anyObjet
is added. The payment
object contains details of the transaction. It is structured as follows:
Property | Example | Description |
---|---|---|
reference | "aca59aa9-830b-4bf7-bb0a-74cd438130ee" | Transaction reference |
senderPhoneNumber | "22951945200" | Phone number of paying user |
senderCountryCode | "BJ" | Phone number of paying user |
senderOperator | "MTN" | Phone number of paying user |
senderFirstName | "iyam" | First name of paying user |
senderLastName | "EVERICH" | Paying User Name |
originalCurrency | "XOF" | Payment currency you indicated in your request |
currency | "XOF" | Local currency of the user's phone number |
status | "pending" | Transaction status (pending or success or error) |
statusMessage | "pending" | Message explaining what exactly happens for the transaction |
statusMessageCode | 202 | Specific code of the exact state of the transaction. See more |
startTimestampInSecond | 1655968250 | Timestamp in seconds when you initiated the payment |
endTimestampInSecond | 0 | Timestamp in seconds when a transaction is completed |
amount | 10 | Amount in local currency that the user pays |
originalAmount | 10 | Amount you specified for the user to pay in your request |
transactionType | "payment" | Transaction Type. Always payment in the case of a payment request |
Handling a deposit request
Below is an example of the initiation of a deposit and the response received in return.
- NodeJs
- Laravel
- PHP
const momo = require("@chipdeals/momo-api");momo.setApiKey("test_FOdigzgSopV8GZggZa89");momo .deposit() .amount(100) .currency("XOF") .to("22951010200") .create() .onSuccess((paymentData) => handlePaymentSuccess(paymentData)) .onError((paymentData) => handlePaymentFailed(paymentData));function handlePaymentSuccess(paymentData) { console.log("deposit succeeded"); console.log("deposit data: ", paymentData);}function handlePaymentFailed(paymentData) { console.log("deposit failed");}
dev@Chipdeals:~/Chipdeals/projects/nodejs-doc$ node deposit.js2022-11-13 11:36:26:129 Disbursement request2022-11-13 11:36:27:765 TEST [202] Transaction is pending2022-11-13 11:36:34:304 TEST [201] Data in validation2022-11-13 11:36:38:938 TEST [203] Data are validated, server is working2022-11-13 11:36:42:374 TEST [200] Successfully processed transaction2022-11-13 11:36:42:375 TEST [OK] 100 XOF sent to customerdeposit succeededdeposit data: { reference: '4c732c68-0bfe-428f-a95e-ae14cc815505', recipientPhoneNumber: '22951010200', recipientOperator: 'MTN', currency: 'XOF', amount: 100, status: 'success', statusMessage: 'Successfully processed transaction', startTimestampInSecond: 1668339387, endTimestampInSecond: 0, recipientCountryCode: 'BJ', originalCurrency: 'XOF', statusMessageCode: 200, originalAmount: 100, transactionType: 'deposit'}
<?phpuse Illuminate\Support\Facades\Route;Route::get('/deposit', function () { $momo = new \Chipdeals\MomoApi\Momo(); $momo->setApiKey("test_FOdigzgSopV8GZggZa89"); $transaction = $momo ->deposit() ->amount(100) ->currency("XOF") ->to('22990630401') ->webhookUrl("https://webhook.site/e73cfd43-9703-4759-bf17-28d172f3b6fd") ->create(); $status = $transaction->getStatus(); if ($status == "pending") { echo "transaction started and is pending <br/>"; print_r($transaction->getArray()); } else { echo "transaction failed"; }});
transaction started and is pending
Array
(
[reference] => f46efbee-1aca-4be7-934f-08ec0f25b726
[phoneNumber] => 22990630401
[currency] => XOF
[operator] => MTN
[firstName] =>
[lastName] =>
[originalCurrency] => XOF
[originalAmount] => 100
[amount] => 100
[status] => pending
[statusMessage] => Transaction is pending
[statusCode] => 202
[startTimestampInSecond] => 1668338996
[endTimestampInSecond] => 0
[isCollection] => false
)
<?phprequire_once("path/to/chipdeals-mobile-money-api.php");$momo = new Momo();$momo->setApiKey("test_FOdigzgSopV8GZggZa89");$transaction = $momo ->deposit() ->amount(100) ->currency("XOF") ->to('22990630401') ->webhookUrl("https://webhook.site/e73cfd43-9703-4759-bf17-28d172f3b6fd") ->create();$status = $transaction->getStatus();if ($status == "pending") { echo "transaction started and is pending <br/>"; print_r($transaction->getArray());} else { echo "transaction failed";}
transaction started and is pending
Array
(
[reference] => f46efbee-1aca-4be7-934f-08ec0f25b726
[phoneNumber] => 22990630401
[currency] => XOF
[operator] => MTN
[firstName] =>
[lastName] =>
[originalCurrency] => XOF
[originalAmount] => 100
[amount] => 100
[status] => pending
[statusMessage] => Transaction is pending
[statusCode] => 202
[startTimestampInSecond] => 1668338996
[endTimestampInSecond] => 0
[isCollection] => false
)
Details of the Response object of a deposit
The response format is the same as the one presented in the introduction
If the request is validated, a deposit
property which replaces anyObjet
is added. The deposit
object contains details of the transaction. It is structured as follows:
Property | Example | Description |
---|---|---|
reference | "f46efbee-1aca-4be7-934f-08ec0f25b726" | Transaction reference |
recipientPhoneNumber | "22951945200" | Beneficiary phone number |
recipientCountryCode | "BJ" | Beneficiary country code |
recipientOperator | "MTN" | Beneficiary operator |
originalCurrency | "XOF" | Payment currency you indicated in your application |
currency | "XOF" | Local Currency of Recipient Phone Number |
status | "pending" | Transaction status (pending or success or error) |
statusMessage | "pending" | Message explaining what exactly happens for the transaction |
statusMessageCode | 202 | Specific code of the exact state of the transaction. See more |
startTimestampInSecond | 1655968250 | Timestamp in seconds when you initiated the payment |
endTimestampInSecond | 0 | Timestamp in seconds when a transaction is completed |
amount | 10 | Amount in local currency that the user receives |
originalAmount | 10 | Amount you specified the user will receive in your request |
transactionType | "deposit" | Transaction Type. Always deposit in the case of a deposit |