Beispiel #1
0
        public Payment CreatePayment(PaypalPaymentParams paymentParams)
        {
            var items        = CartItemsToPayPalItems(paymentParams.Items, paymentParams.Currency.ToString());
            var redirectUrls = new RedirectUrls()
            {
                cancel_url = paymentParams.RedirectUrl, return_url = paymentParams.RedirectUrl
            };

            var amount = new Amount()
            {
                currency = paymentParams.Currency.ToString(),
                total    = paymentParams.Total.ToString(),
                details  = new Details()
                {
                    tax      = paymentParams.Tax.ToString(),
                    shipping = paymentParams.Shipping.ToString(),
                    subtotal = paymentParams.SubTotal.ToString()
                }
            };

            var transactions = CreateTransaction("GetCatz Cart Checkout", paymentParams.OrderId.ToString(), amount, items);

            foreach (var item in items.items)
            {
                Debug.WriteLine("[" + item.price + ", " + item.currency + ", " + item.name + ", " + item.sku + ", " + item.tax + ", " + item.quantity);
            }

            return(CreatePaymentInstance(transactions, redirectUrls, paymentParams.Context));
        }
Beispiel #2
0
        public async Task <ActionResult> Pay()
        {
            var apiContext = _paypalService.GetApiContext();

            if (_cartService.GetCartSize() == 0)
            {
                return(RedirectToAction("Index"));                                 //Dont process if cart is empty
            }
            var order = new Models.Entities.Order();

            var paymentParams = new PaypalPaymentParams
            {
                Context       = apiContext,
                Currency      = PaypalPaymentParams.PaymentCurrency.EUR,
                Items         = _cartService.GetCartItems(),
                OrderId       = order.Id,
                RedirectUrl   = GetReturnUrl(order.Id.ToString()),
                Shipping      = 1,
                TaxPercentage = 0.2m
            };
            var payment = _paypalService.CreatePayment(paymentParams);

            var user = await _context.Users.Where(u => u.Email.Equals(User.Identity.Name)).FirstAsync();

            order.PaymentId = payment.id;
            order.Total     = paymentParams.Total;
            order.SubTotal  = paymentParams.SubTotal;
            order.Tax       = paymentParams.Tax;
            order.Currency  = paymentParams.Currency;
            order.Shipping  = paymentParams.Shipping;
            InsertPurchaseOptions(order);
            user.Orders.Add(order);
            try
            {
                var redirectUrl = GetPayPalRedirectUrl(payment);
                _context.SaveChanges();
                _cartService.ClearCart();
                return(Redirect(redirectUrl));
            }
            catch (Exception ex)
            {
                while (ModelState.GetEnumerator().MoveNext())
                {
                    Debug.WriteLine(ModelState.GetEnumerator().Current.Value.Errors.First());
                }
                Debug.Write(ex.Message);
                throw ex;
            }
        }