PSBlog010
Function Pay-Invoice | |
{ | |
param( | |
[Parameter(Mandatory=$true)] | |
[Microsoft.PowerShell.Commands.WebRequestSession]$WebSession, | |
$EndPoint = $env:AcumaticaTestEndPoint, | |
[Parameter(Mandatory=$true)] | |
[string]$ReferenceNbr, | |
[Parameter(Mandatory=$true)] | |
[string]$PaymentMethod) | |
"Getting Invoice Detail in preparation for payment" | |
$InvoiceAsPSObject = Get-Invoice -WebSession $WebSession -ReferenceNbr $ReferenceNbr | |
$PaymentAmount = $InvoiceAsPSObject.Balance.Value.ToString() | |
$CustomerNbr = $InvoiceAsPSObject.CustomerNbr.Value | |
"Create A payment for invoice $ReferenceNbr" | |
$body = @" | |
{ | |
`"Type`": {`"value`": `"Payment`"}, | |
`"CustomerID`": {`"value`": `"$CustomerNbr`"}, | |
`"PaymentMethod`": {`"value`": `"$PaymentMethod`"}, | |
`"PaymentAmount`": {`"value`": $PaymentAmount}, | |
`"DocumentsToApply`": [{`"ReferenceNbr`": {`"value`": `"$ReferenceNbr`"}}] | |
} | |
"@ | |
$HashArguments = @{ | |
Uri = "$AcumaticaEndPoint/entity/default/18.200.001/Payment" | |
Method = 'PUT' | |
Headers = $headers | |
Body = $body | |
WebSession = $WebSession} | |
$response = Invoke-RestMethod @HashArguments | |
$PaymentAsPSObject = $response | ConvertTo-Json | ConvertFrom-Json | |
$PaymentEntityID = $PaymentAsPSObject.id | |
"Capturing CC Payment for Invoice $ReferenceNbr on EntityID $PaymentEntityID" | |
$body = @" | |
{ | |
`"entity`": | |
{ | |
`"id`": `"$PaymentEntityID`" | |
} | |
} | |
"@ | |
$HashArguments = @{ | |
Uri = "$AcumaticaEndPoint/entity/default/18.200.001/Payment/CaptureCreditCardPayment" | |
Method = 'POST' | |
Headers = $headers | |
Body = $body | |
WebSession = $WebSession} | |
$response = Invoke-RestMethod -Method 'POST' -Headers $headers -Body $body | |
$response | ConvertTo-Json | |
sleep 2 | |
$HashArguments = @{ | |
Uri = "$EndPoint/entity/default/18.200.001/Payment?`$Filter=ReferenceNbr eq `'$($PaymentAsPSObject.ReferenceNbr.value)`'" | |
Method = 'GET' | |
Headers = $headers | |
WebSession = $WebSession} | |
$response = Invoke-RestMethod @HashArguments | |
$PaymentAsPSObject = $response | ConvertTo-Json | ConvertFrom-Json | |
return $PaymentAsPSObject | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment