public async Task <VerifyTransactionResponseModel> VerifyTransaction(Transaction transaction) { var verifyTransactionResult = new VerifyTransactionResponseModel(); var applicationBanks = await _applicationBankRepository.GetFirstBy(x => x.BankId == transaction.BankId && x.ApplicationId == transaction.ApplicationId); var ParsianParams = applicationBanks.ApplicationBankParams.ToList(); var pinParam = ParsianParams.FirstOrDefault(x => x.ParamKey == "ParsianPIN"); if (pinParam == null) { return(verifyTransactionResult); } var ParsianGateway = new ParsianGateway(pinParam.ParamValue); var result = await ParsianGateway.VerifyTransactionAsync(Convert.ToInt64(transaction.BankTrackCode)); if (result.Body.ConfirmPaymentResult.Status == 0 && result.Body.ConfirmPaymentResult.Token > 0) { _logger.LogDebug("Verify Done"); var 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; return(verifyTransactionResult); } var(Success, Message) = ParsianErrors.GetResult(result.Body.ConfirmPaymentResult.Status); _logger.LogDebug("Result code: " + result); transaction.Status = (byte)TransactionStatusEnum.ErrorOnVerify; transaction.BankErrorCode = Convert.ToInt32(result); 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; return(verifyTransactionResult); }
public async Task <IActionResult> Callback(long token, string secondTrackCode) { //long FormToken = Convert.ToInt64(Request.Form["Token"]); var Status = Request.Form["status"]; //var OrderId = Request.Form["OrderId"]; //var TerminalNo = Request.Form["TerminalNo"]; //var RRN = Request.Form["RRN"]; //var HashCardNumber = Request.Form["HashCardNumber"]; //var Amount = Request.Form["Amount"]; var transaction = await _transactionRepository.Get(Guid.Parse(secondTrackCode)); if (transaction.Status == (byte)TransactionStatusEnum.Success || transaction.Status == (byte)TransactionStatusEnum.Cancel) { return(BadRequest()); } var longUrl = transaction.CallbackUrl; var uriBuilder = new UriBuilder(longUrl); var query = HttpUtility.ParseQueryString(uriBuilder.Query); var bankErrorCode = ErrorCodeEnum.UnkownError; try { var(Success, Message) = ParsianErrors.GetResult(int.Parse(Status)); if (Success) { bankErrorCode = ErrorCodeEnum.NoError; transaction.BankErrorCode = (byte)bankErrorCode; transaction.Status = (byte)TransactionStatusEnum.BankOk; transaction.ModifiedOn = DateTime.Now; transaction.ModifiedBy = 1; await _transactionRepository.Update(transaction); query["id"] = transaction.Id.ToString(); query["trackCode"] = transaction.UserTrackCode; query["status"] = true.ToString(); query["errorCode"] = ((byte)bankErrorCode).ToString(); query["message"] = string.Empty; uriBuilder.Query = query.ToString(); return(Redirect(string.Format(uriBuilder.ToString()))); } else { _errorMsg = $"{Status} - {Message}"; } } catch (Exception ex) { _errorMsg = ex.Message; _logger.LogError(_errorMsg); } transaction.Status = (byte)TransactionStatusEnum.Failed; transaction.BankErrorCode = (byte)bankErrorCode; transaction.BankErrorMessage = _errorMsg; transaction.ModifiedOn = DateTime.Now; transaction.ModifiedBy = 1; await _transactionRepository.Update(transaction); query["id"] = transaction.Id.ToString(); query["trackCode"] = transaction.UserTrackCode; query["status"] = false.ToString(); query["errorCode"] = ((byte)bankErrorCode).ToString(); query["message"] = _errorMsg; uriBuilder.Query = query.ToString(); return(Redirect(string.Format(uriBuilder.ToString()))); }
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)); } }