首页 > 解决方案 > STRIPE .NET/REACT-NATIVE:出现错误但 API 调用仍然通过

问题描述

我正在尝试在服务器端使用 .NET 将 Stripe 集成到我的 React-Native 应用程序中。

我的问题是,当我尝试创建客户并向其附加 paymentMethod 时,我收到 SyntaxError: JSON Parse error: Unexpected identifier: "Success",但在 Stripe 仪表板中,客户和 paymentMethod 已创建。

我需要访问我的日志,以便我可以检索 customerId 以附加到 paymentIntents 但由于我收到此错误,它就像它没有通过,即使它确实通过了。

     public static async Task<dynamic> CreateClientAsync(string email, string name, string cardNumber, int month, int year, string cvv)
    {
        var optionstoken = new TokenCreateOptions
        {
            Card = new CreditCardOptions
            {
                Number = cardNumber,
                ExpMonth = month,
                ExpYear = year,
                Cvc = cvv
            }
        };

        var servicetoken = new TokenService();
        Token stripetoken = await servicetoken.CreateAsync(optionstoken);

        var customer = new CustomerCreateOptions
        {
            Email = email,
            Name = name,
            Source = stripetoken.Id,
        };

        Console.WriteLine(" stripetoken attributes :" + stripetoken);

        var services = new CustomerService();
        var created = services.Create(customer);


        var option = new PaymentMethodCreateOptions
        {
            Type = "card",
            Card = new PaymentMethodCardCreateOptions
            {
                Number = cardNumber,
                ExpMonth = month,
                ExpYear = year,
                Cvc = cvv,
            },
        };

        var service = new PaymentMethodService();
        var result = service.Create(option);

        Console.WriteLine(" PaymentMethodService attributes :" + result);

        var options = new PaymentMethodAttachOptions
        {
            Customer = created.Id,
        };
        var method = new PaymentMethodService();
        method.Attach(
          result.Id,
          options
        );

        if (created.Id == null)
        {
            return "Failed";
        }
        else
        {
            return "Success";
        }
    }

}

然后我在我的控制器中调用它

    [HttpPost("stripecustomer")]
    public async Task<dynamic> Client(Models.StripeClientModel sc) 
    {
        return await CreateStripeClient.CreateClientAsync(sc.email, sc.name, sc.cardNumber, sc.month, sc.year, sc.cvv);
    }

这是我的 React-Native 代码

    let collection ={}
    collection.email = this.state.email,
    collection.name = this.state.name,
    collection.cardNumber = this.state.cardNumber,
    collection.month = 10,
    collection.year = 2025,
    collection.cvv = this.state.CVV
    console.warn(collection);
    
    await fetch("https://api/stripecustomer", {
        method: 'POST', // or 'PUT'
        headers: new Headers({
            'Content-Type': 'application/json',
        }),
        body: JSON.stringify(collection),
    })
        .then((response) => response.json())
        .then((collection) => {
            console.log('Success stripe customer created:', collection);
        })
        .catch((error) => {
            console.error('Error:', error);
        });

我也尝试使用 axios,但 axios 根本没有发布到 Stripe,也没有创建任何客户。

如果您需要更多信息,请告诉我!谢谢!

标签: .netreact-nativestripe-payments

解决方案


推荐阅读