public async Task <IActionResult> GetCallbackUrl(string paymentRequestId)
        {
            if (!paymentRequestId.IsValidPaymentRequestId())
            {
                return(BadRequest(PaymentErrorResponseModel.Create(PaymentErrorType.InvalidPaymentId)));
            }

            try
            {
                GetPaymentCallbackModel callbackInfo =
                    await _payCallbackClient.GetPaymentCallback(_headersHelper.MerchantId, paymentRequestId);

                return(Ok(Mapper.Map <GetPaymentCallbackResponseModel>(callbackInfo)));
            }
            catch (Exception ex)
            {
                _log.Error(ex, null, $"request:{new {paymentRequestId}.ToJson()}");

                if (ex is ErrorResponseException errorEx)
                {
                    if (errorEx.StatusCode == HttpStatusCode.NotFound)
                    {
                        return(NotFound());
                    }
                }
                throw;
            }
        }
        public async Task <IActionResult> SetCallbackUrl(string paymentRequestId, [FromQuery] string callbackUrl)
        {
            if (!paymentRequestId.IsValidPaymentRequestId())
            {
                return(BadRequest(PaymentErrorResponseModel.Create(PaymentErrorType.InvalidPaymentId)));
            }

            if (string.IsNullOrWhiteSpace(callbackUrl))
            {
                return(BadRequest(PaymentErrorResponseModel.Create(PaymentErrorType.InvalidCallbackUrl)));
            }

            try
            {
                await _payCallbackClient.SetPaymentCallback(new SetPaymentCallbackModel
                {
                    MerchantId       = _headersHelper.MerchantId,
                    PaymentRequestId = paymentRequestId,
                    CallbackUrl      = callbackUrl
                });

                return(Ok());
            }
            catch (Exception ex)
            {
                _log.Error(ex, null, $@"request:{
                        new
                        {
                            paymentRequestId,
                            callbackUrl
                        }.ToJson()
                    }");
                throw;
            }
        }
Ejemplo n.º 3
0
        public static PaymentErrorResponseModel ToErrorModel(this RefundErrorResponseException src)
        {
            switch (src.Error.Code)
            {
            case RefundErrorType.Unknown:
                return(PaymentErrorResponseModel.Create(PaymentErrorType.RefundIsNotAvailable));

            case RefundErrorType.InvalidDestinationAddress:
                return(PaymentErrorResponseModel.Create(PaymentErrorType.InvalidDestinationAddress));

            case RefundErrorType.MultitransactionNotSupported:
                return(PaymentErrorResponseModel.Create(PaymentErrorType.RefundIsNotAvailable));

            case RefundErrorType.NoPaymentTransactions:
                return(PaymentErrorResponseModel.Create(PaymentErrorType.NoPaymentTransactions));

            case RefundErrorType.NotAllowedInStatus:
                return(PaymentErrorResponseModel.Create(PaymentErrorType.RefundIsNotAvailable));

            case RefundErrorType.PaymentRequestNotFound:
                return(PaymentErrorResponseModel.Create(PaymentErrorType.InvalidPaymentId));

            default:
                return(PaymentErrorResponseModel.Create(PaymentErrorType.RefundIsNotAvailable));
            }
        }
        public async Task <IActionResult> CreatePaymentRequest([FromBody] CreatePaymentRequestModel request)
        {
            if (string.IsNullOrWhiteSpace(request.SettlementAsset))
            {
                return(BadRequest(PaymentErrorResponseModel.Create(PaymentErrorType.InvalidSettlementAsset)));
            }

            if (string.IsNullOrWhiteSpace(request.PaymentAsset))
            {
                return(BadRequest(PaymentErrorResponseModel.Create(PaymentErrorType.InvalidPaymentAsset)));
            }

            try
            {
                var domainRequest =
                    Mapper.Map <CreatePaymentRequest>(request,
                                                      opt => opt.Items["MerchantId"] = _headersHelper.MerchantId);

                CreatePaymentResponse createResponse =
                    await _paymentRequestService.CreatePaymentRequestAsync(domainRequest);

                PaymentRequestDetailsModel paymentRequestDetails =
                    await _paymentRequestService.GetPaymentRequestDetailsAsync(_headersHelper.MerchantId,
                                                                               createResponse.Id);

                return(Ok(paymentRequestDetails.ToStatusApiModel()));
            }
            catch (InvalidSettlementAssetException ex)
            {
                _log.Error(ex, null, $"request: {request.ToJson()}");

                return(BadRequest(PaymentErrorResponseModel.Create(PaymentErrorType.InvalidSettlementAsset)));
            }
            catch (InvalidPaymentAssetException ex)
            {
                _log.Error(ex, null, $"request: {request.ToJson()}");

                return(BadRequest(PaymentErrorResponseModel.Create(PaymentErrorType.InvalidPaymentAsset)));
            }
            catch (Exception ex)
            {
                _log.Error(ex, null, $"request: {request.ToJson()}");

                throw;
            }
        }
        public async Task <IActionResult> GetPaymentRequestStatus(string paymentRequestId)
        {
            if (!paymentRequestId.IsValidPaymentRequestId())
            {
                return(BadRequest(PaymentErrorResponseModel.Create(PaymentErrorType.InvalidPaymentId)));
            }

            try
            {
                var paymentRequestDetails =
                    await _paymentRequestService.GetPaymentRequestDetailsAsync(_headersHelper.MerchantId,
                                                                               paymentRequestId);

                return(Ok(paymentRequestDetails.ToStatusApiModel()));
            }
            catch (Exception ex)
            {
                _log.Error(ex, null, $"request: {new {paymentRequestId}.ToJson()}");
                throw;
            }
        }
        public async Task <IActionResult> Refund(string paymentRequestId, [FromQuery] string destinationAddress)
        {
            if (!paymentRequestId.IsValidPaymentRequestId())
            {
                return(BadRequest(PaymentErrorResponseModel.Create(PaymentErrorType.InvalidPaymentId)));
            }

            try
            {
                PaymentRequestDetailsModel paymentRequestDetails = await _paymentRequestService.RefundAsync(
                    new RefundRequest
                {
                    MerchantId         = _headersHelper.MerchantId,
                    PaymentRequestId   = paymentRequestId,
                    DestinationAddress = destinationAddress
                });

                return(Ok(paymentRequestDetails.ToStatusApiModel()));
            }
            catch (Exception ex)
            {
                _log.Error(ex, null, $@"request: {
                        new
                        {
                            paymentRequestId,
                            destinationAddress
                        }.ToJson()
                    }");

                if (ex is PayInternal.Client.Exceptions.RefundErrorResponseException refundEx)
                {
                    return(BadRequest(refundEx.ToErrorModel()));
                }

                return(BadRequest(PaymentErrorResponseModel.Create(PaymentErrorType.RefundIsNotAvailable)));
            }
        }