Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Api call in js function of angular

I need to call api in javascript function like below but its not taking injected services (subService) or defined variables (formData) in javascript function and getting error of undefined addSub. How should I call api in javascript function?

Please help and guide.

HTML

<button class="btn" (click)="initializePayment(item.id,item.price)">click here</button>

TS

 formData: any;
  loading: boolean = false;

  constructor(
    private subService: SubService,
    ) { }

    initializePayment(sid,amount: number) {
        this.loading = true;
        const paymentHandler = (<any>window).StripeCheckout.configure({
          key: this.stripeKey,
          locale: 'auto',
          token: function (stripeToken: any) {
            console.log({stripeToken});
            const sToken = stripeToken.id;
            this.formData = new FormData();
            this.formData.append('id', this.id);
            this.formData.append('s_id', sid);
            this.formData.append('stripeToken', sToken);
    
            this.subService.addSub(this.formData).subscribe(
              (response) => {
                this.loading = false;
                console.log('resp: ', response);
                if (response['status'] === true) {
                  this.notifyService.showSuccess(
                    'success!!',''
                  );
                  //window.location.reload();
                  this.router.navigate(['/user']);
                } else {
                  this.notifyService.showError(
                    'Something went wrong. Please try later!!!',''
                  );
                }
              },
              (error) => {
                this.notifyService.showError(
                  error,
                  'Oooops Something went wrong. Please try later!!'
                );
                this.loading = false;
              }
            );
            //alert('Stripe token generated!');
          }
        });
      
        paymentHandler.open({
          name: 'test',
          description: 'Upgrade',
          amount: amount * 100
        });
      }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Replace function assigned to token to use an arrow function instead.

token: (stripeToken: any) => {
            console.log({stripeToken});
            const sToken = stripeToken.id;
            this.formData = new FormData();
            this.formData.append('id', this.id);
            this.formData.append('s_id', sid);
            this.formData.append('stripeToken', sToken);
    
            this.subService.addSub(this.formData).subscribe(
              (response) => {
                this.loading = false;
                console.log('resp: ', response);
                if (response['status'] === true) {
                  this.notifyService.showSuccess(
                    'success!!',''
                  );
                  //window.location.reload();
                  this.router.navigate(['/user']);
                } else {
                  this.notifyService.showError(
                    'Something went wrong. Please try later!!!',''
                  );
                }
              },
              (error) => {
                this.notifyService.showError(
                  error,
                  'Oooops Something went wrong. Please try later!!'
                );
                this.loading = false;
              }
            );
            //alert('Stripe token generated!');
          }
        })

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!