Exemplo n.º 1
0
        public void CreateTestOrder()
        {
            using (var _dbcontext = new DatabaseContext())
            {
                try
                {
                    Order od = new Order()
                    {
                        OrderDate            = DateTime.Now,
                        Total                = 123,
                        PaymentTransactionId = orderFacade.GeneratePaymentTransactionId(),
                        HasBeenShipped       = false,
                        CustomerID           = "1-ACB",
                        FirstName            = "TestS",
                        LastName             = "Name",
                        Address              = "Street 123",
                        City         = "England",
                        PostalCode   = "1234",
                        Country      = "Denmark",
                        Phone        = "12345678",
                        Email        = "*****@*****.**",
                        OrderDetails = new List <OrderDetail>
                        {
                            new OrderDetail()
                            {
                                Quantity = 1, ProductId = "10CDQ80N", UnitPrice = 400
                            },
                            new OrderDetail()
                            {
                                Quantity = 1, ProductId = "5MCR-156A", UnitPrice = 552
                            },
                        },
                    };

                    _dbcontext.Orders.Add(od);
                    _dbcontext.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                          eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                              ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    throw;
                }
            }
        }
Exemplo n.º 2
0
        public ActionResult PayWithCard(CheckoutOrderDTO orderInfo)
        {
            var cartList = shoppingCartFacade.GetCartItems();
            var itemList = new ItemList()
            {
                items = new List <Item>()
            };

            //The item's in the cart, and the items we're sending to Paypal is a little different, so we have to pass the cartitems into the paypal item's
            //We do this with a foreach method
            foreach (var x in cartList)
            {
                var pItem = new Item()
                {
                    name     = x.Product.ProductName,
                    currency = "USD",
                    price    = x.Product.UnitPrice.ToString(),
                    quantity = x.Quantity.ToString(),
                    sku      = x.Product.ProductId
                };
                itemList.items.Add(pItem);
            }



            //Address for the payment
            Address billingAddress = new Address();

            billingAddress.city         = orderInfo.Order.City;
            billingAddress.country_code = "US"; //these should not be hard coded
            billingAddress.line1        = orderInfo.Order.Address;
            billingAddress.postal_code  = orderInfo.Order.PostalCode;
            billingAddress.state        = "NY"; //these should not be hard coded


            //Now Create an object of credit card and add above details to it
            //Please replace your credit card details over here which you got from paypal
            CreditCard crdtCard = new CreditCard();

            crdtCard.billing_address = billingAddress;
            crdtCard.cvv2            = orderInfo.Card.cvv2;         //card cvv2 number
            crdtCard.expire_month    = orderInfo.Card.expire_month; //card expire date
            crdtCard.expire_year     = orderInfo.Card.expire_year;  //card expire year
            crdtCard.first_name      = orderInfo.Card.first_name;
            crdtCard.last_name       = orderInfo.Card.last_name;
            crdtCard.number          = orderInfo.Card.number; //enter your credit card number here
            crdtCard.type            = orderInfo.Card.type;   //credit card type here paypal allows 4 types

            // similar as we did for credit card, do here and create details object
            decimal cartTotal  = shoppingCartFacade.GetTotal();
            decimal taxCalc    = ((10 * cartTotal) / 100); //We pay 10% in tax...
            decimal shipCost   = 0;                        //Free shipping
            decimal priceTotal = (cartTotal + taxCalc + shipCost);

            // Specify details of your payment amount.
            var details = new Details()
            {
                tax      = taxCalc.ToString(),
                subtotal = cartTotal.ToString(),
                shipping = shipCost.ToString(),
            };

            // similar as we did for credit card, do here and create amount object
            var amount = new Amount()
            {
                currency = "USD",
                total    = priceTotal.ToString(), // Total must be equal to sum of shipping, tax and subtotal.
                details  = details
            };


            // Now make a transaction object and assign the Amount object
            Transaction tran = new Transaction();

            tran.amount         = amount;
            tran.description    = "SQMY Webshop Stransaction";
            tran.item_list      = itemList;
            tran.invoice_number = Convert.ToString((new Random()).Next(100000));

            // Now, we have to make a list of transaction and add the transactions 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.

                //Basically, apiContext object 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 = Configuration.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 successful else not
                if (createdPayment.state.ToLower() == "approved")
                {
                    try
                    {
                        using (var _dbContext = new DatabaseContext())
                        {
                            MembershipUser customer = Membership.GetUser();

                            //We have to move the CartItem's into a OrderDetails list, like we did with the Api.PayPal item's
                            var OrderDetailList = new List <OrderDetail>();
                            foreach (var x in cartList)
                            {
                                var orderDetail = new OrderDetail()
                                {
                                    ProductId = x.ProductId,
                                    Quantity  = x.Quantity,
                                    UnitPrice = productFacade.GetProductPriceById(x.ProductId)
                                };
                                OrderDetailList.Add(orderDetail);
                            }

                            //Both the Paypal.API and ClassLibrary have a class named Order(), so we have the specify the namespace
                            var CustomerOrder = orderInfo.Order;
                            CustomerOrder.PaymentTransactionId = orderFacade.GeneratePaymentTransactionId();
                            CustomerOrder.CustomerID           = customer.ProviderUserKey.ToString();
                            CustomerOrder.OrderDetails         = OrderDetailList;
                            CustomerOrder.Total     = priceTotal;
                            CustomerOrder.OrderDate = DateTime.Now;

                            _dbContext.Orders.Add(CustomerOrder);
                            _dbContext.SaveChanges();

                            //Clear the cart and send us to successview
                            shoppingCartFacade.EmptyCart();
                            return(View("CheckoutSuccess"));
                        }
                    }
                    catch (Exception ex)
                    {
                        Models.Paypal.Logger.Log("Error: " + ex.Message);
                        return(PartialView("~/views/partials/ErrorPage.cshtml", new Models.ErrorPageModel {
                            ErrorMessage = "Something went wrong..."
                        }));
                    }
                }
                else
                {
                    return(PartialView("~/views/partials/ErrorPage.cshtml", new Models.ErrorPageModel {
                        ErrorMessage = "Payment Failed"
                    }));
                }
            }
            catch (PayPal.PaymentsException ex)
            {
                Models.Paypal.Logger.Log("Error: " + ex.Message);
                return(PartialView("~/views/partials/ErrorPage.cshtml", new Models.ErrorPageModel {
                    ErrorMessage = ex.Details.message
                }));
            }
        }