cancel
Showing results for 
Search instead for 
Did you mean: 

How to redirect to Hosted Payment Page?

Hi,

 

I'm trying to redirect to a hosted payment page using .net with c#. I have the basic app that is getting a token using 'getHostedPaymentPageResponse', and I'm putting the token received to a form and submitting the form to "https://test.authorize.net/payment/payment" with POST method. But I keep getting "Missing or invalid token" error. Following is my code:

 

HTML:

<form runat="server" action="" id="formAuthorizeNetTestPage" name="formAuthorizeNetTestPage" method="POST">
<input type="hidden" name="token" value="" runat="server" id="redirectToken"/>
<p>Customer ID: </p>
<asp:TextBox runat="server" ID="customerID"></asp:TextBox>
<p>Amount: </p>
<asp:TextBox runat="server" ID="amount_input"></asp:TextBox>
<asp:Button ID="Button1" OnClick="Button1_Click" runat="server" text="Pay"/>
</form>

 

Code Behind:

public void Button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Hello, Button1 Clicked");
decimal amount = decimal.Parse(amount_input.Text);
string customerId = customerID.Text;
var ApiResponse = Run(LoginId, TransactionKey, amount, customerId, redirectToken, formAuthorizeNetTestPage, body_container);
//System.Diagnostics.Debug.WriteLine();
}

 

public static ANetApiResponse Run(String ApiLoginID, String ApiTransactionKey, decimal amount, string customerProfileId, System.Web.UI.HtmlControls.HtmlInputHidden token_input, System.Web.UI.HtmlControls.HtmlForm formAuthorizeNetTestPage, System.Web.UI.HtmlControls.HtmlGenericControl body_container)
{
System.Diagnostics.Debug.WriteLine("GetAnAcceptPaymentPage Sample");
ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = AuthorizeNet.Environment.SANDBOX;
ApiOperationBase<ANetApiRequest, ANetApiResponse>.MerchantAuthentication = new merchantAuthenticationType()
{
name = ApiLoginID,
ItemElementName = ItemChoiceType.transactionKey,
Item = ApiTransactionKey,
};

settingType[] settings = new settingType[]{
new settingType()
// More settings can be added here as and when required
};
settings[0] = new settingType();
settings[0].settingName = settingNameEnum.hostedProfileReturnUrl.ToString();
settings[0].settingValue = "https://example.com/";

var request = new getHostedProfilePageRequest();
request.customerProfileId = customerProfileId;
request.hostedProfileSettings = settings;

var controller = new getHostedProfilePageController(request);
controller.Execute();

// get the response from the service (errors contained if any)
var response = controller.GetApiResponse();

// validate response
if (response != null && response.messages.resultCode == messageTypeEnum.Ok)
{
System.Diagnostics.Debug.WriteLine("Message code : " + response.messages.message[0].code);
System.Diagnostics.Debug.WriteLine("Message text : " + response.messages.message[0].text);
System.Diagnostics.Debug.WriteLine("Token : " + response.token);
}
else if (response != null)
{
System.Diagnostics.Debug.WriteLine("Error: " + response.messages.message[0].code + " " + response.messages.message[0].text);
System.Diagnostics.Debug.WriteLine("Failed to get hosted payment page");
}
System.Diagnostics.Debug.WriteLine(response.token);
token_input.Value = response.token;
formAuthorizeNetTestPage.Action = "https://test.authorize.net/payment/payment";
//HttpContext.Current.Response.Redirect("https://test.authorize.net/payment/payment");
body_container.Attributes.Add("onload", "document.getElementById('formAuthorizeNetTestPage').submit();");

return response;
}

 

Thanks!

toirovd
Member
4 REPLIES 4

Please read the steps in INTEGRATING THE FORM USING A REDIRECT section here:-

https://developer.authorize.net/api/reference/features/accept_hosted.html#Integrating_the_Form_using...

Shoagraw
Authorize.Net Developer Authorize.Net Developer
Authorize.Net Developer

Thanks. I got it fixed. Obviously, the form was being submitted with a different token input name instead of just name="token".

toirovd
Member

How did you fix that exactly?

How did you fix that exactly?