Ejemplo n.º 1
0
        public IHttpActionResult CreateRefund(PayPalRefund refund)
        {
            if (refund == null)
                return NotFound();

            refund.ApplicationUserId = this.User.Identity.GetUserId();

            if (ModelState.IsValid)
            {
                refund.Finished = "Not Approved";
                db.Refunds.Add(refund);
                db.SaveChanges();
                MailBuliderCreateRefund(refund.ApplicationUserId);
                return Ok();
            }
            return BadRequest(ModelState);
        }
Ejemplo n.º 2
0
        public IHttpActionResult RefundPayment(string cartId)
        {
            var cart = db.Carts.Find(Int32.Parse(cartId));

            if (cart == null)
                return NotFound();            
            if (cart.TimeOfPurchase.AddDays(7) > DateTime.Now)  //if coupons have been purchased in last 7 
            {                                                   //days then make refund

                if (cart.PaymentId == string.Empty)
                    MakeRefund(cart.Id);

                var apiContext = GetApiContext();

                if (apiContext == null)
                    return StatusCode(HttpStatusCode.NotAcceptable);

                try
                {
                    var payment = Payment.Get(apiContext, cart.PaymentId);  //find payment with selected paymenId

                    var sale = payment.transactions[0].related_resources[0].sale;


                    var refund = new Refund()  //create new refund to be sent to paypal
                    {
                        sale_id = cart.PaymentId,
                        amount = new Amount()
                        {
                            currency = "USD",
                            total = cart.TotalPrice.ToString()
                        }
                    };
                    var response = sale.Refund(apiContext, refund);  //send request for refund

                    var paypalRefund = new PayPalRefund()  //new object refund in our database to be saved
                    {
                        ApplicationUserId = cart.ApplicationUserId,
                        Option = "Automatic Refund Done in 7 Days",
                        Content = "",
                        PaymentId = cart.PaymentId,
                        Finished = "Approved",
                        CartId = cart.Id
                    };

                    cart.Purchased = "Refunded";  //set cart state to Refunded


                    db.Refunds.Add(paypalRefund);
                    db.SaveChanges();
                    Mail mail = db.Mails.Where(x => x.Subject == "Refound").First();
                    MailBulider(cart.ApplicationUserId, cart.Id, mail, paypalRefund.Finished, cart.PaymentId);
                    MakeRefund(cart.Id);
                    return Ok();
                }
                catch
                {
                    return NotFound();
                }
            }
            return BadRequest(cart.Id.ToString() + "," + cart.PaymentId); //if it has passed more then 7 days, user has to 
        }                                   //send request to admin to approve him/her refund