Angular and stripe checkout configuration

46 views
Skip to first unread message

Fofa

unread,
Dec 3, 2019, 5:00:32 AM12/3/19
to Angular and AngularJS discussion
I receive this error ( error TS1005: 'finally' expected.

119   }
and compile failure 
can someone highlight what is wrong in the code. please

import { ComponentOnInit } from '@angular/core';

import { environment } from '../../environments/environment';
import { DataService } from '../data.service';
import { RestApiService } from '../rest-api.service';
import { Router } from '@angular/router';


@Component({
  selector: 'app-cart',
  templateUrl: './cart.component.html',
  styleUrls: ['./cart.component.scss']
})
export class CartComponent implements OnInit {
  btnDisabled = false;
  handlerany;

  quantities: [];

  constructor(
    private dataDataService,
    private restRestApiService,
    private routerRouter
  ) { }

  trackByCartItems(indexnumberitemany) {
    return item._id;
  }

  get cartItems() {
    return this.data.getCart();
  }

  get cartTotal() {
    let total = 0;
    this.cartItems.forEach((dataindex=> {
      total += data['price'] * this.quantities[index];
    });
    return total;
  }

  removeProduct(indexproduct) {
    this.quantities.splice(index1);
    this.data.removeFromCart(product);
  }

  ngOnInit() {
    this.cartItems.forEach(data => {
      this.quantities.push[(1)];
    });
    this.handler = StripeCheckout.configure({
      key: environment.stripeKey,
      image: 'assets/img/favicon.png',
      locale: 'auto',
      token: async stripeToken => {
        let products;
        products = [];
        this.cartItems.forEach((dindex=> {
          products.push({
            product: d['_id'],
            quantity: this.quantities[index]
          });
        });

        try {
          const data = await this.rest.post(
            'http://localhost:5000/api/payment',
            {
              totalPrice: this.cartTotal,
              products,
              stripeToken
            }
          );
          data['success']
            ? (this.data.clearCart(), this.data.success('Purchase Successful'))
            : this.data.error(data['message']);
        } catch (error) {
          this.data.error(error['message']);
        }
      }
    });
  }


  validate() {
    if (!this.quantities.every(data => data > 0)) {
      this.data.warning('Quantity cannot be less than one');
    } else if (!localStorage.getItem('token')) {
      this.router.navigate(['/login'])
      .then(() => {
        this.data.warning('You need to login before making a purchase');
      });
    } else if (!this.data.user['address']) {
      this.router.navigate(['/profile/address'])
      .then(() => {
        this.data.warning('YOU need to login before making a purchase');
      });
    } else {
      this.data.message = '';
      return true;
    }
  }
  checkout() {
    this.btnDisabled = true;
    try {
      if (this.validate()) {
        this.handler.open({
          name: 'ProductUploads',
          description: 'Checkout Payment',
          amount: this.cartTotal * 100,
          closed: () => {
            this.btnDisabled = false;
          }
        });
      } else {
        this.btnDisabled = false;
        }
    }
  }

}


Sander Elias

unread,
Dec 3, 2019, 5:12:40 AM12/3/19
to Angular and AngularJS discussion
Hi Fofa,

I need a bit more context. Can you provide the entire error? Also, point out what lines are failing. I have seen and fixed this error before, but don't recall the details.

Regards
Sander

farida...@gmail.com

unread,
Dec 3, 2019, 4:12:52 PM12/3/19
to ang...@googlegroups.com
Hi Sandler, 
Thank you so much for getting back to me. 

here is the error, I received
"ERROR in src/app/cart/cart.component.ts:119:3 - error TS1005: 'finally' expected.      

119   }
      ~"

if i removed the last  part: the checkout part , angular will compile but it will throw error in the console log when using the checkout cart. , I tried my best to understand where is the error, it is obviously related to stripe configuration. 

line 19 where it is highlighted as the error has nothing but  '}' 

checkout() {
    this.btnDisabled = true;
    try {
      if (this.validate()) {
        this.handler.open({
          name: 'ProductUploads',
          description: 'Checkout Payment',
          amount: this.cartTotal * 100,
          closed: () => {
            this.btnDisabled = false;
          }
        });
      } else {
        this.btnDisabled = false;
        }
    }
  }
--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/c799a249-6b90-43ae-b67a-c99928e26b55%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages