createOrder method

Future<int> createOrder(
  1. double amount,
  2. String currency
)

Create a new order

Implementation

Future<int> createOrder(double amount, String currency) async {
  if (_authToken == null) {
    throw const PaymentInitializationException('Authentication token not available');
  }

  try {
    final response = await _apiService.post<Map<String, dynamic>>(
      ApiConstants.order,
      data: {
        "auth_token": _authToken,
        "delivery_needed": false,
        "amount_cents": ((amount * 100).round()).toString(),
        "currency": currency,
        "items": <Map<String, dynamic>>[]
      },
    );

    if (response.statusCode != null && response.statusCode! >= 200) {
      _orderId = int.tryParse(response.data?["id"]?.toString() ?? '0');
      return _orderId!;
    } else {
      throw const OrderCreationException();
    }
  } catch (e) {
    if (e is PaymobException) rethrow;
    throw const OrderCreationException('Failed to create order');
  }
}