Ejemplo n.º 1
0
        public async Task <JsonResult> ProcessYachtPayment([FromBody] YachtPaymentInfo formData)
        {
            var    charteringUniqueId   = string.Empty;
            var    paymentProvider      = "STRIPE";
            double totalBillAmount      = 0;
            var    currencyCode         = string.Empty;
            var    aqToken              = string.Empty;
            var    transactionId        = string.Empty;
            var    paymentToken         = string.Empty;
            var    stripePaymentRequest = new RequestPaymentStripe();
            var    createdUser          = string.Empty;

            // Validate Form Data
            var isSuccess = (formData != null) && ModelState.IsValid;

            // Calculate total price and save chartering info
            if (isSuccess)
            {
                charteringUniqueId = await SaveCharterInformation(formData.data, paymentProvider);

                isSuccess = (string.IsNullOrEmpty(charteringUniqueId) == false);
            }

            // Get total price and culture code
            if (isSuccess)
            {
                var yachtCharteringViewModel = await GetCharterInformation(charteringUniqueId);

                totalBillAmount = yachtCharteringViewModel.PrepaidValue;
                currencyCode    = yachtCharteringViewModel.CurrencyCode;
            }

            // Get aq token
            if (isSuccess)
            {
                aqToken = await GetAqToken();

                isSuccess = (string.IsNullOrEmpty(aqToken) == false);
            }

            // Create transaction
            if (isSuccess)
            {
                transactionId = await CreateTransaction(stripePaymentRequest, charteringUniqueId, totalBillAmount, currencyCode, aqToken);

                isSuccess = (string.IsNullOrEmpty(transactionId) == false);
            }

            // Get card token from api
            if (isSuccess)
            {
                paymentToken = await GetCardToken(formData, aqToken);

                isSuccess = (string.IsNullOrEmpty(paymentToken) == false);
            }

            // Process payment api
            if (isSuccess)
            {
                var request = new RequestProccessPaymentStripe()
                {
                    ID               = stripePaymentRequest.ID,
                    OrderId          = charteringUniqueId,
                    OrderAmount      = (decimal)totalBillAmount,
                    PaymentMethod    = "Full",
                    Currency         = currencyCode,
                    Description      = "",
                    BackUrl          = "/Yacht",
                    PaymentCardToken = paymentToken
                };
                isSuccess = await ProcessStripePayment(request, aqToken, "STRIPE");
            }

            #region Get current transaction and update status, log
            // Get current transaction
            if (isSuccess)
            {
                var response = await _yachtPaymentService.GetTransaction <ResponseTransactionResultStripe>(transactionId, aqToken);

                isSuccess   = response.IsValidResponse();
                createdUser = response.ResponseData.Data.CreatedUser;
            }

            // Update transaction status
            if (isSuccess)
            {
                var response = await _yachtCharteringService.UpdateCharterStatus(charteringUniqueId, Convert.ToInt32(YachtCharterStatusEnum.Paid));

                isSuccess = response.IsValidResponse();
            }

            // Update payment log
            if (isSuccess)
            {
                // Get current payment log
                var response =
                    await _yachtCharteringService.GetCharteringPaymentLogByCharteringUniqueId(charteringUniqueId, Convert.ToInt32(YachtCharterStatusEnum.Waiting));

                isSuccess = response.IsValidResponse();

                if (isSuccess)
                {
                    var paymentLog = response.ResponseData;
                    paymentLog.PaymentBy  = createdUser;
                    paymentLog.PaymentRef = transactionId;
                    paymentLog.StatusFid  = Convert.ToInt32(YachtCharterStatusEnum.Paid);

                    // Update payment log
                    var resultUpdate =
                        await _yachtCharteringService.UpdateCharterPrivatePaymentLogByCharteringUniqueId(paymentLog, charteringUniqueId);

                    isSuccess = resultUpdate.IsValidResponse();
                }
            }
            #endregion

            return(Json(new { Success = isSuccess }));
        }