コード例 #1
0
        public async Task <ActionResult <PaymentIn> > AddOrUpdateOrderPayment(string orderNumber, [FromBody] PaymentIn payment)
        {
            if (payment.Sum.Amount == 0)
            {
                throw new StorefrontException("Valid payment amount is required");
            }
            //Need to lock to prevent concurrent access to same object
            using (await AsyncLock.GetLockByKey(GetAsyncLockKey(orderNumber, WorkContext)).LockAsync())
            {
                var orderDto = await _orderApi.GetByNumberAsync(orderNumber, string.Empty);

                var authorizationResult = await _authorizationService.AuthorizeAsync(User, orderDto, CanAccessOrderAuthorizationRequirement.PolicyName);

                if (!authorizationResult.Succeeded)
                {
                    return(Unauthorized());
                }
                var paymentDto = orderDto.InPayments.FirstOrDefault(x => x.Id.EqualsInvariant(payment.Id));
                if (paymentDto == null)
                {
                    paymentDto              = payment.ToOrderPaymentInDto();
                    paymentDto.CustomerId   = WorkContext.CurrentUser.Id;
                    paymentDto.CustomerName = WorkContext.CurrentUser.UserName;
                    paymentDto.Status       = "New";
                    orderDto.InPayments.Add(paymentDto);
                }
                else
                {
                    paymentDto.BillingAddress = payment.BillingAddress != null?payment.BillingAddress.ToOrderAddressDto() : null;

                    paymentDto.Status      = payment.Status;
                    paymentDto.GatewayCode = payment.GatewayCode;
                    paymentDto.PaymentMethod.PaymentMethodType = payment.PaymentMethodType;
                }

                await _orderApi.UpdateOrderAsync(orderDto);

                //Need to return payment with generated id
                orderDto = await _orderApi.GetByIdAsync(orderDto.Id, string.Empty);

                if (string.IsNullOrEmpty(paymentDto.Id))
                {
                    //Because we don't know the new payment id we need to get latest payment with same gateway code
                    paymentDto = orderDto.InPayments.Where(x => x.GatewayCode.EqualsInvariant(payment.GatewayCode)).OrderByDescending(x => x.CreatedDate).FirstOrDefault();
                }
                return(paymentDto.ToOrderInPayment(WorkContext.AllCurrencies, WorkContext.CurrentLanguage));
            }
        }
コード例 #2
0
        public void CreateNewPayment_CheckThatNewPaymentStored()
        {
            var existingOrderNumber = "CU1508131823004";
            var orderApi            = GetOrderApiClient();
            var orderDto            = orderApi.OrderModule.GetByNumber(existingOrderNumber);

            var currency = new Currency(new Language("en-US"), "USD");
            var payment  = new PaymentIn(currency)
            {
                GatewayCode = "DefaultManualPaymentMethod",
                Sum         = new Money(1219.19, currency),
                CustomerId  = GetTestWorkContext().CurrentCustomer.Id,
                Purpose     = "test payment"
            };

            var initialCount = orderDto.InPayments.Count;

            orderDto.InPayments.Add(payment.ToOrderPaymentInDto());
            orderApi.OrderModule.Update(orderDto);
            Assert.Equal(initialCount + 1, orderDto.InPayments.Count);
        }
コード例 #3
0
        public async Task <ActionResult> AddOrUpdateOrderPayment(string orderNumber, PaymentIn payment)
        {
            if (payment.Sum.Amount == 0)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Valid payment amount is required"));
            }
            //Need to lock to prevent concurrent access to same object
            using (await AsyncLock.GetLockByKey(GetAsyncLockKey(orderNumber, WorkContext)).LockAsync())
            {
                var orderDto = await GetOrderDtoByNumber(orderNumber);

                var paymentDto = orderDto.InPayments.FirstOrDefault(x => x.Id.EqualsInvariant(payment.Id));
                if (paymentDto == null)
                {
                    paymentDto              = payment.ToOrderPaymentInDto();
                    paymentDto.CustomerId   = WorkContext.CurrentCustomer.Id;
                    paymentDto.CustomerName = WorkContext.CurrentCustomer.FullName;
                    paymentDto.Status       = "New";
                    orderDto.InPayments.Add((orderModel.PaymentIn)paymentDto);
                }
                else
                {
                    paymentDto.BillingAddress = payment.BillingAddress != null?payment.BillingAddress.ToOrderAddressDto() : null;
                }

                await _orderApi.OrderModule.UpdateAsync(orderDto);

                //Need to return payment with generated id
                orderDto = await _orderApi.OrderModule.GetByIdAsync(orderDto.Id);

                if (string.IsNullOrEmpty(paymentDto.Id))
                {
                    //Because we don't know the new payment id we need to get latest payment with same gateway code
                    paymentDto = orderDto.InPayments.Where(x => x.GatewayCode.EqualsInvariant(payment.GatewayCode)).OrderByDescending(x => x.CreatedDate).FirstOrDefault();
                }
                return(Json(paymentDto));
            }
        }