laravel-mtn-momo
laravel-mtn-momo copied to clipboard
Need a guidance
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 :).
https://www.youtube.com/live/EV1NuphqWO4
It's best you share the steps to reproduce the issue… Laravel & Package versions you're using
Alright sure. Give me a moment.
Steps
- Run the code
try { $collection = new Collection(); $transactionId = generatedTransactionId(); // Automatically generated id for the transaction id in order $momoTransactionId = $collection->requestToPay($transactionId, '46733123453', 100, "Thanks for paying our products", "Pay for items"); return $collection->getTransactionStatus($momoTransactionId); } catch(CollectionRequestException $e) { do { printf( "\n\r%s:%d %s (%d) [%s]\n\r", $e->getFile(), $e->getLine(), $e->getMessage(), $e->getCode(), get_class($e) ); } while ($e = $e->getPrevious()); }
- Error throws
Call to a member function isPast() on string
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
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 :)
Ok then
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.
Sure @ljsharp let's keep the issue open, I should look into it later.
Well noted thanks.