Showing posts with label ccAvenue. Show all posts
Showing posts with label ccAvenue. Show all posts

Wednesday, April 9, 2014

C# ccAvenue Payment Gateway Implementation

  1. Payment Request Page ( ASPX )


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <script src="../js/jquery.min.js" type="text/javascript"></script>
        <script>
            jQuery(document).ready(function () {
                jQuery("#btnSubmit").click();
                //Custom.init();
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <table style="display: none;">
            <tr>
                <td>
                    OrderId:
                </td>
                <td>
                    <asp:Label ID="lblOrderId" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Amount:
                </td>
                <td>
                    <asp:Label ID="lblAmount" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    MerchantId:
                </td>
                <td>
                    <asp:Label ID="lblMerchantId" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    RedirectURL:
                </td>
                <td>
                    <asp:Label ID="lblRedirectUrl" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    BillingCustomerName:
                </td>
                <td>
                    <asp:Label ID="lblCustomerName" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    BillingCustomerAddress:
                </td>
                <td>
                    <asp:Label ID="lblCustAddr" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    BillingCustomerCountry:
                </td>
                <td>
                    <asp:Label ID="lblCustCountry" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    BillingCustomerTelephone:
                </td>
                <td>
                    <asp:Label ID="lblCustPhone" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    BillingCustomerEmail:
                </td>
                <td>
                    <asp:Label ID="lblCustEmail" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    BillingCustomerState:
                </td>
                <td>
                    <asp:Label ID="lblCustState" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    BillingCustomerCity:
                </td>
                <td>
                    <asp:Label ID="lblCustCity" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    ZipCode:
                </td>
                <td>
                    <asp:Label ID="lblZipCode" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    CustomerNotes:
                </td>
                <td>
                    <asp:Label ID="lblCustNotes" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    DeliveryCustomerName:
                </td>
                <td>
                    <asp:Label ID="lblDelCustName" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    DeliveryCustomerAddress:
                </td>
                <td>
                    <asp:Label ID="lblDelCustAddr" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    DeliveryCustomerCountry:
                </td>
                <td>
                    <asp:Label ID="lblDelCustCntry" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    DeliveryCustomerTelephone:
                </td>
                <td>
                    <asp:Label ID="lblDelCustTel" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    DeliveryCustomerState:
                </td>
                <td>
                    <asp:Label ID="lblDelCustState" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    DeliveryCustomerCity:
                </td>
                <td>
                    <asp:Label ID="lblDelCustCity" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    DeliveryCustomerZipCode:
                </td>
                <td>
                    <asp:Label ID="lblDelZipCode" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    MerchantParam:
                </td>
                <td>
                    <asp:Label ID="lblMerchantParam" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Label ID="lblPayType" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" PostBackUrl="http://www.ccavenue.com/shopzone/cc_details.jsp" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:HiddenField ID="Merchant_Id" runat="server" />
                    <asp:HiddenField ID="encRequest" runat="server" />
                </td>
            </tr>
        </table>
        <center>
            <asp:Label ID="lb_Message" Style="color: Red; display: block; margin-top: 35px;"
                runat="server" Text=""></asp:Label>
        </center>
        </form>
    </body>
    </html>
  2. Payment Request Page ( ASPX.CS )

    CCACrypto chkSum = new CCACrypto();
    string MerchantId = ccAvenueMerchantId // PROVIDED By ccAvenue
    string WorkingKey = ccWorkingKey // Provided By ccAvenue
       
    protected void Page_Load(object sender, EventArgs e)
    {           
        if (IsPostBack == false)
        {
        //PAYMENT RESPONSE PAGE
        string ResponseUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) +
                    HttpContext.Current.Request.ApplicationPath + "Payment/PaymentResponse.aspx";
        //Get Payment Details from Session

            lblOrderId.Text = TRANSACTION ID
            lblAmount.Text = PAYMENT AMOUNT
            lblMerchantId.Text = MerchantId;
            lblRedirectUrl.Text = ResponseUrl;
            lblCustomerName.Text = CUSTOMER NAME
            lblCustAddr.Text = CUSTOMER ADDRESS
            lblCustCountry.Text = CUSTOMER COUNTRY
            lblCustPhone.Text = CUSTOMER PHONE
            lblCustEmail.Text = CUSTOMER EMAIL
            lblCustState.Text = CUSTOMER STATE
            lblCustCity.Text = CUSTOMER CITY
            lblZipCode.Text = CUSTOMER ZIP
            lblCustNotes.Text = CUSTOMER NOTES
            lblDelCustName.Text = CUSTOMER DELIVERY NAME
            lblDelCustAddr.Text = CUSTOMER DELIVERY ADDRESS
            lblDelCustCntry.Text = CUSTOMER DELIVERY COUNTRY
            lblDelCustTel.Text = CUSTOMER DELIVERY PHONE
            lblDelCustState.Text = CUSTOMER DELIVERY STATE
            lblDelCustCity.Text = CUSTOMER DELIVERY CITY
            lblDelZipCode.Text = CUSTOMER DELIVERY ZIP
            lblMerchantParam.Text = MERCHANT EXTRA PARAMETERS
            lblPayType.Text = PAYMENT TYPE(Credit Card/ Debit Card/ Net Banking)

            string Res = chkSum.getchecksum(MerchantId, lblOrderId.Text, objPaymentDetails.PaymentAmount.ToString(),
                            ResponseUrl, WorkingKey);
            string billingPageHeading = "";
            string ToEncrypt = "Order_Id=" + lblOrderId.Text + "&Amount=" + objPaymentDetails.PaymentAmount + "&Merchant_Id=" + MerchantId + "&Redirect_Url=" + ResponseUrl +
            "&Checksum=" + Res + "&billing_cust_name=" + objPaymentDetails.Cust_Name + "&billing_cust_address=" + objPaymentDetails.Cust_Add + "&billing_cust_country=" + lblCustCountry.Text +
            "&billing_cust_tel=" + objPaymentDetails.Cust_Mobile + "&billing_cust_email=" + objPaymentDetails.Cust_Email + "&billing_cust_state=" + lblCustState.Text +
            "&billing_cust_city=" + lblCustCity.Text + "&billing_zip_code=" + lblZipCode.Text + "&billing_cust_notes=" + lblCustNotes.Text +
            "&delivery_cust_name=" + objPaymentDetails.Cust_Name + "&delivery_cust_address=" + objPaymentDetails.Cust_Add + "&delivery_cust_country=" + lblDelCustCntry.Text +
            "&delivery_cust_tel=" + objPaymentDetails.Cust_Mobile + "&delivery_cust_state=" + lblDelCustState.Text + "&delivery_cust_city=" + lblDelCustCity.Text +
            "&delivery_zip_code=" + lblDelZipCode.Text + "&Merchant_Param=" + lblMerchantParam.Text + "&billingPageHeading=" + billingPageHeading + "&payType=" + lblPayType.Text;

            string Encrypted;
            Encrypted = chkSum.Encrypt(ToEncrypt, WorkingKey);

            Merchant_Id.Value = MerchantId;
            encRequest.Value = Encrypted;

            lb_Message.Text = "Wait... Redirecting to Payment Gateway.";

        }
        else
        {
            lb_Message.Text = "Error on Payment Gateway. Kindly try again.";
        }
        }
    }
  3. Payment Response Page

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
        string WorkingKey = ccWorkingKey // PROVIDED BY ccAvenue
        CCACrypto func = new CCACrypto();
        string encResponse = Request.Form.ToString();

        NameValueCollection Params = new NameValueCollection();
        string[] segments = encResponse.Split('&');
        foreach (string seg in segments)
        {
            string[] parts = seg.Split('=');
            if (parts.Length > 0)
            {
            string Key = parts[0].Trim();
            string Value = parts[1].Trim();

            Params.Add(Key, Value);
            }
        }
        try
        {                    
            //Verify with ccAvenue
            string strVerify = func.verifychecksum(Params["Merchant_Id"].ToString(), Params["Order_Id"].ToString(), Params["Amount"].ToString(), Params["AuthDesc"].ToString(), WorkingKey, Params["Checksum"].ToString());

            if (strVerify.ToUpper() == "TRUE")
            {
            if (objPaymentDetails != null)
            {
                // UPDATE THE PAYMENT STATUS IN DB

                //Update the Payment Status in DB
                //if (Params["AuthDesc"].ToString().Trim() == "Y")
                //    SUCCESS
                //else
                //    FAIL
           
               // REDIRECT TO SUCCESS PAGE OR SHOW THE PAYMENT RESPONSE ON SAME PAGE

            }
            else
            {
                // PAYMENT IS FAIL
                // REDIRECT TO ERROR PAGE
            }
            }
            else
            {
            // PAYMENT VERIFICATION IS FAILED
            // REDIRECT TO ERROR PAGE
            }           
        }
        catch (Exception ex)
        {
            // ERROR IN PROCESSING
            // REDIRECT TO ERROR PAGE
        }
        }
    }