#Slush-dotnet
1 messages · Page 1 of 1 (latest)
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.
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
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?
We don't know, that why we want to debug
Do you have a minute to take a look over screen share?
I am afraid we can't provide such a support. Let's just paste your screenshot here for the browser console
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
Nice! now we know frontend is fine
Let's head over to backend and log your entire request
How exactly do you want me to log it and show you?
I am not super familiar with DOTNET but I guess there must be some way to log to your server console, no?
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[]
if you just check request (don't touch Item), what object would it return?
request = {Controllers.StripeController.PaymentIntentCreateRequest}
Nothing inside?
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; }
}
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?
Does it have some error?
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.
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
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 ==========
So it seems you have an old .NET SDK which only support .NET Core 2.1
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.
Okie I agree that going down that path is not feasible
Hmm
Can you share the full server file?
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; }
}
}
}```
Thanks!
No problem!
Um I don't see this part. Where is it?
What .net core version is reccomended for stripe?
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?
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.
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
Is there any stripe examples?
The example code there (3.0 apparently), the downloaded button (5.0 apparently as we discussed), and this Github repo: https://github.com/stripe-samples/accept-a-payment/tree/main/custom-payment-flow/server/dotnet
I would say it's easier to just upgrade your .NET Core version instead of trying to dig deeper on an old version