Skip to content

Commit

Permalink
Support Get Registered IPNs Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
njoguamos committed Mar 18, 2024
1 parent ede2299 commit d2112a2
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 5 deletions.
65 changes: 64 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ $token = Pesapal::createToken();
# $token->expires_at -> Carbon\Carbon instance
````

### Create Instant Payment Notification.
### Create Instant Payment Notification

To create an instant payment notification, you can use the `createIpn` method in the `Pesapal` class. The method will return an instance of `PesapalIpn` or null if the request fails.

Expand All @@ -123,6 +123,69 @@ You can go ahead and use the `ipn_id` to submit a Submit Order Requests.

> **info** Ensure that that your `pesapal_tokens` table as an `access_token` that is not expired. Of course, if you scheduled the `pesapal:auth` command, you should not worry about the `access_token` being expired.

### Get Registered IPNs Endpoint

There are two ways to get the registered IPNs.

1. You can use the `getIpns` method in the `Pesapal` class to get a IPN from Pesapal API.

```php
use NjoguAmos\Pesapal\Pesapal;

$response = Pesapal::getIpns();
```

```json
[
{
"url": "https://www.myapplication.com/ipn",
"created_date": "2022-03-03T17:29:03.7208266Z",
"ipn_id": "e32182ca-0983-4fa0-91bc-c3bb813ba750",
"error": null,
"status": "200"
},
{
"url": "https://ipn.myapplication.com/application2",
"created_date": "2021-12-05T04:23:45.5509243Z",
"ipn_id": "c3bb813ba750-0983-4fa0-91bc-e32182ca",
"error": null,
"status": "200"
}
]
```

2. or get the IPNs from the database.

```php
use NjoguAmos\Pesapal\Models\PesapalIpn;

$ips = PesapalIpn::all();
```


```php
[
[
"id" => 1
"url" => "http://kautzer.com/omnis-ut-qui-illo-id-laborum-numquam"
"ipn_id" => "767e3275-d504-41a0-920a-dd752aafb5ac"
"type" => 0
"status" => 1
"created_at" => "2024-03-18T08:10:32.000000Z"
"updated_at" => "2024-03-18T05:10:32.000000Z"
],
[
"id" => 2
"url" => "http://www.cole.org/qui-fugiat-accusamus-molestiae-aspernatur-sequi-eum-non-quae.html"
"ipn_id" => "de07604f-c06b-4ccf-9cb5-dd75aaaff99f"
"type" => 0
"status" => 1
"created_at" => "2024-03-18T08:10:33.000000Z"
"updated_at" => "2024-03-18T05:10:33.000000Z"
]
]
```

## Testing

> **Info** Where possible, the tests uses real [sandbox credentials](https://developer.pesapal.com/api3-demo-keys.txt), and as such the request is not mocked. This ensures the stability of the package. Where it is impossible to use real credentials, the request is mocked. Therefore you must be connected to the internet to run the some of the tests.
Expand Down
20 changes: 20 additions & 0 deletions src/Pesapal.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use NjoguAmos\Pesapal\Requests\CreatePesapalToken;
use Carbon\Carbon;
use JsonException;
use NjoguAmos\Pesapal\Requests\GetPesapalIpns;
use Saloon\Exceptions\Request\FatalRequestException;
use Saloon\Exceptions\Request\RequestException;

Expand Down Expand Up @@ -71,4 +72,23 @@ public static function createIpn(string $url, IpnType $ipnType): ?PesapalIpn

return null;
}

/**
* @throws FatalRequestException
* @throws RequestException
* @throws JsonException
*/
public static function getIpns(): ?array
{
$connector = new PesapalConnector();
$request = new GetPesapalIpns();

$response = $connector->send($request);

if ($response->ok()) {
return $response->array();
}

return null;
}
}
16 changes: 16 additions & 0 deletions src/Requests/GetPesapalIpns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace NjoguAmos\Pesapal\Requests;

use Saloon\Enums\Method;
use Saloon\Http\Request;

class GetPesapalIpns extends Request
{
protected Method $method = Method::GET;

public function resolveEndpoint(): string
{
return '/api/URLSetup/GetIpnList';
}
}
23 changes: 19 additions & 4 deletions tests/PesapalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,34 @@
use NjoguAmos\Pesapal\Models\PesapalToken;
use NjoguAmos\Pesapal\Pesapal;

it(description: 'can get access token from pesapal and save to database', closure: function () {
it(description: 'can create access token and save to database', closure: function () {
Pesapal::createToken();

expect(PesapalToken::count())->toBe(1);
expect(value: PesapalToken::count())->toBe(expected: 1);
});

it(description: 'can get Instant Payment Notification from pesapal and save to database', closure: function () {
it(description: 'can create Instant Payment Notification and save to database', closure: function () {
Pesapal::createToken();

Pesapal::createIpn(
url: fake()->url,
ipnType: IpnType::GET,
);

expect(PesapalIpn::count())->toBe(1);
expect(value: PesapalIpn::count())->toBe(expected: 1);
});

it(description: 'can get a list of Instant Payment Notifications', closure: function () {
Pesapal::createToken();

Pesapal::createIpn(
url: fake()->url,
ipnType: IpnType::GET,
);

$response = Pesapal::getIpns();

// For some reason, the response is empty on sandbox environment
// TODO: Refactor this test to use a mock response
expect(value: $response)->toBeEmpty();
});

0 comments on commit d2112a2

Please sign in to comment.