Every time I try to enter a variable amount for the price in the Paypal sandbox, an error pops up stating that a non-negative number is required

Despite trying to convert the return value into a number, I still encountered issues. The total amount works fine when manually entering a value like "1.50", but fails when using a variable.

Below is the function I utilize to calculate and return the final price:

getTotal() {
  let total = 0;
  for (var i = 0; i < this.list.length; i++) {
    if (this.list[i].price) {
      total += this.list[i].price;
    }
  }
  return Number(total.toFixed(2));
}

Here's where I encounter an issue while setting the price for paypal payment:

payment: (data, actions) => {
  return actions.payment.create({
    payment: {
      transactions: [{
        amount: {
          total: this.getTotal,
          currency: 'AUD'
        }
      }]
    }
  });
}

When I assign this.getTotal to something specific like "1.50" it works without any problem. However, in its current state, I keep receiving the following error message...

"Currency amount must be non-negative number, may optionally contain exactly 2 decimal places separated by '.'..."

Answer №1

Opt for this alternative:

return parseFloat(total).toFixed(2);

Rather than using:

return Number(total.toFixed(2))

Answer №2

It is assumed that the total amount also accepts strings. Could you please give this a try?

   calculateTotal() {
      let total = 0;
      for (let i = 0; i < this.items.length; i++) {
          if (this.items[i].price) {
              total += this.items[i].price;
          }
      }
      return total.toFixed(Math.max(2, 2));
    }

The error message indicates that it may explicitly contain ONLY 2 decimal places.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The frontend is not triggering the Patch API call

I am having trouble with my http.patch request not being called to the backend. This issue only occurs when I try calling it from the frontend. Oddly enough, when I tested it in Postman, everything worked perfectly. Testing the backend on its own shows t ...

What causes content to overflow a flex container?

Hey there, I currently have the following structure: <div fxLayout="row" fxLayout.xs="column" fxLayoutAlign="space-around start" fxLayoutGap="2%" style="padding:2%;"> <div fxFlex style="line-height:2;"> <md-card> ...

When running ng new command, an error may occur with dryRunSink.(...).concat not functioning properly

I've been attempting to create a new Angular project by following the steps outlined on this website: https://github.com/angular/angular-cli However, each time I try to initiate a new project using the ng new command, an error occurs. E:\Code&b ...

Exploring Angular2 Guides: Understanding Hierarchical Dependency Injection with a Focus on Service Restoration

As I delved into the Angular2 documentation, a specific example caught my eye in the Hierarchical Dependency Injectors chapter. It introduced me to the Restore service, which enables save/cancel functionality for editing. The code for this service is as f ...

Error 404: Webpack Dev Server Proxy Not Found

Recently, I joined a new team where I am tasked with getting familiar with the project code as a freshman. The project is built using Angular2 and Spring Boot, along with Webpack for bundling assets and proxy settings through Webpack Dev Server to connect ...

Combining JavaScript files can cause conflicts with duplicate identifiers when using Typescript namespaces

As a newcomer to Typescript, I am grappling with the concept of namespaces and how to reference Interfaces that are defined in separate files. Coming from a .NET C# background, I am used to creating each class and interface in their own file. INationalRai ...

After deploying to Heroku, cal-heatmap encounters errors despite functioning correctly in a local environment

I successfully implemented a cal-heatmap instance in my Angular 2 (angular-cli) project locally, but when I deployed the project to Heroku, I encountered some errors that prevent the cal-heatmap from displaying. https://i.stack.imgur.com/8gY90.png The er ...

Effective methods for transmitting reactive form control between child and parent components in Angular 2

I am working with a nested reactive form structure and I have the following setup: Parent HTML Code <form> <child-component></child-component> <button mat-raised-button color="primary">Save</button> </form> Child HT ...

The dropdown menu in Angular 2/4 isn't displaying the selected item

I am currently working on implementing a dropdown feature in my Angular application. The dropdown consists of a list of shops, and when I select a shop, it should display the corresponding content for that shop. However, I've encountered an issue wher ...

Angular2 Service Failing to Return Expected Value

It's frustrating that my services are not functioning properly. Despite spending the last two days scouring Stack Overflow for solutions, I haven't been able to find a solution that matches my specific issue. Here is a snippet of my Service.ts c ...

What should we name the type parameter?

After familiarizing myself with Typescript and understanding the concept of generic type T, I encountered an issue with a simple example. Can you pinpoint what's wrong? function test1<string>(x:number):boolean{ let s:string="hello"; if ...

What is the best way to divide a string into an array containing both linked and non-linked elements?

I'm struggling to find the right solution to my problem. I need to create a view that is enclosed in a clickable div. The content will consist of plain text mixed with clickable URLs - the issue arises when clicking on a link also triggers the method ...

DiscordJS bot using Typescript experiences audio playback issues that halt after a short period of time

I am currently experiencing difficulties with playing audio through a discord bot that I created. The bot is designed to download a song from YouTube using ytdl-core and then play it, but for some reason, the song stops after a few seconds of playing. Bel ...

The specified object is not extensible, hence the property effectTag cannot be added

Upon launching the React application, it initially renders perfectly, but after a few seconds, an error occurs that I am unable to debug. The error is being shown in node_modules/react-dom/cjs/react-dom.development.js:21959. Can anyone provide assistance ...

Using Angular to create an Echarts timeline that showcases 3 distinct categories

Here is an example graph created with Highcharts Struggling to replicate a chart like this using Echarts and ngx-echarts. Has anyone encountered this issue before? Currently utilizing the following dependencies: "@angular/animations": "^17 ...

Developing middleware for managing event handlers

Scenario: I am tasked with managing multiple events that necessitate an "available client". Therefore, in each event handler, my first step is to attempt to acquire an available client. If no client is available, I will send a "Service unavailable" messag ...

What is the best way to prevent double clicks when using an external onClick function and an internal Link simultaneously

Encountering an issue with nextjs 13, let me explain the situation: Within a card component, there is an external div containing an internal link to navigate to a single product page. Using onClick on the external div enables it to gain focus (necessary f ...

Having trouble launching the node pm2 process manager on AWS Elastic Beanstalk, npm update verification did not pass

Having some issues with utilizing pm2 for managing processes in my typescript node application, deployed on elasticbeanstalk. Whenever a new instance is launched by pm2, the following shows up in the logs ---------------------node.js logs---------------- ...

Tips for creating a custom script in my React Native application

My React Native app requires a script to generate static files during the release process. The app is a game that utilizes pre-computed boards, which are resource-intensive to compute. Therefore, I am developing a script that will create these boards and s ...

How to arrange an array in a matrix layout using Angular 2

I have a one-dimensional array containing a list of people that I would like to display in a matrix format using Angular 2. The array includes the following data: [ {Id: 1, FirstName: 'Michael', LastName: 'Jackson'}, {Id: 2, FirstName: ...