initiate method

Future<InternalResponseModel> initiate({
  1. required BuildContext context,
  2. required ShurjopayRequestModel shurjopayRequestModel,
})

Implementation

Future<InternalResponseModel> initiate({
  required BuildContext context,
  required ShurjopayRequestModel shurjopayRequestModel,
}) async {
  TokenRequestModel tokenRequestModel = TokenRequestModel(
    userName: shurjopayRequestModel.configs.userName,
    password: shurjopayRequestModel.configs.password,
  );
  TokenResponseModel? tokenResponseModel = TokenResponseModel();
  try {
    tokenResponseModel = await _tokenController.getToken(
      tokenRequestModel: tokenRequestModel,
    );
    if (tokenResponseModel != null &&
        tokenResponseModel.token != null &&
        tokenResponseModel.storeId != null &&
        tokenResponseModel.tokenType != null &&
        tokenResponseModel.spCode == 200) {
      await shurjoPayserviceLocator<SPDioService>().update(
        baseURL: getBaseUrl(shurjoPayserviceLocator<SPDioService>().env),
        token: tokenResponseModel.token!,
        tokenType: tokenResponseModel.tokenType!,
      );
      CheckoutRequestModel checkoutRequestModel = CheckoutRequestModel(
        token: tokenResponseModel.token!,
        storeID: tokenResponseModel.storeId!.toString(),
        prefix: shurjopayRequestModel.configs.prefix,
        currency: shurjopayRequestModel.currency,
        amount: shurjopayRequestModel.amount,
        orderID: shurjopayRequestModel.orderID,
        discountAmount: shurjopayRequestModel.discountAmount,
        discountPercentage: shurjopayRequestModel.discountPercentage,
        clientIP: shurjopayRequestModel.configs.clientIP,
        customerName: shurjopayRequestModel.customerName,
        customerPhoneNumber: shurjopayRequestModel.customerPhoneNumber,
        customerEmail: shurjopayRequestModel.customerEmail,
        customerAddress: shurjopayRequestModel.customerAddress,
        customerCity: shurjopayRequestModel.customerCity,
        customerState: shurjopayRequestModel.customerState,
        customerPostcode: shurjopayRequestModel.customerPostcode,
        customerCountry: shurjopayRequestModel.customerCountry,
        returnURL: shurjopayRequestModel.returnURL,
        cancelURL: shurjopayRequestModel.cancelURL,
        value1: shurjopayRequestModel.value1,
        value2: shurjopayRequestModel.value2,
        value3: shurjopayRequestModel.value3,
        value4: shurjopayRequestModel.value4,
      );
      CheckoutResponseModel? checkoutResponseModel = CheckoutResponseModel();
      checkoutResponseModel = await _checkoutController.checkout(
        checkoutRequestModel: checkoutRequestModel,
      );
      spPop(context: context);
      if (checkoutResponseModel != null &&
          checkoutResponseModel.checkoutUrl != null &&
          checkoutResponseModel.spOrderId != null) {
        bool? spWebViewResult;
        // ignore: use_build_context_synchronously
        spWebViewResult = await Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => PaymentGatewayWebview(
              checkoutURL: checkoutResponseModel!.checkoutUrl!,
              returnURL: shurjopayRequestModel.returnURL,
              cancelURL: shurjopayRequestModel.cancelURL,
            ),
          ),
        );
        if (spWebViewResult == true) {
          return InternalResponseModel(
            status: true,
            message: "Please call verifyPayment()",
            spOrderID: checkoutResponseModel.spOrderId,
          );
        } else {
          return InternalResponseModel(
            status: true,
            message: "Please call verifyPayment()",
            spOrderID: checkoutResponseModel.spOrderId,
          );
        }
      } else {
        return InternalResponseModel(
          status: false,
          message: "Something went wrong.",
        );
      }
    } else {
      spPop(context: context);
      return InternalResponseModel(
        status: false,
        errorCode: tokenResponseModel?.spCode,
        message: tokenResponseModel?.message ?? "Something went wrong.",
      );
    }
  } catch (_) {
    spPop(context: context);
    return InternalResponseModel(
      status: false,
      message: "Something went wrong.",
    );
  }
}