-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add web plugin for beta release (#476)
* add web support * feat: prepare release for web * fix: analyzer problems * fix: address changes requested * fix: format and update readme
- Loading branch information
1 parent
d439026
commit 08b7a1e
Showing
73 changed files
with
21,260 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import 'dart:convert'; | ||
import 'dart:developer'; | ||
import 'package:stripe_example/.env.dart'; | ||
import 'platforms/stripe_checkout.dart' | ||
if (dart.library.js) 'platforms/stripe_checkout_web.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:stripe_checkout/stripe_checkout.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
final kApiUrl = defaultTargetPlatform == TargetPlatform.android | ||
? 'http://10.0.2.2:4242' | ||
: 'http://localhost:4242'; | ||
|
||
class CheckoutScreenExample extends StatefulWidget { | ||
CheckoutScreenExample({Key? key, this.title, this.snackBarText}) | ||
: super(key: key); | ||
|
||
final String? title; | ||
|
||
final String? snackBarText; | ||
|
||
static final routes = { | ||
// For Flutter web | ||
'/success': (c) => CheckoutScreenExample( | ||
title: 'Checkout - Done', | ||
snackBarText: 'Paid succesfully', | ||
), | ||
'/canceled': (c) => CheckoutScreenExample( | ||
title: 'Checkout - Canceled', | ||
snackBarText: 'Checkout canceled', | ||
), | ||
}; | ||
|
||
@override | ||
_CheckoutScreenExample createState() => _CheckoutScreenExample(); | ||
} | ||
|
||
class _CheckoutScreenExample extends State<CheckoutScreenExample> { | ||
@override | ||
void initState() { | ||
if (widget.snackBarText != null) { | ||
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar(content: Text(widget.snackBarText!)), | ||
); | ||
}); | ||
} | ||
super.initState(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text(widget.title ?? 'Checkout Example'), | ||
), | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
ElevatedButton( | ||
onPressed: getCheckout, | ||
child: Text('Open Checkout'), | ||
) | ||
], | ||
), | ||
), | ||
); | ||
} | ||
|
||
Future<void> getCheckout() async { | ||
final String sessionId = await _createCheckoutSession(); | ||
final result = await redirectToCheckout( | ||
context: context, | ||
sessionId: sessionId, | ||
publishableKey: stripePublishableKey, | ||
); | ||
|
||
if (mounted) { | ||
final text = result.when( | ||
success: () => 'Paid succesfully', | ||
canceled: () => 'Checkout canceled', | ||
error: (e) => 'Error $e'); | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar(content: Text(text)), | ||
); | ||
} | ||
} | ||
|
||
Future<String> _createCheckoutSession() async { | ||
final url = Uri.parse('$kApiUrl/create-checkout-session'); | ||
final response = await http.post( | ||
url, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: json.encode({ | ||
if (kIsWeb) 'port': getUrlPort(), | ||
}), | ||
); | ||
final Map<String, dynamic> bodyResponse = json.decode(response.body); | ||
final id = bodyResponse['id'] as String; | ||
log('Checkout session id $id'); | ||
return id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//@dart= 2.12 | ||
String getUrlPort() { | ||
throw 'Not implemented'; | ||
} | ||
|
||
String getReturnUrl() { | ||
return 'Not implemented'; | ||
} |
5 changes: 5 additions & 0 deletions
5
example/lib/screens/checkout/platforms/stripe_checkout_web.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'dart:html' as html; | ||
|
||
String getUrlPort() => html.window.location.port; | ||
|
||
String getReturnUrl() => html.window.location.href; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.