feat: add CreditCall preauth streaming
This commit is contained in:
@@ -63,6 +63,10 @@ func (outcome creditCallSaleOutcome) streamResult() creditCallStreamResult {
|
||||
func (app *App) EnableCreditCallStreaming() { app.creditCallStreamEnabled = true }
|
||||
|
||||
func (app *App) streamCreditCallSale(w http.ResponseWriter, r *http.Request) {
|
||||
app.streamCreditCall(w, r, false)
|
||||
}
|
||||
|
||||
func (app *App) streamCreditCall(w http.ResponseWriter, r *http.Request, preauth bool) {
|
||||
setPaymentCORS(w)
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
@@ -97,6 +101,10 @@ func (app *App) streamCreditCallSale(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(status)
|
||||
send(paymentStreamMessage{Type: "result", Result: &result})
|
||||
}
|
||||
if preauth && !app.creditCallStreamEnabled {
|
||||
reject(http.StatusServiceUnavailable, "CreditCall preauthorization streaming is not enabled")
|
||||
return
|
||||
}
|
||||
if !app.isPayment && !app.cfg.TestMode {
|
||||
mail.SendEmailOnError(app.cfg.Hotel, app.cfg.Kiosk, "Payment Error", "Attempted payment while payment processing is disabled")
|
||||
reject(http.StatusServiceUnavailable, "Payment processing is disabled")
|
||||
@@ -111,33 +119,53 @@ func (app *App) streamCreditCallSale(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
var request SalePaymentRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
reject(http.StatusBadRequest, "Invalid JSON payload")
|
||||
return
|
||||
}
|
||||
if request.Amount <= 0 {
|
||||
reject(http.StatusBadRequest, "Amount must be greater than zero")
|
||||
return
|
||||
var transaction cmstypes.TransactionRec
|
||||
if preauth {
|
||||
var request creditCallPreauthRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
reject(http.StatusBadRequest, "Invalid JSON payload")
|
||||
return
|
||||
}
|
||||
transaction = cmstypes.TransactionRec{AmountMinorUnits: request.Amount, TransactionType: request.TransactionType, CheckoutDate: request.CheckoutDate}
|
||||
} else {
|
||||
var request SalePaymentRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
reject(http.StatusBadRequest, "Invalid JSON payload")
|
||||
return
|
||||
}
|
||||
if request.Amount <= 0 {
|
||||
reject(http.StatusBadRequest, "Amount must be greater than zero")
|
||||
return
|
||||
}
|
||||
transaction = cmstypes.TransactionRec{AmountMinorUnits: strconv.FormatInt(request.Amount, 10), TransactionType: "Sale"}
|
||||
}
|
||||
if _, ok := w.(http.Flusher); !ok {
|
||||
reject(http.StatusInternalServerError, "Streaming payment updates are not supported")
|
||||
return
|
||||
}
|
||||
|
||||
// Validation belongs to the incoming request. This handoff owns financial dispatch.
|
||||
if r.Context().Err() != nil {
|
||||
return
|
||||
}
|
||||
financialContext := context.WithoutCancel(r.Context())
|
||||
progress := make(chan string, 128)
|
||||
finished := make(chan creditCallSaleOutcome, 1)
|
||||
go func() {
|
||||
outcome := app.executeCreditCallSale(cmstypes.TransactionRec{
|
||||
AmountMinorUnits: strconv.FormatInt(request.Amount, 10), TransactionType: "Sale",
|
||||
}, func(client *http.Client) (creditcall.TransactionResultXML, error) {
|
||||
return callChipDNAStream(client, request.Amount, func(code string) {
|
||||
start := func(client *http.Client) (creditcall.TransactionResultXML, error) {
|
||||
return callChipDNATransactionStream(financialContext, client, transaction, func(code string) {
|
||||
select {
|
||||
case progress <- code:
|
||||
default: // Observational progress may be dropped under backpressure.
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
var outcome creditCallSaleOutcome
|
||||
if preauth {
|
||||
outcome = app.executeCreditCallPreauth(financialContext, transaction, start)
|
||||
} else {
|
||||
outcome = app.executeCreditCallSale(transaction, start)
|
||||
}
|
||||
close(progress)
|
||||
finished <- outcome // Independent of the bounded progress queue.
|
||||
}()
|
||||
@@ -163,16 +191,22 @@ func (app *App) streamCreditCallSale(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
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"`
|
||||
}{strconv.FormatInt(amount, 10), "Sale"})
|
||||
}{transaction.AmountMinorUnits, transaction.TransactionType})
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
// Background plus Client.Timeout reproduces the independent per-call bound.
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, types.LinkStartTransactionStream, bytes.NewReader(payload))
|
||||
// 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user