juice-shop
108 строк · 4.0 Кб
1/*
2* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3* SPDX-License-Identifier: MIT
4*/
5
6import { UntypedFormControl, Validators } from '@angular/forms'
7import { Component, EventEmitter, Input, type OnInit, Output } from '@angular/core'
8import { PaymentService } from '../Services/payment.service'
9import { MatTableDataSource } from '@angular/material/table'
10import { library } from '@fortawesome/fontawesome-svg-core'
11import { faPaperPlane } from '@fortawesome/free-solid-svg-icons'
12import { faTrashAlt } from '@fortawesome/free-regular-svg-icons/'
13import { TranslateService } from '@ngx-translate/core'
14import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
15
16library.add(faPaperPlane, faTrashAlt)
17
18@Component({
19selector: 'app-payment-method',
20templateUrl: './payment-method.component.html',
21styleUrls: ['./payment-method.component.scss']
22})
23
24export class PaymentMethodComponent implements OnInit {
25@Output() emitSelection = new EventEmitter()
26@Input('allowDelete') public allowDelete: boolean = false
27public displayedColumns = ['Number', 'Name', 'Expiry']
28public nameControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
29// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
30public numberControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.min(1000000000000000), Validators.max(9999999999999999)])
31public monthControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
32public yearControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
33public confirmation: any
34public error: any
35public storedCards: any
36public card: any = {}
37public dataSource
38public monthRange: any[]
39public yearRange: any[]
40public cardsExist: boolean = false
41public paymentId: any = undefined
42
43constructor (public paymentService: PaymentService, private readonly translate: TranslateService, private readonly snackBarHelperService: SnackBarHelperService) { }
44
45ngOnInit () {
46this.monthRange = Array.from(Array(12).keys()).map(i => i + 1)
47this.yearRange = Array.from(Array(20).keys()).map(i => i + 2080)
48if (this.allowDelete) {
49this.displayedColumns.push('Remove')
50} else {
51this.displayedColumns.unshift('Selection')
52}
53this.load()
54}
55
56load () {
57this.paymentService.get().subscribe((cards) => {
58this.cardsExist = cards.length
59this.storedCards = cards
60this.dataSource = new MatTableDataSource<Element>(this.storedCards)
61}, (err) => { console.log(err) })
62}
63
64save () {
65this.card.fullName = this.nameControl.value
66this.card.cardNum = this.numberControl.value
67this.card.expMonth = this.monthControl.value
68this.card.expYear = this.yearControl.value
69this.paymentService.save(this.card).subscribe((savedCards) => {
70this.error = null
71this.translate.get('CREDIT_CARD_SAVED', { cardnumber: String(savedCards.cardNum).substring(String(savedCards.cardNum).length - 4) }).subscribe((creditCardSaved) => {
72this.snackBarHelperService.open(creditCardSaved, 'confirmBar')
73}, (translationId) => {
74this.snackBarHelperService.open(translationId, 'confirmBar')
75})
76this.load()
77this.resetForm()
78}, (err) => {
79this.snackBarHelperService.open(err.error?.error, 'errorBar')
80this.resetForm()
81})
82}
83
84delete (id) {
85this.paymentService.del(id).subscribe(() => {
86this.load()
87}, (err) => { console.log(err) })
88}
89
90emitSelectionToParent (id: number) {
91this.emitSelection.emit(id)
92}
93
94resetForm () {
95this.nameControl.markAsUntouched()
96this.nameControl.markAsPristine()
97this.nameControl.setValue('')
98this.numberControl.markAsUntouched()
99this.numberControl.markAsPristine()
100this.numberControl.setValue('')
101this.monthControl.markAsUntouched()
102this.monthControl.markAsPristine()
103this.monthControl.setValue('')
104this.yearControl.markAsUntouched()
105this.yearControl.markAsPristine()
106this.yearControl.setValue('')
107}
108}
109