Let's use the latest version of the in_app_purchase
package in Flutter to implement the API integration for getting courses, a single course, and applying promo codes.
Ensure your pubspec.yaml
file includes the latest in_app_purchase
package:
dependencies:
flutter:
sdk: flutter
in_app_purchase_storekit: ^0.3.0
http: ^0.13.3
Implement the Laravel endpoints to fetch course data and apply promo codes.
<?php
namespace App\Http\Controllers;
use App\Models\Course;
use App\Models\PromoCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class CoursesController extends Controller
{
public function getCourses()
{
$courses = Course::all();
return response()->json($courses);
}
public function getCourse($id)
{
$course = Course::find($id);
if (!$course) {
return response()->json(['error' => 'Course not found'], 404);
}
return response()->json($course);
}
public function applyPromoCode(Request $request)
{
$code = $request->input('code');
$promo = PromoCode::where('code', $code)->first();
if ($promo && $promo->isValid()) {
return response()->json(['success' => true, 'discount' => $promo->discount]);
}
return response()->json(['success' => false, 'message' => 'Invalid promo code'], 400);
}
}
Implement the Flutter app to fetch courses, display them, and handle in-app purchases.
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Course {
final String id;
final String title;
final String description;
final double price;
Course({required this.id, required this.title, required this.description, required this.price});
factory Course.fromJson(Map<String, dynamic> json) {
return Course(
id: json['id'],
title: json['title'],
description: json['description'],
price: json['price'],
);
}
}
class CoursesScreen extends StatefulWidget {
@override
_CoursesScreenState createState() => _CoursesScreenState();
}
class _CoursesScreenState extends State<CoursesScreen> {
List<Course> _courses = [];
@override
void initState() {
super.initState();
_fetchCourses();
}
Future<void> _fetchCourses() async {
final response = await http.get(Uri.parse('https://yourapi.com/api/courses'));
if (response.statusCode == 200) {
final List<dynamic> coursesJson = json.decode(response.body);
setState(() {
_courses = coursesJson.map((json) => Course.fromJson(json)).toList();
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Courses')),
body: ListView.builder(
itemCount: _courses.length,
itemBuilder: (context, index) {
final course = _courses[index];
return ListTile(
title: Text(course.title),
subtitle: Text(course.description),
trailing: Text('\$${course.price}'),
onTap: () {
// Navigate to course details and purchase screen
},
);
},
),
);
}
}
Implementing in-app purchases using the in_app_purchase_storekit
package.
import 'package:flutter/material.dart';
import 'package:in_app_purchase_storekit/in_app_purchase_storekit.dart';
class PurchaseService {
final InAppPurchase _iap = InAppPurchase.instance;
void initialize() {
_iap.purchaseStream.listen(_onPurchaseUpdated);
_iap.restorePurchases();
}
void _onPurchaseUpdated(List<PurchaseDetails> purchaseDetailsList) {
purchaseDetailsList.forEach((PurchaseDetails purchaseDetails) async {
if (purchaseDetails.status == PurchaseStatus.pending) {
// Handle pending state
} else if (purchaseDetails.status == PurchaseStatus.error) {
// Handle error state
} else if (purchaseDetails.status == PurchaseStatus.purchased) {
// Handle successful purchase
}
});
}
void buyProduct(ProductDetails productDetails) {
final PurchaseParam purchaseParam = PurchaseParam(productDetails: productDetails);
_iap.buyNonConsumable(purchaseParam: purchaseParam);
}
}
void _applyPromoCode(String code) async {
final response = await http.post(
Uri.parse('https://yourapi.com/api/apply-promo-code'),
headers: {
'Authorization': 'Bearer ${your_token}',
'Content-Type': 'application/json'
},
body: json.encode({'code': code}),
);
if (response.statusCode == 200) {
final discount = json.decode(response.body)['discount'];
// Apply discount logic
} else {
// Handle invalid promo code
}
}
By following these steps, you can implement the required API endpoints and integrate them into your Flutter application with in-app purchases.