定制 mxgel/mpesa 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mxgel/mpesa

Composer 安装命令:

composer require mxgel/mpesa

包简介

Safaricom M-Pesa integration

README 文档

README

This is a non-official implementation of Safaricom's Mpesa G2 API

How do I get set up?

The preferred way is installation via composer:-

composer require mxgel/mpesa

That's all.

Auth

To communicate with the daraja APIs, you'll always require an access token. This has been simplified for you in this library. All you have to do is provide an instance of Mxgel\MPesa\Auth\Auth to the execute method. A piece of code like:-

    /**
     * @var Auth
     */
    private $auth;

    /**
     * @return \Mxgel\MPesa\Auth\Auth
     */
    public function getAuth(): Auth
    {
        if (!$this->auth) {
            return $this->auth = new Mxgel\MPesa\Auth\Auth([
                'key'    => config('key'),
                'secret' => config('secret'),
            ]);
        }

        return $this->auth;
    }
    

MPESA Express

This API is used to initiate online payment on behalf of a customer.

Example

Sample code (in Laravel) to initiate the payment can be something like this (use Mxgel\MPesa\Requests\LNMO).

$request = LNMO::make($amount, $phoneNumber, $shortCode, 'Wallet top up');
$request->setPassKey(config('LNMO_passkey'))
    ->setBusinessShortCode(config('LNMO_short_code'))
    ->setCallBackURL($callback);


$resp = $request->execute($this->getAuth());
Log::info('STk data', $request->toArray());

//NOTE: For a real application, save this details in a data store

We now need to write a callback that will receive data from safaricom upon request completion by the customer. Remember to save request details above in a data store

Here is a sample of the callback in laravel.

For easy handling of the received data, we use Mxgel\MPesa\Responses\LNMOCallbackResponse class. This strips away unnecessary data and just give us the juice.

// Assuming u have an Express model, u can have this.

Log::debug('LNMO Data', $this->request->all());
$resp = new LNMOCallbackResponse($this->request->get('Body'));
Express::whereMerchantRequestId($resp->getMerchantRequestID())
    ->whereCheckoutRequestId($resp->getCheckoutRequestID())
    ->whereConfirmed(false)                 // Ensure it's yet to be updated
    ->update([
        'receipt_number' => $resp->getMpesaReceiptNumber(),
        'confirmed'      => $resp->completed(),
    ]);

Sometimes you might want to write a cron job to update un-confirmed transactions. Luckily, we can use the status check api, and it's a breeze. Use Mxgel\MPesa\Requests\LNMQ class like so

$request = new LNMQ([
    'businessShortCode' => $business_short_code,
    'checkoutRequestID' => $checkout_request_id,
    'passKey'           => config('LNMO_passkey'),
]);

// Remember our auth object? Yes, we need it.
$resp = $request->execute($this->getAuth());
Log::info("Execute status with data: ", $request->toArray());
Log::info("Executed status check with response", $resp->toArray());
if ($resp->completed()) {
    // Your code here...
}

At this point, your STK should be fine.

统计信息

  • 总下载量: 62
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固