How to redirect to another razor page after CreatePaymentAsync

We are testing Square payment APIs in Visual Studio MVC Razor pages following the example (

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()

// To learn more about splitting payments with additional recipients,
// see the Payments API documentation on our [developer site]
// (
var createPaymentRequest = new CreatePaymentRequest.Builder(
sourceId: token,
idempotencyKey: idempotencyKey)

            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!

Yes, our example doesn’t redirect you but you can modify our example to redirect the customer with something from this post. :slight_smile:

Perhaps a better way -
The sample js that is submitting the payment simply puts the words “Payment Successful!” on the screen by calling a “showSuccess()” function (see below).
Simply use window.location.href() right after (or instead of) this to display the payment success receipt/details.

        window.showSuccess('Payment Successful!');
       /*add this line*/ window.location.href = "/PaymentResult";