Monday, April 25, 2016

Authorize.Net SIM using ASP.NET MVC

If you need to integrate with Authorize.Net as your Payment Gateway, you might already know that there are multiple methods to integrate with Authorize.Net and one of the many methods available is the SIM method (Server Integration Method).

Unfortunately, Authorize.Net has removed most of their SIM code samples in favor of their new API implementation.

If you are using ASP.NET Web Forms and need to provide some additional server-side logic and processing, unfortunately, you will have to "re-create" the entire form post or form submission manually in server-side code to accomplish this.

But the good news is that someone has provided sample code on how to do just that!

In this article (http://www.jigar.net/articles/viewhtmlcontent78.aspx), you can find sample code on how to perform a "Remote Post".

This basically allows you to re-construct the form as well as all of the form field values and then re-post the form to the Url of your choosing.  In this case, you will either be posting to https://test.authorize.net/gateway/transact.dll or https://secure2.authorize.net/gateway/transact.dll.

Unfortunately, the code sample provided in the article addresses ASP.NET Web Forms and does not address how to accomplish the same task using ASP.NET MVC!

Well, using ASP.NET MVC, the solution can be modified to perform the post via the HttpPost method of the Controller as follows:


[HttpPost]
public ActionResult Index(AuthNetSIMModel model)
{
    
    //Validate the model values
    if (ModelState.IsValid)
    {
        RemotePost remotePostForm = new RemotePost();

        remotePostForm.Url = AuthorizeNetSIM.GetPaymentUrl();
        remotePostForm.Post(AuthorizeNetSIM.PopulateFormRequest(model.PaymentInfo));

        return View("Success");
    }//if

    ModelState.AddModelError("", "Model State is invalid");
    return View(model);
}

This then provides a suitable solution for ASP.NET MVC as well!

No comments:

Post a Comment