示例#1
0
        public async Task <IActionResult> Execute(string id, string subject)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException(nameof(id));
            }

            var order = await _orderRepository.Get(id);

            if (order == null)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Server, ErrorDescriptions.TheOrderDoesntExist);
                return(_controllerHelper.BuildResponse(System.Net.HttpStatusCode.NotFound, error));
            }

            if (order.Subject != subject && order.SellerId != subject)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Server, ErrorDescriptions.TheOrderCannotBeAccessedByYou);
                return(_controllerHelper.BuildResponse(System.Net.HttpStatusCode.BadRequest, error));
            }

            if (order.OrderPayment == null)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Server, ErrorDescriptions.TheOrderDoesntHavePayment);
                return(_controllerHelper.BuildResponse(System.Net.HttpStatusCode.BadRequest, error));
            }

            _halResponseBuilder.AddLinks(l => l.AddSelf("/" + Constants.RouteNames.OrderTransaction.Replace("{id}", id)));
            if (order.OrderPayment.PaymentMethod != OrderPayments.Paypal)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Server, ErrorDescriptions.ThePaymentMethodIsNotSupported);
                return(_controllerHelper.BuildResponse(System.Net.HttpStatusCode.InternalServerError, error));
            }

            var token = await _paypalOauthClient.GetAccessToken(_settingsProvider.GetPaypalClientId(),
                                                                _settingsProvider.GetPaypalClientSecret(),
                                                                new PaypalOauthClientOptions
            {
                ApplicationMode = PaypalApplicationModes.sandbox
            });

            var payment = await _paypalClient.GetPayment(new GetPaymentParameter
            {
                AccessToken = token.AccessToken,
                PaymentId   = order.OrderPayment.TransactionId
            });

            return(new OkObjectResult(_responseBuilder.GetPaypalTransaction(payment)));
        }