Skip to content

Commit

Permalink
feat: started creating customer in Stripe
Browse files Browse the repository at this point in the history
  • Loading branch information
JanssenBrm committed Jul 28, 2023
1 parent 6a69331 commit 25f247d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 31 deletions.
68 changes: 50 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"moment": "^2.29.4",
"ol": "^6.2.1",
"rxjs": "^6.5.4",
"stripe": "^12.16.0",
"uuid": "^7.0.2",
"zone.js": "~0.11.4"
},
Expand Down
9 changes: 5 additions & 4 deletions scripts/setenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ const targetPath = isProduction

const environmentFileContent = `
export const environment = {
production: "${isProduction}",
api: "${process.env.API_URL}",
firebase: {
production: "${isProduction}",
api: "${process.env.API_URL}",
firebase: {
apiKey: "${process.env.FB_API_KEY}",
authDomain: "${process.env.FB_AUTH_DOMAIN}",
databaseURL: "${process.env.FB_DATABASE_URL}",
projectId: "${process.env.FB_PROJECT_ID}",
storageBucket: "${process.env.FB_STORAGE_BUCKET}",
messagingSenderId: "${process.env.FB_MESSAGE_SENDER_ID}",
appId: "${process.env.FB_APP_ID}",
measurementId: "${process.env.FB_MEASUREMENT_ID}"
measurementId: "${process.env.FB_MEASUREMENT_ID}",
},
stripeKey: "${process.env.STRIPE_KEY}",
owm_key: "${process.env.OWM_KEY}"
};
`;
Expand Down
1 change: 1 addition & 0 deletions src/app/models/userdata.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface UserData {
id: string;
name: string;
role: UserRole;
customerId?: string;
}
31 changes: 22 additions & 9 deletions src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { Injectable } from '@angular/core';
import { auth, User } from 'firebase';
import { AngularFireAuth } from '@angular/fire/auth';
import { Router } from '@angular/router';
import { BehaviorSubject, Observable, of } from 'rxjs';
import { BehaviorSubject, from, Observable, of } from 'rxjs';
import { AngularFirestore, DocumentSnapshot } from '@angular/fire/firestore';
import { UserData, UserRole } from '../models/userdata.model';
import { AngularFireAnalytics } from '@angular/fire/analytics';
import { catchError, map, switchMap, take, tap } from 'rxjs/operators';
import { StripeService } from './stripe.service';

@Injectable({
providedIn: 'root',
Expand All @@ -22,15 +23,16 @@ export class AuthService {
public fbAuth: AngularFireAuth,
public router: Router,
private fireStore: AngularFirestore,
private analytics: AngularFireAnalytics
private analytics: AngularFireAnalytics,
private stripeService: StripeService
) {
this.user = new BehaviorSubject<User>(undefined);
this.userData = new BehaviorSubject<UserData>(undefined);
this.fbAuth.authState
.pipe(
switchMap((user: User) => {
if (user) {
return this.readUserData(user.uid).pipe(
return this.readUserData(user).pipe(
map((data: UserData) => ({
user,
data,
Expand All @@ -45,8 +47,8 @@ export class AuthService {
this.user.next(user);
if (user) {
localStorage.setItem('user', JSON.stringify(user));
this.updateUser(user, data?.role).then(() => {
console.log('User updated', user);
this.updateUser(user, data).then(() => {
console.log('User updated');
});
this.router.navigate(['map']);
} else {
Expand All @@ -55,25 +57,36 @@ export class AuthService {
});
}

private async updateUser(user: User, role: UserRole) {
private async updateUser(user: User, data: UserData) {
await this.fireStore
.collection<UserData>('users')
.doc(user.uid)
.set({
id: user.uid,
name: user.displayName,
role: role || UserRole.BASIC,
role: data.role || UserRole.BASIC,
customerId: data.customerId,
});
}

private readUserData(uid: string): Observable<UserData> {
private readUserData(user: User): Observable<UserData> {
return this.fireStore
.collection<UserData>('users')
.doc(uid)
.doc(user.uid)
.get()
.pipe(
take(1),
map((data: DocumentSnapshot<UserData>) => data.data()),
switchMap((data: UserData) =>
data.customerId
? of(data)
: from(
this.stripeService.createCustomer(data, user.email).then((id) => ({
...data,
customerId: id,
}))
).pipe()
),
catchError((error: any) => {
console.error('Could not retrieve user data', error);
return of(undefined);
Expand Down
19 changes: 19 additions & 0 deletions src/app/services/stripe.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import { UserData } from '../models/userdata.model';
import Stripe from 'stripe';
import { environment } from '../../environments/environment';

@Injectable({
providedIn: 'root',
})
export class StripeService {
private stripe = new Stripe(environment.stripeKey, null);

public async createCustomer(user: UserData, email: string): Promise<string> {
const customer = await this.stripe.customers.create({
name: user.name,
email,
});
return customer.id;
}
}

0 comments on commit 25f247d

Please sign in to comment.