# pax8.ps1 param ( [Parameter(Mandatory = $true)][string]$CompanyName, [Parameter(Mandatory = $true)][string]$Phone, [Parameter(Mandatory = $true)][string]$Website, [Parameter(Mandatory = $true)][string]$Street, [Parameter(Mandatory = $true)][string]$City, [Parameter(Mandatory = $true)][string]$Province, [Parameter(Mandatory = $true)][string]$PostalCode, [Parameter(Mandatory = $true)][string]$Country, [Parameter(Mandatory = $true)][hashtable]$Credentials ) function Get-Pax8AccessToken { param ( [string]$ClientId, [string]$ClientSecret ) $body = @{ client_id = $ClientId client_secret = $ClientSecret audience = "https://api.pax8.com" grant_type = "client_credentials" } | ConvertTo-Json try { $response = Invoke-RestMethod -Method Post -Uri "https://api.pax8.com/v1/token" -ContentType "application/json" -Body $body return $response.access_token } catch { throw "Failed to obtain Pax8 access token: $($_.Exception.Message)" } } function Invoke-Pax8Provision { param ( [string]$CompanyName, [string]$Phone, [string]$Website, [string]$Street, [string]$City, [string]$Province, [string]$PostalCode, [string]$Country, [hashtable]$Credentials ) $clientId = $Credentials.Pax8Client $clientSecret = $Credentials.Pax8Secret if (-not $clientId -or -not $clientSecret) { throw "Missing Pax8 credentials." } $accessToken = Get-Pax8AccessToken -ClientId $clientId -ClientSecret $clientSecret $companyPayload = @{ name = $CompanyName phone = $Phone website = $Website externalId = "" billOnBehalfOfEnabled = $true selfServiceAllowed = $true orderApprovalRequired = $false address = @{ street = $Street city = $City stateOrProvince = $Province postalCode = $PostalCode country = "Canada" } } | ConvertTo-Json -Depth 3 try { $response = Invoke-RestMethod -Method Post -Uri "https://api.pax8.com/v1/companies" ` -Headers @{ Authorization = "Bearer $accessToken" } ` -ContentType "application/json" -Body $companyPayload Write-Host "[PAX8] Company provisioned successfully: $($response.id)" } catch { if ($_.Exception.Response -and $_.Exception.Response.StatusCode.Value__ -eq 422) { throw "[PAX8] A company with this information already exists." } else { throw "[PAX8] Company creation failed: $($_.Exception.Message)" } } }