Paypal shows the amount as 3,574,989.00whentheactualcurrenttotal35,749.89. Fix the issue: Cart.js: import styled from "styled-components";
import { useCartContext } from "./context/cart_context";
import CartItem from "./components/CartItem";
import { NavLink } from "react-router-dom";
import { Button } from "./styles/Button";
import FormatPrice from "./Helpers/FormatPrice";
import GooglePayButton from '@google-pay/button-react';
import { PayPalButtons, PayPalScriptProvider } from '@paypal/react-paypal-js';
The issue seems to be with how the total_price is being formatted or passed to the payment APIs for PayPal and Google Pay. As per the description, the amount is shown as $3,574,989.00 instead of $35,749.89, which indicates that the price might be getting multiplied or incorrectly converted into a larger number.
However, the provided code snippet doesn't include the implementation of the FormatPrice component or function, which might be where the conversion error is occurring. Additionally, the actual logic for calculating total_price is not shown as it is part of the useCartContext context.
To diagnose and fix this issue, ensure that:
The total_price calculation is correct. Go to the implementation of the useCartContext and verify that the calculation of total_price is correct and not multiplying the price by an unintended factor.
Inspect the FormatPrice function or component. The function or component that is formatting the price for display purposes might be incorrectly converting the price. Ensure that it only formats the number for display and does not unintentionally alter the value. It should simply format to two decimal places and possibly add commas for thousands, but not change the actual amount's scale.
Given the components and setup you have, the total_price should be handled as a decimal number correctly reflecting the total cost in dollars and cents.
For diagnostic purposes, here are the necessary steps and checks:
Check total_price calculation: Ensure that wherever the total_price is calculated, it is done so accurately. It should exactly reflect the total sum of cart items without any unintended scaling.
Ensure FormatPrice does not alter the amount: It should only format the visual representation and not the actual value passed to PayPal or Google Pay. If FormatPrice is used to format the value passed to the payment processors, remove it and format the price only for display purposes. Use the raw total_price (ensuring it's correctly calculated) for payments.
Since your current setup directly uses total_price for payment APIs:
amount: {
value: total_price.toString(), // PayPal expects a string
},
totalPrice: total_price.toString(), // Google Pay expects string format
Ensure that total_price is correctly calculated, and directly use it. If total_price requires conversion or formatting, ensure it's done correctly without changing the intended amount.
If after these checks the issue persists, verify if any external or parent components manipulate total_price or if any conversions might lead to discrepancies. Always ensure the variable passed to the payment APIs represents the actual, intended amount in string format as both PayPal and Google Pay documentation suggests.
Without the actual implementation details of FormatPrice and useCartContext, these are the best steps to diagnose and potentially fix the described problem.