crysoft/mpesa-bundle

Symfony Bundle 用于 Safaricom 在线 Mpesa API 实现

v1.0.0 2017-06-09 10:24 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:32:53 UTC


README

这是一个用于集成 Safaricom M-Pesa 在线 API 的 Symfony Bundle。该 API 允许商家发起在线 C2B(通过网页的 Paybill)交易。商家提交认证详情、交易详情、回调 URL 和回调方法。

通常在请求提交后,商家会立即收到请求有效性的反馈,但此 Bundle 实现了对该状态的定制请求。C2B API 通过 USSD 推送处理客户验证和认证。然后客户确认交易。

要求

  • PHP 5.6 或更高版本
  • Symfony 2.6 或更高版本

安装

要安装此 Bundle,运行以下命令,您将通过 [Packagist][4] 获取最新版本。

composer require crysoft/mpesa-bundle

加载 Bundle

在 AppKernel.php 中加载 Bundle

new Crysoft\MpesaBundle\CrysoftMpesaBundle(),

Bundle 的配置

在 config.yml 中配置

crysoft_mpesa:
    mpesa:
        
        endpoint:           https://safaricom.co.ke/mpesa_online/lnmo_checkout_server.php?wsdl                   
        callback_url:       http://yourcallbackurl.com/successurl                                                
        callback_method:    POST                                                                                
        paybill_number:     123456                                                                               
        pass_key:           verysecretlongkey                                                                    

        ...

配置选项

M-Pesa API 端点。请确认这一点,因为它可能会在某些时候发生变化

在交易完成后,Safaricom 将查询的完整回调 URL。

  • callback_method: POST

要使用的回调方法。也可以是 GET

  • paybill_number: 123456

商家的 Paybill 号码。

  • pass_key: verysecretlongkey

Safaricom 在注册时给出的 SAG Passkey。你可能需要请求它。

用法

Bundle 的用法很简单。在您的控制器中使用它,您可以通过 "$this->container" 访问服务容器,它反过来又访问配置变量。

在您的控制器中执行

public function checkoutAction()
{
     //Instantiate a new Mpesa Request passing it the Service Container(Don't worry if you dont know what that is, 
     //Symfony does :-) just do as i do below and you'll be fine)
     $mpesa = new Mpesa($this->container);
     
     //Generate a New Transaction Id unique to thsi transaction. You can generate it however you want or simply use an 
     //Order ID or User ID. it's completely up to you as long as it's unique
     // We just provided this method as an easy way out for those of us too lazy to think of a different way of doing it.   
     $transactionId = $mpesa->generateTransactionNumber();
    
     $response = $mpesa->request(2500)->from(0722000000)->usingReferenceId(456876)->usingTransactionId($transactionId)->transact();

}

您可以像上面那样将方法调用链式连接成一个单独的调用。此 Bundle 包含一个名为 "generateTransactionNumber()" 的便捷方法,它会为您生成一个随机交易编号。请注意生成的交易 ID/编号,因为您将使用它来查询交易状态,而不是等待 Safaricom 进行回调。

请求状态非常简单,因为此 Bundle 也提供了一个简单的方法来做这件事

public function checkstatusAction()
{
    //Use the same Mpesa Class as before
    $mpesa = new Mpesa($this->container);
    
    //Chain the requests. Note this Transaction ID has to be the EXACT same one you used for the Mpesa Transaction request above. 
    $response = $mpesa->usingTransactionId($transactionId)->requestStatus();
    
    //And just to make your life easier we created another class in the Bundle to run through the response from Mpesa and give you the Status
    
    //Use the response above and pass it to the Bundle's MpesaStatus Class
    $mpesaStatus = new MpesaStatus($response);
    
    //Use the Classes Getter methods to get the Bits in the Response. Here is an example of how to do it
    
        $customerNumber             =       $mpesaStatus->getCustomerNumber();
        $transactionAmount          =       $mpesaStatus->getTransactionAmount();
        $transactionStatus          =       $mpesaStatus->getTransactionStatus();
        $transactionDate            =       $mpesaStatus->getTransactionDate();
        $mPesaTransactionId         =       $mpesaStatus->getMpesaTransactionId();
        $merchantTransactionId      =       $mpesaStatus->getMerchantTransactionId();
        $transactionDescription     =       $mpesaStatus->getTransactionDescription();
}

您不必命名变量,就像我们命名的那样,您可以随意命名。就是这样。您就可以开始了

##测试

在测试时要小心,Paybill 将从 Mpesa 中扣除金额。您可以使用 10 kes,这是允许的最小金额。

##许可证

M-Pesa 包是开源软件,许可协议为 MIT 许可