public IHttpActionResult getPayment([FromBody] ProcessPaymentDTO processPaymentDTO) { if (!ModelState.IsValid) { return(BadRequest("Invalid payment details")); } //update the paymentdetails table var paymentFound = _context.PaymentDetails.FirstOrDefault(p => p.txtRef == processPaymentDTO.txnref); if (paymentFound == null) { return(BadRequest("Invalid payment detail transaction Ref!")); } //return payment details var paymentResponseDTO = new PaymentResponseDTO { responseCode = paymentFound.ResponseCode, responsedescription = paymentFound.Description, txtref = paymentFound.txtRef, payref = paymentFound.Payref, retref = paymentFound.RetRef }; return(Ok(paymentResponseDTO)); }
public IActionResult ControlPaymentResponse(PaymentResponseDTO dto) { if (!User.Identity.IsAuthenticated) { return(Unauthorized("You need to be logged in to perform this action")); } string email = User.Identity.Name; var user = _userRepository.GetBy(email); if (user == null) { return(BadRequest("User not found")); } var registration = _registrationRepository.GetLast(email); if (registration == null) { return(BadRequest("No registration found")); } if (registration.Paid) { return(BadRequest("You have already paid for this registration")); } var valid = EncoderHelper.ControlShaSign(_config, dto); registration.Paid = valid; _registrationRepository.Update(registration, email); var route = _routeRepository.GetBy(registration.RouteId); return(Ok(new { TourName = route.TourName, Valid = valid })); }
public static bool ControlShaSign(IConfiguration _config, PaymentResponseDTO dto) { string hash; string key = _config["PaymentResponse:Key"]; string input = //"AAVADDRESS=" + dto.Aavaddress + key + //"ACCEPTANCE=" + dto.Acceptance + key + "AMOUNT=" + dto.Amount + key + //"BRAND=" + dto.Brand + key + //"CARDNO=" + dto.CardNo + key + //"CN=" + dto.CN + key + "CURRENCY=" + dto.Currency + key + //"ED=" + dto.ED + key + //"IP=" + dto.IP + key + "NCERROR=" + dto.NCError + key + "ORDERID=" + dto.OrderID + key + "PAYID=" + dto.PayId + key + //"PM=" + dto.PM + key + "STATUS=" + dto.Status + key;//+ //"TRXDATE=" + dto.TRXDate + key; using (SHA1 sha1Hash = SHA1.Create()) { byte[] sourceBytes = Encoding.UTF8.GetBytes(input); byte[] hashBytes = sha1Hash.ComputeHash(sourceBytes); hash = BitConverter.ToString(hashBytes).Replace("-", String.Empty).ToLower(); } return(dto.ShaSign.ToLower().Equals(hash)); }
public async Task <IActionResult> ProcessPayment(PaymentRequestDTO paymentRequest) { try { if (ModelState.IsValid) { //in this the processing the payment take place var paymentState = await _paymentRequestService.ProcessingPayment(paymentRequest); var paymentResponse = new PaymentResponseDTO() { IsProcessed = paymentState.PaymentState == PaymentStateEnum.Processed , PaymentState = paymentState }; //this will check if the payment is processed or not if (!paymentResponse.IsProcessed) { return(StatusCode(500, new { error = "Payment could not be processed" })); } return(Ok(paymentResponse)); } else { return(BadRequest(ModelState)); } } catch (Exception ex) { _logger.LogError(ex, ex.Message); return(StatusCode(500)); } }
private async Task <PaymentResponse> SavePaymentResponseAsync(PaymentResponseDTO paymentResponseDTO, Card card_used) { PaymentResponse paymentResponse = _mapper.Map <PaymentResponse>(paymentResponseDTO); paymentResponse.Card_id = card_used.Id; return(await _paymentResponseRepository.CreateAsync(paymentResponse)); }
public async Task <PaymentResponseDTO> GetPaymentByAcquirerRefrenceIdAsync(Guid acuirer_reference_id) { PaymentResponse paymentResponse = await _paymentResponseRepository.GetByAcquirerRefrenceIdAsync(acuirer_reference_id); PaymentResponseDTO paymentResponseDTO = _mapper.Map <PaymentResponseDTO>(paymentResponse); if (paymentResponse.Card_id != Guid.Empty) { Card card = await _cardService.RetrieveCardByIdAsync(paymentResponse.Card_id); if (card != null) { CardResponseDTO cardResponseDTO = new CardResponseDTO(card.Number); cardResponseDTO.Id = card.Id; cardResponseDTO.Expiry_Month = card.Expiry_Month; cardResponseDTO.Expiry_Year = card.Expiry_Year; paymentResponseDTO.Card_Info = cardResponseDTO; } } return(paymentResponseDTO); }
public async Task <PaymentResponseDTO> ProcessPaymentAsync(Guid transaction_reference, PaymentRequestDTO paymentRequest) { PaymentResponseDTO paymentResponseDTO = _mapper.Map <PaymentResponseDTO>(paymentRequest); paymentResponseDTO.Transaction_Reference_Id = transaction_reference; try { #region Save/Retrieve Card Card card_to_use; //An Existing/Previously used card was provided so, retrieve from db if (paymentRequest.Card_Info.Id.HasValue && paymentRequest.Card_Info.Id != Guid.Empty) { card_to_use = await _cardService.RetrieveCardByIdAndCvvAsync(paymentRequest.Card_Info.Id.Value, paymentRequest.Card_Info.Cvv); if (card_to_use == null) { paymentResponseDTO.Status = StatusCode.INVALID_DATA; paymentResponseDTO.Errors = new List <string>() { "Could not retrieve card, Invalid card id or cvv was provided" }; return(paymentResponseDTO); } } //A new card was provided so save to database (Not validating if the card exisits as it could be that another merchant saved the same user's card) else { card_to_use = await _cardService.SaveCardAsync(paymentRequest.Card_Info); } CardResponseDTO cardResponseDTO = new CardResponseDTO(card_to_use.Number); cardResponseDTO.Id = card_to_use.Id; cardResponseDTO.Expiry_Month = card_to_use.Expiry_Month; cardResponseDTO.Expiry_Year = card_to_use.Expiry_Year; paymentResponseDTO.Card_Info = cardResponseDTO; #endregion #region Acquirer request and response //Call the Acquiring Bank Service, set the acquirer_reference_id AcquirerRequestDTO acquirerRequestDTO = new AcquirerRequestDTO(); acquirerRequestDTO.Transaction_Reference_id = transaction_reference; acquirerRequestDTO.Amount = paymentRequest.Amount; acquirerRequestDTO.Currency = paymentRequest.Currency; acquirerRequestDTO.card = card_to_use; acquirerRequestDTO.Payment_Description = paymentRequest.Payment_Description; AcquirerResponseDTO acquirerResponseDTO = _acquirerService.ForwardPaymentRequestToAcquirer(acquirerRequestDTO); paymentResponseDTO.Acquirer_Reference_Id = acquirerResponseDTO.Payment_Reference_Id; paymentResponseDTO.Status = acquirerResponseDTO.Status; paymentResponseDTO.Processed_On = acquirerResponseDTO.Processed_On; #endregion //Save Payment Response await SavePaymentResponseAsync(paymentResponseDTO, card_to_use); return(paymentResponseDTO); } catch (Exception ex) { //Log the exception paymentResponseDTO.Errors = new List <string>() { StringConstants.UNEXPECTED_ERROR_MESSAGE }; paymentResponseDTO.Status = Domain.DTOs.StatusCode.UNKNOWN; } return(paymentResponseDTO); }
public HttpResponseMessage ManageCallback(PaymentResponseDTO paymentResponse) { var test = HttpContext.Current.Request.UserHostAddress; if (paymentResponse == null) { return(RedirectUIToDefaultPage(5)); } else { PaymentTransactions paymentTrans = new PaymentTransactions { Amount = paymentResponse.amount, DatePaid = paymentResponse.datepaid, DigitalReceipt = paymentResponse.digitalreceipt, InvoiceId = paymentResponse.invoiceid, IssuerBank = paymentResponse.issuerbank, Payload = paymentResponse.payload, RespCode = paymentResponse.respcode, RRN = paymentResponse.rrn, TerminalID = paymentResponse.terminalid, TraceNumber = paymentResponse.tracenumber }; using (var unitOfWOrk = new UnitOfWork()) { unitOfWOrk.TransRepository.Add(paymentTrans); unitOfWOrk.SaveChanges(); if (paymentResponse.respcode < 0) { return(RedirectUIToDefaultPage(1)); } var duplicateDigitalReciept = unitOfWOrk.TransRepository.Get (x => x.DigitalReceipt == paymentResponse.digitalreceipt); if (duplicateDigitalReciept.Count() > 1) { return(RedirectUIToDefaultPage(2)); } if (paymentResponse.respcode == 0) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri("https://mabna.shaparak.ir:8081"); var requestApprove = new ApprovalRequest { TnxId = paymentTrans.DigitalReceipt, Tid = paymentTrans.TerminalID }; var res = client.PostAsJsonAsync("/v1/PeymentApi/Advice", requestApprove).GetAwaiter().GetResult(); res.EnsureSuccessStatusCode(); var approvResponse = res.Content.ReadAsAsync <ApprovalResponse>().GetAwaiter().GetResult(); if (approvResponse != null) { paymentTrans.AdviceReturnId = approvResponse.ReturnId; paymentTrans.AdviceStatus = approvResponse.Status; unitOfWOrk.TransRepository.Edit(paymentTrans); unitOfWOrk.SaveChanges(); if (approvResponse.ReturnId >= 0) { if (paymentTrans.Amount != approvResponse.Amount) { if (RollBackPayment(ref paymentTrans, client, requestApprove)) { unitOfWOrk.TransRepository.Edit(paymentTrans); unitOfWOrk.SaveChanges(); } return(RedirectUIToDefaultPage(3)); } else { if (!FinallAddToJournal(paymentTrans)) { RollBackPayment(ref paymentTrans, client, requestApprove); return(RedirectUIToDefaultPage(6)); } } } else { return(RedirectUIToDefaultPage(4)); } } else { return(RedirectUIToDefaultPage(5)); } } } return(RedirectUIToDefaultPage(0)); } //return Request.CreateResponse(HttpStatusCode.OK); }