We are testing Square payment APIs in Visual Studio MVC Razor pages following the example (https://github.com/square/connect-api-examples/tree/master/connect-examples/v2/csharp_payment).
We only tried “Pay With Card” method, and
CreatePaymentAsync() works fine with good response.Payment result. But we are not able to find a way to redirect from this current razor page to a new razor page. Below is the code sample:
Razor page/view model 1 = “ProcessPaymentModel”:
public async Task OnPostAsync()
{
…
try
{
// 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)
.ReferenceId(ReferenceId)
.Build();
var response = await PaymentsApi.CreatePaymentAsync(createPaymentRequest);
var payment = response.Payment;
var PaymentResult = JsonConvert.SerializeObject(payment);
//---- problem/question area ----
// return new JsonResult(new { payment = response.Payment });
}
catch (ApiException e)
{
return new JsonResult(new { errors = e.Errors });
}
}
Razor page/view model 2 = “PaymentResultModel”:
public void OnGet()
{
string a = “breakpoint set here”;
}
at view 1, line “/---- problem/question area ----”,
I tried the following ways of redirecting, but none is working:
(1). return new JsonResult(new { redirect_url = Url.Page(“PaymentResult”) });
(2). return new RedirectToPage(“PaymentResult”);
(3). Redirect(“PaymentResult”); //-- suggested by “Bryan-Square” Employee
Although at view 2, the breakpoint line has been hit when view 1 uses return new RedirectToPage.
========
Many thanks in advance!