Click here to Skip to main content
15,509,916 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a cart of items that I am using Stripe Api as a method of payment but I get the exception of missing the param amount,
Stripe.StripeException: 'Missing required param: amount.'
why am I getting the issue since I am including an amount?

C#
public IActionResult CheckOut(string stripeEmail, string stripeToken, bool charged)
        {
            var customers = new CustomerService();
            var charges = new ChargeService();
            var customer = customers.Create(new CustomerCreateOptions{ 
                Email=stripeEmail,
                Source=stripeToken
            });
            var charge = charges.Create(new ChargeCreateOptions
            {
                Amount = ViewBag.stripe,  //--------Issue here---------//
                Description = "product purchased",
                Currency = "usd",
                Customer = customer.Id,
                ReceiptEmail = stripeEmail,
            });


Inside my index I declared the viewbags as:

C#
ViewBag.total = Cost(ViewBag.pricing, ViewBag.tax);
ViewBag.stripe = ViewBag.total * 100;


The cost method I used for ViewBag.total:
C#
public double Cost(double total, double tax)
        {
            return total + tax;
        }


What I have tried:

What I tried is creating another var amount which equals the ViewBag.stripe
C#
var customers = new CustomerService();
var charges = new ChargeService();
var amount = ViewBag.stripe; // <----- Attempted Fix ------->
var customer = customers.Create(new CustomerCreateOptions{
    Email=stripeEmail,
    Source=stripeToken
});


I then tried changing ViewBag.stripe in my index to:

C#
ViewBag.total = Cost(ViewBag.pricing, ViewBag.tax);
ViewBag.stripe = StripeTotal(ViewBag.total); // <----- Attempted Fix ------->


And having my StripeTotal method return ViewBag.total * 100 instead
C#
public double StripeTotal(double amount)
        {
            return ViewBag.total * 100;
        }


I have also tried changing it to
Amount = Convert.ToInt32(ViewBag.stripeTotal)

but instead of the error that I am missing amount, I get another error so this did not work either
I further tried searching online but I just cant seem to find a similar case
Posted
Updated 31-Mar-21 20:06pm
v2
Comments
Arfat M 1-Apr-21 1:41am    
A positive integer representing how much to charge in the smallest currency unit
(e.g., 100 cents to charge $1.00). The minimum amount is $0.50 USD
or equivalent in charge currency. The amount value supports up to eight digits (e.g., a value of 99999999 for a USD charge of $999,999.99)

Normally, if you are parsing the string values to integers the best approach is to handle when a string is not an integer. So, instead of:
C#
Convert.ToInt32(ViewBag.stripeTotal)
I would recommend that you do:
C#
int amount = 0;
if (int.TryParse(ViewBag.stripeTotal, out amount)) {
   // okay, use the value and continue
} else {
   // handle the situation where your stripTotal is not valid
   // debug it here
}

Read more here: Int32.TryParse Method (System) | Microsoft Docs[^]
 
Share this answer
 
C#
int StripeAmount = Convert.ToInt32(ViewBag.stripe);

var Amount = (int)(StripeAmount * 100);

var charge = charges.Create(new ChargeCreateOptions
            {
                Amount = Amount,  //--------Pass amount here ---------//
                Description = "product purchased",
                Currency = "usd",
                Customer = customer.Id,
                ReceiptEmail = stripeEmail,
            });
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900