#Slush-dotnet

1 messages · Page 1 of 1 (latest)

faint lava
#

Hi, by which part you can't pass in items?

subtle hound
#

Hey! Thanks for responding @faint lava.

#

Not exactly sure how to say this without showing you but I have this: async function initialize() {
const response = await fetch("api/Stripe/create-payment-intent", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items }),
});
const { clientSecret } = await response.json();

            const appearance = {
                theme: 'stripe',
            };
            elements = stripe.elements({ appearance, clientSecret });

            const paymentElement = elements.create("payment");
            paymentElement.mount("#payment-element");
        }
#

Items being: const items = [{ id: "xl-tshirt" }];

#

On the server side when the CalculateOrderAmount(Item[] items) method gets called from the javascript call "items" is null.

#

I'm more than happy to hop in a chat and share my screen if that makes life easier 🙂

#

Exactly as the guide has it

#

Everything is getting called correctly, just when CalculateOrderAmount gets called the "items" is null.

faint lava
#

Sorry to hear it doesn't work for you. Have you tried logging the request.Items, or the whole request?

#

You can also open browser console, to confirm the AJAX call from JS actually has the items data

subtle hound
#

I have checked on backend

#

Its calling the Create method from the frontend so im assuming the ajax call is working

#

why would it not be sending over the data?

faint lava
#

We don't know, that why we want to debug

subtle hound
#

Do you have a minute to take a look over screen share?

faint lava
#

I am afraid we can't provide such a support. Let's just paste your screenshot here for the browser console

subtle hound
#

Theres no error in the console

#

What would you like me to log?

#

I added that console.log in there

#

Thats what I got

#

It didnt show the "items" but im not sure if I console.log the right thing

faint lava
#

Open the "Network" tab and find "Fetch/XHR" requests, then locate yours

subtle hound
faint lava
#

Nice! now we know frontend is fine

#

Let's head over to backend and log your entire request

subtle hound
#

How exactly do you want me to log it and show you?

faint lava
#

I am not super familiar with DOTNET but I guess there must be some way to log to your server console, no?

subtle hound
#

Not exactly no.

#

But I can debug it and see the value in which it holds

#

It just says "Items null"

#

‡ Controllers.StripeController.PaymentIntentCreateRequest.Items.get returned null Controllers.StripeController.Item[]

faint lava
#

if you just check request (don't touch Item), what object would it return?

subtle hound
#

request = {Controllers.StripeController.PaymentIntentCreateRequest}

faint lava
#

Nothing inside?

subtle hound
#

nothing

#

Other than "Items null"

faint lava
#

Just for sure you do have these definition, right?

    public class Item
    {
      [JsonProperty("id")]
      public string Id { get; set; }
    }

    public class PaymentIntentCreateRequest
    {
      [JsonProperty("items")]
      public Item[] Items { get; set; }
    }
subtle hound
#

Yes

faint lava
#

Um looks like there could be issues with setup code.

#

Sorry to ask this but, could you download the example project from the button in that Doc, and see if it has the same issue?

subtle hound
#

I did try and do that

#

But i couldnt even get the vs file to run

faint lava
#

Does it have some error?

subtle hound
#

No, that one I couldnt even get to run in Visual Studio

#

Right now im not having any errors everything is working great

#

except I cant get the items to pass onto server side with the call.

faint lava
#

Yes I know but it's literally the request object somehow doesn't contain any information from frontend

#

I am curious why the example project doesn't run for you. What is exactly "couldn't even run". What does it tell you? a compile error?

#

If you want to put the example project aside, then you have the only choice to debug your current server

subtle hound
#

1>------ Build started: Project: StripeExample, Configuration: Debug Any CPU ------
1>C:\Program Files\dotnet\sdk\2.1.617\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(137,5): error NETSDK1045: The current .NET SDK does not support targeting .NET Core 5.0. Either target .NET Core 2.1 or lower, or use a version of the .NET SDK that supports .NET Core 5.0.
1>Done building project "StripeExample.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

faint lava
#

So it seems you have an old .NET SDK which only support .NET Core 2.1

subtle hound
#

Weird, it didnt effect it on my main project.

#

Im running .net core 2.1 on my project which im trying to debug

#

Everything is working and building fine, and I have the stripe package installed.

faint lava
#

Okie I agree that going down that path is not feasible

#

Hmm

#

Can you share the full server file?

subtle hound
#

Yes!

#
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Stripe;
using System.IO;


// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Tourny.Controllers
{

    [Route("api/[controller]/create-payment-intent")]
    public class StripeController : Controller
    {
        // GET: api/<controller>
        [HttpPost]
        public ActionResult Create(PaymentIntentCreateRequest request)
        {
            StripeConfiguration.ApiKey = "XXXXX";
            var paymentIntentService = new PaymentIntentService();
            var paymentIntent = paymentIntentService.Create(new PaymentIntentCreateOptions
            {
                Amount = CalculateOrderAmount(request.Items),
                Currency = "usd",
                AutomaticPaymentMethods = new PaymentIntentAutomaticPaymentMethodsOptions
                {
                    Enabled = true,
                },
            });

            return Json(new { clientSecret = paymentIntent.ClientSecret });
        }

        private int CalculateOrderAmount(Item[] items)
        {
            // Replace this constant with a calculation of the order's amount
            // Calculate the order total on the server to prevent
            // people from directly manipulating the amount on the client
            return 1400;
        }

        public class Item
        {
            [JsonProperty("id")]
            public string Id { get; set; }
        }

        public class PaymentIntentCreateRequest
        {
            [JsonProperty("items")]
            public Item[] Items { get; set; }
        }
    }

    

    }```
faint lava
#

Thanks!

subtle hound
#

No problem!

faint lava
#

Um I don't see this part. Where is it?

subtle hound
#

What .net core version is reccomended for stripe?

faint lava
#

We support 2.0+. Of course newer is better but 2.1 is > 2.0 so you are good

#

Is that 2.1 specific syntax?

subtle hound
#

Apparently yes

#

Apparently its 3.1 specific syntax

#

As per your comment, you are targeting .NET Core 2.1, but the UseEndpoints extension method was introduced in 3.0.

#

I am getting an error on a lot of different things in there.

faint lava
#

So that example code is for 3.0. If you would like to make it work in 2.1 I am afraid you will have to adapt the syntax somehow

subtle hound
#

Is there any stripe examples?

faint lava
#

I would say it's easier to just upgrade your .NET Core version instead of trying to dig deeper on an old version