diff --git a/lib/anypay/any_pay_payment_committed_info.dart b/lib/anypay/any_pay_payment_committed_info.dart new file mode 100644 index 000000000..126b3d92e --- /dev/null +++ b/lib/anypay/any_pay_payment_committed_info.dart @@ -0,0 +1,17 @@ +import 'package:flutter/foundation.dart'; +import 'package:cake_wallet/anypay/any_pay_trasnaction.dart'; + +class AnyPayPaymentCommittedInfo { + const AnyPayPaymentCommittedInfo({ + @required this.uri, + @required this.currency, + @required this.chain, + @required this.transactions, + @required this.memo}); + + final String uri; + final String currency; + final String chain; + final List transactions; + final String memo; +} \ No newline at end of file diff --git a/lib/anypay/anypay_api.dart b/lib/anypay/anypay_api.dart index 2804d0616..c0727bc29 100644 --- a/lib/anypay/anypay_api.dart +++ b/lib/anypay/anypay_api.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'package:cake_wallet/anypay/any_pay_payment_committed_info.dart'; import 'package:flutter/foundation.dart'; import 'package:http/http.dart'; import 'package:cw_core/crypto_currency.dart'; @@ -57,7 +58,7 @@ class AnyPayApi { return AnyPayPayment.fromMap(decodedBody); } - Future payment( + Future payment( String uri, {@required String chain, @required String currency, @@ -71,9 +72,21 @@ class AnyPayApi { 'currency': currency, 'transactions': transactions.map((tx) => {'tx': tx.tx, 'tx_hash': tx.id, 'tx_key': tx.key}).toList()}; final response = await post(uri, headers: headers, body: utf8.encode(json.encode(body))); - - if (response.statusCode != 200) { - return null; + if (response.statusCode == 400) { + final decodedBody = json.decode(response.body) as Map; + throw Exception(decodedBody['message'] as String); } + + if (response.statusCode != 200) { + throw Exception('Unexpected response'); + } + + final decodedBody = json.decode(response.body) as Map; + return AnyPayPaymentCommittedInfo( + uri: uri, + currency: currency, + chain: chain, + transactions: transactions, + memo: decodedBody['memo'] as String); } } \ No newline at end of file diff --git a/lib/ionia/ionia_anypay.dart b/lib/ionia/ionia_anypay.dart index b0b92e4d8..b84dbf64d 100644 --- a/lib/ionia/ionia_anypay.dart +++ b/lib/ionia/ionia_anypay.dart @@ -12,6 +12,7 @@ import 'package:cake_wallet/anypay/any_pay_chain.dart'; import 'package:cake_wallet/anypay/any_pay_trasnaction.dart'; import 'package:cake_wallet/bitcoin/bitcoin.dart'; import 'package:cake_wallet/monero/monero.dart'; +import 'package:cake_wallet/anypay/any_pay_payment_committed_info.dart'; class IoniaAnyPay { IoniaAnyPay(this.ioniaService, this.anyPayApi, this.wallet); @@ -31,7 +32,7 @@ class IoniaAnyPay { return anyPayApi.paymentRequest(invoice.uri); } - Future commitInvoice(AnyPayPayment payment) async { + Future commitInvoice(AnyPayPayment payment) async { final transactionCredentials = payment.instructions .where((instruction) => instruction.type == AnyPayPaymentInstruction.transactionType) .map((AnyPayPaymentInstruction instruction) { @@ -80,10 +81,10 @@ class IoniaAnyPay { }) .toList(); - await anyPayApi.payment( - payment.paymentUrl, - chain: payment.chain, - currency: payment.chain, - transactions: transactions); + return await anyPayApi.payment( + payment.paymentUrl, + chain: payment.chain, + currency: payment.chain, + transactions: transactions); } } \ No newline at end of file