PowerShell Requires Chilkat v11.0.0+
PowerShell
PayPal: Get a payable invoice link for your own email
See more PayPal Examples
Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.
Chilkat PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
# See Unlock Chilkat before making requests.
# For the merchant's internal backoffice application. Load the sandbox token
# from the merchant's credential store. Keep it out of publicly distributed apps.
# See Get a PayPal access token.
$http = New-Object Chilkat.Http
$http.AuthToken = "SANDBOX_ACCESS_TOKEN"
$resp = New-Object Chilkat.HttpResponse
$http.SetUrlVar("id","INVOICE_ID")
$success = $http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",$resp)
if ($success -ne $true) {
$("No HTTP response: " + $http.LastErrorText)
exit
}
if (($resp.StatusCode -lt 200) -or ($resp.StatusCode -ge 300)) {
$errorInfo = New-Object Chilkat.JsonObject
$errorInfo.Load($resp.BodyStr)
$("HTTP " + $resp.StatusCode + " " + $errorInfo.StringOf("name") + " " + $errorInfo.StringOf("details[0].issue"))
$("PayPal debug ID: " + $errorInfo.StringOf("debug_id"))
exit
}
$result = New-Object Chilkat.JsonObject
$parsedResult = $result.Load($resp.BodyStr)
if ($parsedResult -ne $true) {
$("Expected a JSON response; HTTP " + $resp.StatusCode)
exit
}
if ($result.StringOfEquals("status","DRAFT",$true) -ne $true) {
$("Select an unsent draft.")
exit
}
# Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
$now = New-Object Chilkat.CkDateTime
$now.SetFromCurrentSystemTime()
$stamp = New-Object Chilkat.StringBuilder
$stamp.Append($result.StringOf("detail.invoice_date"))
$stamp.Append("T00:00:00Z")
$issue = New-Object Chilkat.CkDateTime
$validDate = $issue.SetFromTimestamp($stamp.GetAsString())
if (($validDate -ne $true) -or ($now.DiffSeconds($issue) -lt 0)) {
$("Select a draft with an issue date of today or earlier.")
exit
}
$body = New-Object Chilkat.JsonObject
$body.UpdateBool("send_to_recipient",$false)
$body.UpdateBool("send_to_invoicer",$false)
$success = $http.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send",$body,"application/json",$resp)
if ($success -ne $true) {
$("No HTTP response: " + $http.LastErrorText)
exit
}
if (($resp.StatusCode -lt 200) -or ($resp.StatusCode -ge 300)) {
$sendError = New-Object Chilkat.JsonObject
$sendError.Load($resp.BodyStr)
$("HTTP " + $resp.StatusCode + " " + $sendError.StringOf("name") + " " + $sendError.StringOf("details[0].issue"))
$("PayPal debug ID: " + $sendError.StringOf("debug_id"))
exit
}
# Read back the invoice: acceptance of the send alone does not guarantee it is payable.
$success = $http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",$resp)
if ($success -ne $true) {
$("No HTTP response: " + $http.LastErrorText)
exit
}
if (($resp.StatusCode -lt 200) -or ($resp.StatusCode -ge 300)) {
$invoiceError = New-Object Chilkat.JsonObject
$invoiceError.Load($resp.BodyStr)
$("HTTP " + $resp.StatusCode + " " + $invoiceError.StringOf("name") + " " + $invoiceError.StringOf("details[0].issue"))
$("PayPal debug ID: " + $invoiceError.StringOf("debug_id"))
exit
}
$invoice = New-Object Chilkat.JsonObject
$parsedInvoice = $invoice.Load($resp.BodyStr)
if ($parsedInvoice -ne $true) {
$("Expected a JSON response; HTTP " + $resp.StatusCode)
exit
}
if ((($invoice.StringOfEquals("status","SENT",$true) -eq $true) -or ($invoice.StringOfEquals("status","UNPAID",$true) -eq $true) -or ($invoice.StringOfEquals("status","SHARED",$true) -eq $true)) -and ($invoice.HasMember("detail.metadata.recipient_view_url") -eq $true)) {
$("Customer payment page: " + $invoice.StringOf("detail.metadata.recipient_view_url"))
exit
}
$("Invoice is " + $invoice.StringOf("status") + ". Retrieve it again when payable.")