Esempio n. 1
0
        public void CreateAndExecutePayment()
        {
            AppSettingsReader appSettingsReader = new AppSettingsReader();
            string            clientId          = (string)appSettingsReader.GetValue("clientId", typeof(string));
            string            clientSecret      = (string)appSettingsReader.GetValue("clientSecret", typeof(string));
            var apicontext = PayPalConfiguration.GetAPIContext(clientId, clientSecret);

            PaymentController controller = new PaymentController();
            var          guid            = Convert.ToString((new Random()).Next(100000));
            string       baseURI         = "http://localhost:37256/Paypal/PayWithPayPal?guid=" + guid;
            BouquetOrder bouquet         = new BouquetOrder(1, 1, new Bouquet(6, "gardenia", 2.00, 0));
            var          payment         = controller.CreatePayment(apicontext, baseURI, bouquet);

            var context = new Mock.MockHttpContext();

            context.m_request.m_queryString.Add("PayerID", guid);
            context.m_request.m_queryString.Add("guid", guid);
            context.Session.Add(guid, payment.id);

            controller.ControllerContext = new ControllerContext {
                HttpContext = context, RouteData = new RouteData()
            };
            var result = controller.ExecutePayment(apicontext, guid, payment.id);

            Assert.IsNotNull(apicontext, "PayPal API context is null");
            Assert.IsNotNull(payment.id, "Fail to create payment");
            //must be failed because the data are fake.
            Assert.IsTrue(result.state.ToLower() != "approved", result.state.ToLower());
        }
Esempio n. 2
0
        /// <summary>
        /// Buy a product
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <param name="shipping"></param>
        /// <param name="tax"></param>
        /// <returns></returns>
        public ActionResult CreateOrder(int id, string name, double price, double shipping, double tax)
        {
            var        bouquet    = new Bouquet(id, name, price, shipping);
            var        order      = new BouquetOrder(1, tax, bouquet);
            APIContext apiContext = PayPalConfiguration.GetAPIContext();

            if (apiContext == null)
            {
                var error = new ErrorMessage();
                error.Message = "Configuration error";
                return(View("FailureView", error));
            }
            return(Payment(apiContext, order));
        }
Esempio n. 3
0
        public void Payment()
        {
            AppSettingsReader appSettingsReader = new AppSettingsReader();
            string            clientId          = (string)appSettingsReader.GetValue("clientId", typeof(string));
            string            clientSecret      = (string)appSettingsReader.GetValue("clientSecret", typeof(string));
            var apicontext = PayPalConfiguration.GetAPIContext(clientId, clientSecret);

            PaymentController controller = new PaymentController();

            controller.ControllerContext = new ControllerContext {
                HttpContext = new Mock.MockHttpContext(), RouteData = new RouteData()
            };
            BouquetOrder bouquet = new BouquetOrder(3, 1, new Bouquet(6, "gardenia", 2.00, 0));

            var result    = controller.Payment(apicontext, bouquet);
            var url       = ((RedirectResult)(result)).Url;
            var permanent = ((RedirectResult)(result)).Permanent;

            Assert.IsTrue(permanent == false, "Should have redirected");
            Assert.IsNotNull(result, "url null");
        }
Esempio n. 4
0
        /// <summary>
        /// Create url on which paypal sendsback the data
        /// </summary>
        /// <param name="apiContext"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public ActionResult Payment(APIContext apiContext, BouquetOrder order)
        {
            string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Payment/PayWithPaypal?";
            //guid will be used in the payment execution
            var guid           = Convert.ToString((new Random()).Next(100000));
            var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid, order);

            if (createdPayment == null)
            {
                var error = new ErrorMessage();
                error.Message = "Payment error";
                return(View("FailureView", error));
            }
            //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"))
                {
                    paypalRedirectUrl = lnk.href;
                }
            }
            // saving the paymentID in the key guid
            Session.Add(guid, createdPayment.id);

            if (paypalRedirectUrl == null)
            {
                var error = new ErrorMessage();
                error.Message = "Url not found";
                return(View("FailureView", error));
            }
            return(Redirect(paypalRedirectUrl));
        }
Esempio n. 5
0
        /// <summary>
        /// Create Payment object of PayPal
        /// </summary>
        /// <param name="apiContext"></param>
        /// <param name="redirectUrl"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public Payment CreatePayment(APIContext apiContext, string redirectUrl, BouquetOrder order)
        {
            var itemList = new ItemList()
            {
                items = new List <Item>()
            };

            itemList.items.Add(new Item()
            {
                name     = order.Bouquet.Name,
                currency = "USD",
                price    = order.Bouquet.Price,
                quantity = order.Quatity,
                sku      = Convert.ToString((new Random()).Next(200))
            });
            var payer = new Payer()
            {
                payment_method = "paypal"
            };
            //configure RedirectUrls object
            var redirUrls = new RedirectUrls()
            {
                cancel_url = redirectUrl,
                return_url = redirectUrl
            };
            var details = new Details()
            {
                tax      = order.Tax,
                shipping = order.Bouquet.Shipping,
                subtotal = order.SubTotal
            };
            // Total must be equal to sum of shipping, tax and subtotal.
            var amount = new Amount()
            {
                currency = "USD",
                total    = order.Total,
                details  = details
            };
            var transactionList = new List <Transaction>();

            transactionList.Add(new Transaction()
            {
                description    = order.Bouquet.Name,
                invoice_number = Convert.ToString((new Random()).Next(1000)),
                amount         = amount,
                item_list      = itemList
            });
            this.payment = new Payment()
            {
                intent        = "sale",
                payer         = payer,
                transactions  = transactionList,
                redirect_urls = redirUrls
            };
            try
            {
                return(this.payment.Create(apiContext));
            }
            catch (PayPal.PaymentsException ex)
            {
                Console.WriteLine(ex);
                return(null);
            }
        }
 public BouquetOrder Update(BouquetOrder entity)
 {
     throw new NotImplementedException();
 }
 public BouquetOrder Add(BouquetOrder entity)
 {
     context.BouquetOrders.Add(entity);
     context.SaveChanges();
     return(entity);
 }