Implementation
Future<PaymentResponse> paymenCreate(Map<String, dynamic> payment,
List<String> hints, String? challengePath) async {
var subPath = challengePath ?? '/payments';
var url = hosts.api + subPath;
var opts = RequestOptions.fromClientId(clientId)
.setData({
...payment,
'hints': hints,
})
.setVersion(1)
.setTimeout(timeout);
var response = await requester.request(url, opts);
Map<String, dynamic> body = jsonDecode(await response.getBody());
if (body['challenges'] != null &&
(body['challenges'] as List<dynamic>).isNotEmpty) {
var fetchChallenge = (body['challenges'] as List<dynamic>)
.map((e) => PaymentChallenge.fromJSON(e))
.where((c) => c.type == 'fetch')
.first;
return paymenCreate(payment, hints, fetchChallenge.path);
}
if (body['hints'] != null && (body['hints'] as List<dynamic>).isNotEmpty) {
var hintsResp = HintsResponse.fromJSON(body);
return paymenCreate(payment, [...hints, ...hintsResp.hints], null);
}
return PaymentResponse.fromJSON(body);
}