safaricom / mpesa-php-sdk

A PHP sdk for the new Mpesa RESTful APIs
188 stars 211 forks source link

Callback value #57

Open Focuslinkstech opened 11 months ago

Focuslinkstech commented 11 months ago

Is there a way to pass some value when initializing transactions and the same value will be return as callback values?

e.g

return [ ‘invoice’ : ‘inv100837’ ];

and the callback value will be like:

{
"Body": {
"stkCallback": {
"MerchantRequestID": "29115-34620561-1",
"CheckoutRequestID": "ws_CO_191220191020363925",
"ResultCode": 0,
"ResultDesc": "The service request is processed successfully.",
"CallbackMetadata": {
"Item": [{
"Name": "Amount",
"Value": 1.00
},
{
"Name": "MpesaReceiptNumber",
"Value": "NLJ7RT61SV"
},
{
"Name": "TransactionDate",
"Value": 20191219102115
},
{
"Name": "PhoneNumber",
"Value": 254708374149
}],

     return [
          “invoice”: “inv100837”
                 ]
     }        
  }    

} }

the invoice number can be use to verify the transactions.

shabran01 commented 11 months ago

Yes, you can pass a value when initializing a transaction and use that value to identify and verify the transaction in the callback response. In your example, you're using an "invoice" value to uniquely identify a transaction.

Here's how you can structure the transaction initiation and callback response to include the "invoice" value:

  1. Transaction Initiation: When initiating a transaction, you can include the "invoice" value in the request payload or parameters. For example:

{ "invoice": "inv100837", // Other transaction-related information }

1.Callback Response: When the callback is triggered after the transaction is processed, you can include the "invoice" value in the callback response to link it to the original transaction:

json Copy code { "Body": { "stkCallback": { "MerchantRequestID": "29115-34620561-1", "CheckoutRequestID": "ws_CO_191220191020363925", "ResultCode": 0, "ResultDesc": "The service request is processed successfully.", "CallbackMetadata": { "Item": [ { "Name": "Amount", "Value": 1.00 }, { "Name": "MpesaReceiptNumber", "Value": "NLJ7RT61SV" }, { "Name": "TransactionDate", "Value": 20191219102115 }, { "Name": "PhoneNumber", "Value": "254708374149" }, { "Name": "Invoice", "Value": "inv100837" // Include the invoice value here } ] } } } } In the callback response, you've added an "Invoice" item in the "CallbackMetadata" section, which contains the "invoice" value associated with the transaction. You can use this value to verify and link the callback response to the original transaction based on the "invoice" number.

Focuslinkstech commented 11 months ago

here is what i want to send to api using STKpush? the value did not return

$curl_Tranfer2_post_data = [
    'BusinessShortCode' => $Business_Code,
    'Password' => $password,
    'Timestamp' =>$Time_Stamp,
    'TransactionType' =>$Type_of_Transaction,
    'Amount' => $trx['price'],
    'PartyA' => $phone_number,
    'PartyB' => $Business_Code,
    'PhoneNumber' => $phone_number,
    'CallBackURL' => $CallBackURL,
    'AccountReference' => $config['CompanyName'],
    'TransactionDesc' => $trx['plan_name'],
    'Trxid' => $trx['id'],
    'AmountToPay' => $trx['price'],
    ];
shabran01 commented 11 months ago

It looks like you're trying to construct an array of data that you want to send via an API using STKpush. STKpush is a feature in mobile money systems that allows for payments to be initiated via USSD.

The data you've provided seems to be in PHP format, likely for use in a cURL request. Here's a breakdown of the fields you're including:

  1. BusinessShortCode: Your business's short code.
  2. Password: A password associated with your request.
  3. Timestamp: A timestamp for the request.
  4. TransactionType: Type of transaction (presumably a specific code or string indicating the type of transaction).
  5. Amount: The amount of the transaction.
  6. PartyA: The phone number initiating the transaction.
  7. PartyB: The business's short code again.
  8. PhoneNumber: The phone number initiating the transaction (repeated).
  9. CallBackURL: The URL to which transaction response will be sent.
  10. AccountReference: Reference associated with the account.
  11. TransactionDesc: Description of the transaction (presumably the plan name in this case).
  12. Trxid: Transaction ID.
  13. AmountToPay: The amount to be paid in the transaction (presumably the same as the 'Amount').

To send this data using cURL in PHP, you would typically encode it as JSON and include it in the cURL request. Here's a basic example:

$curl_transfer_url = 'YOUR_API_ENDPOINT_URL'; // Replace with your API endpoint URL

$curl_transfer_post_data = json_encode($curl_Tranfer2_post_data);

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $curl_transfer_url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_transfer_post_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch); curl_close($ch);

// Handle the response as needed echo $response; // Or process the response in another way

Make sure to replace 'YOUR_API_ENDPOINT_URL' with the actual endpoint URL you need to send the data to. Also, ensure that the endpoint expects the data in the format you're sending (in this case, as JSON).

If the values are not returning as expected, make sure you're handling the response properly from the API and check for any errors or issues with the endpoint you're calling.

Focuslinkstech commented 11 months ago

Yes, i have did that already, can the stkpush sent back the amontToPay and trxid value send to the api in stkpush because it doesn’t send it back?

shabran01 commented 11 months ago

Yes, the STK Push API can send back the AmountToPay and Trxid values that were sent to the API in the request. To do this, you need to set the IncludeCustomerTrxid parameter to true in the request. This will cause the M-Pesa API to return the CustomerTrxid in the response. The CustomerTrxid is the same as the Trxid parameter that you sent in the request.

The following is an example of a curl command that can be used to send an STK Push request with the IncludeCustomerTrxid parameter set to true:

curl -X POST \ -H "Content-Type: application/json" \ -d '{ "BusinessShortCode": "YOUR_BUSINESS_SHORTCODE", "Password": "YOUR_PASSWORD", "Timestamp": "YOUR_TIMESTAMP", "TransactionType": "C2B", "Amount": 100, "PartyA": "254722000000", "PartyB": "YOUR_BUSINESS_SHORTCODE", "PhoneNumber": "254722000000", "CallBackURL": "YOUR_CALLBACK_URL", "AccountReference": "YOUR_ACCOUNT_REFERENCE", "TransactionDesc": "YOUR_TRANSACTION_DESCRIPTION", "IncludeCustomerTrxid": true }' \ https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest If the STK Push request is successful, the M-Pesa API will return a JSON response with the following information:

MerchantRequestID: A unique identifier for the transaction on the M-Pesa side. CheckoutRequestID: A unique identifier for the transaction on the merchant's side. ResponseType: The type of response. CustomerMessage: A message that will be displayed to the customer on their phone. ErrorCode: An error code if the request failed. ErrorMessage: An error message if the request failed. CustomerTrxid: The customer's transaction ID, which is the same as the Trxid parameter that you sent in the request. You can then use the CustomerTrxid to track the status of the transaction or to refund the customer.

shabran01 commented 11 months ago

STK push requests typically involve initiating a payment or transaction directly with a mobile money service provider. The response from the mobile money provider typically confirms the success or failure of the payment request, along with other relevant details such as transaction ID, status, and more.

The AmountToPay and Trxid values you include in the STK push request are typically for your internal reference and tracking purposes. They are not automatically sent back in the response from the mobile money provider because they are specific to your system and not part of the standard STK push response.

To track the AmountToPay and Trxid values associated with a particular transaction, you should store them in your system's database or log them when you initiate the STK push request. You can then use these values to match the response from the mobile money provider with the original request you made.

In summary, the mobile money provider's response to an STK push will confirm the success or failure of the transaction, but it won't include the AmountToPay and Trxid values you sent in the request. You'll need to handle and manage these values within your own system.

On Sun, Sep 24, 2023 at 6:47 PM Focuslinkstech @.***> wrote:

Yes, i have did that already, can the stkpush sent back the amontToPay and trxid value send to the api in stkpush because it doesn’t send it back?

— Reply to this email directly, view it on GitHub https://github.com/safaricom/mpesa-php-sdk/issues/57#issuecomment-1732601450, or unsubscribe https://github.com/notifications/unsubscribe-auth/BCVRLFQACLXJJOJDQBXKLBDX4BI2NANCNFSM6AAAAAA5BL5HIU . You are receiving this because you commented.Message ID: @.***>