示例#1
0
		public async Task AddTicketAsync(Ticket ticket)
		{
			var data = AppFactory.Mapper.Value.Map<Data.Entity.Table.Ticket>(ticket);

			await DataFactory.TicketService.Value.InsertAsync(data);
		}
		private PayFormModel GetPaymentForm(Ticket ticket)
		{
			KaznacheyPaymentSystem kaznachey;
			int paySystemId;
			switch (ticket.PaymentType.ToLowerInvariant())
			{
				case "kaznackey":
					kaznachey = new KaznacheyPaymentSystem(Configuration.KaznackeyMerchantId, Configuration.KaznackeyMerchantSecreet);
					paySystemId = kaznachey.GetMerchantInformation().PaySystems[0].Id;
					break;
				case "liqpay":
					kaznachey = new KaznacheyPaymentSystem(Configuration.LiqPayMerchantId, Configuration.LiqPayMerchantSecreet);
					paySystemId = kaznachey.GetMerchantInformation().PaySystems[3].Id;
					break;
				default:
					kaznachey = new KaznacheyPaymentSystem(Configuration.KaznackeyMerchantId, Configuration.KaznackeyMerchantSecreet);
					paySystemId = kaznachey.GetMerchantInformation().PaySystems[0].Id;
					break;
			}

			var paymentRequest = new PaymentRequest(paySystemId);
			paymentRequest.Language = "RU";
			paymentRequest.Currency = "UAH";
			paymentRequest.PaymentDetail = new PaymentDetails
			{
				EMail = ticket.Attendee.EMail,
				MerchantInternalUserId = ticket.Attendee.EMail,
				MerchantInternalPaymentId = string.Format("{0}-{1}", ticket.Attendee.EMail, ticket.TicketType),
				BuyerFirstname = ticket.Attendee.FirstName,
				BuyerLastname = ticket.Attendee.LastName,
				ReturnUrl = string.Format("{0}/profile/my", _host),
				StatusUrl = string.Format("{0}/api/tickets/paymentconfirm", _host)
			};
			paymentRequest.Products = new List<Product>
			{
				new Product
				{
					ProductId = ticket.TicketType.ToString(),
					ProductItemsNum = 1,
					ProductName = string.Format("{0} {1} билет на AzureDay {2} ({3})",
						ticket.Attendee.FirstName,
						ticket.Attendee.LastName,
						Configuration.Year,
						ticket.TicketType.ToDisplayString()),
					ProductPrice = (decimal) ticket.Price
				}
			};

			var form = kaznachey.CreatePayment(paymentRequest).ExternalFormHtml;

			var model = new PayFormModel
			{
				Form = form
			};
			return model;
		}
		public async Task<ActionResult> Pay(PayModel model)
		{
			decimal ticketPrice = AppFactory.TicketService.Value.GetTicketPrice(model.TicketType);

			var ticket = new Ticket
			{
				Price = (double)ticketPrice,
				TicketType = model.TicketType,
				PaymentType = model.PaymentType
			};

			if (!string.IsNullOrEmpty(model.PromoCode))
			{
				var coupon = await AppFactory.CouponService.Value.GetValidCouponByCodeAsync(model.PromoCode);
				if (coupon != null)
				{
					ticketPrice = AppFactory.CouponService.Value.GetPriceWithCoupon(ticketPrice, coupon);
					await AppFactory.CouponService.Value.UseCouponByCodeAsync(model.PromoCode);

					ticket.Price = (double)ticketPrice;
					ticket.Coupon = coupon;
				}
			}

			ticket.IsPayed = ticket.Price <= 0;

			if (ticket.Attendee == null)
			{
				var email = User.Identity.Name;
				ticket.Attendee = await AppFactory.AttendeeService.Value.GetAttendeeByEmailAsync(email);
			}

			await AppFactory.TicketService.Value.AddTicketAsync(ticket);

			if (ticket.IsPayed)
			{
				return RedirectToAction("My");
			}
			else
			{
				return RedirectToAction("Pay", ticket);
			}
		}
		public async Task<ActionResult> Pay(Ticket ticket)
		{
			if (ticket == null)
			{
				return RedirectToAction("My");
			}
			
			if (ticket.Attendee == null)
			{
				var email = User.Identity.Name;
				ticket.Attendee = await AppFactory.AttendeeService.Value.GetAttendeeByEmailAsync(email);
			}

			var model = GetPaymentForm(ticket);

			return View("PayForm", model);
		}