public async Task <IActionResult> Go(Guid id)
        {
            _logger.LogDebug("Pay - Id : " + id);
            var transaction = await _transactionRepository.Get(id);

            if (transaction == null ||
                transaction.Status == (byte)TransactionStatusEnum.Cancel)
            {
                return(View("Error", "لینک وارد شده صحیح نیست."));
            }

            if (transaction.Status == (byte)TransactionStatusEnum.Success ||
                transaction.Status == (byte)TransactionStatusEnum.BankOk)
            {
                return(View("Error", "این تراکنش قبلا پرداخت شده است."));
            }

            if (transaction.ExpirationTime.HasValue &&
                transaction.ExpirationTime < DateTime.Now)
            {
                var persianCalendar = new PersianCalendar();
                var date            =
                    $"{persianCalendar.GetYear(transaction.ExpirationTime.Value)}/{persianCalendar.GetMonth(transaction.ExpirationTime.Value)}/" +
                    $"{persianCalendar.GetDayOfMonth(transaction.ExpirationTime.Value)}" +
                    $" - {transaction.ExpirationTime.Value.Hour}:{transaction.ExpirationTime.Value.Minute}";
                return(View("Error",
                            $"این لینک تا تاریخ {date} معتبر بوده است، لطفا از پشتیبانی تقاضای لینک جدید بفرمایید."));
            }

            var applicationBank = await _applicationBankRepository.GetFirstBy(x =>
                                                                              x.ApplicationId == transaction.ApplicationId && x.BankId == transaction.BankId);

            switch (transaction.Bank.Code)
            {
            case (byte)BankCodeEnum.Parsian:
                _logger.LogDebug("Parsian");
                if (!string.IsNullOrWhiteSpace(transaction.BankTrackCode))
                {
                    var url = string.Format(applicationBank.Bank.PostUrl, transaction.BankTrackCode);
                    return(Redirect(url));
                }

                var pinParam = applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "ParsianPIN");

                var parsianGateway = new ParsianGateway(pinParam.ParamValue);
                var resp           = await parsianGateway.SalePaymentRequestasync(Convert.ToInt64(transaction.Amount),
                                                                                  Convert.ToInt64(transaction.UserTrackCode), transaction.BankRedirectUrl, transaction.Id.ToString());

                if (resp.Body.SalePaymentRequestResult.Status == 0)
                {
                    _logger.LogDebug("authority after callback", resp.Body.SalePaymentRequestResult.Token);
                    transaction.BankTrackCode = resp.Body.SalePaymentRequestResult.Token.ToString();
                    await _transactionRepository.Update(transaction);

                    var url = string.Format(applicationBank.Bank.PostUrl, transaction.BankTrackCode);
                    return(Redirect(url));
                }
                else
                {
                    _logger.LogDebug("Critical Error: Payment Error. StatusCode={0} - Message={1}", resp.Body.SalePaymentRequestResult.Status, ParsianErrors.GetResult(resp.Body.SalePaymentRequestResult.Status).Message);
                    transaction.BankTrackCode = resp.Body.SalePaymentRequestResult.Token.ToString();
                    switch (resp.Body.SalePaymentRequestResult.Status)
                    {
                    case -126:
                        transaction.ErrorCode        = (byte)ErrorCodeEnum.InvalidPin;
                        transaction.BankErrorMessage = "پین فروشنده درست نیست";
                        break;

                    case 2:
                    case -1533:
                    case -1536:
                        transaction.ErrorCode        = (byte)ErrorCodeEnum.OperationAlreadyDone;
                        transaction.BankErrorMessage = "عملیات قبلا با موفقیت انجام شده است";
                        break;

                    case -112:
                        transaction.ErrorCode        = (byte)ErrorCodeEnum.UserTrackCodeIsInvalid;
                        transaction.BankErrorMessage = "شماره تراكنش فروشنده درست نیست";
                        break;
                    }

                    await _transactionRepository.Update(transaction);

                    return(View("Error", transaction.BankErrorMessage));
                }

            case (byte)BankCodeEnum.Mellat:
                _logger.LogDebug("mellat");
                var terminalParam = applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "MellatTerminalId");
                var userNameParam = applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "MellatUserName");
                var passwordParam = applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "MellatPassword");

                var mellatGateway = new MellatGateway(terminalParam.ParamValue, userNameParam.ParamValue, passwordParam.ParamValue);
                var mellatResp    = await mellatGateway.BpPayRequest(Convert.ToInt32(transaction.Amount),
                                                                     Convert.ToInt32(transaction.UserTrackCode), transaction.BankRedirectUrl);

                _logger.LogError((mellatResp == null).ToString());

                if (mellatResp != null)
                {
                    _logger.LogError(mellatResp.Body.@return);

                    var resultArray = [email protected](',');
                    if (resultArray[0] == "0")
                    {
                        var refId = resultArray[1];
                        transaction.BankTrackCode = refId;
                        await _transactionRepository.Update(transaction);

                        ViewBag.RefId = refId;
                        return(View("Mellat", transaction));
                    }

                    _logger.LogError("Critical Error: Payment Error. StatusCode={0}", resultArray[0]);
                    transaction.ErrorCode        = (byte)MellatHelper.ErrorResult(resultArray[0]);
                    transaction.BankErrorCode    = Convert.ToInt32(resultArray[0]);
                    transaction.BankErrorMessage = MellatHelper.MellatResult(resultArray[0]);
                    await _transactionRepository.Update(transaction);

                    return(View("Error", transaction.BankErrorMessage));
                }
                else
                {
                    _logger.LogError("Critical Error: Payment Error. StatusCode=no resp");

                    transaction.BankErrorMessage = "امکان اتصال به درگاه بانک وجود ندارد";
                    await _transactionRepository.Update(transaction);

                    return(View("Error", transaction.BankErrorMessage));
                }

            case (byte)BankCodeEnum.Efarda:
                _logger.LogDebug("Efarda");

                var EfardaServiceId =
                    applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "serviceId");
                var EfardaUsername =
                    applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "userName");
                var EfardaPassword =
                    applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "password");

                EfardaGateway efardaGateway = new EfardaGateway(_logger, EfardaUsername.ParamValue, EfardaPassword.ParamValue, EfardaServiceId.ParamValue);
                var           traceNumber   = await efardaGateway.GetTraceId(transaction.UserTrackCode, transaction.Amount.ToString("###############0"), transaction.BankRedirectUrl, transaction.Mobile.HasValue?transaction.Mobile.Value.ToString() : string.Empty);

                transaction.BankTrackCode = traceNumber;
                await _transactionRepository.Update(transaction);

                ViewBag.Username    = EfardaUsername.ParamValue;
                ViewBag.traceNumber = traceNumber;

                return(View("Efarda", transaction));

            case (byte)BankCodeEnum.Saman:
            default:
                _logger.LogDebug("Saman");
                ViewBag.Params = applicationBank.ApplicationBankParams.ToList();
                return(View("Saman", transaction));
            }
        }
        public async Task <VerifyTransactionResponseModel> VerifyTransaction(Transaction transaction)
        {
            _logger.LogError("Verify Mellat");
            var verifyTransactionResult = new VerifyTransactionResponseModel();

            var applicationBank = await _applicationBankRepository.GetFirstBy(x =>
                                                                              x.ApplicationId == transaction.ApplicationId && x.BankId == transaction.BankId);

            var terminalId = applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "MellatTerminalId")
                             ?.ParamValue;
            var userName = applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "MellatUserName")
                           ?.ParamValue;
            var password = applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "MellatPassword")
                           ?.ParamValue;

            var trans = await _transactionRepository.GetFirstBy(x => x.Id == transaction.Id);

            var saleOrderId     = Convert.ToInt64(trans.UserTrackCode);
            var saleReferenceId = Convert.ToInt64(trans.BankTrackCode);

            var mellatGateway = new MellatGateway(terminalId, userName, password);
            var verifyResult  = (await mellatGateway.BpVerifyRequest(saleOrderId, saleReferenceId)).Body.@return;
            var message       = "تراکنش بازگشت داده شد";

            if (!string.IsNullOrEmpty(verifyResult))
            {
                if (verifyResult == "0")
                {
                    var inquiryResult = (await mellatGateway.BpInquiryRequest(saleOrderId, saleReferenceId)).Body
                                        .@return;
                    if (inquiryResult == "0")
                    {
                        //ViewBag.Message = "پرداخت با موفقیت انجام شد.";
                        //ViewBag.SaleReferenceId = SaleReferenceId;
                        // پرداخت نهایی
                        // تایید پرداخت
                        var settleResult = (await mellatGateway.BpSettleRequest(saleOrderId, saleReferenceId)).Body
                                           .@return;
                        if (settleResult != null)
                        {
                            if (settleResult == "0" || settleResult == "45")
                            {
                                //تراکنش تایید و ستل شده است
                                _logger.LogError("Verify Done!!!!!");

                                message                = "بانک صحت رسید دیجیتالی شما را تصدیق نمود. فرایند خرید تکمیل گشت";
                                message               += "<br/>" + " شماره رسید : " + transaction.BankTrackCode;
                                transaction.Status     = (byte)TransactionStatusEnum.Success;
                                transaction.ModifiedOn = DateTime.Now;
                                transaction.ModifiedBy = 1;
                                await _transactionRepository.Update(transaction);

                                verifyTransactionResult.Status    = true;
                                verifyTransactionResult.ErrorCode = (byte)ErrorCodeEnum.NoError;
                                verifyTransactionResult.Message   = message;
                                _logger.LogError("Mellat Verify Done!");
                                return(verifyTransactionResult);
                            }

                            //تراکنش تایید شده ولی ستل نشده است
                            _logger.LogError("Verify Done!!!!!");

                            message                = "بانک صحت رسید دیجیتالی شما را تصدیق نمود. فرایند خرید تکمیل گشت";
                            message               += "<br/>" + " شماره رسید : " + transaction.BankTrackCode;
                            transaction.Status     = (byte)TransactionStatusEnum.WaitingForSettle;
                            transaction.ModifiedOn = DateTime.Now;
                            transaction.ModifiedBy = 1;
                            await _transactionRepository.Update(transaction);

                            verifyTransactionResult.Status    = true;
                            verifyTransactionResult.ErrorCode = (byte)ErrorCodeEnum.NoError;
                            verifyTransactionResult.Message   = message;
                            _logger.LogError("Mellat Verify Done!");
                            return(verifyTransactionResult);
                        }
                    }
                    else
                    {
                        //string Rvresult;
                        //عملیات برگشت دادن مبلغ
                        var result = (await mellatGateway.BpReversalRequest(saleOrderId, saleReferenceId)).Body.@return;
                        message = "تراکنش بازگشت داده شد";

                        _logger.LogError("resultcode" + result);
                        transaction.Status           = (byte)TransactionStatusEnum.ErrorOnVerify;
                        transaction.BankErrorCode    = Convert.ToInt32(result);
                        transaction.BankErrorMessage = MellatResult(result);
                        transaction.ModifiedOn       = DateTime.Now;
                        transaction.ModifiedBy       = 1;
                        await _transactionRepository.Update(transaction);

                        verifyTransactionResult.Status    = false;
                        verifyTransactionResult.ErrorCode = (byte)ErrorCodeEnum.VerifyError;
                        verifyTransactionResult.Message   = message;
                        _logger.LogError("Mellat Verify reverse!");
                        return(verifyTransactionResult);
                    }
                }
                else
                {
                    //ViewBag.Message = MellatHelper.MellatResult(Result);
                    //ViewBag.SaleReferenceId = "**************";
                    message = "تراکنش بازگشت داده شد";

                    _logger.LogError("errr1");
                    transaction.Status           = (byte)TransactionStatusEnum.ErrorOnVerify;
                    transaction.BankErrorCode    = (byte)ErrorCodeEnum.VerifyError;
                    transaction.BankErrorMessage = message;
                    transaction.ModifiedOn       = DateTime.Now;
                    transaction.ModifiedBy       = 1;
                    await _transactionRepository.Update(transaction);

                    verifyTransactionResult.Status    = false;
                    verifyTransactionResult.ErrorCode = (byte)ErrorCodeEnum.VerifyError;
                    verifyTransactionResult.Message   = message;
                    _logger.LogError("Mellat Verify error1!");
                    return(verifyTransactionResult);
                }
            }
            else
            {
                //ViewBag.Message = "شماره رسید قابل قبول نیست";
                //ViewBag.SaleReferenceId = "**************";

                _logger.LogError("err2");
                transaction.Status           = (byte)TransactionStatusEnum.ErrorOnVerify;
                transaction.BankErrorCode    = (byte)ErrorCodeEnum.VerifyError;
                transaction.BankErrorMessage = message;
                transaction.ModifiedOn       = DateTime.Now;
                transaction.ModifiedBy       = 1;
                await _transactionRepository.Update(transaction);

                verifyTransactionResult.Status    = false;
                verifyTransactionResult.ErrorCode = (byte)ErrorCodeEnum.VerifyError;
                verifyTransactionResult.Message   = message;
                _logger.LogError("Mellat Verify error2!");
                return(verifyTransactionResult);
            }

            return(verifyTransactionResult);
        }
Example #3
0
        public async Task <IActionResult> Index(Guid id)
        {
            var transaction = await _transactionRepository.Get(id);

            if (transaction == null ||
                transaction.Status == (byte)TransactionStatusEnum.Cancel)
            {
                return(View("Error", "لینک وارد شده صحیح نیست."));
            }

            if (transaction.Status == (byte)TransactionStatusEnum.Success ||
                transaction.Status == (byte)TransactionStatusEnum.BankOk)
            {
                return(View("Error", "این تراکنش قبلا پرداخت شده است."));
            }

            if (transaction.ExpirationTime.HasValue &&
                transaction.ExpirationTime < DateTime.Now)
            {
                PersianCalendar persianCalendar = new PersianCalendar();
                string          date            = $"{persianCalendar.GetYear(transaction.ExpirationTime.Value)}/{persianCalendar.GetMonth(transaction.ExpirationTime.Value)}/" +
                                                  $"{persianCalendar.GetDayOfMonth(transaction.ExpirationTime.Value)}" +
                                                  $" - {transaction.ExpirationTime.Value.Hour}:{transaction.ExpirationTime.Value.Minute}";
                return(View("Error", $"این لینک تا تاریخ {date} معتبر بوده است، لطفا از پشتیبانی تقاضای لینک جدید بفرمایید."));
            }

            var applicationBank = await _applicationBankRepository.GetFirstBy(x => x.ApplicationId == transaction.ApplicationId && x.BankId == transaction.BankId);

            switch (transaction.Bank.Code)
            {
            case (byte)BankCodeEnum.Parsian:
                _logger.LogInformation("Parsian");
                var pinParam = applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "ParsianPIN");
                Parsian.ParsianGateway parsianGateway = new Parsian.ParsianGateway(pinParam.ParamValue);
                var resp = await parsianGateway.PinPaymentRequest(Convert.ToInt32(transaction.Amount), Convert.ToInt32(transaction.UserTrackCode), transaction.CallbackUrl);

                var paramsInfo = applicationBank.ApplicationBankParams.ToList();
                if (resp.Body.status == 0)
                {
                    _logger.LogInformation("authority after callback", resp.Body.authority);
                    var url = string.Format(applicationBank.Bank.PostUrl, resp.Body.authority);
                    transaction.BankTrackCode = resp.Body.authority.ToString();
                    await _transactionRepository.Update(transaction);

                    return(Redirect(url));
                }
                else
                {
                    _logger.LogInformation("Critical Error: Payment Error. StatusCode={0}", resp.Body.status);
                    transaction.BankTrackCode = resp.Body.authority.ToString();
                    switch (resp.Body.status)
                    {
                    case 20:
                    case 22:
                        transaction.ErrorCode        = (byte)ErrorCodeEnum.InvalidPin;
                        transaction.BankErrorMessage = "پين فروشنده درست نميباشد";
                        break;

                    case 30:
                        transaction.ErrorCode        = (byte)ErrorCodeEnum.OperationAlreadyDone;
                        transaction.BankErrorMessage = "عمليات قبلا با موفقيت انجام شده است";
                        break;

                    case 34:
                        transaction.ErrorCode        = (byte)ErrorCodeEnum.UserTrackCodeIsInvalid;
                        transaction.BankErrorMessage = "شماره تراكنش فروشنده درست نميباشد";
                        break;
                    }

                    await _transactionRepository.Update(transaction);

                    return(BadRequest(transaction.BankErrorMessage));
                }

            case (byte)BankCodeEnum.Mellat:
                _logger.LogInformation("mellat");
                var termialParam  = applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "MellatTerminalId");
                var userNameParam = applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "MellatUserName");
                var passwordParam = applicationBank.ApplicationBankParams.FirstOrDefault(x => x.ParamKey == "MellatPassword");

                var           terminalID    = termialParam.ParamValue;
                var           userName      = userNameParam.ParamValue;
                var           password      = passwordParam.ParamValue;
                MellatGateway mellatGateway = new MellatGateway(terminalID, userName, password);
                var           mellatResp    = await mellatGateway.bpPayRequest(Convert.ToInt32(transaction.Amount), Convert.ToInt32(transaction.UserTrackCode), transaction.BankRedirectUrl);

                var mellatParamsInfo = applicationBank.ApplicationBankParams.ToList();
                _logger.LogError((mellatResp == null).ToString());

                if (mellatResp != null)
                {
                    _logger.LogError(mellatResp.Body.@return);

                    string[] ResultArray = [email protected](',');
                    if (ResultArray[0].ToString() == "0")
                    {
                        var refId = ResultArray[1];
                        transaction.BankTrackCode = refId;
                        await _transactionRepository.Update(transaction);

                        ViewBag.RefId = refId;
                        return(View("Mellat", transaction));
                    }
                    else
                    {
                        _logger.LogError("Critical Error: Payment Error. StatusCode={0}", ResultArray[0].ToString());
                        transaction.ErrorCode        = (byte)MellatHelper.ErrorResult(ResultArray[0].ToString());
                        transaction.BankErrorCode    = Convert.ToInt32(ResultArray[0].ToString());
                        transaction.BankErrorMessage = MellatHelper.MellatResult(ResultArray[0].ToString());
                        await _transactionRepository.Update(transaction);

                        return(BadRequest(transaction.BankErrorMessage));
                    }
                }
                else
                {
                    _logger.LogError("Critical Error: Payment Error. StatusCode=no resp");

                    transaction.BankErrorMessage = "امکان اتصال به درگاه بانک وجود ندارد";
                    await _transactionRepository.Update(transaction);

                    return(BadRequest(transaction.BankErrorMessage));
                }

            case (byte)BankCodeEnum.Saman:
            default:
                _logger.LogInformation("Saman");
                ViewBag.Params = applicationBank.ApplicationBankParams.ToList();
                return(View("Saman", transaction));
            }
        }