robiulsuny

đŸ’ŗđŸ“ą Flutter āĻ…ā§āϝāĻžāĻĒ⧇ Stripe Payment Integration āĻ•āϰ⧁āύ — āϖ⧁āĻŦ āϏāĻšāĻœā§‡!

Flutter āĻ…ā§āϝāĻžāĻĒ⧇ Payment System āϞāĻžāĻ—āĻŦ⧇? āφāϜ āĻĻ⧇āĻ–āĻŋā§Ÿā§‡ āĻĻāĻŋāĻšā§āĻ›āĻŋ āϕ⧀āĻ­āĻžāĻŦ⧇ āĻŽāĻžāĻ¤ā§āϰ ā§Ē āϧāĻžāĻĒ⧇ Stripe Payment Gateway āχāύāϟāĻŋāĻ—ā§āϰ⧇āϟ āĻ•āϰāĻž āϝāĻžā§Ÿ! 🚀


🧩 Stripe āĻāϰ dependency āϝ⧋āĻ— āĻ•āϰ⧁āύ

pubspec.yaml āĻĢāĻžāχāϞ⧇ āύāĻŋāĻšā§‡āϰ āϕ⧋āĻĄ āĻĻāĻŋāύ:

flutter_stripe: ^10.0.0
http: ^0.13.5

āϟāĻžāĻ°ā§āĻŽāĻŋāύāĻžāϞ⧇ āϰāĻžāύ āĻ•āϰ⧁āύ:

flutter pub get

🧠 Stripe initialize āĻ•āϰ⧁āύ main.dart āĻĢāĻžāχāϞ⧇

void main() async {
  _setup();  
  runApp(MyApp());
}

Future<void> _setup()async{
  WidgetsFlutterBinding.ensureInitialized();
  Stripe.publishableKey=stripePublishableKey;
}

Copy Keys from your stripe account

const String stripePublishableKey = "pk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const String stripeSecretKey = "sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

đŸ› ī¸ Step-by-Step Stripe Integration:

1. 🧾 Make Payment Flow Method

   Future<void> makePayment() async {
    try {
      String? clientSecret = await _createPaymentIntent(10, "usd");
      if (clientSecret == null) return;

      // Step 1: Initialize payment sheet
      await Stripe.instance.initPaymentSheet(
        paymentSheetParameters: SetupPaymentSheetParameters(
          paymentIntentClientSecret: clientSecret,
          merchantDisplayName: "Khal",
        ),
      );

      await _processPayment();
    } catch (e) {
      print("Payment error: ${e.toString()}");
    }
  }

2. đŸ’ĩ Create Payment Intent Method

  Future<String?> _createPaymentIntent(int amount, String currency) async {
    try {
      Map<String, dynamic> body = {
        'amount': _calculateAmount(amount),
        'currency': currency,
      };

      var response = await http.post(
        Uri.parse("https://api.stripe.com/v1/payment_intents"),
        body: body,
        headers: {
          'Authorization': 'Bearer $stripeSecretKey',
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      );

      if (response.statusCode == 200) {
        final json = jsonDecode(response.body);
        return json['client_secret'];
      } else {
        print("Failed to create payment intent: ${response.body}");
        return null;
      }
    } catch (e) {
      print("Error creating PaymentIntent: ${e.toString()}");
      return null;
    }
  }

### 3. đŸ’ŗ Create Process Payment Method

  Future<void> _processPayment() async {
    try {
      await Stripe.instance.presentPaymentSheet();
      print("Payment Successful");
      Get.to(()=>OrderConfirmationView());
    } catch (e) {
      print("Error presenting payment sheet: ${e.toString()}");
    }
  }

4. 🧾 Calculate Amount

  String _calculateAmount(int amount) {
    // Stripe expects amount in cents
    final calculated = amount * 100;
    return calculated.toString();
  }

All Code Snipet

class StripeService {
  StripeService._();

  static final StripeService instance = StripeService._();

  Future<void> makePayment() async {
    try {
      String? clientSecret = await _createPaymentIntent(10, "usd");
      if (clientSecret == null) return;

  
      await Stripe.instance.initPaymentSheet(
        paymentSheetParameters: SetupPaymentSheetParameters(
          paymentIntentClientSecret: clientSecret,
          merchantDisplayName: "Khal",
        ),
      );


      await _processPayment();
    } catch (e) {
      print("Payment error: ${e.toString()}");
    }
  }

  Future<String?> _createPaymentIntent(int amount, String currency) async {
    try {
      Map<String, dynamic> body = {
        'amount': _calculateAmount(amount),
        'currency': currency,
      };

      var response = await http.post(
        Uri.parse("https://api.stripe.com/v1/payment_intents"),
        body: body,
        headers: {
          'Authorization': 'Bearer $stripeSecretKey',
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      );

      if (response.statusCode == 200) {
        final json = jsonDecode(response.body);
        return json['client_secret'];
      } else {
        print("Failed to create payment intent: ${response.body}");
        return null;
      }
    } catch (e) {
      print("Error creating PaymentIntent: ${e.toString()}");
      return null;
    }
  }

  Future<void> _processPayment() async {
    try {
      await Stripe.instance.presentPaymentSheet();
      print("Payment Successful");
      Get.to(()=>OrderConfirmationView());
    } catch (e) {
      print("Error presenting payment sheet: ${e.toString()}");
    }
  }

  String _calculateAmount(int amount) {
    // Stripe expects amount in cents
    final calculated = amount * 100;
    return calculated.toString();
  }
}

✅ āĻŦā§āϝāĻžāϏ! Stripe āχāύāϟāĻŋāĻ—ā§āϰ⧇āĻļāύ āĻļ⧇āώ! 💸

āĻŸā§‡āĻ¸ā§āϟ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ Stripe āĻāϰ āĻŸā§‡āĻ¸ā§āϟ āĻ•āĻžāĻ°ā§āĻĄ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧁āύ:

āĻ•āĻžāĻ°ā§āĻĄ: 4242 4242 4242 4242  
āϤāĻžāϰāĻŋāĻ–: āϝ⧇āϕ⧋āύ⧋ āĻ­āĻŦāĻŋāĻˇā§āĻ¯ā§Ž āϤāĻžāϰāĻŋāĻ– (08/76) 
CVC: āϝ⧇āϕ⧋āύ⧋ 3 āĻĄāĻŋāϜāĻŋāϟ (3333)

⚡ āĻ…āϤāĻŋāϰāĻŋāĻ•ā§āϤ āϟāĻŋāĻĒāϏ:

✅ FVM āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāϞ⧇ Flutter version handle āϏāĻšāϜ āĻšā§Ÿ

✅ Secret Key āĻ•āĻ–āύ⧋ client-side āĻ āϰāĻžāĻ–āĻŦ⧇āύ āύāĻž (production āĻ backend āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧁āύ)

✅ āϏāĻŦāϏāĻŽā§Ÿ test mode āĻĻāĻŋā§Ÿā§‡ development āĻ•āϰ⧁āύ

Next
Getx_Cli