Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 
Showing posts with label Tips and Tricks. Show all posts
Showing posts with label Tips and Tricks. Show all posts

Download: ASP.NET mvc4: Get response from authorize.net after payment (Auto redirect)

Tuesday

                 In this post we will learn how to get response from Authorize.Net credit card payment using ASP.NET MVC4

One of my client projects I need to implement Authorize.net payment gateway. So just implemented using SIM (Server Integration Method). But my client wants, if the payment getting success then shown a custom receipt page and save the transaction details in to our own database. In other case if the payment getting fail then provides repayment option.

In SIM method this is not possible (or not advisable) because we using “x_relay_response” to get response from Authorize.net. Note,this is just relay the url, not redirect to the result page. And URL always points to the https://test.authorize.net/gateway/transact.dll. And the response time also only 10 seconds. If your server not responds within 10 seconds then you get time out error like

“An error occurred while trying to report this transaction to the merchant. An email has been sent to the merchant informing them of the error. The following is a result of the attempt to charge your credit card.”

This error indicates that Authorize.Net is unable to connect to the page that you have specified as your relay response URL. To avoid this kind of errors you need to go with Authorize.net’s another one integration method AIM (Advanced Integration Method)


You can get more about AIM method from here http://www.authorize.net/support/AIM_guide.pdf

Implement  AIM :
First create a sandbox account in Authorize.Net. https://developer.authorize.net/sandbox/. After you registered you will get the API Credentials

API Login ID
Transaction Key
Secret Question

Create you view page as per your requirement. Here I'm taking from Authorize.net default payment screen



Then add that details  into your web.config file


<!-- Payment Authorize.NET - Begins --> <add key="Payment.AuthLoginID" value="XXXXXXXXXX" /> <add key="Payment.AuthTransactionKey" value="XXXXXXXXX" /> <add key="Payment.TestMode" value="true" /> <add key="Payment.URL" value="https://test.authorize.net/gateway/transact.dll" /> <!--<add key="Payment.URL" value="https://secure.authorize.net/gateway/transact.dll" />--> <!-- Payment Authorize.NET - End -->


Then call the following function into your post method


private string AuthorizePayment(PaymentDataModel payDataModel)
        {
            
            string AuthNetVersion = "3.1"; // Contains CCV support
            string AuthNetLoginID = System.Configuration.ConfigurationManager.AppSettings["Payment.AuthLoginID"].ToString();
            string AuthNetTransKey = System.Configuration.ConfigurationManager.AppSettings["Payment.AuthTransactionKey"].ToString();
            string AuthNetMode = System.Configuration.ConfigurationManager.AppSettings["Payment.TestMode"].ToString();

            WebClient objRequest = new WebClient();
            System.Collections.Specialized.NameValueCollection objInf = new System.Collections.Specialized.NameValueCollection(30);
            System.Collections.Specialized.NameValueCollection objRetInf = new System.Collections.Specialized.NameValueCollection(30);
            byte[] objRetBytes;
            string[] objRetVals;
            string retMessage;

            objInf.Add("x_version", AuthNetVersion);
            objInf.Add("x_delim_data", "True");
            objInf.Add("x_login", AuthNetLoginID);
            // objInf.Add("x_password", AuthNetPassword);
            objInf.Add("x_tran_key", AuthNetTransKey);
            objInf.Add("x_relay_response", "False");

            // Switch this to False once you go live
            objInf.Add("x_test_request", AuthNetMode);

            objInf.Add("x_delim_char", ",");
            objInf.Add("x_encap_char", "|");

            // Billing Address
            objInf.Add("x_first_name", payDataModel.FirstName);
            objInf.Add("x_last_name", payDataModel.LastName);
            objInf.Add("x_address", payDataModel.Address);
            objInf.Add("x_city", payDataModel.City);
            objInf.Add("x_state", payDataModel.State);
            objInf.Add("x_zip", payDataModel.ZIP);
            objInf.Add("x_country", payDataModel.Country);
            objInf.Add("x_email", payDataModel.Email);
            objInf.Add("x_fax", payDataModel.Fax);
            objInf.Add("x_phone", payDataModel.Phone);

            objInf.Add("x_description", payDataModel.Description);

            // Card Details
            objInf.Add("x_card_num", payDataModel.CardNumber);
            objInf.Add("x_exp_date", payDataModel.CardExpiryDate);

            // Authorisation code of the card (CCV)
            objInf.Add("x_card_code", payDataModel.CCV);

            objInf.Add("x_method", "CC");
            objInf.Add("x_type", "AUTH_CAPTURE");
            objInf.Add("x_amount", payDataModel.Amount);

            // Currency setting. Check the guide for other supported currencies
            objInf.Add("x_currency_code", "USD");

            try
            {
                // Pure Test Server
                objRequest.BaseAddress = System.Configuration.ConfigurationManager.AppSettings["Payment.URL"].ToString().Trim();

                objRetBytes = objRequest.UploadValues(objRequest.BaseAddress, "POST", objInf);
                objRetVals = System.Text.Encoding.ASCII.GetString(objRetBytes).Split(",".ToCharArray());

                if (objRetVals[0].Trim(char.Parse("|")) == "1")
                {
                    // Returned Authorisation Code
                    ViewBag.AuthNetCode = objRetVals[4].Trim(char.Parse("|"));
                    // Returned Transaction ID
                    ViewBag.AuthNetTransID = objRetVals[6].Trim(char.Parse("|"));
                    return retMessage = "1";
                }
                else
                {
                    // Error!
                    retMessage = objRetVals[3].Trim(char.Parse("|")) + " (" + objRetVals[2].Trim(char.Parse("|")) + ")";

                    if (objRetVals[2].Trim(char.Parse("|")) == "44")
                    {
                        // CCV transaction decline
                        retMessage += "Our Card Code Verification (CCV) returned the following error: ";

                        switch (objRetVals[38].Trim(char.Parse("|")))
                        {
                            case "N":
                                retMessage += "Card Code does not match.";
                                break;
                            case "P":
                                retMessage += "Card Code was not processed.";
                                break;
                            case "S":
                                retMessage += "Card Code should be on card but was not indicated.";
                                break;
                            case "U":
                                retMessage += "Issuer was not certified for Card Code.";
                                break;
                        }
                    }

                    if (objRetVals[2].Trim(char.Parse("|")) == "45")
                    {
                        if (retMessage.Length > 1)
                            retMessage += "<br />n";

                        // AVS transaction decline
                        retMessage += "Our Address Verification System (AVS) returned the following error: ";

                        switch (objRetVals[5].Trim(char.Parse("|")))
                        {
                            case "A":
                                retMessage += " the zip code entered does not match the billing address.";
                                break;
                            case "B":
                                retMessage += " no information was provided for the AVS check.";
                                break;
                            case "E":
                                retMessage += " a general error occurred in the AVS system.";
                                break;
                            case "G":
                                retMessage += " the credit card was issued by a non-US bank.";
                                break;
                            case "N":
                                retMessage += " neither the entered street address nor zip code matches the billing address.";
                                break;
                            case "P":
                                retMessage += " AVS is not applicable for this transaction.";
                                break;
                            case "R":
                                retMessage += " please retry the transaction; the AVS system was unavailable or timed out.";
                                break;
                            case "S":
                                retMessage += " the AVS service is not supported by your credit card issuer.";
                                break;
                            case "U":
                                retMessage += " address information is unavailable for the credit card.";
                                break;
                            case "W":
                                retMessage += " the 9 digit zip code matches, but the street address does not.";
                                break;
                            case "Z":
                                retMessage += " the zip code matches, but the address does not.";
                                break;
                        }
                    }

                    // strError contains the actual error
                    ViewBag.ErrorMsg = retMessage;
                    return retMessage;
                }
            }
            catch (Exception ex)
            {
                return retMessage = ex.Message;
            }
        }
That's it. You can download sample  for get response from authorize.net payment gateway using asp,net mvc project. Please share this post to social media if its is help you and also don't forgot to write some comments. 

Fading / Blinking rounded corner button only with pure css(without image)

Wednesday

Css only Rounded corners Opacity Gradient buttons:
                                        This is the source code for, No JavaScript, No Images, No Flash - CSS Only! Blinking round corner button.

source code for css only rounded corner fade button

<html><head>
<style>
nav a#blink {
padding: 10px 10px;
margin-right: 10px;
margin-bottom: 5px;
border-radius:20px;
display: inline-block;
color: #421f00;
background-color:#e6a560;
background: -moz-linear-gradient(
top,
#e6a560 0%,
#ca6609);
background: -o-linear-gradient(
top,
#e6a560 0%,
#ca6609);
background: -webkit-gradient(
linear, left top, left bottom, 
from(#e6a560),
to(#ca6609));
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#e6a560', endColorstr='#ca6609');
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border: 1px solid #ffd5ab;
-moz-box-shadow:
0px 1px 3px rgba(000,000,000,0.5),
inset 0px 0px 2px rgba(66,31,0,0.7);
-webkit-box-shadow:
0px 1px 3px rgba(000,000,000,0.5),
inset 0px 0px 2px rgba(66,31,0,0.7);
text-shadow:
0px -1px 0px rgba(171,87,13,0.4),
0px 1px 0px rgba(255,255,255,0.3); 
opacity:0.7;
-webkit-animation-name: buttonPulse;
-webkit-animation-duration: 4s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
}

@-webkit-keyframes buttonPulse { 
0% { opacity: 0.5 }

50% { opacity: 1 }

100% { opacity: 0.5 }
} 
</style>
</head>
<body>
<nav role="navigation">
<a href="http://codingcluster.blogspot.in/2012/10/fadeblinking-rounded-corner-button-only.html" id="blink">Hay Look at my skin</a>
</nav>
</body>
</html>

Demo:

Avoid jQuery conflict with Other JavaScript library(prototype,YUI,mootools)

Thursday

Avoid jquery conflict with other javascript libraries:
                  In this jQuery article, I will explain how to avoid jquery conflict with other javascript libraries.


javascript libraries such as YUI, prototype conflict with Jquery library as prototype library also uses `$` sign.To resolve this issue, jQuery has .noConflict() method.

.noConflict() method: Relinquish jQuery's control of the $ variable (override the $ function)
we can avoid prototype and jquery conflict using .noConflict() method.


Code:

<html>  
<title>Avoid conflict between JQuery and prototype,YUI,mootools (Other javascript Libraries)</title>
<head>  
    <script src="/js/prototype.js"></script>  
    <script src="/js/jquery.js"></script>  
<script type="text/javascript" src="../other_lib.js"></script>
<script language="javascript" type="text/javascript">  
    jQuery.noConflict();  
    jQuery(document).ready(function(){  
     jQuery("#t1").show();  
 // Do something with jQuery
   });  
     $('t1').show();  
  // Do something with prototype
 </script>  
 </head>  
 </html> 

iPhone tricks:Insert string value with single quotes ( Replace single quote in sqlite3 insert query - objective c ) in iphone sdk

Friday

                                             I am working in SQLite3 database for Iphone App.In Objecitve-c one of my query is getting failed because of the value "This is user's profile" as it contains single quote. So I'm using the following solution to resolve my problem. By using the string replacement we can save the apostrophe into the database. Look at the following sample

strAddress =  [strAddress stringByReplacingOccurrencesOfString:@"'" withString:@"\''"];


How to programmatically prevent iphone auto-lock?
                            Simply disabled the idle timer to prevent outo-lock the iphone while your app is open. Just add the following line into your apps delegate.m file.

- (void)applicationDidFinishLaunching:(UIApplication *)application {   application.idleTimerDisabled = YES;  }


How to hide keyboard when you press the button - iphone sdk?
           In button action you just write

 [yourTextField resignFirstResponder].

It will hide your keyboard.

How to take a screenshot with the iPhone?
                 To take a screenshot, hold the home button and click the sleep button. The screen will flash white and the screenshot will be stored in your camera roll.
 
                                         

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets