fix(creditcall): require fresh confirmation approval for sale
This commit is contained in:
@@ -219,8 +219,8 @@ func TestCreditCallLegacyAndStreamingSaleParity(t *testing.T) {
|
||||
{"cancelled", "Cancelled", "", 0, false, "start receipt"},
|
||||
{"timeout", "TIMEOUT", "", 0, false, "start receipt"},
|
||||
{"confirmation declined", "Approved", "Declined", 1, false, "confirm receipt"},
|
||||
{"confirmation omissions", "Approved", "omitted", 1, true, "start receipt"},
|
||||
{"malformed confirmation", "Approved", "malformed", 1, true, "start receipt"},
|
||||
{"confirmation omissions", "Approved", "omitted", 1, false, "start receipt"},
|
||||
{"malformed confirmation", "Approved", "malformed", 1, false, "start receipt"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
fields := map[string]string{types.TransactionResult: test.startResult, types.Reference: "native-ref",
|
||||
@@ -314,6 +314,160 @@ func TestCreditCallLegacyAndStreamingSaleParity(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreditCallFreshConfirmationResults(t *testing.T) {
|
||||
fixtures := map[string]creditCallDiagnosticFixture{}
|
||||
for _, fixture := range creditCallDiagnosticFixtures() {
|
||||
fixtures[fixture.name] = fixture
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, outcome string
|
||||
amount int64
|
||||
}{
|
||||
{"confirmation_xml/mixed_case", "approved", 1234},
|
||||
{"confirmation_xml/omitted", "unknown", 1234},
|
||||
{"confirmation_xml/empty_body", "unknown", 1234},
|
||||
{"confirmation_xml/whitespace_body", "unknown", 1234},
|
||||
{"confirmation_xml/malformed_retains_approval", "unknown", 1234},
|
||||
{"confirmation_xml/malformed_after_approved", "unknown", 1234},
|
||||
{"confirmation_xml/malformed_after_error", "unknown", 1234},
|
||||
{"confirmation_xml/malformed_after_declined", "unknown", 1234},
|
||||
{"confirmation_xml/wrong_root", "unknown", 1234},
|
||||
{"confirmation_xml/sdk_only_disconnected", "unknown", 1234},
|
||||
{"confirmation_xml/sdk_only_not_found", "unknown", 1234},
|
||||
{"confirmation_xml/duplicate_approved_declined", "unknown", 1234},
|
||||
{"confirmation_xml/duplicate_declined_approved", "approved", 1234},
|
||||
{"confirmation_xml/duplicate_error_approved", "approved", 1234},
|
||||
{"confirmation_xml/duplicate_approved_empty", "unknown", 1234},
|
||||
{"confirmation_xml/duplicate_empty_approved", "approved", 1234},
|
||||
{"confirmation_xml/duplicate_approved_error", "unknown", 1234},
|
||||
{"confirmation_result/DECLINED", "unknown", 1234},
|
||||
{"confirmation_result/ERROR", "unknown", 1234},
|
||||
{"confirmation_result/CANCELLED", "unknown", 1234},
|
||||
{"confirmation_result/CANCELED", "unknown", 1234},
|
||||
{"confirmation_result/TIMEOUT", "unknown", 1234},
|
||||
{"confirmation_result/UNKNOWN", "unknown", 1234},
|
||||
{"confirmation_result/", "unknown", 1234},
|
||||
{"confirmation_metadata/returned_amount", "approved", 3100},
|
||||
{"confirmation_metadata/overrides", "approved", 2500},
|
||||
{"confirmation_metadata/empty_overrides", "approved", 3100},
|
||||
} {
|
||||
for _, legacy := range []bool{false, true} {
|
||||
mode := "modern"
|
||||
if legacy {
|
||||
mode = "legacy"
|
||||
}
|
||||
t.Run(mode+"/"+tc.name, func(t *testing.T) {
|
||||
fixture, ok := fixtures["sale/"+tc.name]
|
||||
if !ok {
|
||||
t.Fatalf("missing confirmation fixture %q", tc.name)
|
||||
}
|
||||
got := captureCreditCallDiagnostic(t, fixture, legacy)
|
||||
if len(got.Requests) != 2 {
|
||||
t.Fatalf("Sale(%s) requests=%+v, want one start and one confirmation", tc.name, got.Requests)
|
||||
}
|
||||
if got.HTTPStatus != http.StatusOK || got.Requests[1].Path != "/confirm-transaction/" {
|
||||
t.Errorf("Sale(%s) HTTP/requests=%d/%+v, want HTTP 200, one start and one confirmation", tc.name, got.HTTPStatus, got.Requests)
|
||||
}
|
||||
for _, request := range got.Requests {
|
||||
if !request.FreshCallDeadline || request.Method != http.MethodPost {
|
||||
t.Errorf("Sale(%s) request=%+v, want POST with an independent 300-second timeout", tc.name, request)
|
||||
}
|
||||
}
|
||||
var confirmation creditcall.ConfirmTransactionRequest
|
||||
if err := xml.Unmarshal([]byte(got.Requests[1].Body), &confirmation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if confirmation.Amount != "1234" || confirmation.Reference != "SYNTH-STAGE2" {
|
||||
t.Errorf("Sale(%s) confirmation=%+v, want the original requested amount and authorization reference", tc.name, confirmation)
|
||||
}
|
||||
if legacy {
|
||||
if strings.HasPrefix(got.Legacy.Data, "/successful?") != (tc.outcome == "approved") {
|
||||
t.Errorf("Sale(%s) legacy=%+v, want success only for fresh approval", tc.name, got.Legacy)
|
||||
}
|
||||
if fixture.correctedConfirmation && (got.Legacy.Status.Code != http.StatusOK || got.Legacy.Status.Message != "unknown" || got.Legacy.Data != "/unsuccessful?Description=Transaction+unknown&MsgType=error") {
|
||||
t.Errorf("Sale(%s) legacy=%+v, want unchanged status code and explicit uncertainty failure response", tc.name, got.Legacy)
|
||||
}
|
||||
} else if got.Final.Outcome != tc.outcome || got.Final.Retryable || got.Final.HTTPStatus != http.StatusOK || got.Final.AmountMinor != tc.amount || got.Final.Reason != fixture.reason || got.Final.Message != fixture.message {
|
||||
t.Errorf("Sale(%s) final=%+v, want %s/nonretryable/HTTP200/amount%d and %q/%q", tc.name, got.Final, tc.outcome, tc.amount, fixture.reason, fixture.message)
|
||||
}
|
||||
wantReceipt := "synthetic initial receipt"
|
||||
if tc.name == "confirmation_metadata/overrides" {
|
||||
wantReceipt = "synthetic confirmation receipt"
|
||||
}
|
||||
if tc.name == "confirmation_metadata/empty_overrides" {
|
||||
wantReceipt = ""
|
||||
}
|
||||
if !reflect.DeepEqual(got.Receipts, []string{wantReceipt}) || got.PersistenceCount != 0 {
|
||||
t.Errorf("Sale(%s) receipts/persistence=%v/%d, want [%q]/0", tc.name, got.Receipts, got.PersistenceCount, wantReceipt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreditCallSaleConfirmationPreservesMergedFields(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, rawXML, receipt string
|
||||
confirmation map[string]string
|
||||
approved bool
|
||||
}{
|
||||
{name: "minimal approval", confirmation: map[string]string{types.TransactionResult: "APPROVED"}, receipt: "synthetic initial receipt", approved: true},
|
||||
{name: "confirmation overrides", confirmation: map[string]string{types.TransactionResult: "APPROVED", types.Reference: "SYNTH-CONFIRMED", types.TotalAmount: "2500", types.CardHash: "", types.ReceiptDataCardholder: "synthetic confirmation receipt"}, receipt: "synthetic confirmation receipt", approved: true},
|
||||
{name: "SDK-only uncertainty", confirmation: map[string]string{types.Errors: "ClientNotConnectedToServer"}, receipt: "synthetic initial receipt"},
|
||||
{name: "partial approved XML", rawXML: "<TransactionResult><Entry><Key>TRANSACTION_RESULT</Key><Value>APPROVED</Value></Entry><Entry>", receipt: "synthetic initial receipt"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
initial := creditCallDiagnosticFields("APPROVED", "")
|
||||
initial[types.TotalAmount] = "3100"
|
||||
initial["AUTH_DATE_TIME"], initial["CARDEASE_REFERENCE"] = "20300101120000", "synthetic-authorization"
|
||||
expected := map[string]string{}
|
||||
for key, value := range initial {
|
||||
if key != types.ReceiptDataCardholder {
|
||||
expected[key] = value
|
||||
}
|
||||
}
|
||||
for key, value := range tc.confirmation {
|
||||
if key != types.ReceiptDataCardholder {
|
||||
expected[key] = value
|
||||
}
|
||||
}
|
||||
var receipts []string
|
||||
starts, confirms := 0, 0
|
||||
body := &creditCallDiagnosticBody{}
|
||||
app := &App{cfg: &config.ConfigRec{}, creditCallReceipt: func(receipt string) { receipts = append(receipts, receipt) }}
|
||||
app.creditCallTransport = creditCallRoundTrip(func(r *http.Request) (*http.Response, error) {
|
||||
confirms++
|
||||
if r.URL.Path != "/confirm-transaction/" {
|
||||
t.Errorf("unexpected confirmation path %q", r.URL.Path)
|
||||
}
|
||||
wire := tc.rawXML
|
||||
if wire == "" {
|
||||
wire = chipDNAFixture(t, tc.confirmation)
|
||||
}
|
||||
body.Reader = strings.NewReader(wire)
|
||||
return &http.Response{StatusCode: http.StatusOK, Body: body}, nil
|
||||
})
|
||||
outcome := app.executeCreditCallSale(cmstypes.TransactionRec{AmountMinorUnits: "1234"}, func(*http.Client) (creditcall.TransactionResultXML, error) {
|
||||
starts++
|
||||
var result creditcall.TransactionResultXML
|
||||
for key, value := range initial {
|
||||
result.Entries = append(result.Entries, creditcall.EntryXML{Key: key, Value: value})
|
||||
}
|
||||
return result, nil
|
||||
})
|
||||
if outcome.Approved != tc.approved || !reflect.DeepEqual(outcome.Payment.Fields, expected) || outcome.Payment.CardholderReceipt != tc.receipt {
|
||||
t.Errorf("Sale(%s) approved/fields/receipt=%t/%v/%q, want %t/%v/%q", tc.name, outcome.Approved, outcome.Payment.Fields, outcome.Payment.CardholderReceipt, tc.approved, expected, tc.receipt)
|
||||
}
|
||||
if !tc.approved && (outcome.Status.Message != "unknown" || outcome.FailureType != "unknown" || outcome.Payment.Status.Message != "APPROVED") {
|
||||
t.Errorf("Sale(%s) status/failure/payment status=%+v/%q/%+v, want uncertainty without mutating merged authorization evidence", tc.name, outcome.Status, outcome.FailureType, outcome.Payment.Status)
|
||||
}
|
||||
if starts != 1 || confirms != 1 || !body.closed || !reflect.DeepEqual(receipts, []string{tc.receipt}) {
|
||||
t.Errorf("Sale(%s) starts/confirms/closed/receipts=%d/%d/%t/%v, want 1/1/true/[%q]", tc.name, starts, confirms, body.closed, receipts, tc.receipt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreditCallConfirmationRetryAndFailureParity(t *testing.T) {
|
||||
for _, streaming := range []bool{false, true} {
|
||||
var attempts int
|
||||
|
||||
Reference in New Issue
Block a user