I’m building a web application that leverages the Square SDK/API with a .Net 3.1 MVC app to handle credit card payments.
I found the sample solution that leverages Razor pages using C#
The issue I’m facing, the app can process the payment fine but when I try to do a page redirect to a “payment confirmation” page the same page (with the card form) remains on the browser window. In other words I dont get to see my payment confirmation screen. Below is a my ProcessPayment
public class ProcessPaymentModel : PageModel
{
private SquareClient client;
private string locationId;
public ProcessPaymentModel(Microsoft.Extensions.Configuration.IConfiguration configuration)
{
var environment = configuration[“AppSettings:Environment”] == “sandbox” ?
Square.Environment.Sandbox : Square.Environment.Production;
client = new SquareClient.Builder()
.Environment(environment)
.AccessToken(configuration["AppSettings:AccessToken"])
.UserAgentDetail("sample_app_csharp_payment") // Remove or replace this detail when building your own app
.Build();
locationId = configuration["AppSettings:LocationId"];
}
public async Task OnPostAsync()
{
var request = JObject.Parse(await new StreamReader(Request.Body).ReadToEndAsync());
var token = (String)request[“token”];
var idempotencyKey = (String)request[“idempotencyKey”];
var PaymentsApi = client.PaymentsApi;
// Get the currency for the location
var retrieveLocationResponse = await client.LocationsApi.RetrieveLocationAsync(locationId: locationId);
var currency = retrieveLocationResponse.Location.Currency;
// Monetary amounts are specified in the smallest unit of the applicable currency.
// This amount is in cents. It's also hard-coded for $1.00,
// which isn't very useful.
var amount = new Money.Builder()
.Amount(100L)
.Currency(currency)
.Build();
// To learn more about splitting payments with additional recipients,
// see the Payments API documentation on our [developer site]
// (https://developer.squareup.com/docs/payments-api/overview).
var createPaymentRequest = new CreatePaymentRequest.Builder(
sourceId: token,
idempotencyKey: idempotencyKey,
amountMoney: amount)
.Build();
try
{
var response = await PaymentsApi.CreatePaymentAsync(createPaymentRequest);
if (response.Payment.Status == "COMPLETED" && response.Errors == null)
{
return new RedirectToPageResult("/Confirmation");
}
//return new JsonResult(new { payment = response.Payment });
}
catch (ApiException e)
{
return new JsonResult(new { errors = e.Errors });
}
}
}
This is a simplified page I wish to display after payment is successful
@page
@model sqRazorSample.Pages.ConfirmationModel
@{
ViewData[“Title”] = “Payment Processed”;
}