Monday 29 October 2012

Online Credit Card Transaction in ASP.NET Using PayPal Pro / dodirect

  string strUsername = "api user name";
            string strPassword = "pasword";
            string strSignature = "signature";
            string strCredentials = "USER=" + strUsername + "&PWD=" + strPassword + "&SIGNATURE=" + strSignature;

            string strNVPSandboxServer = "https://api-3t.sandbox.paypal.com/nvp";
            string strAPIVersion = "50.0";
       
            string strNVP = strCredentials + "&METHOD=DoDirectPayment" +
            "&CREDITCARDTYPE=" + "credit card type"+
            "&ACCT=" + "credit card number" +
            "&EXPDATE=" + "expire date" +
            "&CVV2=" + "cvv number"+
            "&AMT=" + "100" +
            "&FIRSTNAME=" + "Gopal" +
            "&LASTNAME=" + "Singh"+
            "&IPADDRESS= ip address" +
            "&STREET=" + "street" +
            "&CITY=" + "city"+
            "&STATE=" +"state"+
            "&COUNTRY=" + "contry" +
            "&ZIP=" +"1234" + "" +
            "&CURRENCYCODE=GBP" +
            "&PAYMENTACTION=Sale" +
            "&VERSION=" + strAPIVersion;


            try
            {
                //Create web request and web response objects, make sure you using the correct server (sandbox/live)
                HttpWebRequest wrWebRequest = (HttpWebRequest)WebRequest.Create(strNVPSandboxServer);
                wrWebRequest.Method = "POST";
                StreamWriter requestWriter = new StreamWriter(wrWebRequest.GetRequestStream());
                requestWriter.Write(strNVP);
                requestWriter.Close();

                // Get the response.
                HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
                StreamReader responseReader = new StreamReader(wrWebRequest.GetResponse().GetResponseStream());

                //and read the response
                string responseData = responseReader.ReadToEnd();
                responseReader.Close();

                string result = Server.UrlDecode(responseData);

                string[] arrResult = result.Split('&');
                Hashtable htResponse = new Hashtable();
                string[] responseItemArray;
                foreach (string responseItem in arrResult)
                {
                    responseItemArray = responseItem.Split('=');
                    htResponse.Add(responseItemArray[0], responseItemArray[1]);
                }

                string strAck = htResponse["ACK"].ToString();
                Response.Write(strAck);

                if (strAck == "Success" || strAck == "SuccessWithWarning")
                {
                    string strAmt = htResponse["AMT"].ToString();
                    string strCcy = htResponse["CURRENCYCODE"].ToString();
                    string strTransactionID = htResponse["TRANSACTIONID"].ToString();
                    string strSuccess = "Thank you, your order for: $" + strAmt + " " + strCcy + " has been processed.";


                }
                else
                {
                    string strErr = "Error: " + htResponse["L_LONGMESSAGE0"].ToString();
                    string strErrcode = "Error code: " + htResponse["L_ERRORCODE0"].ToString();
           

                }
            }
            catch (Exception ex)
            {
                // do something to catch the error, like write to a log file.
                // Response.Write("error processing");
                //  Response.End();
             
            }

Friday 12 October 2012

convert text file into csv file in c#


  try
            {

             
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Title = "Open file as...";
                dialog.Filter = "txt files (*.txt)|*.txt";
                dialog.RestoreDirectory = true;

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string csvFileName = dialog.FileName;
                    //get the user to specify the                  
                    TextReader reader = new StreamReader(csvFileName);
                    string a = reader.ReadToEnd();

                    char[] array = a.ToCharArray();

                    reader.Close();
                    MessageBox.Show("File Converted! Please save");

                    SaveFileDialog dialogSave = new SaveFileDialog();
                    dialogSave.Title = "Save file as...";
                    dialogSave.Filter = "CSV files (*.csv)|*.csv";
                    dialogSave.RestoreDirectory = true;

                    if (dialogSave.ShowDialog() == DialogResult.OK)
                    {
                        string saveFile = dialogSave.FileName;
                        //get the user to specify the                



                        FileStream aFile = new FileStream(saveFile, FileMode.OpenOrCreate);

                    StreamWriter sw = new StreamWriter(aFile);

                    for (int i = 0; i < array.Length; i++)
                    {

                        if (array[i] == '\n' && array[i] == '\r')

                            sw.WriteLine();

                        else

                            sw.Write(a[i] + ",");

                    }

                    sw.Close();
                    }
                 
                }



            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error creating the download report:" + ex.Message);

            }