diff --git a/lib/src/protocol/models/message_data.dart b/lib/src/protocol/models/message_data.dart index 568b941..a3b6ddb 100644 --- a/lib/src/protocol/models/message_data.dart +++ b/lib/src/protocol/models/message_data.dart @@ -314,24 +314,104 @@ class OrderData extends MessageData { } class OrderStatusData extends MessageData { - final String orderStatus; + final Status status; - OrderStatusData({required this.orderStatus}); + OrderStatusData({required this.status}); factory OrderStatusData.fromJson(Map json) { return OrderStatusData( - orderStatus: json['orderStatus'], + status: Status.fromJson(json['status']), ); } @override Map toJson() { return { - 'orderStatus': orderStatus, + 'status': status.toJson(), }; } } +enum Status { + payinPending, + payinInitiated, + payinSettled, + payinFailed, + payinExpired, + payoutPending, + payoutInitiated, + payoutSettled, + payoutFailed, + refundPending, + refundInitiated, + refundFailed, + refundSettled; + + static Status fromJson(String json) { + switch (json) { + case 'PAYIN_PENDING': + return Status.payinPending; + case 'PAYIN_INITIATED': + return Status.payinInitiated; + case 'PAYIN_SETTLED': + return Status.payinSettled; + case 'PAYIN_FAILED': + return Status.payinFailed; + case 'PAYIN_EXPIRED': + return Status.payinExpired; + case 'PAYOUT_PENDING': + return Status.payoutPending; + case 'PAYOUT_INITIATED': + return Status.payoutInitiated; + case 'PAYOUT_SETTLED': + return Status.payoutSettled; + case 'PAYOUT_FAILED': + return Status.payoutFailed; + case 'REFUND_PENDING': + return Status.refundPending; + case 'REFUND_INITIATED': + return Status.refundInitiated; + case 'REFUND_FAILED': + return Status.refundFailed; + case 'REFUND_SETTLED': + return Status.refundSettled; + default: + throw Exception('Unknown status: $json'); + } + } + + String toJson() { + switch (this) { + case Status.payinPending: + return 'PAYIN_PENDING'; + case Status.payinInitiated: + return 'PAYIN_INITIATED'; + case Status.payinSettled: + return 'PAYIN_SETTLED'; + case Status.payinFailed: + return 'PAYIN_FAILED'; + case Status.payinExpired: + return 'PAYIN_EXPIRED'; + case Status.payoutPending: + return 'PAYOUT_PENDING'; + case Status.payoutInitiated: + return 'PAYOUT_INITIATED'; + case Status.payoutSettled: + return 'PAYOUT_SETTLED'; + case Status.payoutFailed: + return 'PAYOUT_FAILED'; + case Status.refundPending: + return 'REFUND_PENDING'; + case Status.refundInitiated: + return 'REFUND_INITIATED'; + case Status.refundFailed: + return 'REFUND_FAILED'; + case Status.refundSettled: + return 'REFUND_SETTLED'; + } + } +} + class OrderInstructionsData extends MessageData { final PaymentInstruction payin; final PaymentInstruction payout; diff --git a/test/helpers/test_data.dart b/test/helpers/test_data.dart index 523b875..2b17d89 100644 --- a/test/helpers/test_data.dart +++ b/test/helpers/test_data.dart @@ -169,7 +169,7 @@ class TestData { aliceDid.uri, pfiDid.uri, TypeId.generate(MessageKind.rfq.name), - OrderStatusData(orderStatus: 'order status'), + OrderStatusData(status: Status.payinInitiated), ); } diff --git a/test/protocol/models/order_status_test.dart b/test/protocol/models/order_status_test.dart index 4affc65..64897ba 100644 --- a/test/protocol/models/order_status_test.dart +++ b/test/protocol/models/order_status_test.dart @@ -17,13 +17,13 @@ void main() async { TestData.pfi, TestData.alice, TypeId.generate(MessageKind.rfq.name), - OrderStatusData(orderStatus: 'my status'), + OrderStatusData(status: Status.payinInitiated), ); expect(orderStatus.metadata.id, startsWith(MessageKind.orderstatus.name)); expect(orderStatus.metadata.kind, equals(MessageKind.orderstatus)); expect(orderStatus.metadata.protocol, equals('1.0')); - expect(orderStatus.data.orderStatus, equals('my status')); + expect(orderStatus.data.status, equals(Status.payinInitiated)); }); test('can parse and verify order status from a json string', () async {