コード例 #1
0
        public ActionResult PaymentWithPaypal()
        {
            //getting the apiContext as earlier
            APIContext apiContext = PaypalConfigruation.GetAPIContext();

            try
            {
                string payerId = Request.Params["PayerID"];

                if (string.IsNullOrEmpty(payerId))
                {
                    string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Paypal/PaymentWithPayPal?";


                    var guid = Convert.ToString((new Random()).Next(100000));

                    var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid);


                    var links = createdPayment.links.GetEnumerator();

                    string paypalRedirectUrl = null;

                    while (links.MoveNext())
                    {
                        Links lnk = links.Current;

                        if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                        {
                            paypalRedirectUrl = lnk.href;
                        }
                    }

                    // saving the paymentID in the key guid
                    Session.Add(guid, createdPayment.id);

                    return(Redirect(paypalRedirectUrl));
                }
                else
                {
                    var guid = Request.Params["guid"];

                    var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);

                    if (executedPayment.state.ToLower() != "approved")
                    {
                        return(View("FailureView"));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Error" + ex.Message);
                return(View("FailureView"));
            }

            return(View("SuccessView"));
        }
コード例 #2
0
        //#region thanh toan the
        //public ActionResult PaymentWithCreditCard()
        //{
        //    //create and item for which you are taking payment
        //    //if you need to add more items in the list
        //    //Then you will need to create multiple item objects or use some loop to instantiate object
        //    Item item = new Item();
        //    item.name = "Demo Item";
        //    item.currency = "USD";
        //    item.price = "5";
        //    item.quantity = "1";
        //    item.sku = "sku";

        //    //Now make a List of Item and add the above item to it
        //    //you can create as many items as you want and add to this list
        //    List<Item> itms = new List<Item>();
        //    itms.Add(item);
        //    ItemList itemList = new ItemList();
        //    itemList.items = itms;

        //    //Address for the payment
        //    Address billingAddress = new Address();
        //    billingAddress.city = "NewYork";
        //    billingAddress.country_code = "US";
        //    billingAddress.line1 = "23rd street kew gardens";
        //    billingAddress.postal_code = "43210";
        //    billingAddress.state = "NY";


        //    //Now Create an object of credit card and add above details to it
        //    CreditCard crdtCard = new CreditCard();
        //    crdtCard.billing_address = billingAddress;
        //    crdtCard.cvv2 = "874";
        //    crdtCard.expire_month = 1;
        //    crdtCard.expire_year = 2020;
        //    crdtCard.first_name = "Aman";
        //    crdtCard.last_name = "Thakur";
        //    crdtCard.number = "1234567890123456";
        //    crdtCard.type = "discover";
        //    // Specify details of your payment amount.
        //    Details details = new Details();
        //    details.shipping = "1";
        //    details.subtotal = "5";
        //    details.tax = "1";

        //    // Specify your total payment amount and assign the details object
        //    Amount amnt = new Amount();
        //    amnt.currency = "USD";
        //    // Total = shipping tax + subtotal.
        //    amnt.total = "7";
        //    amnt.details = details;

        //    // Now make a trasaction object and assign the Amount object
        //    Transaction tran = new Transaction();
        //    tran.amount = amnt;
        //    tran.description = "Description about the payment amount.";
        //    tran.item_list = itemList;
        //    tran.invoice_number = "your invoice number which you are generating";

        //    // Now, we have to make a list of trasaction and add the trasactions object
        //    // to this list. You can create one or more object as per your requirements

        //    List<Transaction> transactions = new List<Transaction>();
        //    transactions.Add(tran);

        //    // Now we need to specify the FundingInstrument of the Payer
        //    // for credit card payments, set the CreditCard which we made above

        //    FundingInstrument fundInstrument = new FundingInstrument();
        //    fundInstrument.credit_card = crdtCard;

        //    // The Payment creation API requires a list of FundingIntrument

        //    List<FundingInstrument> fundingInstrumentList = new List<FundingInstrument>();
        //    fundingInstrumentList.Add(fundInstrument);

        //    // Now create Payer object and assign the fundinginstrument list to the object
        //    Payer payr = new Payer();
        //    payr.funding_instruments = fundingInstrumentList;
        //    payr.payment_method = "credit_card";

        //    // finally create the payment object and assign the payer object & transaction list to it
        //    Payment pymnt = new Payment();
        //    pymnt.intent = "sale";
        //    pymnt.payer = payr;
        //    pymnt.transactions = transactions;

        //    try
        //    {
        //        //getting context from the paypal, basically we are sending the clientID and clientSecret key in this function
        //        //to the get the context from the paypal API to make the payment for which we have created the object above.

        //        //Code for the configuration class is provided next

        //        // Basically, apiContext has a accesstoken which is sent by the paypal to authenticate the payment to facilitator account. An access token could be an alphanumeric string

        //        APIContext apiContext = PaypalConfigruation.GetAPIContext();

        //        // Create is a Payment class function which actually sends the payment details to the paypal API for the payment. The function is passed with the ApiContext which we received above.

        //        Payment createdPayment = pymnt.Create(apiContext);

        //        //if the createdPayment.State is "approved" it means the payment was successfull else not

        //        if (createdPayment.state.ToLower() != "approved")
        //        {
        //            return View("FailureView");
        //        }
        //    }
        //    catch (PayPal.PayPalException ex)
        //    {
        //        Logger.Log("Error: " + ex.Message);
        //        return View("FailureView");
        //    }

        //    return View("SuccessView");
        //}
        //#endregion

        #region thanh toan bang tk paypal
        public ActionResult PaymentWithPaypal()
        {
            //getting the apiContext as earlier
            APIContext apiContext = PaypalConfigruation.GetAPIContext();

            try
            {
                string payerId = Request.Params["PayerID"];

                if (string.IsNullOrEmpty(payerId))
                {
                    //this section will be executed first because PayerID doesn't exist

                    //it is returned by the create function call of the payment class

                    // Creating a payment

                    // baseURL is the url on which paypal sendsback the data.

                    // So we have provided URL of this controller only

                    string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Paypal/PaymentWithPayPal?";

                    //guid we are generating for storing the paymentID received in session

                    //after calling the create function and it is used in the payment execution

                    var guid = Convert.ToString((new Random()).Next(100000));

                    //CreatePayment function gives us the payment approval url

                    //on which payer is redirected for paypal acccount payment

                    var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid);

                    //get links returned from paypal in response to Create function call

                    var links = createdPayment.links.GetEnumerator();

                    string paypalRedirectUrl = null;

                    while (links.MoveNext())
                    {
                        Links lnk = links.Current;

                        if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                        {
                            //saving the payapalredirect URL to which user will be redirected for payment
                            paypalRedirectUrl = lnk.href;
                        }
                    }

                    // saving the paymentID in the key guid
                    Session.Add(guid, createdPayment.id);

                    return(Redirect(paypalRedirectUrl));
                }
                else
                {
                    // This section is executed when we have received all the payments parameters

                    // from the previous call to the function Create

                    // Executing a payment

                    var guid = Request.Params["guid"];

                    var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);

                    if (executedPayment.state.ToLower() != "approved")
                    {
                        return(View("FailureView"));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Error" + ex.Message);
                return(View("FailureView"));
            }

            return(View("SuccessView"));
        }
コード例 #3
0
        public ActionResult PaymentWithPaypal()
        {
            List <GioHang> giohang = Session["giohang"] as List <GioHang>;

            if (giohang == null)
            {
                int d = 0;
            }
            else
            {
                string         sdt       = Request.Form["sdt"];
                string         hoten     = Request.Form["hoten"];
                string         email     = Request.Form["email"];
                string         diachi    = Request.Form["diachi"];
                string         khuyenmai = Request.Form["khuyenmai"];
                List <GioHang> cart      = (List <GioHang>)Session["giohang"];
                if (User.Identity.IsAuthenticated)
                {
                    User    user    = db.Users.FirstOrDefault(x => x.Email == User.Identity.Name);
                    DonHang donHang = new DonHang();
                    donHang.SDT              = sdt;
                    donHang.NgayDat          = DateTime.Now;
                    donHang.GiaVanChuyen     = 20000;
                    donHang.KhuyenMai        = "";
                    donHang.TongTien         = cart.Sum(x => x.Gia * x.SoLuong);
                    donHang.IDUser           = user.IDUser;
                    donHang.TinhTrangDonHang = "Đang duyệt";
                    donHang.LoaiTT           = "Paypal";
                    db.DonHangs.Add(donHang);
                    db.SaveChanges();
                    if (donHang.User.Ranking == 1)
                    {
                        foreach (var item in cart)
                        {
                            ChiTietDonHang chiTietDonHang = new ChiTietDonHang()
                            {
                                SoLuong  = item.SoLuong,
                                TongTien = 0,
                                MaSach   = item.MaSach,
                                MaDH     = donHang.MaDH,
                            };
                            db.ChiTietDonHangs.Add(chiTietDonHang);
                            db.SaveChanges();
                            var sach = db.Saches.FirstOrDefault(x => x.MaSach == item.MaSach);
                            sach.SoLuong         = sach.SoLuong - item.SoLuong;
                            db.Entry(sach).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                        }
                        var dtl = db.Users.FirstOrDefault(x => x.IDUser == donHang.User.IDUser);
                        dtl.DiemTichLuy     = dtl.DiemTichLuy + cart.Sum(x => x.Gia * x.SoLuong);
                        db.Entry(dtl).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                    else if (donHang.User.Ranking == 2)
                    {
                        donHang.TongTien = cart.Sum(x => x.Gia * x.SoLuong) * 95 / 100;
                        db.DonHangs.Add(donHang);
                        db.SaveChanges();
                        foreach (var item in cart)
                        {
                            ChiTietDonHang chiTietDonHang = new ChiTietDonHang()
                            {
                                SoLuong  = item.SoLuong,
                                TongTien = 0,
                                MaSach   = item.MaSach,
                                MaDH     = donHang.MaDH,
                            };
                            db.ChiTietDonHangs.Add(chiTietDonHang);
                            db.SaveChanges();
                            var sach = db.Saches.FirstOrDefault(x => x.MaSach == item.MaSach);
                            sach.SoLuong         = sach.SoLuong - item.SoLuong;
                            db.Entry(sach).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                        }
                        var dtl = db.Users.FirstOrDefault(x => x.IDUser == donHang.User.IDUser);
                        dtl.DiemTichLuy     = dtl.DiemTichLuy + cart.Sum(x => x.Gia * x.SoLuong);
                        db.Entry(dtl).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                    else if (donHang.User.Ranking == 3)
                    {
                        donHang.TongTien = cart.Sum(x => x.Gia * x.SoLuong) * 90 / 100;
                        foreach (var item in cart)
                        {
                            ChiTietDonHang chiTietDonHang = new ChiTietDonHang()
                            {
                                SoLuong  = item.SoLuong,
                                TongTien = 0,
                                MaSach   = item.MaSach,
                                MaDH     = donHang.MaDH,
                            };
                            db.ChiTietDonHangs.Add(chiTietDonHang);
                            db.SaveChanges();
                            var sach = db.Saches.FirstOrDefault(x => x.MaSach == item.MaSach);
                            sach.SoLuong         = sach.SoLuong - item.SoLuong;
                            db.Entry(sach).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                        }
                        var dtl = db.Users.FirstOrDefault(x => x.IDUser == donHang.User.IDUser);
                        dtl.DiemTichLuy     = dtl.DiemTichLuy + cart.Sum(x => x.Gia * x.SoLuong);
                        db.Entry(dtl).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                    else if (donHang.User.Ranking == 4)
                    {
                        donHang.TongTien = cart.Sum(x => x.Gia * x.SoLuong) * 85 / 100;
                        foreach (var item in cart)
                        {
                            ChiTietDonHang chiTietDonHang = new ChiTietDonHang()
                            {
                                SoLuong  = item.SoLuong,
                                TongTien = 0,
                                MaSach   = item.MaSach,
                                MaDH     = donHang.MaDH,
                            };
                            db.ChiTietDonHangs.Add(chiTietDonHang);
                            db.SaveChanges();
                            var sach = db.Saches.FirstOrDefault(x => x.MaSach == item.MaSach);
                            sach.SoLuong         = sach.SoLuong - item.SoLuong;
                            db.Entry(sach).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                        }
                        var dtl = db.Users.FirstOrDefault(x => x.IDUser == donHang.User.IDUser);
                        dtl.DiemTichLuy     = dtl.DiemTichLuy + cart.Sum(x => x.Gia * x.SoLuong);
                        db.Entry(dtl).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                }
                else
                {
                    DonHang donHang = new DonHang();
                    donHang.SDT          = sdt;
                    donHang.NgayDat      = DateTime.Now;
                    donHang.GiaVanChuyen = 0;
                    donHang.KhuyenMai    = "";

                    donHang.TongTien         = cart.Sum(x => x.Gia * x.SoLuong);
                    donHang.TinhTrangDonHang = "Đang duyệt";
                    donHang.LoaiTT           = "Paypal";
                    db.DonHangs.Add(donHang);
                    db.SaveChanges();
                    foreach (var item in cart)
                    {
                        ChiTietDonHang chiTietDonHang = new ChiTietDonHang()
                        {
                            SoLuong  = item.SoLuong,
                            TongTien = 0,
                            MaSach   = item.MaSach,
                            MaDH     = donHang.MaDH,
                        };
                        db.ChiTietDonHangs.Add(chiTietDonHang);
                        db.SaveChanges();
                        var sach = db.Saches.FirstOrDefault(x => x.MaSach == item.MaSach);
                        sach.SoLuong         = sach.SoLuong - item.SoLuong;
                        db.Entry(sach).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                }
            }

            //getting the apiContext as earlier
            APIContext apiContext = PaypalConfigruation.GetAPIContext();

            try
            {
                string payerId = Request.Params["PayerID"];

                if (string.IsNullOrEmpty(payerId))
                {
                    string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Paypal/PaymentWithPayPal?";


                    var guid = Convert.ToString((new Random()).Next(100000));

                    var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid);


                    var links = createdPayment.links.GetEnumerator();

                    string paypalRedirectUrl = null;

                    while (links.MoveNext())
                    {
                        Links lnk = links.Current;

                        if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                        {
                            paypalRedirectUrl = lnk.href;
                        }
                    }

                    // saving the paymentID in the key guid
                    Session.Add(guid, createdPayment.id);

                    return(Redirect(paypalRedirectUrl));
                }
                else
                {
                    var guid = Request.Params["guid"];

                    var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);

                    if (executedPayment.state.ToLower() != "approved")
                    {
                        return(View("FailureView"));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Error" + ex.Message);
                return(View("FailureView"));
            }
            giohang.Clear();
            return(View("SuccessView"));
        }