Пример #1
0
def UserAddCard(request, username):
	''' Adds a card from either the reservation page or the user profile page.
	Displays success or error message and returns user to originating page.'''

	user = User.objects.get(username=username)
	if not request.method == 'POST' or request.user != user:
		return HttpResponseRedirect('/404')

	token = request.POST.get('stripeToken')
	if not token:
		messages.add_message(request, messages.INFO, "No credit card information was given.")
		return HttpResponseRedirect("/people/%s" % username)

	reservation_id = request.POST.get('res-id')
	if reservation_id:
		reservation = Reservation.objects.get(id=reservation_id)

	stripe.api_key = settings.STRIPE_SECRET_KEY

	try:
		customer = stripe.Customer.create(
			card=token,
			description=user.email
		)
		profile = user.profile
		profile.customer_id = customer.id
		profile.save()

		# if the card is being added from the reservation page, then charge the card
		if reservation_id:
			try:
				# charges card, saves payment details and emails a receipt to
				# the user
				payment_gateway.charge_card(reservation)
				send_receipt(reservation)
				reservation.confirm()
				days_until_arrival = (reservation.arrive - datetime.date.today()).days
				if days_until_arrival <= reservation.location.welcome_email_days_ahead:
					guest_welcome(reservation)
				messages.add_message(request, messages.INFO, 'Thank you! Your payment has been processed and a receipt emailed to you at %s. You will receive an email with house access information and other details %d days before your arrival.' % (user.email, reservation.location.welcome_email_days_ahead))
				return HttpResponseRedirect(reverse('reservation_detail', args=(reservation.location.slug, reservation.id)))
			except stripe.CardError, e:
				raise stripe.CardError(e)
		# if the card is being added from the user profile page, just save it. 
		else:
Пример #2
0
def ReservationConfirm(request, reservation_id, location_slug):
	reservation = Reservation.objects.get(id=reservation_id)
	if not (request.user.is_authenticated() and request.user == reservation.user 
		and request.method == "POST" and reservation.is_approved()):
		return HttpResponseRedirect("/")

	if not reservation.user.profile.customer_id:
		messages.add_message(request, messages.INFO, 'Please enter payment information to confirm your reservation.')
	else:
		try:
			payment_gateway.charge_card(reservation)
			reservation.confirm()
			send_receipt(reservation)
			# if reservation start date is sooner than WELCOME_EMAIL_DAYS_AHEAD,
			# need to send them house info manually. 
			days_until_arrival = (reservation.arrive - datetime.date.today()).days
			if days_until_arrival <= reservation.location.welcome_email_days_ahead:
				guest_welcome(reservation)
			messages.add_message(request, messages.INFO, 'Thank you! Your payment has been received and a receipt emailed to you at %s' % reservation.user.email)
		except stripe.CardError, e:
			messages.add_message(request, messages.ERROR, 'Drat, it looks like there was a problem with your card: %s.' % (e))
Пример #3
0
def ReservationManageAction(request, location_slug, reservation_id):
	if not request.method == 'POST':
		return HttpResponseRedirect('/404')

	location = get_location(location_slug)
	reservation = Reservation.objects.get(id=reservation_id)
	reservation_action = request.POST.get('reservation-action')
	try:
		if reservation_action == 'set-tentative':
			reservation.approve()
		elif reservation_action == 'set-confirm':
			reservation.confirm()
			days_until_arrival = (reservation.arrive - datetime.date.today()).days
			if days_until_arrival <= location.welcome_email_days_ahead:
				guest_welcome(reservation)
		elif reservation_action == 'set-comp':
			reservation.comp()
		elif reservation_action == 'refund-card':
			try:
				payments = reservation.payments()
				if payments.count() == 0:
					Reservation.ResActionError("No payments to refund!")
				if payments.count() > 1:
					Reservation.ResActionError("Multiple payments found!")
				payment_gateway.issue_refund(payments[0])
			except stripe.CardError, e:
				raise Reservation.ResActionError(e)
		elif reservation_action == 'res-charge-card':
			try:
				payment_gateway.charge_card(reservation)
				reservation.confirm()
				send_receipt(reservation)
				days_until_arrival = (reservation.arrive - datetime.date.today()).days
				if days_until_arrival <= location.welcome_email_days_ahead:
					guest_welcome(reservation)
			except stripe.CardError, e:
				raise Reservation.ResActionError(e)