Skip to content

Commit

Permalink
Merge pull request #8 from flutter-stripe/mock_server
Browse files Browse the repository at this point in the history
Mock server
  • Loading branch information
jonasbark authored Apr 6, 2021
2 parents 16d6c94 + 97f65d7 commit 200fa7b
Show file tree
Hide file tree
Showing 21 changed files with 10,083 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"name": "example",
"cwd": "stripe/example",
"request": "launch",
"type": "dart"
"type": "dart",
},

]
}
1 change: 1 addition & 0 deletions stripe/example/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env.dart
# Miscellaneous
*.class
*.log
Expand Down
1 change: 1 addition & 0 deletions stripe/example/android/app/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<application android:usesCleartextTraffic="true"/>
</manifest>
1 change: 1 addition & 0 deletions stripe/example/lib/.env.example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const stripePublishableKey = "<ADD_YOUR_KEY_HERE>";
5 changes: 5 additions & 0 deletions stripe/example/lib/config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:flutter/foundation.dart';

final kApiUrl = defaultTargetPlatform == TargetPlatform.android
? 'http://10.0.2.2:4242'
: 'http://localhost:4242';
4 changes: 3 additions & 1 deletion stripe/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:flutter/services.dart';
import 'package:stripe/stripe.dart';
import 'package:stripe_example/config.dart';
import '.env.dart';
import 'package:stripe_example/screens/home_screen.dart';

void main() async {
Expand All @@ -19,7 +21,7 @@ class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StripeProvider(
publishableKey: 'pk_test_oDBTDWWrb1kezz6lwq9zmeoW00XmZlpvM6',
publishableKey: stripePublishableKey,
merchantIdentifier: 'Hello',
child: DismissFocusOverlay(
child: MaterialApp(
Expand Down
56 changes: 42 additions & 14 deletions stripe/example/lib/screens/setup_future_payment_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import 'dart:convert';
import 'dart:developer';

import 'package:flutter/material.dart' hide Card;
import 'package:stripe/stripe.dart';
import 'package:http/http.dart' as http;
import 'package:stripe_example/config.dart';

class SetupFuturePaymentScreen extends StatefulWidget {
@override
Expand Down Expand Up @@ -45,9 +50,9 @@ class _SetupFuturePaymentScreenState extends State<SetupFuturePaymentScreen> {
if (_card == null) {
return;
}

try {
// 1. Create setup intent on backend
const clientSecret = ''; //await createSetupIntentOnBackend(email);
final clientSecret = await createSetupIntentOnBackend('test@gmail.com');

// 2. Gather customer billing information (ex. email)
final BillingDetails billingDetails = BillingDetails(
Expand All @@ -61,22 +66,27 @@ class _SetupFuturePaymentScreenState extends State<SetupFuturePaymentScreen> {
); // mocked data for tests

// 3. Confirm setup intent
await Stripe.instance
.confirmSetupIntent(
clientSecret,
PaymentMethodParams.card(
cardDetails: _card,
//billingDetails,
))
.then((setupIntentResult) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(

final setupIntentResult = await Stripe.instance.confirmSetupIntent(
clientSecret,
PaymentMethodParams.card(
cardDetails: _card,
//billingDetails,
));
log('Setup Intent created $setupIntentResult');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Success: Setup intent created. Intent status: ${setupIntentResult}')));
'Success: Setup intent created. Intent status: ${setupIntentResult}'),
),
);
//setSetupIntent(setupIntentResult);
}).catchError((error) {

} catch (error, s) {
log('Error while saving payment', error: error, stackTrace: s);
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Error code: ${error}')));
});
}
}

void _handleOffSessionPayment() {}
Expand Down Expand Up @@ -109,4 +119,22 @@ class _SetupFuturePaymentScreenState extends State<SetupFuturePaymentScreen> {
Alert.alert('Success', 'The payment was confirmed successfully!');
}*/
}

Future<String> createSetupIntentOnBackend(String email) async {
final url = Uri.parse('${kApiUrl}/create-setup-intent');
final response = await http.post(
url,
headers: {
'Content-Type': 'application/json',
},
body: json.encode({
'email': email,
}),
);
final Map<String, dynamic> bodyResponse = json.decode(response.body);
final clientSecret = bodyResponse['clientSecret'] as String;
log('Client token $clientSecret');

return clientSecret;
}
}
8 changes: 8 additions & 0 deletions stripe/example/server/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Stripe API keys - see https://stripe.com/docs/development/quickstart#api-keys
STRIPE_PUBLISHABLE_KEY=pk_test
STRIPE_SECRET_KEY=sk_test

# Required to run webhook
# See README on how to use the Stripe CLI to setup
# Ignore when running `without-webhooks` samples
STRIPE_WEBHOOK_SECRET=whsec_
6 changes: 6 additions & 0 deletions stripe/example/server/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# don't ever lint node_modules
node_modules
# don't lint build output (make sure it's set to your correct build folder name)
build
# don't lint nyc coverage output
coverage
23 changes: 23 additions & 0 deletions stripe/example/server/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"prettier",
"jest"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"no-console": 1,
"prettier/prettier": 2
},
"env": {
"browser": true,
"node": true,
"jest/globals": true
}
}
107 changes: 107 additions & 0 deletions stripe/example/server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test
19 changes: 19 additions & 0 deletions stripe/example/server/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug TypeScript in Node.js",
"preLaunchTask": "npm: build",
"program": "${workspaceFolder}/src/index.ts",
"protocol": "inspector",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
],
"sourceMaps": true,
"smartStep": true,
"internalConsoleOptions": "openOnSessionStart"
}
]
}
14 changes: 14 additions & 0 deletions stripe/example/server/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
]
}
]
}
44 changes: 44 additions & 0 deletions stripe/example/server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 🧰 Simple TypeScript Starter | 2020

> We talk about a lot of **advanced Node.js and TypeScript** concepts on [the blog](https://khalilstemmler.com), particularly focused around Domain-Driven Design and large-scale enterprise application patterns. However, I received a few emails from readers that were interested in seeing what a basic TypeScript starter project looks like. So I've put together just that.
### Features

- Minimal
- TypeScript v4
- Testing with Jest
- Linting with Eslint and Prettier
- Pre-commit hooks with Husky
- VS Code debugger scripts
- Local development with Nodemon

### Scripts

#### `npm run start:dev`

Starts the application in development using `nodemon` and `ts-node` to do hot reloading.

#### `npm run start`

Starts the app in production by first building the project with `npm run build`, and then executing the compiled JavaScript at `build/index.js`.

#### `npm run build`

Builds the app at `build`, cleaning the folder first.

#### `npm run test`

Runs the `jest` tests once.

#### `npm run test:dev`

Run the `jest` tests in watch mode, waiting for file changes.

#### `npm run prettier-format`

Format your code.

#### `npm run prettier-watch`

Format your code in watch mode, waiting for file changes.

6 changes: 6 additions & 0 deletions stripe/example/server/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"watch": ["src"],
"ext": ".ts,.js",
"ignore": [],
"exec": "ts-node ./src/index.ts"
}
Loading

0 comments on commit 200fa7b

Please sign in to comment.