public async Task<ActionResult> CreateAjax(RegisterPaymentModel model)
        {
            if (!ModelState.IsValid)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            // 구매 정보 확인
            var checkout = checkoutRepository.GetById(model.CheckoutId);
            if (checkout == null)
            {
                ModelState.AddModelError(nameof(model.CheckoutId), "Checkout Id가 존재하지 않습니다.");
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            // 새 결제 저장
            var payment = AspNetCore.Models.Payment.Create(model.CheckoutId);
            paymentRepository.Add(payment);

            // 새 결제 내용을 아임포트에 등록
            await paymentsApi.PrepareAsync(new PaymentPreparation
            {
                Amount = checkout.Amount,
                TransactionId = payment.TransactionId,
            });

            // 아임포트로부터 결제 내용을 알림 받을 주소:
            var notificationUrl = Url.HttpRouteUrl(
                null,
                new
                {
                    controller = "Payment",
                    action = nameof(Notification),
                    transactionId = payment.TransactionId,
                });
            // 결제 완료시 돌아올 URL.
            // PC와 모바일 웹 브라우저 동작 차이를 줄이기 위해
            // 항상 내부 사이트로 돌아오는 것이 좋습니다.
            var returnUrl = Url.Action(nameof(Refresh), "Payment", new { transactionId = payment.TransactionId });

            var result = new ContentResult();
            result.Content = JsonConvert.SerializeObject(new PaymentRequest
            {
                Amount = checkout.Amount,
                AppScheme = AppScheme,
                Currency = "KRW",
                CustomerEmail = checkout.CustomerEmail,
                CustomerName = checkout.CustomerName,
                CustomerPhoneNumber = checkout.CustomerPhoneNumber,
                IsDigital = checkout.IsDigital,
                Method = checkout.PaymentMethod,
                NotificationUrl = notificationUrl,
                PaymentGateway = checkout.PaymentGateway,
                ReturnUrl = returnUrl,
                Title = checkout.Name,
                TransactionId = payment.TransactionId,
                UseEscrow = checkout.UseEscrow,
                VirtualBankExpiration = checkout.VirtualBankExpirationTime,
                // 다음은 설정할 필요 없음.
                //MerchantId = null,  // 상점 구분 ID
                //CustomData = null,
                //CustomerAddress = null,
                //CustomerPostCode = null,
            }, serializerSettings);
            result.ContentEncoding = Encoding.UTF8;
            result.ContentType = "application/json";
            return result;
        }
Esempio n. 2
0
        public async Task <ActionResult> CreateAjax(RegisterPaymentModel model)
        {
            if (!ModelState.IsValid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // 구매 정보 확인
            var checkout = checkoutRepository.GetById(model.CheckoutId);

            if (checkout == null)
            {
                ModelState.AddModelError(nameof(model.CheckoutId), "Checkout Id가 존재하지 않습니다.");
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // 새 결제 저장
            var payment = AspNetCore.Models.Payment.Create(model.CheckoutId);

            paymentRepository.Add(payment);

            // 새 결제 내용을 아임포트에 등록
            await paymentsApi.PrepareAsync(new PaymentPreparation
            {
                Amount        = checkout.Amount,
                TransactionId = payment.TransactionId,
            });

            // 아임포트로부터 결제 내용을 알림 받을 주소:
            var notificationUrl = Url.HttpRouteUrl(
                null,
                new
            {
                controller    = "Payment",
                action        = nameof(Notification),
                transactionId = payment.TransactionId,
            });
            // 결제 완료시 돌아올 URL.
            // PC와 모바일 웹 브라우저 동작 차이를 줄이기 위해
            // 항상 내부 사이트로 돌아오는 것이 좋습니다.
            var returnUrl = Url.Action(nameof(Refresh), "Payment", new { transactionId = payment.TransactionId });

            var result = new ContentResult();

            result.Content = JsonConvert.SerializeObject(new PaymentRequest
            {
                Amount              = checkout.Amount,
                AppScheme           = AppScheme,
                Currency            = "KRW",
                CustomerEmail       = checkout.CustomerEmail,
                CustomerName        = checkout.CustomerName,
                CustomerPhoneNumber = checkout.CustomerPhoneNumber,
                IsDigital           = checkout.IsDigital,
                Method              = checkout.PaymentMethod,
                NotificationUrl     = notificationUrl,
                PaymentGateway      = checkout.PaymentGateway,
                ReturnUrl           = returnUrl,
                Title                 = checkout.Name,
                TransactionId         = payment.TransactionId,
                UseEscrow             = checkout.UseEscrow,
                VirtualBankExpiration = checkout.VirtualBankExpirationTime,
                // 다음은 설정할 필요 없음.
                //MerchantId = null,  // 상점 구분 ID
                //CustomData = null,
                //CustomerAddress = null,
                //CustomerPostCode = null,
            }, serializerSettings);
            result.ContentEncoding = Encoding.UTF8;
            result.ContentType     = "application/json";
            return(result);
        }
        public async Task <IActionResult> CreateAjax([FromBody] RegisterPaymentModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // 구매 정보 확인
            var checkout = checkoutRepository.GetById(model.CheckoutId);

            if (checkout == null)
            {
                ModelState.AddModelError(nameof(model.CheckoutId), "Checkout Id가 존재하지 않습니다.");
                return(BadRequest(ModelState));
            }

            // 새 결제 저장
            var payment = Models.Payment.Create(model.CheckoutId);

            paymentRepository.Add(payment);

            // 새 결제 내용을 아임포트에 등록
            await paymentsApi.PrepareAsync(new PaymentPreparation
            {
                Amount        = checkout.Amount,
                TransactionId = payment.TransactionId,
            });

            // 아임포트로부터 결제 내용을 알림 받을 주소:
            var notificationUrl = Url.Link(
                null,
                new
            {
                controller    = "Payment",
                action        = nameof(OnNotification),
                transactionId = payment.TransactionId,
            });
            // 결제 완료시 돌아올 URL.
            // PC와 모바일 웹 브라우저 동작 차이를 줄이기 위해
            // 항상 내부 사이트로 돌아오는 것이 좋습니다.
            var returnUrl = Url.Action(nameof(Refresh), "Payment", new { transactionId = payment.TransactionId });

            return(Json(new PaymentRequest
            {
                Amount = checkout.Amount,
                AppScheme = AppScheme,
                Currency = "KRW",
                CustomerEmail = checkout.CustomerEmail,
                CustomerName = checkout.CustomerName,
                CustomerPhoneNumber = checkout.CustomerPhoneNumber,
                IsDigital = checkout.IsDigital,
                Method = checkout.PaymentMethod,
                NotificationUrl = notificationUrl,
                PaymentGateway = checkout.PaymentGateway,
                ReturnUrl = returnUrl,
                Title = checkout.Name,
                TransactionId = payment.TransactionId,
                UseEscrow = checkout.UseEscrow,
                VirtualBankExpiration = checkout.VirtualBankExpirationTime,
                // 다음은 설정할 필요 없음.
                //MerchantId = null,  // 상점 구분 ID
                //CustomData = null,
                //CustomerAddress = null,
                //CustomerPostCode = null,
            }));
        }