Payment API CreatePayment methods does not return back for the sandbox environment

Payment API CreatePayment methods does not return back for the sandbox environment using C#

var paymentRequest = new CreatePaymentRequest.Builder(parameters[“CardSourceId”], Utils.NewIdempotencyKey())
.AmountMoney(new Money(Convert.ToInt64(Math.Round(Convert.ToDecimal(parameters[“TXN_AMOUNT”]))), “USD”))
.Note(parameters[“Description”])
.ShippingAddress(shippingAddress)
.BillingAddress(billingAddress)
.ReferenceId(parameters[“ORDER_ID”])
.BuyerEmailAddress(parameters[“EMAIL”])
.LocationId(locationId)
.Build();

            try
            {
                var gatewayResult = client.PaymentsApi.CreatePayment(paymentRequest);

                if (gatewayResult.Errors.Any() || !gatewayResult.Payment.Status.ToLower().Equals("completed"))
                {
                    result.AddError("Payment Failed, Please Try Again.");
                    return result;
                }

                result.Data.Result["GatewayResponseMessage"] = gatewayResult.Payment.Status;
                result.Data.Result["GatewayTransactionID"] = gatewayResult.Payment.Id;
                result.Data.Result["OrderNumber"] = parameters["ORDER_ID"];
                result.Data.Result["PaymentProof"] = gatewayResult.Payment.Id;
                result.Data.Result["PaymentMode"] = parameters["PaymentType"];
                return result;
            }
            catch(ApiException ex)
            {
                result.Errors.Add(ex.Message);
                return result;
            }
1 Like

Got Same issue… Updated to v30. and Timeout. I submitted to support… No Answer yet.

hi @rbettinelli @bjshah

I am currently looking into this, but curious to know if either of you are building using .Net Framework, or if your app is using .Net Core

Thanks!

Using .net not core

Robert

Also was talking with Byran on discord this afternoon as I have a case open. Using work account nvca.on.ca.

Robert

@rbettinelli

Ah ok, yeah we have been seeing similar reported issues by others here: .NET SDK v25.2.0 and 26.0.0 freeze/lock IIS/VS - #20 by pauld

For now, when working with .NET not using Core, you should be able to wrap the SDK calling code with Task.Run(“api_you_want_to_call”).Wait();

We are still working with our SDK team to have a better experience for developers building with .NET framework

Jordan

I am using .net framework

Can you provide a complete code sample with this solution in place for .net 4.7 / 4.8?

Robert

@rbettinelli

Something like this

using Square.Apis;
using Square.Models;
using Square;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Extensions.Configuration;
using Square.Exceptions;
using System.Threading.Tasks;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        private static ISquareClient client;
        private static IConfigurationRoot config;

        protected void Page_Load(object sender, EventArgs e)
        {
            var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true);

            config = builder.Build();
            var accessToken = config["AppSettings:AccessToken"];
            client = new SquareClient.Builder()
                .Environment(Square.Environment.Sandbox)
                .AccessToken(accessToken)
                .Build();

            Task.Run(RetrieveLocationsAsync()).Wait();
        }

        static async Task RetrieveLocationsAsync()
        {
            try
            {
                ListLocationsResponse response = await client.LocationsApi.ListLocationsAsync();
                foreach (Location location in response.Locations)
                {
                    Console.WriteLine("location:\n  country =  {0} name = {1}",
            location.Country, location.Name);
                }
            }
            catch (ApiException e)
            {
                var errors = e.Errors;
                var statusCode = e.ResponseCode;
                var httpContext = e.HttpContext;
                Console.WriteLine("ApiException occurred:");
                Console.WriteLine("Headers:");
                foreach (var item in httpContext.Request.Headers)
                {
                    //Display all the headers except Authorization
                    if (item.Key != "Authorization")
                    {
                        Console.WriteLine("\t{0}: \t{1}", item.Key, item.Value);
                    }
                }
                Console.WriteLine("Status Code: \t{0}", statusCode);
                foreach (Error error in errors)
                {
                    Console.WriteLine("Error Category:{0} Code:{1} Detail:{2}", error.Category, error.Code, error.Detail);
                }

                // Your error handling code
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occurred");
                // Your error handling code
            }
        }
    }
}

Let me know if this works for you

Your sample was helpful. I was able to get payments and responses to now work.