def ipn():
	
	"""
	The IPN is the method that PayPal uses to
	return information about the payment.

	This function will receive this info and
	validate it, marking the user's order as
	confirmed or not.

	PayPal only confirm the receipt of this
	information if you return the same info
	it passed to you. So, we return the
	request.vars at the end of the function.
	"""

	import json
	write_logs(request)

	pending = Pending(id=int(request.vars.invoice), confirmed=False)
	if not pending:
		raise HTTP(404)

	already_confirmed = Confirmed(pending_id=pending.id)
	if already_confirmed:
		return dict(status="Already Confirmed", data=already_confirmed)

	if request.vars.payment_status == 'Completed':
		confirmed = Confirmed.insert(
									order_id=pending.order_id,
									pending_id=pending.id,
									confirmation_time=request.now
									)

		pending.update_record(confirmed=True)
		Order(id=pending.order_id).update_record(status=2)

		message = T('We just confirmed your payment for order number %s.') % pending.order_id.token
		message += '\n'
		message += T('The total amount was R$%.2f.') % pending.order_id.amount
		message += '\n'
		message += T('You can check your payment history after login in to your profile.')
		message += '\n\n'
		message += T('Thank you!')
		mail.send(to=pending.order_id.user_id.email,
				subject=T("Payment confirmed!"),
				message = message
				)
	else:
		if request.is_local:
			path = '/tmp/ipn_not_completed.txt'
		else:
			## common path for logs
			path = '/var/log/ipn_not_completed.txt'
		message = '-'*80
		message += '\nIPN NOT COMPLETED\n'
		message += str(request.vars)
		message += '\n'
		log_in_file(message, path)
		Order(id=pending.order_id).update_record(status=3)
		mail_message = T('There was a problem with your payment!')
		mail_message += '\n\n'
		mail_message += T("Something happened and we couldn't verify your payment.")
		mail_message += '\n'
		mail_message += T("If you're sure you paid the order, please contact us. Otherwise, try to pay again later.")
		mail_message += '\n\n'
		mail_message += T("Thank you.")
		mail.send(to=pending.order_id.user_id.email,
				subject=T('Something went wrong!'),
				message = mail_message
				)
	return json.dumps(request.vars)
dataset/ETHPy150Open juliarizza/web2courses/controllers/payments.py/ipn/original