The following code was tested with test credentials and worked as expected, but when attempting to purchase a number with the production credentials I always get [HTTP 400] Unable to create record: +xxxxxxx is not available
Below is the code that retrieves the available numbers.
public function getAvailablePhoneNumbers(): array
{
if ($this->useTestClient) {
return [self::VALID_TEST_MAGIC_PHONE];
}
$numbers = $this->getClient()
->availablePhoneNumbers('US')
->local
->read([
'smsEnabled' => true,
]);
/* @var LocalInstance $number */
return array_map(function ($number) {
return $number->phoneNumber;
}, $numbers);
}
After retrieving the list of phoneNumber
I try to acquire a number in the list or pass to the next in the list if it fails, all the numbers on the list fail with the same error, the code that tries to acquire it is.
public function purchaseNumber(string $number): \Twilio\Rest\Api\V2010\Account\IncomingPhoneNumber\LocalInstance
{
return $this->getClient()
->incomingPhoneNumbers
->local
->create($number, [
'SmsApplicationSid' => config('twilio.sms_app_sid'),
]);
}
Edit: I tried to acquire one of the unavailable numbers form the log in the Twilio console and it succeeded.
The issues was that the service wrapper that we created around Twilio SDK was swapping the testing credentials at incorrect times, therefore in the test API those numbers were in fact unavailable.
What helped to debug this was using the environment variable DEBUG_HTTP_TRAFFIC
to force the Twilio SDK to output debug information for the requests, where we noticed the wrong credentials being used.
The solution was to fix the wrapper code to use the correct credentials outside of test mode.