Skip to content
This repository was archived by the owner on Nov 1, 2025. It is now read-only.

Commit c050f21

Browse files
committed
Added initial functionality
1 parent cd12f1b commit c050f21

19 files changed

+620
-52
lines changed

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) Codebar AG <ruslan.steiger@codebar.ch>
3+
Copyright (c) Codebar AG <info@codebar.ch>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 155 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,203 @@
66
[![Psalm](https://github.com/codebar-ag/laravel-twilio-verify/actions/workflows/psalm.yml/badge.svg)](https://github.com/codebar-ag/laravel-twilio-verify/actions/workflows/psalm.yml)
77
[![Check & fix styling](https://github.com/codebar-ag/laravel-twilio-verify/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/codebar-ag/laravel-twilio-verify/actions/workflows/php-cs-fixer.yml)
88

9-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
9+
This package was developed to give you a quick start to communicate start and
10+
check a verification with Twilio Verify.
1011

11-
## Installation
12+
⚠️ This package is not designed as a replacement of the official
13+
[Twilio REST API](https://www.twilio.com/docs/verify/api).
14+
See the documentation if you need further functionality. ⚠️
15+
16+
## 💡 What is Twilio Verify?
17+
An elegant third-party integration to validate users with SMS, Voice, Email and
18+
Push. Add verification to any step of your user‘s journey with a single API.
19+
20+
## 🛠 Requirements
21+
22+
- PHP: `^8.0`
23+
- Laravel: `^8.12`
24+
- Twilio Account
25+
26+
## ⚙️ Installation
1227

1328
You can install the package via composer:
1429

1530
```bash
16-
composer require codebar-ag/laravel_twilio_verify
31+
composer require codebar-ag/laravel-twilio-verify
1732
```
1833

19-
You can publish and run the migrations with:
34+
Add the following environment variables to your `.env` file:
2035

2136
```bash
22-
php artisan vendor:publish --provider="CodebarAg\TwilioVerify\TwilioVerifyServiceProvider" --tag="laravel_twilio_verify-migrations"
23-
php artisan migrate
37+
TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
38+
TWILIO_AUTH_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
39+
TWILIO_SERVICE_SID=VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
40+
```
41+
42+
## 🏗 Usage
43+
44+
```php
45+
use CodebarAg\TwilioVerify\Facades\TwilioVerify;
46+
47+
/**
48+
* Start a new SMS verification to the phone number.
49+
*/
50+
$verificationStart = TwilioVerify::start(to: '+12085059915');
51+
52+
/**
53+
* Check a verification code.
54+
*/
55+
$verificationCheck = TwilioVerify::check(to: '+12085059915', code: '1337');
2456
```
2557

58+
### 🔢 Verification Code Limits
59+
60+
- The generated code is valid for (10 minutes)[ https://www.twilio.com/docs/verify/api/rate-limits-and-timeouts].
61+
- You have attempted to send the verification code more than 5 times and have
62+
(reached the limit)[https://www.twilio.com/docs/api/errors/60203].
63+
64+
## 🏋️ DTO showcase
65+
66+
```php
67+
CodebarAg\TwilioVerify\DTO\SendCodeAttempt {
68+
+time: Illuminate\Support\Carbon // Carbon
69+
+channel: "sms" // string
70+
+attempt_sid: "VLMn1NOnmIVWMITO4FbVGxNmEMJ72KKaB2" // string
71+
}
72+
```
73+
74+
```php
75+
CodebarAg\TwilioVerify\DTO\Lookup {
76+
+carrier: CodebarAg\TwilioVerify\DTO\Carrier {
77+
+error_code: null // ?string
78+
+name: "Carrier Name" // string
79+
+mobile_country_code: "310" // string
80+
+mobile_network_code: "150" // string
81+
+type: "150" // string
82+
}
83+
}
84+
```
85+
86+
```php
87+
CodebarAg\TwilioVerify\DTO\VerificationStart
88+
+sid: "VEkEJNNkLugY4hietPDbcqUUZz3G5NoTTZ" // string
89+
+service_sid: "VAssMsB84NdN0aJJceYsExX1223qAmrubx" // string
90+
+account_sid: "ACizUsoInA3dbKR5LA9tOqqA0O3NFSHSNc" // string
91+
+to: "+41795555825" // string
92+
+channel: "sms" // string
93+
+status: "pending" // string
94+
+valid: false // bool
95+
+created_at: Illuminate\Support\Carbon // Carbon
96+
+updated_at: Illuminate\Support\Carbon // Carbon
97+
+lookup: CodebarAg\TwilioVerify\DTO\Lookup {...} // Lookup
98+
+send_code_attempts: Illuminate\Support\Collection { // Collection
99+
0 => CodebarAg\TwilioVerify\DTO\SendCodeAttempt {
100+
+time: Illuminate\Support\Carbon // Carbon
101+
+channel: "sms" // string
102+
+attempt_sid: "VLTvj9jhh76cI78Hc1x0c3UORWJwwqVeTN" // string
103+
}
104+
]
105+
}
106+
+url: "https://verify.twilio.com/v2/Services/VAssMsB84NdN0aJJceYsExX1223qAmrubx/Verifications" // string
107+
}
108+
```
109+
110+
```php
111+
CodebarAg\TwilioVerify\DTO\VerificationCheck {
112+
+sid: "VEvRzh4hPUqmAjeC6li092VNT0yfd23lag" // string
113+
+service_sid: "VAxSR0Wq91djjG9PAYtrtjt11f0I4lqdwa" // string
114+
+account_sid: "ACcI5zbEYvLr0vPIUTQzWkTpP5DPqTCYDK" // string
115+
+to: "+41795555825" // string
116+
+channel: "sms" // string
117+
+status: "approved" // string
118+
+valid: true // bool
119+
+created_at: Illuminate\Support\Carbon // Carbon
120+
+updated_at: Illuminate\Support\Carbon // Carbon
121+
}
122+
```
123+
124+
## 🔧 Configuration file
125+
26126
You can publish the config file with:
27127
```bash
28-
php artisan vendor:publish --provider="CodebarAg\TwilioVerify\TwilioVerifyServiceProvider" --tag="laravel_twilio_verify-config"
128+
php artisan vendor:publish --provider="CodebarAg\TwilioVerify\TwilioVerifyServiceProvider" --tag="laravel-twilio-verify-config"
29129
```
30130

31131
This is the contents of the published config file:
32132

33133
```php
134+
34135
return [
136+
137+
/*
138+
|--------------------------------------------------------------------------
139+
| Twilio Verify Configuration
140+
|--------------------------------------------------------------------------
141+
|
142+
| You can find your Account SID and Auth Token in the Console Dashboard.
143+
| Additionally you should create a new Verify service and paste it in
144+
| here. Afterwards you are ready to communicate with Twilio Verify.
145+
|
146+
*/
147+
148+
'url' => env('TWILIO_URL', 'https://verify.twilio.com/v2/Services'),
149+
'account_sid' => env('TWILIO_ACCOUNT_SID', 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'),
150+
'auth_token' => env('TWILIO_AUTH_TOKEN', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'),
151+
'service_sid' => env('TWILIO_SERVICE_SID', 'VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'),
152+
35153
];
36154
```
37155

38-
## Usage
156+
## ✨ Events
39157

40-
```php
41-
$laravel_twilio_verify = new CodebarAg\TwilioVerify();
42-
echo $laravel_twilio_verify->echoPhrase('Hello, Spatie!');
158+
Following events are fired:
159+
160+
```php
161+
use CodebarAg\TwilioVerify\Events\TwilioVerifyResponseLog;
162+
163+
// Log each response from the Twilio REST API.
164+
TwilioVerifyResponseLog::class => [
165+
//
166+
],
167+
```
168+
169+
## 🚧 Testing
170+
171+
Copy your own phpunit.xml-file.
172+
```bash
173+
cp phpunit.xml.dist phpunit.xml
43174
```
44175

45-
## Testing
176+
Modify environment variables in the phpunit.xml-file:
177+
```xml
178+
<env name="TWILIO_ACCOUNT_SID" value="ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"/>
179+
<env name="TWILIO_AUTH_TOKEN" value="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"/>
180+
<env name="TWILIO_SERVICE_SID" value="VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"/>
181+
```
46182

183+
Run the tests:
47184
```bash
48185
composer test
49186
```
50187

51-
## Changelog
188+
## 📝 Changelog
52189

53190
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
54191

55-
## Contributing
192+
## ✏️ Contributing
56193

57194
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
58195

59-
## Security Vulnerabilities
196+
## 🧑‍💻 Security Vulnerabilities
60197

61198
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
62199

63-
## Credits
200+
## 🙏 Credits
64201

65202
- [Ruslan Steiger](https://github.com/SuddenlyRust)
66203
- [All Contributors](../../contributors)
204+
- [Skeleton Repository from Spatie](https://github.com/spatie/package-skeleton-laravel)
205+
- [Laravel Package Training from Spatie](https://spatie.be/videos/laravel-package-training)
67206

68207
## License
69208

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"require": {
2020
"php": "^8.0",
2121
"friendsofphp/php-cs-fixer": "^2.18",
22+
"guzzlehttp/guzzle": "^7.0.1",
2223
"illuminate/contracts": "^8.0",
2324
"spatie/laravel-package-tools": "^1.4.3"
2425
},

config/twilio-verify.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

config/twilio_verify.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Twilio Verify Configuration
8+
|--------------------------------------------------------------------------
9+
|
10+
| You can find your Account SID and Auth Token in the Console Dashboard.
11+
| Additionally you should create a new Verify service and paste it in
12+
| here. Afterwards you are ready to communicate with Twilio Verify.
13+
|
14+
*/
15+
16+
'url' => env('TWILIO_URL', 'https://verify.twilio.com/v2/Services'),
17+
'account_sid' => env('TWILIO_ACCOUNT_SID', 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'),
18+
'auth_token' => env('TWILIO_AUTH_TOKEN', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'),
19+
'service_sid' => env('TWILIO_SERVICE_SID', 'VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'),
20+
21+
];

phpunit.xml.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,10 @@
3636
<logging>
3737
<junit outputFile="build/report.junit.xml"/>
3838
</logging>
39+
<php>
40+
<env name="TWILIO_URL" value="https://verify.twilio.com/v2/Services"/>
41+
<env name="TWILIO_ACCOUNT_SID" value="ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"/>
42+
<env name="TWILIO_AUTH_TOKEN" value="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"/>
43+
<env name="TWILIO_SERVICE_SID" value="VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"/>
44+
</php>
3945
</phpunit>

src/DTO/Carrier.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace CodebarAg\TwilioVerify\DTO;
4+
5+
class Carrier
6+
{
7+
public static function fromJson(array $data): self
8+
{
9+
return new static(
10+
error_code: $data['error_code'],
11+
name: $data['name'],
12+
mobile_country_code: $data['mobile_country_code'],
13+
mobile_network_code: $data['mobile_network_code'],
14+
type: $data['type'],
15+
);
16+
}
17+
18+
public function __construct(
19+
public ?string $error_code,
20+
public string $name,
21+
public string $mobile_country_code,
22+
public string $mobile_network_code,
23+
public string $type,
24+
) {
25+
}
26+
27+
public static function fake(
28+
?string $error_code = null,
29+
?string $name = null,
30+
?string $mobile_country_code = null,
31+
?string $mobile_network_code = null,
32+
?string $type = null,
33+
): self {
34+
return new static(
35+
error_code: $error_code ?? null,
36+
name: $name ?? 'Carrier Name',
37+
mobile_country_code: $mobile_country_code ?? '310',
38+
mobile_network_code: $mobile_network_code ?? '150',
39+
type: $type ?? '150',
40+
);
41+
}
42+
}

src/DTO/Lookup.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace CodebarAg\TwilioVerify\DTO;
4+
5+
class Lookup
6+
{
7+
public static function fromJson(array $lookup): self
8+
{
9+
return new static(
10+
carrier: Carrier::fromJson($lookup['carrier']),
11+
);
12+
}
13+
14+
public function __construct(public Carrier $carrier)
15+
{
16+
}
17+
18+
public static function fake(?Carrier $carrier = null): self
19+
{
20+
return new static(
21+
carrier: $carrier ?? Carrier::fake(),
22+
);
23+
}
24+
}

src/DTO/SendCodeAttempt.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace CodebarAg\TwilioVerify\DTO;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Support\Collection;
7+
use Illuminate\Support\Str;
8+
9+
class SendCodeAttempt
10+
{
11+
public static function fromJson(array $attempts): Collection
12+
{
13+
return collect($attempts)->map(function (array $attempt) {
14+
return new static(
15+
time: Carbon::parse($attempt['time']),
16+
channel: $attempt['channel'],
17+
attempt_sid: $attempt['attempt_sid'],
18+
);
19+
});
20+
}
21+
22+
public function __construct(
23+
public Carbon $time,
24+
public string $channel,
25+
public string $attempt_sid,
26+
) {
27+
}
28+
29+
public static function fake(
30+
?Carbon $time = null,
31+
?string $channel = null,
32+
?string $attempt_sid = null,
33+
): self {
34+
return new static(
35+
time: $time ?? now(),
36+
channel: $channel ?? 'sms',
37+
attempt_sid: $attempt_sid ?? 'VL' . Str::random(32),
38+
);
39+
}
40+
}

0 commit comments

Comments
 (0)