Files
InstaClientPS/tools/pax8.ps1
2025-07-08 20:33:22 -04:00

87 lines
2.7 KiB
PowerShell

# 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 = $Country
}
} | 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 {
throw "[PAX8] Company creation failed: $($_.Exception.Message)"
}
}
# Entrypoint if sourced
Invoke-Pax8Provision -CompanyName $CompanyName -Phone $Phone -Website $Website -Street $Street -City $City -Province $Province -PostalCode $PostalCode -Country $Country -Credentials $Credentials