Angular 19 . Paypal REST API - Request is not well-formed, syntactically incorrect, or...

Discussão em 'Angular' iniciado por Stefano Giammori, Março 6, 2025.

  1. Where i'm wrong??

    payment.service.ts

    autenticaService(): Observable<Object> {

    const uri = '/v1/oauth2/token';
    const data = 'grant_type=client_credentials';
    let body = new URLSearchParams();
    body.set('grant_type', 'client_credentials');

    return this.httpClient.post(
    `${this.endpoint_url}/v1/oauth2/token`, body ).pipe(
    map(
    data => {
    return data;
    }
    ))
    }

    createOrder = (): Observable<Object> => {

    let order_data_json = {
    'intent': 'CAPTURE',
    'purchase_units': [{
    'amount': {
    'currency_code': 'EUR',
    'value': '0.01'
    }
    }]
    };

    const data = JSON.stringify(order_data_json);

    return this.httpClient.post(
    `${this.endpoint_url}/v2/checkout/orders`, data, { headers }).pipe(
    map(
    data => {
    console.log(data);
    return data;
    }
    ))
    }


    shopping-cart.ts

    acquista = () => {
    //https://www.paypal.com/ncp/payment/AB32BUFY4R824
    this.paymentService.autenticaService().subscribe(
    body => {
    let authPaymentToken = this.paymentService.parseBearer(JSON.stringify(body));
    this.cookieService.set("TokenKey", 'Bearer ..here i put the token..');
    this.paymentService.createOrder().subscribe(
    body => {
    console.log(body);
    let link = Object(body);
    let prodotti: IArticoli[] = [];
    this.ordine = {
    idord: '000002',
    idutente: '123Stezza',
    idorganizzazione: 'mygroup',
    listaProdotti: this.prodotti,
    mailverificata: false,
    pagamentoeseguito: false,
    date: new Date(),
    codStat: 'in attesa',
    linkpagamento: link['links'][3]['href']
    }

    console.log(this.ordine);
    this.ordiniService.insOrdine(this.ordine).subscribe({
    next: (response) => {
    console.log(response);
    //window.location.href = link['links'][1]['href'];
    this.paymentService.captureOrder(this.ordine.linkpagamento).subscribe({
    next: (response) => {
    console.log(response);
    //window.location.href = link['links'][1]['href'];
    },
    error: (error) => {
    console.log(error);
    }
    });
    },
    error: (error) => {
    console.log(error);
    }
    });
    }
    )
    });
    }


    interceptor


    if (req.url.indexOf(`${environment.authPaypalUri}/v1/oauth2/token`) === 0) {
    const auth = `${environment.client_id}:${environment.client_secret}`;
    let authString = "Basic " + window.btoa(auth);

    req = req.clone({
    setHeaders: { 'Authorization': `${authString}` }
    });
    console.log(req);
    return next.handle(req);
    } else if (req.url.indexOf(`${environment.authPaypalUri}/v2/checkout/orders`) === 0) {
    const auth = `${environment.client_id}:${environment.client_secret}`;
    let authPaymentToken = this.cookieService.get("TokenKey");
    req = req.clone({
    setHeaders: { 'Content-Type': 'application/json; charset=utf-8', 'Authorization': `${authPaymentToken}`, 'Accept' : 'application/json', }
    });
    return next.handle(req);
    }

    I'm going crazy with this stuff.. how can i resolve? I think i'm putting everything ok.

    I've followed this hints : https://developer.paypal.com/docs/api/orders/v2/#orders_get

    I can get the token, sometimes, but the "createOrder()" method doesn't want to go over..

    HttpRequest HttpResponse

    Continue reading...

Compartilhe esta Página