How to retrive payment result value after secure paytabs payment done in PayTabs payment gateway in asp.net

Joined
May 11, 2023
Messages
1
Reaction score
0

i am using a PayTabs payment gateway in my project for online payment system.​

i got the transaction response before payment done like this (// "Id,ProfileId,Endpoint,ServerKey,TranType,TranClass,CartId,CartCurrency,CartAmount,CartDescription,PaypageLang,HideShipping,IsFramed,ReturnURL,CallbackURL")

but now i want response status after secure paytabs payment done in PayTabs payment gateway. So how i get all payment data that given through paytabs in my redirection page i am using a asp.net here is my asp code

ASP.net:
public partial class Payment_Test : Page
{
    MainClass mc = new MainClass();
    SqlConnection cn;
    StringBuilder sb = new StringBuilder();
    stardptestEntities db = new stardptestEntities();
    string OrderUri = "https://secure.telr.com/gateway/order.json";
    string IVPMethod = "Create";
    string IVPStore = "23257";
    string IVPAuthKey = "dJXg@4bNy7m#LhBQ7";
    string IVPCurrency = "AED";//"https://webhook.site/1d624200-66t8-4e27-8ec5-341cc43883ea";//
    string IVPReturnUrl = "http://localhost:55332/Payment-Test.aspx?{0}";
    //***string IVPReturnUrl = ConfigurationManager.AppSettings["Url"].ToString() + "Payment-Test.aspx?i=demo&status=success";
    string EncryptedOrderId = "";

    int ProfileId = 107905;
    string ServerKey = "*******-*******-******";
    string paymentGatwayUrl = "https://secure.paytabs.com/";

    protected void Page_Load(object sender, EventArgs e)
    {
        string i = "";
        string status = "";
        string orderid = "";


        if (!string.IsNullOrWhiteSpace(orderid = Request.QueryString.Get("orderid")))
        {
            //CreateOrderAsync().Wait();
            CreatePayTabOrderAsync().Wait();
        }
        // else if (Request.QueryString.HasKeys() && !string.IsNullOrWhiteSpace(status = Request.QueryString.Get("status")))
        //{
        //** Response.Redirect("invoice.aspx");
        else if (Request.QueryString.HasKeys() && !string.IsNullOrWhiteSpace(i = Request.QueryString.Get("i")) && !string.IsNullOrWhiteSpace(status = Request.QueryString.Get("status")))
        {
            TransactionCompletedCalledFromTelr(HttpUtility.UrlDecode(i).Replace(" ", "+"), status);
            (frmPay.FindControl("txtCallback") as TextBox).Text = HttpContext.Current.Request.Url.
              AbsoluteUri.Replace(HttpContext.Current.Request.Url.Query, "") + "?{0}";
        }
        else
        {
            //**
            TransactionCompletedCalledFromTelr("", "A");
            string payid = "";
            if (payid != "")
            {
                lbl1.Text = "hello team";
            }
            pnlTransactionResult.Visible = false;
            (frmPay.FindControl("txtCallback") as TextBox).Text = HttpContext.Current.Request.Url.AbsoluteUri + "?{0}";
        }

    }

    

    
    
    public async Task<Transaction_Response> Pay()
    {
        string ordid = Session["orderid"].ToString();
        int ordidz = int.Parse(Session["orderid"].ToString());
        var obj = db.cms_order.Where(c => c.order_id == ordidz).FirstOrDefault();
        float orderAmount = (float)Convert.ToDouble(obj.amt.ToString());
        string orderDesc = "ffffffff";

        Transaction transaction = new Transaction();
        transaction.Id = 1;
        transaction.ProfileId = ProfileId;
        transaction.Endpoint = paymentGatwayUrl;
        transaction.ServerKey = ServerKey;
        transaction.TranType = "sale";
        transaction.TranClass = "ecom";
        transaction.CartId = ordid;
        transaction.CartCurrency = IVPCurrency;
        transaction.CartAmount = orderAmount;
        transaction.CartDescription = orderDesc;
        transaction.PaypageLang = "en";
        transaction.HideShipping = false;
        transaction.IsFramed = false;
        transaction.ReturnURL = IVPReturnUrl;
        transaction.CallbackURL = IVPReturnUrl;
        // "Id,ProfileId,Endpoint,ServerKey,TranType,TranClass,CartId,CartCurrency,CartAmount,CartDescription,PaypageLang,HideShipping,IsFramed,ReturnURL,CallbackURL"
        if (transaction == null)
        {
            return null; // NotFound();
        }

        Transaction_Response r = Send(transaction);

        if (r.IsSuccess())
        {
            transaction.Tran_Ref = r.tran_ref;
            transaction.TriedToPay = true;
            //
            transaction.CallbackURL = IVPReturnUrl + "?tran_ref=" + r.tran_ref;
            //

            Response.Redirect(r.redirect_url);
            //Response.Redirect(ConfigurationManager.AppSettings["Url"].ToString() +"invoice.aspx?orderid='133'");
        }

        return r; // RedirectToAction(nameof(Details), new { id });
    }

    public Transaction_Response Send(Transaction transaction)
    {
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        string base_url = transaction.Endpoint; // "https://secure.paytabs.com/";
        string payment_url = base_url + "payment/request";

        string body = JsonConvert.SerializeObject(transaction);

        //var client = new RestClient(payment_url);
        var client = new RestClient();
        //client.UseJson();
        //client.AddHandler("applications/json", )
        // payment_url = "https://secure.paytabs.com/payment/page/REF/redirect";
        var request = new RestRequest(payment_url, Method.POST);
        request.AddHeader("authorization", transaction.ServerKey);
        request.AddParameter("text/plain", body, ParameterType.RequestBody);
        //request.AddJsonBody(transaction);

        var response = client.Execute(request);
        //__Transaction_Response tran_res = new JsonSerializer().Deserialize<Transaction_Response>((JsonReader)response);

        //Transaction_Response tran_res = new RestSharp.Deserializers.JsonDeserializer().Deserialize<Transaction_Response>(response);
        Transaction_Response tran_res = JsonConvert.DeserializeObject<Transaction_Response>(response.Content);
        //new RestSharp.Deserializers.JsonDeserializer().Deserialize<Transaction_Response>(response);

        return tran_res;
    }
}
 

Members online

Forum statistics

Threads
473,979
Messages
2,570,183
Members
46,719
Latest member
login dogas.info

Latest Threads

Top