Exemple #1
0
 public ReservationRepository(PoolReservationEntities context, UnitOfWork unitOfWork) : base(context, unitOfWork)
 {
 }
Exemple #2
0
 public UnitOfWork(PoolReservationEntities context)
 {
     this.dbContext = context;
     this.InitializeFields();
 }
Exemple #3
0
 public UnitOfWork()
 {
     this.dbContext = new PoolReservationEntities();
     this.InitializeFields();
 }
 public MiscellaneousRepository(PoolReservationEntities context, UnitOfWork unitOfWork) : base(context, unitOfWork)
 {
 }
Exemple #5
0
 public Repository(PoolReservationEntities context, UnitOfWork unitOfWork)
 {
     this.dbContext  = context;
     this.unitOfWork = unitOfWork;
 }
        public HttpResponseMessage ProcessTransaction(IncomingProcessTransaction model)
        {
            return(ErrorFactory.Handle(() =>
            {
                var userId = User?.Identity?.GetUserId();

                if (string.IsNullOrWhiteSpace(userId))
                {
                    throw new Exception();
                }

                var context = new PoolReservationEntities();

                using (var unitOfWork = new UnitOfWork(context))
                {
                    PrepareAndGetReservationForProcessing_Result reservation = null;

                    try
                    {
                        reservation = context.PrepareAndGetReservationForProcessing(model.ReservationId)?.FirstOrDefault();
                    }
                    catch (Exception)
                    {
                        context.ChangeProcessingStatusToPending(model.ReservationId);
                    }



                    if (reservation == null)
                    {
                        try
                        {
                            context.ChangeProcessingStatusToPending(model.ReservationId);
                        }
                        catch (Exception)
                        {
                        }

                        return JsonFactory.CreateJsonMessage(null, HttpStatusCode.NotFound, this.Request);
                    }

                    if (reservation.StatusId != Convert.ToInt32(ReservationGroupStatusEnum.PROCESSING))
                    {
                        try
                        {
                            context.ChangeProcessingStatusToPending(model.ReservationId);
                        }
                        catch (Exception)
                        {
                        }

                        return JsonFactory.CreateJsonMessage(new OutgoingMessage {
                            Action = "wrongStatus"
                        }, HttpStatusCode.NotFound, this.Request);
                    }

                    ReservationGroup reservationDBObject = null;


                    try
                    {
                        reservationDBObject = unitOfWork.Reservations.GetReservationWithItems(userId, model.ReservationId);

                        if (reservationDBObject == null)
                        {
                            return JsonFactory.CreateJsonMessage(null, HttpStatusCode.NotFound, this.Request);
                        }
                    }
                    catch (Exception)
                    {
                        context.ChangeProcessingStatusToPending(model.ReservationId);
                    }


                    if (reservationDBObject.StatusId != Convert.ToInt32(ReservationGroupStatusEnum.PROCESSING))
                    {
                        try
                        {
                            context.ChangeProcessingStatusToPending(model.ReservationId);
                        }
                        catch (Exception)
                        {
                        }

                        return JsonFactory.CreateJsonMessage(new OutgoingMessage {
                            Action = "wrongStatus"
                        }, HttpStatusCode.NotFound, this.Request);
                    }

                    ReservationTransaction resTransaction = null;
                    int stripeModifiedPriceInCents = 0;
                    try
                    {
                        decimal priceToCharge = 0M;

                        foreach (var item in reservationDBObject.ReserveItems)
                        {
                            if (item.IsDeleted == true)
                            {
                                continue;
                            }

                            priceToCharge += item.FinalPrice;
                        }



                        decimal stripeModifiedPrice = priceToCharge * 100.0M;

                        stripeModifiedPriceInCents = (int)Math.Round(stripeModifiedPrice); //Must Update Repo If Changed.  Rounds to the nearest penny.

                        if (stripeModifiedPriceInCents <= 50)
                        {
                            return JsonFactory.CreateJsonMessage(new OutgoingMessage {
                                Action = "noPriceToCharge"
                            }, HttpStatusCode.NotFound, this.Request);
                        }

                        resTransaction = unitOfWork.Reservations.AddStripeChargeToPendingReservation(userId, userId, model.ReservationId, model.StripeTokenId, priceToCharge);

                        unitOfWork.Complete();
                    }
                    catch (Exception e)
                    {
                        context.ChangeProcessingStatusToPending(model.ReservationId);
                    }



                    string chargeId = null;
                    try
                    {
                        if (resTransaction == null)
                        {
                            throw new Exception();
                        }

                        var myCharge = new StripeChargeCreateOptions();

                        // always set these properties
                        myCharge.Amount = stripeModifiedPriceInCents;
                        myCharge.Currency = "usd";

                        // set this if you want to
                        myCharge.Description = "JustMosey Reservation";

                        myCharge.SourceTokenOrExistingSourceId = model.StripeTokenId;

                        // (not required) set this to false if you don't want to capture the charge yet - requires you call capture later
                        myCharge.Capture = true;

                        var chargeService = new StripeChargeService();
                        StripeCharge stripeCharge = chargeService.Create(myCharge);

                        chargeId = stripeCharge.Id;

                        unitOfWork.Reservations.UpdateStripeSuccessfulChargeOnPendingReservation(userId, userId, resTransaction.Id, chargeId);

                        unitOfWork.Complete();
                    }
                    catch (Exception e)
                    {
                        try
                        {
                            var refundService = new StripeRefundService();

                            StripeRefund refund = refundService.Create(chargeId, new StripeRefundCreateOptions()
                            {
                                Amount = stripeModifiedPriceInCents,
                                Reason = StripeRefundReasons.Unknown
                            });
                        }
                        catch (Exception)
                        {
                        }

                        try
                        {
                            unitOfWork.Reservations.RefundStripeSuccessfulChargeOnPendingReservation(userId, userId, resTransaction.Id);
                            unitOfWork.Complete();
                        }
                        catch (Exception)
                        {
                        }

                        try
                        {
                        }
                        catch (Exception)
                        {
                            context.ChangeProcessingStatusToPending(model.ReservationId);
                        }

                        return JsonFactory.CreateJsonMessage(new OutgoingMessage {
                            Action = "unknownErrorAfterProcessing"
                        }, HttpStatusCode.InternalServerError, this.Request);
                    }

                    try
                    {
                        var updatedReservationDBObject = unitOfWork.Reservations.GetReservationWithItems(userId, model.ReservationId);

                        var outgoingRes = OutgoingReservationGroup.Parse(updatedReservationDBObject);

                        return JsonFactory.CreateJsonMessage(outgoingRes, HttpStatusCode.OK, this.Request);
                    }
                    catch (Exception)
                    {
                        return JsonFactory.CreateJsonMessage(null, HttpStatusCode.OK, this.Request);
                    }
                }
            }, this.Request));
        }