createPaymentRequest static method

Future<Map<String, dynamic>> createPaymentRequest({
  1. UddoktapayCredentials? uddoktapayCredentials,
  2. required CustomerDetails customer,
  3. required String amount,
  4. Map? metadata,
  5. String? webhookUrl,
  6. required BuildContext context,
})

Implementation

static Future<Map<String, dynamic>> createPaymentRequest({
  UddoktapayCredentials? uddoktapayCredentials,
  required CustomerDetails customer,
  required String amount,
  Map? metadata,
  String? webhookUrl,
  required BuildContext context,
}) async {
  final Map<String, dynamic> requestData = {
    'full_name': customer.fullName,
    'email': customer.email,
    'amount': amount,
    'metadata': metadata ?? {"order_id": "10", "product_id": "5"},
    'redirect_url': AppConfig.redirectURL,
    'cancel_url': AppConfig.cancelURL,
    'return_type': 'GET',
    if (webhookUrl != null) 'webhook_url': webhookUrl,
  };

  final controller = Get.put(PaymentController());

  if (uddoktapayCredentials == null) {
    controller.panelURL.value = AppConfig.sandboxURL;
    controller.apiKey.value = AppConfig.sandboxAPIKey;
  } else {
    controller.panelURL.value = uddoktapayCredentials.panelURL;
    controller.apiKey.value = uddoktapayCredentials.apiKey;
  }

  try {
    final http.Response response = await http.post(
      Uri.parse(
        uddoktapayCredentials == null
            ? AppConfig.sandboxURL + Endpoints.createPayment
            : '${uddoktapayCredentials.panelURL}${Endpoints.createPayment}',
      ),
      headers: <String, String>{
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'RT-UDDOKTAPAY-API-KEY': uddoktapayCredentials == null
            ? AppConfig.sandboxAPIKey
            : uddoktapayCredentials.apiKey,
      },
      body: jsonEncode(requestData),
    );

    if (response.statusCode == 200) {
      final dynamic responseData = jsonDecode(response.body);
      return {
        'status': responseData['status'],
        'message': responseData['message'],
        'payment_url': responseData['payment_url'],
      };
    } else {
      final error = jsonDecode(response.body)['message'];
      snackBar(error, context);
      debugPrint(error);
      throw Exception(error);
    }
  } catch (error) {
    snackBar('Something is wrong', context);
    throw Exception('Something is wrong');
  }
}