callNowPaymentsService method

Future callNowPaymentsService(
  1. BuildContext context
)

Implementation

Future callNowPaymentsService(BuildContext context) async {
  try {
    final response = await IncodesPaymentServices.nowPaymentsCreateInvoice(
      apiKey: "Q3HG6S0-SPKMAPY-JSENY6Z-VS6T68E", // Pass your Live or Sandbox API Key here
      priceAmount: 150,
      priceCurrency: "USD",
      payCurrency: "USDTTRC20",
      orderId: null, // Optional: Pass your Order ID, or leave null to auto-generate
      orderDescription: "Test order for NOWPayments",
      // REPLACE THESE WITH YOUR ACTUAL APP DEEP LINKS
      successUrl: "yourdeeplink://payment/success",
      cancelUrl: "yourdeeplink://payment/cancel",
      isTestMode: true,
    );

    final url = Uri.parse(response.invoiceUrl);
    if (await canLaunchUrl(url)) {
      await launchUrl(url, mode: LaunchMode.inAppWebView);
      // Note: url_launcher does not wait for the user to finish the payment.
      // It just opens the browser overlay.
    } else {
      IncodesListener().showToast(msg: "Could not launch payment sheet");
    }

    print("=== NOWPayments Invoice Success ===");
    print("Invoice ID: ${response.id}");
    print("Invoice URL: ${response.invoiceUrl}");
    print("=========================");

    // IMPORTANT: NOWPayments Invoices CANNOT be polled from the frontend
    // because we don't receive a payment ID.
    // To show Success/Failure toasts reliably, you MUST:
    // 1. Set up Deep Linking for `success_url` and `cancel_url` in the API call above.
    // 2. OR rely on your server's Webhook (IPN) to send a push notification to the app.
  } catch (e) {
    IncodesListener().showToast(msg: "NOWPayments Error: $e");
    print("=== NOWPayments Error ===");
    print(e.toString());
    print("=========================");
  }
}