cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Direct Post with Razor and MVC 4

I am attempting to convert the quick start sample for Direct Post for use with Razor (MVC 4)  and running into some problems.  The main one right now is that Html.BeginSIMForm seems to render the form at the top of the page (above the DOCTYPE tag) and does not enclose the "order_id" hidden input or the submit button.

 

Any suggestions?

 

Here's my code:

 

        @using (Html.BeginSIMForm(

                    Model.ResponseUrl,

                    Model.AuthorizationRequest.Amount,

                    Model.AuthorizationRequest.Credentials.LoginId,

                    Model.AuthorizationRequest.Credentials.TransactionKey,
                    Model.AuthorizationRequest.Credentials.IsTestMode))
  {

            @Html.Raw( @Html.CheckoutFormInputs(true))

            @Html.Hidden("order_id",Model.AuthorizationRequest.OrderId)

          

      <input type="submit" value="Submit Payment"/>

 

   }

 

Renders this:

 

<form action='https://test.authorize.net/gateway/transact.dll'method='post'>

<input type='hidden' name='x_fp_hash'value='-- Value removed --' \>

<input type='hidden' name='x_fp_sequence'value='146'\>

<input type='hidden' name='x_fp_timestamp'value='1395158912' \>

<input type='hidden' name='x_login'value='MYLOGINHERE' \>

<input type='hidden' name='x_amount'value='50.0' \>

<input type='hidden' name='x_relay_url'value='http://localhost:49358/payment/sim' \>

<input type='hidden' name='x_relay_response'value='TRUE' \>

</form><!DOCTYPE html>

 

tsorelle
Member
3 REPLIES 3

Hello tsorelle

It doesn't look like anyone has responded yet, but someone still may have feedback on what you're looking for. I'd recommend subscribing to this topic so that you'll be alerted via email if anyone else from the community is able to respond with any comments. To subscribe, click Topic Options at the top of this thread and then select Subscribe. You'll then receive an email once anyone replies to your post.

Thanks,

Richard

RichardH
Administrator Administrator
Administrator

Making some progress but still having issues.  I downloaded the SDK and found that the SIMForm.OpenForm() method was writing directly to the HttpResponse, as one would do in a webforms server control.  This might have worked in MVC 2 but does not work in later versions with the Razor view engine.  I created a wrapper class and defined some HtmlHelper extensions that do the job.  Taking care to delegate as much as possible to the services in the latest AuthorizeNet NuGet packages.  Also had to wrap helpers that return a plain string in order to return an HtmlString, which avoids unwanted encoding.

 

The payment entry form works in test mode, and an "unsettled" transaction was posted.  However, it failed to redirect to my confirmation page.  Instead, I got this message from https://test.authorize.net/gateway/transact.dll.

 

An error occurred while trying to report this transaction to the merchant. An e-mail has been sent . . .

 

Is it not possible to test the redirect until a merchant account has been established?  Or is something wrong with the redirect?

 

Here's my fix for the submit form:

   

public static classAuthnetSIMFormExtensions {

       

///<summary>

       

///  Replaces CheckoutFormBuilders.BeginSSIMForm for compatibility with Razor view engine

       

///</summary>

       

public static HtmlString StartSIMForm(thisHtmlHelper helper, string returnUrl, decimal amount, string apiLogin, string transactionKey, bool isTest = true)

        {

           var seq = Crypto.GenerateSequence();

           var stamp = Crypto.GenerateTimestamp();

           var fingerPrint = Crypto.GenerateFingerprint(transactionKey,

                apiLogin, amount, seq.ToString(), stamp.ToString());

           

           var result = newStringBuilder();

           

            result.Append(

string.Format("<form action = '{0}' method = 'post'>\n", isTest ? Gateway.TEST_URL : Gateway.LIVE_URL));

            result.Append(

"\t\t<input type = 'hidden' name = 'x_fp_hash' value = '" + fingerPrint + "' \\>\n");

            result.Append(

"\t\t<input type = 'hidden' name = 'x_fp_sequence' value = '" + seq + "'\\>\n");

            result.Append(

"\t\t<input type = 'hidden' name = 'x_fp_timestamp' value = '" + stamp + "' \\>\n");

            result.Append(

"\t\t<input type = 'hidden' name = 'x_login' value = '" + apiLogin + "' \\>\n");

            result.Append(

"\t\t<input type = 'hidden' name = 'x_amount' value = '" + amount + "' \\>\n");

            result.Append(

"\t\t<input type = 'hidden' name = 'x_relay_url' value = '" + returnUrl + "' \\>\n");

            result.Append(

"\t\t<input type = 'hidden' name = 'x_relay_response' value = 'TRUE' \\>\n");

           

      return new HtmlString(result.ToString());

        }

       

public static HtmlString EndSIMForm(thisHtmlHelper helper)

        {          

     return new HtmlString("</form>");

        }

       

///<summary>

       

/// Wraps CheckoutFormBuilders.CheckoutFormInputs returning HtmlString to avoid unwanted encoding.

       

///</summary>

       

public static HtmlString AuthnetCheckoutFormInputs(thisHtmlHelper helper, bool isTest = false)

        {

      var inputs =

        AuthorizeNet.Helpers.CheckoutFormBuilders.CheckoutFormInputs(helper, isTest);

         

      return new HtmlString( inputs );

        }

 

 

In the View:

 

         @Html.StartSIMForm(

                    Model.ResponseUrl,

                    Model.AuthorizationRequest.Amount,

                    Model.AuthorizationRequest.Credentials.LoginId,

                    Model.AuthorizationRequest.Credentials.TransactionKey,

                    Model.AuthorizationRequest.Credentials.IsTestMode)

            @Html.AuthnetCheckoutFormInputs(Model.AuthorizationRequest.Credentials.IsTestMode)

            @Html.Hidden("order_id", Model.AuthorizationRequest.OrderId)

           

     <div id="price-button"style="clear:both;margin-bottom:10px;margin-top:5px">               

        <input type="submit" value="Submit Payment"/>

     </div>

            @Html.EndSIMForm()

 

I have not looked at the quick start example for direct post specifically but I do work with MVC 4 and authorize a lot. Most of the Authorize examples are very informative but you generally you have to pull out the principals and apply it to your situation. I like MVC4 once I got into using it. My suggestion would be; 2. Use the controller to do all the work. Pass the data from the form to the controller and make the call to the authorize server from the controller. Once you get the response you can display it on the same form or redirect them to different page. if interested and need more info i can more detailed