juice-shop

Форк
0
/
payment-method.component.ts 
108 строк · 4.0 Кб
1
/*
2
 * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3
 * SPDX-License-Identifier: MIT
4
 */
5

6
import { UntypedFormControl, Validators } from '@angular/forms'
7
import { Component, EventEmitter, Input, type OnInit, Output } from '@angular/core'
8
import { PaymentService } from '../Services/payment.service'
9
import { MatTableDataSource } from '@angular/material/table'
10
import { library } from '@fortawesome/fontawesome-svg-core'
11
import { faPaperPlane } from '@fortawesome/free-solid-svg-icons'
12
import { faTrashAlt } from '@fortawesome/free-regular-svg-icons/'
13
import { TranslateService } from '@ngx-translate/core'
14
import { SnackBarHelperService } from '../Services/snack-bar-helper.service'
15

16
library.add(faPaperPlane, faTrashAlt)
17

18
@Component({
19
  selector: 'app-payment-method',
20
  templateUrl: './payment-method.component.html',
21
  styleUrls: ['./payment-method.component.scss']
22
})
23

24
export class PaymentMethodComponent implements OnInit {
25
  @Output() emitSelection = new EventEmitter()
26
  @Input('allowDelete') public allowDelete: boolean = false
27
  public displayedColumns = ['Number', 'Name', 'Expiry']
28
  public nameControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
29
  // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
30
  public numberControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.min(1000000000000000), Validators.max(9999999999999999)])
31
  public monthControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
32
  public yearControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
33
  public confirmation: any
34
  public error: any
35
  public storedCards: any
36
  public card: any = {}
37
  public dataSource
38
  public monthRange: any[]
39
  public yearRange: any[]
40
  public cardsExist: boolean = false
41
  public paymentId: any = undefined
42

43
  constructor (public paymentService: PaymentService, private readonly translate: TranslateService, private readonly snackBarHelperService: SnackBarHelperService) { }
44

45
  ngOnInit () {
46
    this.monthRange = Array.from(Array(12).keys()).map(i => i + 1)
47
    this.yearRange = Array.from(Array(20).keys()).map(i => i + 2080)
48
    if (this.allowDelete) {
49
      this.displayedColumns.push('Remove')
50
    } else {
51
      this.displayedColumns.unshift('Selection')
52
    }
53
    this.load()
54
  }
55

56
  load () {
57
    this.paymentService.get().subscribe((cards) => {
58
      this.cardsExist = cards.length
59
      this.storedCards = cards
60
      this.dataSource = new MatTableDataSource<Element>(this.storedCards)
61
    }, (err) => { console.log(err) })
62
  }
63

64
  save () {
65
    this.card.fullName = this.nameControl.value
66
    this.card.cardNum = this.numberControl.value
67
    this.card.expMonth = this.monthControl.value
68
    this.card.expYear = this.yearControl.value
69
    this.paymentService.save(this.card).subscribe((savedCards) => {
70
      this.error = null
71
      this.translate.get('CREDIT_CARD_SAVED', { cardnumber: String(savedCards.cardNum).substring(String(savedCards.cardNum).length - 4) }).subscribe((creditCardSaved) => {
72
        this.snackBarHelperService.open(creditCardSaved, 'confirmBar')
73
      }, (translationId) => {
74
        this.snackBarHelperService.open(translationId, 'confirmBar')
75
      })
76
      this.load()
77
      this.resetForm()
78
    }, (err) => {
79
      this.snackBarHelperService.open(err.error?.error, 'errorBar')
80
      this.resetForm()
81
    })
82
  }
83

84
  delete (id) {
85
    this.paymentService.del(id).subscribe(() => {
86
      this.load()
87
    }, (err) => { console.log(err) })
88
  }
89

90
  emitSelectionToParent (id: number) {
91
    this.emitSelection.emit(id)
92
  }
93

94
  resetForm () {
95
    this.nameControl.markAsUntouched()
96
    this.nameControl.markAsPristine()
97
    this.nameControl.setValue('')
98
    this.numberControl.markAsUntouched()
99
    this.numberControl.markAsPristine()
100
    this.numberControl.setValue('')
101
    this.monthControl.markAsUntouched()
102
    this.monthControl.markAsPristine()
103
    this.monthControl.setValue('')
104
    this.yearControl.markAsUntouched()
105
    this.yearControl.markAsPristine()
106
    this.yearControl.setValue('')
107
  }
108
}
109

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.