completePayment method

void completePayment(
  1. void verifyPayment(
    1. String
    ),
  2. void onError(
    1. CFErrorResponse,
    2. String
    ),
  3. String card_cvv,
  4. String card_holder_name,
  5. String card_expiry_month,
  6. String card_expiry_year,
  7. Map<String, dynamic> session,
  8. bool save_payment_method,
  9. String? instrument_id,
)

Implementation

void completePayment(final void Function(String) verifyPayment,
    final void Function(CFErrorResponse, String) onError,
    String card_cvv,
    String card_holder_name,
    String card_expiry_month,
    String card_expiry_year,
    Map<String, dynamic> session,
    bool save_payment_method,
    String? instrument_id) {

  Map<String, String> card = {};

  if(instrument_id != null) {
    card = {
      "instrument_id": instrument_id,
      "card_cvv": card_cvv,
    };
  } else {
    card = {
      "card_holder_name": card_holder_name,
      "card_cvv": card_cvv,
      "card_expiry_month": card_expiry_month,
      "card_expiry_year": card_expiry_year,
      "card_number": _controller.text.replaceAll(' ', ''),
    };
  }

  Map<String, dynamic> data = {
    "session": session,
    "card": card,
    "save_payment_method": save_payment_method,
  };

  // Create Method channel here
  MethodChannel methodChannel = const MethodChannel(
      'flutter_cashfree_pg_sdk');
    methodChannel.invokeMethod("doCardPayment", data).then((value) {
      if(value != null) {
        final body = json.decode(value);
        var status = body["status"] as String;
        switch (status) {
          case "exception":
            var data = body["data"] as Map<String, dynamic>;
            var cfErrorResponse = CFErrorResponse("FAILED", data["message"] as String, "invalid_request", "invalid_request");
            onError(cfErrorResponse, session["order_id"] ?? "order_id_not_found");
            break;
          case "success":
            var data = body["data"] as Map<String, dynamic>;
            verifyPayment(data["order_id"] as String);
            break;
          case "failed":
            var data = body["data"] as Map<String, dynamic>;
            var errorResponse = CFErrorResponse(
                data["status"] as String, data["message"] as String,
                data["code"] as String, data["type"] as String);
            onError(errorResponse, data["order_id"] as String);
            break;
        }
      }
    });
}