Hi all,
I'm testing the waters with integration in a simple MVC project while I wait for my accounting team to finish our account authorization.
Unfortunately, I'm having trouble getting the compiled page to output the correct HTML. I basically copy/pasted the sample code from
https://developer.authorize.net/integration/fifteenminutes/csharp#hosted , then modified it to build with my project. The only difference from the sample is that I'm using Razor markup. The page looks like this:
@{ ViewBag.Title = "First Test"; } <h1>@ViewData["message"]</h1> @using (Html.BeginSIMForm("http://YOURSERVER.com/home/sim", 1.99M, "YOUR_API_LOGIN_ID", "YOUR_TRANSACTION_KEY", true)) { @Html.Raw(Html.CheckoutFormInputs(true)); @Html.Hidden("order_id","1234"); <input type = "submit" value = "Pay" /> }
This looks like it should be all fine and dandy, but the problem seems to be with the "BeginSIMForm" helper: the <form>...</form> block gets output before everything else on the page, so when looking at the html on the rendered page, I see the form block up top, then the DOCTYPE, html, head, body, etc.
I tried replacing BeginSIMForm with just a basic BeginForm, and if I do that, the form is rendered in the body of the page, as one would expect.
Is there something special I need to do to get this to work, or will the HtmlHelpers just not work with MVC3/Razor markup? If not, that might be something worth documenting somewhere (or at least, that they only work with ASP markup).
I appreciate any feedback I might get on this.
05-27-2011 12:21 PM
Well, I started poking around and the issue seems to be the use of Response.Write in the html helper "BeginSIMForm".
I know I could just create the form myself, and put in each of the variables in the View, but I like the idea of using a helper for this, so I just wrote my own, using the ViewContext.Writer property of the HtmlHelper, instead of Response.Write. Seems to work great now.
Just posting this follow-up in case it helps someone else in the future.
05-27-2011 01:48 PM
how did you get this to work?? I'm using MVC3 too and after adding the reference to the DLL I get an error from the web.config:
c:\Users\ekkis\...\Web.config(105): error CS0234: The type or namespace name 'Helpers' does not exist in the namespace 'AuthorizeNet' (are you missing an assembly reference?)
the line I've added is:
<addnamespace="AuthorizeNet.Helpers"/>
I've looked in the assembly and there is indeed no such type... there is ArbApiSample, AuthorizeNet, and AuthorizeNet.APICore... what am I doing wrong?
08-16-2011 04:29 PM
it turns out there is a separate AuthorizeNet.Helpers.dll that is what I needed a reference to (I had originally added AuthorizeNet.dll), which I would have presumed contains HtmlHelpers. for some reason I can't just do:
@Html.BeginSIMForm()
though if I look in the assembly I see:
AuthorizeNet.Helpers.CheckoutFormBuilders.BeginSIMForm
so calling it like this:
@using (AuthorizeNet.Helpers.CheckoutFormBuilders.BeginSIMForm(Html, http://www..., 1.99M, "...", "...", true))
seems to work but produces bad output, breaking jsquery-1.6.2 on line 4057 with the error that a thrown exception was not caught.
if I look at the form it produces I see it urlencoded everything so I get:
<h2>Payment Information</h2>
instead of <h2>Payment Information</h2>
wtf is going on here?
08-16-2011 05:17 PM - edited 08-16-2011 05:17 PM
@using (AuthorizeNet.Helpers.CheckoutFormBuilders.BeginSIMForm(Html, http://www..., 1.99M, "...", "...", true))
In the SDK sample application, the line look like this
using (Html.BeginSIMForm("http://YOURSERVER.com/orders/sim", order.Price, ConfigurationManager.AppSettings["ApiLogin"], ConfigurationManager.AppSettings["TransactionKey"], true))
@using (Html.BeginSIMForm(http://www..., 1.99M, "...", "...", true))
08-17-2011 06:00 AM
if I try to do as you suggest I get the following error:
Error43'System.Web.Mvc.HtmlHelper<dynamic>' does not contain a definition for 'BeginSIMForm' and no extension method 'BeginSIMForm' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<dynamic>' could be found (are you missing a using directive or an assembly reference?)c:\Users\...\Documents\Visual Studio 2010\Projects\...\Website\Views\Home\Employers.cshtml134Website
I have a reference to AuthorizeNet.Helpers and I've now added one to AuthorizeNet (do I need it?) but still...
08-17-2011 02:06 PM
Did you add
<add namespace="AuthorizeNet.Helpers"/>
to the web.config? There might be more than one <namespaces /> section
I'm using regular MVC and if I didn't put have that it will error with
CS1061: 'System.Web.Mvc.HtmlHelper<object>' does not contain a definition for 'CheckoutFormInputs' and no extension method 'CheckoutFormInputs' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<object>' could be found (are you missing a using directive or an assembly reference?)
08-17-2011 02:21 PM - edited 08-17-2011 02:25 PM
yes, just like you have it, in the system.web/pages/namespaces section
08-17-2011 02:25 PM
ah... figured it out. I had added it to the \Website\Web.config, but for MVC3 there is a section configuration/system.web.webPages.Razor/pages/namespaces in the \Website\Views\Web.config - adding it there fixed the problem!
and for the sake of anyone else wondering, no, a reference to AuthorizeNet.dll is not necessary, only to AuthorizeNet.Helpers.dll
08-17-2011 02:31 PM
> so I just wrote my own, using the ViewContext.Writer property of the HtmlHelper, instead of Response.Write
how did you do this? do you have code you could share?
08-17-2011 06:59 PM