mtvbrianking / laravel-mtn-momo

Laravel MTN MoMo API Integration
MIT License
135 stars 47 forks source link

Need a guidance #79

Open ljsharp opened 1 year ago

ljsharp commented 1 year ago

Hello, I have been using Paystack for some times but now want to try MoMo API for our new project and I tried this package and done with the setup. But when trying to run it, an error threw saying Call to a member function isPast() on string even thought the token has been generated after the setup. I am totally new to MoMo API so if you know any tutorials in relation to this package for me to learn, please share it with me. Thanks :).

mtvbrianking commented 1 year ago

https://www.youtube.com/live/EV1NuphqWO4

It's best you share the steps to reproduce the issue… Laravel & Package versions you're using

ljsharp commented 1 year ago

Alright sure. Give me a moment.

ljsharp commented 1 year ago

Steps

D:\learnings\laravel-10\vendor\bmatovu\laravel-mtn-momo\src\Traits\TokenUtilTrait .php:75 public function getProduct() { return $this->product; }

/**
 * Get expires at.
 *
 * @return string|\Datetime
 */
public function getExpiresAt()
{
    return $this->expires_at;
}

/**
 * Determine if a token is expired.
 *
 * @return bool
 */

public function isExpired()
{
    if (is_null($this->expires_at)) {
        return false;
    }

    return $this->expires_at->isPast();
}

}



Please help. Thanks
ljsharp commented 1 year ago

I just figured it out. I checked that if I delete a token whose column product is collection exists in the database, it does not throw the error and works fine. Thanks for the video you shared :)

mtvbrianking commented 1 year ago

Ok then

ljsharp commented 1 year ago

Sorry, let me reopen this issue. Please the issue is not solved yet because every time I checked the token does not expire yet like 40 minutes left to expire, it still threw the error. I checked that the error came from a middleware so I dived into your package and found this function of Token model.

public function isExpired()
    {
        if (is_null($this->expires_at)) {
            return false;
        }

        $expires_at = \DateTime::createFromFormat('Y-m-d H:i:s', $this->expires_at);

        $now = new \DateTime();

        return $now > $expires_at;
    }

When run this code - return Token::firstWhere('product', 'collection'); The result returns.

{
"id": 9,
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0.eyJjbGllbnRJZCI6IjA2OGVlYWNiLTU1ODUtNDZjYS05MDJmLWMzZDRhYjY5MjY4OCIsImV4cGlyZXMiOiIyMDIzLTAzLTA0VDA4OjU4OjM3LjY3MiIsInNlc3Npb25JZCI6IjI2ZjM5NGNlLWRlZGItNGJkZi04ZGRkLTkzNjRiZWZmZjQ3ZSJ9.AvIRb8VyU7HiIGlFLDFTGhyZtNBEKQZ2X986A2rKgp9f0Y20puLL1dmQS8SKV9xYYEsQwrIkJg5WC52aPwbY22_nopTP72VI0q2R0zhRM-8R7LaI3MHV9eR7rh6p8dYN-PoRrLDgkP6zlhc5Xo9qG5OVbsME7OLp7e7POFGYfblkijLDn75CTg6Hr3D0efYEpF9mjrWQ4ceLp7JQBIJGTYn92bj1f_tkwPoAbPYO0u96SlB_splXEkWaGV1YEtZ2OZVgYTNwSGWEZMoZwtzHCVprOa_7b1l4Rg_m0KP_pNbcrctuhbK-RNuycxA7dH-ZWU317TRhngsg0BEP1iX_AQ",
"refresh_token": null,
"token_type": "Bearer",
"product": "collection",
"created_at": "2023-03-04T07:58:39.000000Z",
"updated_at": "2023-03-04T07:58:39.000000Z",
"expires_at": "2023-03-04 08:58:39",
"deleted_at": null
}

You can see that the token's expires is at 8:58:39am but the error is still thrown when the token is bot expired. I suggest you to use Carbon like

public function isExpired()
    {
        if (is_null($this->expires_at)) {
            return false;
        }

      $now = Carbon::now();
      $expiresAt = Carbon::parse($collectModel->expires_at);

      return $now->isAfter($expiresAt);
    }

Thanks.

mtvbrianking commented 1 year ago

Sure @ljsharp let's keep the issue open, I should look into it later.

ljsharp commented 1 year ago

Well noted thanks.