Tuesday, September 3, 2013

Implementing PayPal on ASP.net website

JUST  FOLLOW THESE THREE STEPS :
set : assign all credential , qty price
get  :get token id , paypalid

do capture :with the hep of paypalid do final payment

Step 1:  In Checkout Page :call PayPal method

   protected void lnkPaypal_Click(object sender, EventArgs e)
    {
            //PaymentThrougPaypal();
            try
            {
                //UpdatePackageOrder();
                PaymentThrougPaypal();
            }
            catch (Exception ex)
            {
                ErrorLog error = new ErrorLog();
                error.lUsrId = Test.BusinessLayer.BusinessAccessLayer.User.Current.lUsrId;
                error.sClientIP = Request.UserHostAddress;
                error.sAbsoluteUri = Request.Url.AbsoluteUri;
                error.sErrMsg = ex.Message;
                error.sStackTrace = ex.Message;
                error.Create();
                //if (ex.Message != "Thread was being aborted.")
                //RollBackUpdatePackageOrder();
            }
    }
    #region Paypal integration
    private void PaymentThrougPaypal()
    {
      //  Session["payment_amt"] = 1000;
        //NVPAPICaller test = new NVPAPICaller();
        string retMsg = "";
        string token = "";
        string sLogo = "http://www.test.com/App_Themes/Default/images/logo1.jpg";
        Test.Paypal.PaypalIntegration.PaypalIntegration paypalRequest = new Test.Paypal.PaypalIntegration.PaypalIntegration();
        paypalRequest.OrderID = OrderId;
        //paypalRequest.ReqConfirmShipping = "1";
        paypalRequest.logo = sLogo;

        Order pOrder = new Order(OrderId);
            if (pOrder.decNetAmount != null)
            {
               // Math.Round((decimal)order.decNetAmount, 2).ToString();
               string host = "www.paypal.com";//"www.paypal.com";//
               // string host = "www.sandbox.paypal.com";
                //string amt = pOrder.decNetAmount.ToString();
               decimal amt1 =Convert.ToDecimal(pOrder.decNetAmount);
               string amt = Math.Round((decimal)amt1, 2).ToString();
              // string amt = Math.Round((decimal)amt1, 2).ToString() + ((pOrder.decPackagePrice.HasValue) ? pOrder.decPackagePrice.Value : 0);
                Test.Paypal.Paypal.Paypal eSet = new Test.Paypal.Paypal.Paypal();
                Test.Paypal.PaypalIntegration.PaypalIntegration _paypalRequest = new Test.Paypal.PaypalIntegration.PaypalIntegration();
               token = eSet.ECSetExpressCheckoutCode(amt, "http://www.test.com/paydetails.aspx", "http://www.test.com/Checkout.aspx", com.paypal.soap.api.PaymentActionCodeType.Order, com.paypal.soap.api.CurrencyCodeType.AUD, paypalRequest, Convert.ToInt64(pOrder.lPOrderId));
 Response.Redirect("https://" + host + "/cgi-bin/webscr?cmd=_express-checkout" + "&token=" + token);
            }
       
    }
    #endregion


Step 2: In Separate Page do all coding to make transactions, this page will run in background
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.paypal.sdk.services;
using com.paypal.soap.api;
using com.paypal.sdk.profiles;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web.Routing;
using GenerateCodeSOAP;
using System.IO;
using System.Collections;
using System.Net;
using System.Net.Mail;
using System.Xml;
using Test.Common;
using Test.Paypal.Paypal;
using Test.BusinessLayer.BusinessAccessLayer;
using Test.BusinessLayer.DataAccessLayer;


public partial class paydetails : FrontPageBase
{
    string token = "";
    string payerId = "";
    string amt = "";
    string InvoiceId = "";
    string cardHolderNm = "";
    string bilAdd = "";
    string traId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["token"] != null)
        {
            GetExpressCheckoutDetailsResponseType resp = new GetExpressCheckoutDetailsResponseType();
            Test.Paypal.ECGetExpressCheckout ecGet = new Test.Paypal.ECGetExpressCheckout();
            resp = ecGet.ECGetExpressCheckoutCode(Request.QueryString["token"].ToString());
            //Response.Write(resp);
            payerId = resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerID.ToString();
            amt = resp.GetExpressCheckoutDetailsResponseDetails.PaymentDetails.OrderTotal.Value;
            token = resp.GetExpressCheckoutDetailsResponseDetails.Token.ToString();
            InvoiceId = resp.GetExpressCheckoutDetailsResponseDetails.InvoiceID.ToString();
            cardHolderNm = (!string.IsNullOrEmpty(resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerName.FirstName) ? resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerName.FirstName : string.Empty)
                + "  " + (!string.IsNullOrEmpty(resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerName.LastName) ? resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerName.LastName : string.Empty);
            bilAdd = (!string.IsNullOrEmpty(resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.CityName.ToString()) ? resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.CityName.ToString() : string.Empty)
            + "<br/>" + (!string.IsNullOrEmpty(resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.Street1.ToString()) ? resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.Street1.ToString() : string.Empty)
            + "<br/>" + (!string.IsNullOrEmpty(resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.Street2.ToString()) ? resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.Street2.ToString() : string.Empty)
            + "<br/>" + (!string.IsNullOrEmpty(resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.CountryName.ToString()) ? resp.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.CountryName.ToString() : string.Empty);

        }

        if (token != "" && payerId != "")
        {
            DoExpressCheckoutPaymentResponseType ecDoPayment = new DoExpressCheckoutPaymentResponseType();
            Test.Paypal.ECDoExpressCheckout doPayment = new Test.Paypal.ECDoExpressCheckout();
            ecDoPayment = doPayment.ECDoExpressCheckoutCode(token, payerId, amt, com.paypal.soap.api.PaymentActionCodeType.Order, com.paypal.soap.api.CurrencyCodeType.AUD);
            string ack = ecDoPayment.Ack.ToString();
            string authorizationId = ecDoPayment.DoExpressCheckoutPaymentResponseDetails.PaymentInfo.TransactionID;
            string amtValue = ecDoPayment.DoExpressCheckoutPaymentResponseDetails.PaymentInfo.GrossAmount.Value;

            com.paypal.soap.api.CurrencyCodeType amtCurrencyCodeType = com.paypal.soap.api.CurrencyCodeType.AUD;
            Test.Paypal.DoCapture doCapt = new Test.Paypal.DoCapture();
            //Response.Write(ack);
            DoCaptureResponseType doCaptrDetails = new DoCaptureResponseType();
            //  doCaptrDetails = doCapt.DoCaptureCode(authorizationId, note, amtValue, amtCurrencyCodeType, InvoiceId);
            doCaptrDetails = doCapt.DoCaptureCode(authorizationId, amtValue, amtCurrencyCodeType, InvoiceId);
            string captrAck = doCaptrDetails.Ack.ToString();
            //Response.Write(captrAck);
            // Response.Write(ack + "//" + InvoiceId + "//" + captrAck);
            // Response.End();
            if ((ack.ToLower() == "success") && (!String.IsNullOrEmpty(InvoiceId)) && (captrAck.ToLower() == "success"))
            {
                try
                {

                    Order pOrder = new Order(Convert.ToInt64(InvoiceId));
                    Outlet outlet = new Outlet(pOrder.lOutletId);
                    pOrder.orderStatus = Order.OrderStatus.Placed;
                    pOrder.pymtStatus = Order.PaymentStatus.Made;
                    pOrder.sTransactionId = doCaptrDetails.DoCaptureResponseDetails.PaymentInfo.TransactionID.ToString();
                    Response.Write(pOrder.sTransactionId);
                    traId = doCaptrDetails.DoCaptureResponseDetails.PaymentInfo.TransactionID.ToString();
                    pOrder.sPaymentType = "Paypal";
                    pOrder.sGatewaynm = "Paypal";
                    string paypal="paypal";
                    Invoice.GenerateInvoice_New(pOrder,paypal);
                    Invoice.GenerateInvoice_Customer(pOrder,paypal);
                    pOrder.Update();

                    #region Email to Customer with invoice attachment

                    DataTable dtOutletChainColContTemplVal;
                    CMS cms;
                    ContentTemplate conTemp;

                    dtOutletChainColContTemplVal = Test.BusinessLayer.DataAccessLayer.DBChain.PopulateOutletChainsColumnValue(outlet.lOutletId, "TaxInvEmlCustId");

                    if (dtOutletChainColContTemplVal != null)
                    {
                        conTemp = new ContentTemplate(Int16.Parse(dtOutletChainColContTemplVal.Rows[0][0].ToString()));
                        cms = new CMS(Test.BusinessLayer.BusinessAccessLayer.Site.Current.lSiteId, conTemp.shContentTempId);
                    }
                    else
                    {
                        conTemp = new ContentTemplate(Resources.ContentName.mailorderandtaxinvoice);
                        cms = new CMS(Test.BusinessLayer.BusinessAccessLayer.Site.Current.lSiteId, conTemp.shContentTempId);
                    }

  InsertPapalInfo();

Response.Redirect("http://test.com/thank-you-for-your-order.aspx");
}

catch (Exception ex)
                {
                   
                    Response.Write("catch");
                    throw;

                }

            }
            else
            {
                Response.Redirect("http://test.com/Checkout.aspx");
            }

else
        {
            Response.Redirect("http://test.com/Checkout.aspx");
        }



#region method to update Paypal user info
    public void InsertPapalInfo()
    {

        test.BusinessLayer.DataAccessLayer.DBPaypalInfo.InsertPaypalInfo(Convert.ToInt64(InvoiceId), cardHolderNm, bilAdd, traId);

    }
    #endregion

Step 3 :Configure all .cs files

a)      DoCapture.cs
b)      ECDoExpressCheckout.cs
c)       ECGetExpressCheckout.cs
d)      ECSetExpressCheckout.cs
e)      Paypal.cs
f)       PaypalIntegration.cs
And Payflow_dotNET.dll to bin now paypal will work for you thanks !!!!


No comments:

Post a Comment