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
        }
        }
    }

15 comments:

  1. Hello ur code is fine but till error 110 help me my no is-8961188812 id-raju@vtpl.us

    ReplyDelete
    Replies
    1. Hi Raju,

      Please post your code at
      http://myway2code.com/2014/04/09/c-ccavenue-payment-gateway-implementation/

      And also make sure that you are using the MerchantId and Working key provided by ccAvenue.

      Delete
  2. how can i get CCACrypto namesapce

    ReplyDelete
  3. hey pls help i getting error :

    Error Code: 6001
    Error Description: Invalid+payment+option+or+promotions+code

    ReplyDelete
    Replies
    1. Are u using correct merchant id??

      Please refer documentation provided by ccavenue for error code 6001

      Delete
  4. Hi.

    I have registered merchant in CCavenue. I have not received CCA Crypto methods.

    Where can i get it?

    ReplyDelete
  5. i am getting error code 0 and no error message just blank like CCAvenue Transaction Error Page in asp.net c#

    ReplyDelete
  6. Hello,
    You have nowhere created the object 'objPaymentDetails' and in the response page u are using it. You u have forgot to create one then what is it. the param key, you are using in proper case, is it correct or case doesnot matter here.
    in CCAvenue asp web kit string
    string encResponse = ccaCrypto.Decrypt(Request.Form["encResp"],workingKey);
    and you have defined it as
    encResponse = Request.Form.ToString();
    what is the difference in both.
    please help me .. i m facing lot of problems, i m not able to receive the payment status and unable to update the payment status in database.

    ReplyDelete
    Replies
    1. i think the following line is the new mechanism provided by ccAvaenue
      string encResponse = ccaCrypto.Decrypt(Request.Form["encResp"],workingKey);

      but Request.Form.ToString(); will return all the response(from ccAvaenu) in string format and it will have '&' separater.

      Delete
  7. i have error in objparameterDetails in code i dont know how to clear

    ReplyDelete
  8. what is objPaymentDetails.PaymentAmount.ToString() in above code

    ReplyDelete
  9. i have error in objparameterDetails in code i dont know how to clear

    ReplyDelete
  10. Receiving error as null object reference at the line
    string encResponse = ccaCrypto.Decrypt(Request.Form["encResp"],workingKey);

    ReplyDelete
  11. Your Affiliate Profit Machine is ready -

    Plus, getting it set up is as easy as 1-2-3!

    Follow the steps below to make money...

    STEP 1. Input into the system what affiliate products you want to push
    STEP 2. Add some PUSH button traffic (it LITERALLY takes 2 minutes)
    STEP 3. See how the system grow your list and up-sell your affiliate products all on it's own!

    Are you ready to start making money?

    You can test-drive the system for yourself risk free...

    ReplyDelete