initiate method

Future<DGePayPaymentRespModel> initiate({
  1. required BuildContext context,
  2. required DGePayConfigs dGePayConfigs,
  3. required PaymentReqModel paymentReqModel,
})

Implementation

Future<DGePayPaymentRespModel> initiate({
  required BuildContext context,
  required DGePayConfigs dGePayConfigs,
  required PaymentReqModel paymentReqModel,
}) async {
  try {
    authRespModel = await _tokenController.getToken(
      dGePayConfigs: dGePayConfigs,
    );
    if (authRespModel?.statusCode == 200 && authRespModel?.data != null) {
      String json = sortJson(jsonEncode(paymentReqModel));
      String sorted = replaceEach(json);
      String signature =
          generateSignature(sorted, dGePayConfigs.clientAPIKey);
      String encryptedData = encryptData(json, dGePayConfigs.clientSecretKey);

      var headers = {
        'Authorization': 'Bearer ${authRespModel!.data!.accessToken!}',
        'Content-Type': 'application/json',
        'signature': signature,
      };

      final response = await http.post(
          Uri.parse(
              getBaseUrl(dGePayConfigs.env) + ApiEndpoints.initiatePayment),
          headers: headers,
          body: encryptedData);

      PaymentRespModel paymentRespModel =
          PaymentRespModel.fromJson(jsonDecode(response.body));

      if (response.statusCode == 200) {
        if (paymentRespModel.success == 0) {
          if (context.mounted) dGePayPop(context: context);
          return DGePayPaymentRespModel(
            status: false,
            errorCode: authRespModel?.statusCode,
            message: paymentRespModel.error![0],
          );
        } else {
          if (context.mounted) dGePayPop(context: context);
          dynamic result;
          if (context.mounted){
           result = await Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => PaymentGatewayWebview(
                checkoutURL: paymentRespModel.data!.webviewUrl!,
                returnURL: 'returnURL',
                cancelURL: 'cancelURL',
              ),
            ),
          );}
          return DGePayPaymentRespModel(
              status: true,
              message: result,
              dGePayOrderId: paymentReqModel.uniqueTxnId);
        }
      } else {
        if (context.mounted) dGePayPop(context: context);
        return DGePayPaymentRespModel(
            status: false,
            errorCode: authRespModel?.statusCode,
            message: paymentRespModel.message);
      }
    } else {
      if (context.mounted) dGePayPop(context: context);
      return DGePayPaymentRespModel(
        status: false,
        errorCode: authRespModel?.statusCode,
        message: authRespModel?.error![0] ?? "Something went wrong.",
      );
    }
  } catch (_) {
    if (context.mounted) dGePayPop(context: context);
    return DGePayPaymentRespModel(
      status: false,
      message: "Something went wrong.",
    );
  }
}