Exemple #1
0
        public async Task <IActionResult> Index([FromForm] PaymentViewModel model)
        {
            try
            {
                //gateway request
                PaymentGatewayRequest gatewayRequest = new PaymentGatewayRequest
                {
                    CardHolderName = model.CardHolderName,
                    //clear credit card unnecessary characters
                    CardNumber        = model.CardNumber?.Replace(" ", string.Empty).Replace(" ", string.Empty),
                    ExpireMonth       = model.ExpireMonth,
                    ExpireYear        = model.ExpireYear,
                    CvvCode           = model.CvvCode,
                    CardType          = model.CardType,
                    Installment       = model.Installment,
                    TotalAmount       = model.TotalAmount,
                    OrderNumber       = Guid.NewGuid().ToString(),
                    CurrencyIsoCode   = "949",
                    LanguageIsoCode   = "tr",
                    CustomerIpAddress = HttpContext.Connection.RemoteIpAddress.ToString()
                };

                //bank
                Bank bank = await _bankService.GetById(model.BankId.Value);

                gatewayRequest.BankName = Enum.Parse <BankNames>(bank.SystemName);

                //bank parameters
                System.Collections.Generic.List <BankParameter> bankParameters = await _bankService.GetBankParameters(bank.Id);

                gatewayRequest.BankParameters = bankParameters.ToDictionary(key => key.Key, value => value.Value);

                //create payment transaction
                PaymentTransaction payment = new PaymentTransaction
                {
                    OrderNumber    = Guid.Parse(gatewayRequest.OrderNumber),
                    UserIpAddress  = gatewayRequest.CustomerIpAddress,
                    UserAgent      = HttpContext.Request.Headers[HeaderNames.UserAgent],
                    BankId         = model.BankId.Value,
                    CardPrefix     = gatewayRequest.CardNumber.Substring(0, 6),
                    CardHolderName = gatewayRequest.CardHolderName,
                    Installment    = model.Installment,
                    TotalAmount    = model.TotalAmount,
                    BankRequest    = JsonConvert.SerializeObject(gatewayRequest)
                };

                //mark as created
                payment.MarkAsCreated();

                //insert payment transaction
                await _paymentService.Insert(payment);

                var responseModel = new
                {
                    GatewayUrl = new Uri($"{Request.GetHostUrl(false)}{Url.RouteUrl("Confirm", new { paymentId = payment.OrderNumber })}"),
                    Message    = "Redirecting to gateway..."
                };

                return(Ok(responseModel));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);

                return(Ok(new { errorMessage = "İşlem sırasında bir hata oluştu." }));
            }
        }