send method
Send request and resolve to a GatewayTransportResponse
when the network call completes. Implementations should
throw a TimeoutException or GatewayTransportError
only for transport-level failures (DNS, TCP reset, TLS
handshake, request timeout). HTTP errors (4xx / 5xx) are
returned as a normal response with the appropriate
statusCode.
Implementation
@override
Future<GatewayTransportResponse> send(
GatewayTransportRequest request,
) async {
requests.add(request);
// The fake refuses to forward a body that carries any of
// the banned keys at the TOP LEVEL of the JSON object.
// Production transports also see only allow-listed top-
// level keys (see GatewayTransactionRequest); this
// assertion catches future regressions in tests.
final assertion = _assertBannedKeysAbsent(request.body);
if (assertion != null) {
throw GatewayTransportError(
message: 'FakeGatewayTransport rejected a request carrying banned '
'top-level key "$assertion". The gateway-handoff layer must '
'never serialize PAN / track / CVV / ARQC / cardholder data.',
);
}
if (artificialDelay != null) {
await Future<void>.delayed(artificialDelay!);
}
final err = nextError;
nextError = null;
if (err != null) throw err;
final responder = this.responder;
if (responder != null) {
return responder(request);
}
return GatewayTransportResponse(
statusCode: 200,
body: '{"status":"approved","approved":true,"transactionId":"fake-1"}',
);
}