Installation
composer require linepayamak/laravel
composer dump-autoload
Register Provider
Add to config/app.php under Package Service Providers:
LinePayamak\Support\Laravel\ServiceProvider::class,
Publish Config
php artisan vendor:publish --provider="LinePayamak\Support\Laravel\ServiceProvider"
Update .env with your LinePayamak credentials (e.g., LINEPAYAMAK_USERNAME, LINEPAYAMAK_PASSWORD).
First Use Case Fetch remaining credit in a controller:
use LinePayamak\Support\Laravel\Facade as LinePayamak;
public function checkCredit()
{
$credit = LinePayamak::GetCredit();
return response()->json(['credit' => $credit]);
}
Service Initialization
LinePayamak::method()) for simplicity or resolve the service manually:
$client = resolve('LinePayamak');
$client->SendSMS($to, $message);
SMS Sending
LinePayamak::SendSMS(
$recipient, // e.g., '09123456789'
$message, // string
$senderID? // optional, e.g., 'MyBrand'
);
Balance Management
$balance = LinePayamak::GetCredit(); // Returns remaining credit
LinePayamak::GetLastError(); // Check for errors after operations
Batch Operations For bulk SMS, loop and handle errors:
foreach ($recipients as $phone) {
$result = LinePayamak::SendSMS($phone, 'Hello!');
if ($result !== true) {
Log::error("Failed to send to $phone: " . LinePayamak::GetLastError());
}
}
Integration with Queues Dispatch SMS jobs to avoid timeouts:
SendSMSJob::dispatch($recipient, $message)->onQueue('sms');
(Note: Requires custom job class; package doesn’t natively support queues.)
Customizing Requests
Override the default SOAP client in config/linepayamak.php:
'soap_options' => [
'trace' => 1, // Enable for debugging
'exceptions' => true,
],
Logging Responses Extend the service provider to log all requests/responses:
// In a custom service provider
$this->app->extend('LinePayamak', function ($client) {
$client->setLogger(app(\Psr\Log\LoggerInterface::class));
return $client;
});
Testing Mock the SOAP client in tests:
$mock = Mockery::mock('LinePayamak\Support\LinePayamakClient');
$mock->shouldReceive('SendSMS')->andReturn(true);
$this->app->instance('LinePayamak', $mock);
SOAP Extension Missing
Class 'SoapClient' not found.extension=soap in php.ini and restart PHP.Character Limits
mb_strlen() to validate message length:
if (mb_strlen($message, 'UTF-8') > 300) {
throw new \InvalidArgumentException('Message exceeds 300 characters.');
}
Error Handling
GetLastError() only captures the last operation’s error. Clear it after checking:
$result = LinePayamak::SendSMS($to, $msg);
if (!$result) {
$error = LinePayamak::GetLastError();
LinePayamak::ClearLastError(); // Reset for next call
throw new \RuntimeException($error);
}
Rate Limits
503 errors:
sleep(1); // Delay between bulk SMS sends
UTF-8 Encoding
$message = mb_convert_encoding($message, 'UTF-8');
Configuration
.env:
LINEPAYAMAK_USERNAME=your_username
LINEPAYAMAK_PASSWORD=your_password
LINEPAYAMAK_SENDER_ID=YourBrand
Debugging SOAP
Enable trace in config/linepayamak.php to log raw SOAP requests/responses:
'soap_options' => ['trace' => 1],
Access logs via:
$client = resolve('LinePayamak');
$logs = $client->getSoapClient()->__getLastRequest();
Extending Functionality
// In a custom service provider
$this->app->extend('LinePayamak', function ($client) {
$client->setCustomMethod(function ($params) {
return $client->__soapCall('CustomMethod', [$params]);
});
return $client;
});
Fallback for Failures Implement a retry mechanism for transient failures:
use Illuminate\Support\Facades\Retry;
Retry::times(3)->attempt(function () {
LinePayamak::SendSMS($to, $msg);
});
Documentation Gaps
__soapCall() directly:
$client->__soapCall('UnsupportedMethod', [$params]);
How can I help you explore Laravel packages today?