Esempio n. 1
0
        public async Task <UOrder_Order> GetCartOrderAsync(string userId)
        {
            using var log = BeginFunction(nameof(CartUserService), nameof(GetCartOrderAsync), userId);
            try
            {
                // HACK: Review logic
                //AssertIsEndUser(userId);
                //await Assert(SecurityPolicy.IsAuthorized, userId).ConfigureAwait(false);

                var ordererReference = CreateOrdererReference.FromUserId(userId);
                var ordererId        = await OrderMicroService.AllocateOrdererAsync(ordererReference);

                var mOrder = await OrderMicroService.GetCartOrderAsync(ordererId).ConfigureAwait(false);

                if (mOrder == null)
                {
                    return(null);
                }

                //var mProjectSnapshots = new List<MProject_ProjectSnapshot>();
                //foreach (var mOrderItem in mOrder.OrderItems)
                //{
                //    var projectSnapshotId = ParseProjectSnapshotId.FromOrderableReference(mOrderItem.OrderableReference);
                //    var mProjectSnapshot = await ProjectMicroService.GetProjectSnapshotAsync(projectSnapshotId).ConfigureAwait(false);
                //    mProjectSnapshots.Add(mProjectSnapshot);
                //}

                var result = OrderUserService.Create.UOrder_Order(mOrder, null);

                log.Result(result);

                return(result);
            }
            catch (Exception ex)
            {
                log.Exception(ex);
                throw;
            }
        }
Esempio n. 2
0
        public async Task <UCart_Cart> GetCartAsync(string userId)
        {
            using var log = BeginFunction(nameof(CartUserService), nameof(GetCartAsync), userId);
            try
            {
                // HACK: Review logic
                //AssertIsEndUser(userId);
                //await Assert(SecurityPolicy.IsAuthorized, userId).ConfigureAwait(false);

                var ordererReference = CreateOrdererReference.FromUserId(userId);
                var ordererId        = await OrderMicroService.AllocateOrdererAsync(ordererReference);

                var mOrder = await OrderMicroService.GetCartOrderAsync(ordererId).ConfigureAwait(false);

                if (mOrder == null)
                {
                    return(null);
                }

                var mProjectOrder = OrderUserService.Create.UOrder_Order(mOrder, null);

                var squareCustomerReference = CreateSquareCustomerReference.FromUserId(userId);
                var squareCustomerId        = await SquareMicroService.AllocateSquareCustomerAsync(squareCustomerReference);

                var squarePaymentReference = CreateSquarePaymentReference.FromOrderId(mOrder.OrderId);
                var squarePaymentId        = await SquareMicroService.AllocateSquarePaymentAsync(squarePaymentReference, squareCustomerId);

                var mSquarePayment = await SquareMicroService.GetSquarePaymentAsync(squarePaymentId);

                var mWebPaymentRequest = mSquarePayment.WebPaymentRequests.Where(r =>
                                                                                 r.WebRequestTypeCode == SquarePaymentWebRequestTypeCodes.Payment &&
                                                                                 r.ProcessingStatusCode != SquareProcessingStatusCodes.Cancelled).SingleOrDefault();

                Cart_PaymentStatus paymentStatus;
                if (mWebPaymentRequest == null)
                {
                    paymentStatus = Cart_PaymentStatus.Required;
                }
                else
                {
                    switch (mWebPaymentRequest.ProcessingStatusCode)
                    {
                    case SquareProcessingStatusCodes.Rejected:
                        paymentStatus = Cart_PaymentStatus.Required;
                        break;

                    case SquareProcessingStatusCodes.Processed:
                        paymentStatus = Cart_PaymentStatus.Complete;
                        break;

                    case SquareProcessingStatusCodes.Pending:
                    case SquareProcessingStatusCodes.Processing:
                    case SquareProcessingStatusCodes.Transmitting:
                    case SquareProcessingStatusCodes.Transmitted:
                    case SquareProcessingStatusCodes.Exception:
                        paymentStatus = Cart_PaymentStatus.InProgress;
                        break;

                    default:
                        // Includes SquarePaymentWebRequestStatusTypes.Cancelled - should have been filtered out above.
                        //
                        throw new InvalidOperationException($"Unknown payment transaction status type {mWebPaymentRequest.ProcessingStatusCode}.");
                    }
                }

                var result = new UCart_Cart()
                {
                    Order         = mProjectOrder,
                    PaymentStatus = paymentStatus
                };

                log.Result(result);

                return(result);
            }
            catch (Exception ex)
            {
                log.Exception(ex);
                throw;
            }
        }