package handlers import ( "bufio" "bytes" "context" "encoding/json" "errors" "fmt" "io" "net/http" "strconv" "gitea.futuresens.co.uk/futuresens/cmstypes" "gitea.futuresens.co.uk/futuresens/hardlink/v2/internal/creditcall" "gitea.futuresens.co.uk/futuresens/hardlink/v2/internal/paymentsvc" "gitea.futuresens.co.uk/futuresens/hardlink/v2/internal/types" ) // EnableCreditCallStreaming selects the existing CreditCall execution core. func (app *App) EnableCreditCallStreaming() { app.paymentService = paymentsvc.NewService(&creditCallProcessor{app: app}) } func callChipDNAStream(client *http.Client, amount int64, onStatus func(string)) (creditcall.TransactionResultXML, error) { return callChipDNATransactionStream(context.Background(), client, cmstypes.TransactionRec{ AmountMinorUnits: strconv.FormatInt(amount, 10), TransactionType: "Sale", }, onStatus) } func callChipDNATransactionStream(ctx context.Context, client *http.Client, transaction cmstypes.TransactionRec, onStatus func(string)) (creditcall.TransactionResultXML, error) { var result creditcall.TransactionResultXML payload, err := json.Marshal(struct { Amount string `json:"amount"` TransactionType string `json:"transactionType"` }{transaction.AmountMinorUnits, transaction.TransactionType}) if err != nil { return result, err } // Dispatch owns ctx; Client.Timeout retains the independent 300-second call bound. req, err := http.NewRequestWithContext(ctx, http.MethodPost, types.LinkStartTransactionStream, bytes.NewReader(payload)) if err != nil { return result, err } req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/x-ndjson") response, err := client.Do(req) if err != nil { return result, err } defer response.Body.Close() if response.StatusCode != http.StatusOK { return result, fmt.Errorf("chipdna stream returned HTTP %d", response.StatusCode) } reader := bufio.NewReader(response.Body) for { line, err := reader.ReadBytes('\n') if err != nil { if errors.Is(err, io.EOF) { return result, fmt.Errorf("chipdna stream ended before a complete final frame") } return result, fmt.Errorf("read chipdna stream: %w", err) } var frame struct { Type string `json:"type"` Source string `json:"source"` Value string `json:"value"` Result map[string]string `json:"result"` } if err := json.Unmarshal(line, &frame); err != nil { return result, fmt.Errorf("invalid chipdna JSON frame") } switch frame.Type { case "status": if code := creditcall.ProgressStatus(frame.Source, frame.Value); code != "" && onStatus != nil { onStatus(code) } case "result": if len(frame.Result) == 0 { return result, fmt.Errorf("chipdna final frame has no transaction fields") } for key, value := range frame.Result { result.Entries = append(result.Entries, creditcall.EntryXML{Key: key, Value: value}) } return result, nil // Later frames cannot repeat confirmation/finalization. case "error": return result, fmt.Errorf("chipdna stream reported an error") default: return result, fmt.Errorf("unknown chipdna frame type") } } }