Skip to content

Importing Class

Mauko Maunde edited this page Jan 8, 2021 · 2 revisions

Import the class namespace into your class or app to make it available for use. Replace C2B with your API of choice. We will be using C2B here.

The C2B API enables Paybill and Buy Goods merchants to integrate to M-Pesa and receive real time payments notifications. A user pays through the traditional payment process (i.e goes to M-Pesa menu on their phone and makes the payment to your shortcode). The transaction details are then sent to your app.

This could come in handy and work as a backup to STK push, should the prompt fail, either because the user has not enabled their SIM, or it timed out before they saw it. In this case you should display appropriate instructions for the user to make this payment, making sure to supply your shortcode, and account number (for Paybills).

See how to set up B2C here and B2B here.

use Osen\Mpesa\C2B;

The classes use static methods and does not need to be instantiated. This is to persist configuration in memory troughout execution of the script. To pass configuration options to the object, use the init() method at the top of your script. The headoffice key is only required for Till Numbers. Paybill users can ignore it.

C2B::init(
    array(
        "env"               => "sandbox",
        "type"              => 4, // For Paybill, or, 2 for Till, 1	for MSISDN
        "shortcode"         => "174379",
        "headoffice"        => "174379", // Ignore if using Paybill
        "key"               => "Your Consumer Key",
        "secret"            => "Your Consumer Secret",
        "username"          => "", // Required for B2B and B2C APIs only
        "password"          => "", // Required for B2B and B2C APIs only
        "passkey"           => "Your Online Passkey",
        "validation_url"    => "api/lipwa/validate",
        "confirmation_url"  => "api/lipwa/confirm",
        "callback_url"      => "api/lipwa/reconcile",
        "results_url"       => "api/lipwa/results")
    )
);

TIP: You can just pass your URL endpoints for testing on sandbox, the system will use the test credentials provided from Daraja. e.g

C2B::init(
    array(
        "validation_url"    => "api/lipwa/validate",
        "confirmation_url"  => "api/lipwa/confirm",
        "callback_url"      => "api/lipwa/reconcile",
        "results_url"       => "api/lipwa/results")
    )
);
Clone this wiki locally