Flutter āĻ ā§āϝāĻžāĻĒā§ Payment System āϞāĻžāĻāĻŦā§? āĻāĻ āĻĻā§āĻāĻŋā§ā§ āĻĻāĻŋāĻā§āĻāĻŋ āĻā§āĻāĻžāĻŦā§ āĻŽāĻžāϤā§āϰ ā§Ē āϧāĻžāĻĒā§ Stripe Payment Gateway āĻāύāĻāĻŋāĻā§āϰā§āĻ āĻāϰāĻž āϝāĻžā§! đ
pubspec.yaml āĻĢāĻžāĻāϞ⧠āύāĻŋāĻā§āϰ āĻā§āĻĄ āĻĻāĻŋāύ:
flutter_stripe: ^10.0.0
http: ^0.13.5
flutter pub get
void main() async {
_setup();
runApp(MyApp());
}
Future<void> _setup()async{
WidgetsFlutterBinding.ensureInitialized();
Stripe.publishableKey=stripePublishableKey;
}
const String stripePublishableKey = "pk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const String stripeSecretKey = "sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
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()}");
}
}
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()}");
}
}
String _calculateAmount(int amount) {
// Stripe expects amount in cents
final calculated = amount * 100;
return calculated.toString();
}
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 āĻāϰ āĻā§āϏā§āĻ āĻāĻžāϰā§āĻĄ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰā§āύ:
āĻāĻžāϰā§āĻĄ: 4242 4242 4242 4242
āϤāĻžāϰāĻŋāĻ: āϝā§āĻā§āύ⧠āĻāĻŦāĻŋāώā§āϝ⧠āϤāĻžāϰāĻŋāĻ (08/76)
CVC: āϝā§āĻā§āύ⧠3 āĻĄāĻŋāĻāĻŋāĻ (3333)
â FVM āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāϞ⧠Flutter version handle āϏāĻšāĻ āĻšā§
â Secret Key āĻāĻāύ⧠client-side āĻ āϰāĻžāĻāĻŦā§āύ āύāĻž (production āĻ backend āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰā§āύ)
â āϏāĻŦāϏāĻŽā§ test mode āĻĻāĻŋā§ā§ development āĻāϰā§āύ