145 linhas
4,6 KiB
Markdown
145 linhas
4,6 KiB
Markdown
<p align="center"><img src="assets/logo.png" alt="Interweb" width="280"></p>
|
|
|
|
# Laravel package for Interweb Angola
|
|
|
|
<!-- badges:start -->
|
|
[](https://git.interweb.ao/interweb-ao/laravel/releases) [](LICENSE)
|
|
<!-- badges:end -->
|
|
|
|
Send SMS notifications through the SMS Center, send your app's mail through E-mail Center, and work with domains, DNS and WHOIS from Laravel, all on the Interweb Angola API.
|
|
|
|
[Português](README-pt.md)
|
|
|
|
## Install
|
|
|
|
```bash
|
|
composer require interweb-ao/laravel
|
|
```
|
|
|
|
Laravel 8 to 12, PHP 7.4 or newer with cURL. No other packages. Laravel finds the service provider and the `InterwebAo` facade on its own.
|
|
|
|
Add your API key to `.env`. That is the only setting you need: the address of the API is already in the package.
|
|
|
|
```dotenv
|
|
INTERWEB_API_KEY=sandbox_...
|
|
```
|
|
|
|
Create the key in the [API Center](https://www.meu.interweb.ao/api-center.php) of Meu Interweb. Start with a `sandbox_` key: writes are simulated and nothing is charged. When you are ready, create a `prod_` key with only the permissions you use. Check the key with:
|
|
|
|
```bash
|
|
php artisan interweb_ao:check
|
|
```
|
|
|
|
To change the language of the errors, the timeout or the default SMS sender, publish the config file:
|
|
|
|
```bash
|
|
php artisan vendor:publish --tag=interweb_ao-config
|
|
```
|
|
|
|
## SMS notifications
|
|
|
|
Return `'sms'` from `via()` and write a `toSms()` method:
|
|
|
|
```php
|
|
use InterwebAo\Laravel\Notifications\SmsMessage;
|
|
|
|
class OrderShipped extends Notification
|
|
{
|
|
public function via($notifiable)
|
|
{
|
|
return ['mail', 'sms'];
|
|
}
|
|
|
|
public function toSms($notifiable)
|
|
{
|
|
return SmsMessage::create('Your order 1234 is on its way.');
|
|
// ->sender('MYSHOP') for an approved sender, ->sendAt(now()->addHour()) to schedule
|
|
}
|
|
}
|
|
```
|
|
|
|
The number comes from the notifiable:
|
|
|
|
```php
|
|
public function routeNotificationForSms($notification)
|
|
{
|
|
return $this->phone; // +244923123456 or 923 123 456
|
|
}
|
|
```
|
|
|
|
Or send to any number: `Notification::route('sms', '+244923123456')->notify(new OrderShipped());`
|
|
|
|
Queued notifications are safe to retry: the same notification never sends the SMS twice. SMS must be active in the SMS Center of the account, and the key needs `sms:write`.
|
|
|
|
## Mail through E-mail Center
|
|
|
|
Add a mailer to `config/mail.php`:
|
|
|
|
```php
|
|
'mailers' => [
|
|
'interweb_ao' => [
|
|
'transport' => 'interweb_ao',
|
|
],
|
|
// ...
|
|
],
|
|
```
|
|
|
|
Then set `MAIL_MAILER=interweb_ao` in `.env`, or use it for some emails only with `Mail::mailer('interweb_ao')`. Mailables, notifications by mail, attachments, Cc, Bcc and your own `X-` headers all work. The sender (`MAIL_FROM_ADDRESS`) must be on a sender domain verified in E-mail Center, and the key needs `email_center:write`.
|
|
|
|
You can also send with an E-mail Center template by adding `'template_id' => 7` to the mailer, and use `'variables'` for its values.
|
|
|
|
## The rest of the API
|
|
|
|
```php
|
|
use InterwebAo\Laravel\Facades\InterwebAo;
|
|
|
|
InterwebAo::domains()->availability('mycompany.co.ao');
|
|
InterwebAo::whois()->lookup('example.co.ao'); // works without a key too
|
|
InterwebAo::dns()->records('example.co.ao');
|
|
InterwebAo::dns()->upsert('example.co.ao', 'www', 'A', '192.0.2.10', 3600);
|
|
InterwebAo::sms()->send('+244923123456', 'Hello!');
|
|
InterwebAo::sms()->balance();
|
|
InterwebAo::emailCenter()->send(['from' => 'notifications@example.co.ao', 'to' => 'ana@example.org', 'subject' => 'Hi', 'text' => 'Hello Ana']);
|
|
|
|
// Any other operation of the API:
|
|
InterwebAo::get('/v1/services');
|
|
InterwebAo::post('/v1/domains/register', ['domain' => 'mycompany.co.ao', 'years' => 1]);
|
|
```
|
|
|
|
Or inject `InterwebAo\Laravel\Client` where you need it. `POST` requests carry an `Idempotency-Key`, so a retry never registers or sends twice. Answers `429`, `500`, `502` and `503` are retried after a short wait.
|
|
|
|
`InterwebAo::wasSimulated()` tells you whether the last write was only simulated (Sandbox key).
|
|
|
|
## Errors
|
|
|
|
Every error is an `InterwebAo\Laravel\ApiException`:
|
|
|
|
```php
|
|
use InterwebAo\Laravel\ApiException;
|
|
|
|
try {
|
|
InterwebAo::domains()->register('mycompany.co.ao', 1);
|
|
} catch (ApiException $e) {
|
|
$e->getApiCode(); // insufficient_credit, rate_limited, insufficient_scope...
|
|
$e->getStatus(); // 402, 429, 403...
|
|
$e->hint(); // what to do, in one sentence
|
|
$e->getDetails(); // extra data, such as add_funds_url
|
|
$e->getCorrelationId(); // give this to support, never the key
|
|
}
|
|
```
|
|
|
|
The full list of codes is in the [API documentation](https://docs.interweb.ao/api-rules/).
|
|
|
|
## Tests
|
|
|
|
```bash
|
|
php tests/run.php
|
|
```
|
|
|
|
## License
|
|
|
|
MIT. See [LICENSE](LICENSE).
|
|
|
|
---
|
|
|
|
Maintained by RLX Holdings.
|