PrintToVeraeTimeStamp/cmd/verae-print/stampcore.go
George Lambert 6f8796c25e
Some checks failed
windows-msi / full-msi-unsigned (push) Has been cancelled
Open Zap Billing when PrintIt stamps fail for credits
On HTTP 402/429 (and credit/billing error bodies), hold the print job and open
https://zap.verae.ai/portal/?tab=billing so the user can reload prepaid balance.
Throttle browser opens to once per 10 minutes. Auth failures still use the
sign-in flow unchanged.
2026-10-20 01:07:39 -04:00

77 lines
2.6 KiB
Go

package main
import (
"fmt"
"strings"
"time"
"github.com/VeraeLLC/PrintToVeraeTimeStamp/internal/auth"
"github.com/VeraeLLC/PrintToVeraeTimeStamp/internal/job"
"github.com/VeraeLLC/PrintToVeraeTimeStamp/internal/veraeapi"
"github.com/VeraeLLC/PrintToVeraeTimeStamp/internal/vlog"
)
// clientForChannel builds an API client from the stored credentials.
// Missing xVerae- key (and no JWT fallback) → errNoKey.
func clientForChannel(ch auth.Channel, lj vlog.Job) (*veraeapi.Client, auth.Channel, error) {
client := &veraeapi.Client{Channel: ch}
switch ch {
case auth.ChannelZapier:
tok, err := auth.LoadXVeraeToken()
if err != nil {
if jwt, jerr := auth.LoadVeraeJWT(); jerr == nil {
lj.Printf("auth: no xVerae- token, using verae channel (auth.properties)")
client.Channel, client.VeraeToken = auth.ChannelVerae, jwt
return client, auth.ChannelVerae, nil
}
return nil, ch, fmt.Errorf("%w (%v)", errNoKey, err)
}
client.XVeraeToken = tok
default:
tok, err := auth.LoadVeraeJWT()
if err != nil {
return nil, ch, fmt.Errorf("verae channel: %v", err)
}
client.VeraeToken = tok
}
return client, ch, nil
}
// stampFile stamps one PDF with the current key (no holding / prompting).
func stampFile(pdf string, t stampTarget, lj vlog.Job) (job.Result, error) {
client, ch, err := clientForChannel(auth.PreferredChannel(), lj)
if err != nil {
return job.Result{}, err
}
client.HTTPDo = loggingDo(lj)
start := time.Now()
res, err := job.Run(job.Options{SourcePDF: pdf, OutDir: t.OutDir, OutPath: t.OutPath, DocumentName: safeStem(t.Title) + ".pdf", Channel: ch, Client: client})
if err != nil {
return res, err
}
lj.Printf("stamp: OK in %s receipt=%s sha256=%s\n original=%q\n cert=%q\n json=%q",
time.Since(start).Round(time.Millisecond), res.ReceiptID, res.SHA256, res.OriginalPDF, res.CertificatePDF, res.ReceiptJSON)
return res, nil
}
func authReason(err error) string {
if veraeapi.IsAuthError(err) {
return "Verae did not accept your signing key (it may have expired or been revoked). Please sign in again."
}
return "No Verae signing key is set up for this user yet. Please sign in or create an account."
}
func billingReason(err error) string {
if err != nil && strings.TrimSpace(err.Error()) != "" {
return "Your Verae account needs funds before PrintIt can stamp. Opening Billing so you can add prepaid balance.\n\n(" + shortErr(err) + ")"
}
return "Your Verae account needs funds before PrintIt can stamp. Opening Billing so you can add prepaid balance."
}
func shortErr(err error) string {
s := err.Error()
if len(s) > 160 {
return s[:157] + "…"
}
return s
}