cashfree / flutter-cashfree-pg-sdk

Cashfree Flutter Plugin
https://www.cashfree.com/
MIT License
3 stars 8 forks source link

On Flutter Web Callback Functions not working #59

Closed siman302 closed 2 months ago

siman302 commented 2 months ago

I try to implement the cashflow on the flutter web. Everything is working fine, expect then call back functions.

Note: I am testing on the sandbox and so I think domain restriction is not an issue, because I am developer from Pakistan

` // Verify Payment Callback Future verifyPayment(String orderId) async { final verifyUrl = "${_cashfreeBaseUrl}pg/orders/$orderId";

try {
  final response = await http.get(
    Uri.parse(verifyUrl),
    headers: {
      'Content-Type': 'application/json',
      'x-client-id': _apiKey,
      'x-client-secret': _secretKey,
      'x-api-version': '2023-08-01',
    },
  );

  if (response.statusCode == 200) {
    final data = jsonDecode(response.body);
    log("UPI Payment verification successful for Order ID: $orderId");
    // Add further handling for successful payment
  } else {
    log("Failed to verify UPI payment: ${response.statusCode} - ${response.body}");
  }
} catch (e) {
  log("Error while verifying UPI payment: $e");
}

}

// Error Callback void onError(CFErrorResponse errorResponse, String orderId) { log("Payment error for Order ID: $orderId"); log("Error message: ${errorResponse.getMessage()}"); // Add further error handling logic }

// Web Checkout Function Future webCheckout(CFSession session) async { try { // Build the checkout object with the session var cfWebCheckout = CFWebCheckoutPaymentBuilder().setSession(session).build();

  // Set the callback before initiating the payment
  _cfPaymentGatewayService.setCallback(verifyPayment, onError);

  log('Initiating payment...');
  _cfPaymentGatewayService.doPayment(cfWebCheckout);
} on CFException catch (e) {
  log("CFException during payment: ${e.message}");
} catch (e) {
  log("Unexpected error during payment: $e");
}

}`

verifyPayment and onError functions are not invoke, when I check it in details in the cfpaymentgatewayservice.dart file, I found that in function doPayment, the invokeMethod("doWebPayment") return the null value instantly, not wait until the doWebPayment finish execution that why verifyPayment and onError functions not call. Kindly fix the doWebPayment functions, Thanks.

`doPayment(CFPayment cfPayment) { if (verifyPayment == null || onError == null) { throw CFException(CFExceptionConstants.CALLBACK_NOT_SET); }

Map<String, dynamic> data = <String, dynamic>{};

if (cfPayment is CFCardPayment) {
  CFCardPayment cfCardPayment = cfPayment;
  _initiateCardPayment(cfCardPayment);
} else {
  if (cfPayment is CFDropCheckoutPayment) {
    CFDropCheckoutPayment dropCheckoutPayment = cfPayment;
    data = _convertToMap(dropCheckoutPayment);
  } else if (cfPayment is CFWebCheckoutPayment) {
    CFWebCheckoutPayment webCheckoutPayment = cfPayment;
    data = _convertToWebCheckoutMap(webCheckoutPayment);
  } else if (cfPayment is CFUPIPayment) {
    CFUPIPayment cfupiPayment = cfPayment;
    data = _convertToUPItMap(cfupiPayment);
  } else if (cfPayment is CFNetbankingPayment) {
    CFNetbankingPayment cfNetbankingPayment = cfPayment;
    data = _convertToNetbankingMap(cfNetbankingPayment);
  }

  // Create Method channel here
  MethodChannel methodChannel =
      const MethodChannel('flutter_cashfree_pg_sdk');
  if (cfPayment is CFDropCheckoutPayment) {
    methodChannel.invokeMethod("doPayment", data).then((value) {
      responseMethod(value);
    });
  } else if (cfPayment is CFWebCheckoutPayment) {
    methodChannel.invokeMethod("doWebPayment", data).then((value) {
      responseMethod(value);
    });
  } else if (cfPayment is CFUPIPayment) {
    methodChannel.invokeMethod("doUPIPayment", data).then((value) {
      responseMethod(value);
    });
  } else if (cfPayment is CFNetbankingPayment) {
    methodChannel.invokeMethod("doNetbankingPayment", data).then((value) {
      responseMethod(value);
    });
  }
}

}`

suhas-cashfree commented 2 months ago

Are you getting any error here ?

` // Set the callback before initiating the payment _cfPaymentGatewayService.setCallback(verifyPayment, onError);

log('Initiating payment...'); _cfPaymentGatewayService.doPayment(cfWebCheckout); } on CFException catch (e) { log("CFException during payment: ${e.message}"); } catch (e) { log("Unexpected error during payment: $e"); }`

siman302 commented 2 months ago

Yes, @suhas-cashfree, the verifyPayment and onError functions are not being invoked when a payment is made, whether it fails or succeeds.

suhas-cashfree commented 2 months ago

Are you getting any error here -> log("CFException during payment: ${e.message}");

Or here -> log("Unexpected error during payment: $e");

siman302 commented 2 months ago

No, Not get any error, payment done successfully. verifyPayment and onError functions not invoke, that an issue.

Check the code video. flutter web. https://drive.google.com/file/d/1lo4Er_7_psRsZunQuGDdAty3LOqQ8An4/view?usp=sharing

suhas-cashfree commented 2 months ago

Hey hi This is expected behaviour What you are doing is a redirect flow You received verify callback as soon as the web page opened.

You have to have a "return_url" when you create the order. So that once the payment is complete, there is a redirection that will happen to that url

Check return url here -> https://docs.cashfree.com/reference/pgcreateorder

siman302 commented 2 months ago

Yes, I set the return_url, which I send in the request when I get create the order. That link will open successfully, when payment process in completed, whether it completed or failed.

Still I don't received the callbacks. @suhas-cashfree

suhas-cashfree commented 2 months ago

That’s what I was saying. In this flow, you will not get callbacks. You should check the order status on redirection to your return url. The return url will have order id in query params

suhas-cashfree commented 2 months ago

Closing this ticket. Please raise a new one, if the issue still persists

siman302 commented 2 months ago

Thanks @suhas-cashfree