# Path: yithlibraryserver/contributions/email.py
# def send_thankyou_email(request, donation):
#     return send_email(
#         request,
#         'yithlibraryserver.contributions:templates/email_thankyou',
#         donation,
#         'Thanks for your contribution!',
#         [donation['email']],
#     )
# 
# Path: yithlibraryserver/contributions/email.py
# def send_notification_to_admins(request, donation):
#     context = {
#         'home_link': request.route_url('home'),
#     }
#     context.update(donation)
#     return send_email_to_admins(
#         request,
#         'yithlibraryserver.contributions:templates/email_admin_notification',
#         context,
#         'A new donation was received!',
#     )
# 
# Path: yithlibraryserver/contributions/models.py
# def create_donation(request, data):
#     amount = int(data['amount'])
#     donation = {
#         'amount': amount,
#         'firstname': data['firstname'],
#         'lastname': data['lastname'],
#         'city': data['city'],
#         'country': data['country'],
#         'state': data['state'],
#         'street': data['street'],
#         'zip': data['zip'],
#         'email': data['email'],
#         'creation': request.datetime_service.utcnow(),
#     }
#     if include_sticker(amount):
#         donation['send_sticker'] = not ('no-sticker' in data)
#     else:
#         donation['send_sticker'] = False
# 
#     if request.user is not None:
#         donation['user'] = request.user['_id']
#     else:
#         donation['user'] = None
# 
#     _id = request.db.donations.insert(donation, safe=True)
#     donation['_id'] = _id
#     return donation
# 
# Path: yithlibraryserver/contributions/models.py
# def include_sticker(amount):
#     return amount > 1
# 
# Path: yithlibraryserver/contributions/paypal.py
# class PayPalExpressCheckout(object):
# 
#     def __init__(self, request):
#         self.request = request
#         self.nvp_url = request.registry.settings['paypal_nvp_url']
#         self.express_checkout_url = request.registry.settings['paypal_express_checkout_url']
# 
#     def get_express_checkout_token(self, amount):
#         return_url = self.request.route_url('contributions_paypal_success_callback')
#         cancel_url = self.request.route_url('contributions_paypal_cancel_callback')
#         payload = PayPalPayload(self.request, 'SetExpressCheckout')
#         payload.add_payment_info(amount)
#         payload.add_callbacks(return_url, cancel_url)
# 
#         response = requests.post(self.nvp_url, data=payload)
#         if response.ok:
#             data = urlparse.parse_qs(response.text)
#             ack = data['ACK'][0]
#             if ack == 'Success':
#                 return data['TOKEN'][0]
# 
#     def get_express_checkout_url(self, token):
#         url = self.express_checkout_url + '?cmd=_express-checkout&token=%s'
#         return url % token
# 
#     def get_express_checkout_details(self, token, payerid):
#         payload = PayPalPayload(self.request, 'GetExpressCheckoutDetails')
#         payload.add_token(token, payerid)
# 
#         response = requests.post(self.nvp_url, data=payload)
# 
#         if response.ok:
#             data = urlparse.parse_qs(response.text)
#             ack = data['ACK'][0]
#             if ack == 'Success':
#                 amount = data['AMT'][0]
#                 amount = int(amount.split('.')[0])
#                 return {
#                     'amount': amount,
#                     'firstname': data['FIRSTNAME'][0],
#                     'lastname': data['LASTNAME'][0],
#                     'city': data['SHIPTOCITY'][0],
#                     'country': data['SHIPTOCOUNTRYNAME'][0],
#                     'state': data['SHIPTOSTATE'][0],
#                     'street': data['SHIPTOSTREET'][0],
#                     'zip': data['SHIPTOZIP'][0],
#                     'email': data['EMAIL'][0],
#                 }
# 
#     def do_express_checkout_payment(self, token, payerid, amount):
#         payload = PayPalPayload(self.request, 'DoExpressCheckoutPayment')
#         payload.add_payment_info(amount)
#         payload.add_token(token, payerid)
# 
#         response = requests.post(self.nvp_url, data=payload)
# 
#         if response.ok:
#             data = urlparse.parse_qs(response.text)
#             ack = data['ACK'][0]
#             if ack == 'Success':
#                 return True
# 
#         return False
# 
# Path: yithlibraryserver/i18n.py
# def deform_translator(term):
# def locale_negotiator(request):


import random
from pyramid.httpexceptions import HTTPBadRequest, HTTPFound
from pyramid.i18n import get_locale_name
from pyramid.view import view_config
from yithlibraryserver.contributions.email import send_thankyou_email
from yithlibraryserver.contributions.email import send_notification_to_admins
from yithlibraryserver.contributions.models import create_donation
from yithlibraryserver.contributions.models import include_sticker
from yithlibraryserver.contributions.paypal import PayPalExpressCheckout
from yithlibraryserver.i18n import TranslationString as _
    return HTTPBadRequest('Wrong action or method')


@view_config(route_name='contributions_paypal_success_callback',
             renderer='templates/contributions_confirm.pt')
def contributions_paypal_success(request):
    error_msg = _('There was a problem in the confirmation process. Please start the checkout again')
    if request.method == 'POST':
        if 'submit' in request.POST:
            token = request.POST.get('token', None)
            payerid = request.POST.get('payerid', None)
            amount = request.POST.get('amount', None)

            success = False

            if token and payerid and amount:
                try:
                    amount = int(amount)
                except ValueError:
                    return HTTPBadRequest('Amount must be an integer')

                paypal = PayPalExpressCheckout(request)
                success = paypal.do_express_checkout_payment(token, payerid,
                                                             amount)

            if success:
                donation = create_donation(request, request.POST)
                send_thankyou_email(request, donation)
                send_notification_to_admins(request, donation)
                request.session.flash(

# Path: yithlibraryserver/contributions/email.py
# def send_thankyou_email(request, donation):
#     return send_email(
#         request,
#         'yithlibraryserver.contributions:templates/email_thankyou',
#         donation,
#         'Thanks for your contribution!',
#         [donation['email']],
#     )
# 
# Path: yithlibraryserver/contributions/email.py
# def send_notification_to_admins(request, donation):
#     context = {
#         'home_link': request.route_url('home'),
#     }
#     context.update(donation)
#     return send_email_to_admins(
#         request,
#         'yithlibraryserver.contributions:templates/email_admin_notification',
#         context,
#         'A new donation was received!',
#     )
# 
# Path: yithlibraryserver/contributions/models.py
# def create_donation(request, data):
#     amount = int(data['amount'])
#     donation = {
#         'amount': amount,
#         'firstname': data['firstname'],
#         'lastname': data['lastname'],
#         'city': data['city'],
#         'country': data['country'],
#         'state': data['state'],
#         'street': data['street'],
#         'zip': data['zip'],
#         'email': data['email'],
#         'creation': request.datetime_service.utcnow(),
#     }
#     if include_sticker(amount):
#         donation['send_sticker'] = not ('no-sticker' in data)
#     else:
#         donation['send_sticker'] = False
# 
#     if request.user is not None:
#         donation['user'] = request.user['_id']
#     else:
#         donation['user'] = None
# 
#     _id = request.db.donations.insert(donation, safe=True)
#     donation['_id'] = _id
#     return donation
# 
# Path: yithlibraryserver/contributions/models.py
# def include_sticker(amount):
#     return amount > 1
# 
# Path: yithlibraryserver/contributions/paypal.py
# class PayPalExpressCheckout(object):
# 
#     def __init__(self, request):
#         self.request = request
#         self.nvp_url = request.registry.settings['paypal_nvp_url']
#         self.express_checkout_url = request.registry.settings['paypal_express_checkout_url']
# 
#     def get_express_checkout_token(self, amount):
#         return_url = self.request.route_url('contributions_paypal_success_callback')
#         cancel_url = self.request.route_url('contributions_paypal_cancel_callback')
#         payload = PayPalPayload(self.request, 'SetExpressCheckout')
#         payload.add_payment_info(amount)
#         payload.add_callbacks(return_url, cancel_url)
# 
#         response = requests.post(self.nvp_url, data=payload)
#         if response.ok:
#             data = urlparse.parse_qs(response.text)
#             ack = data['ACK'][0]
#             if ack == 'Success':
#                 return data['TOKEN'][0]
# 
#     def get_express_checkout_url(self, token):
#         url = self.express_checkout_url + '?cmd=_express-checkout&token=%s'
#         return url % token
# 
#     def get_express_checkout_details(self, token, payerid):
#         payload = PayPalPayload(self.request, 'GetExpressCheckoutDetails')
#         payload.add_token(token, payerid)
# 
#         response = requests.post(self.nvp_url, data=payload)
# 
#         if response.ok:
#             data = urlparse.parse_qs(response.text)
#             ack = data['ACK'][0]
#             if ack == 'Success':
#                 amount = data['AMT'][0]
#                 amount = int(amount.split('.')[0])
#                 return {
#                     'amount': amount,
#                     'firstname': data['FIRSTNAME'][0],
#                     'lastname': data['LASTNAME'][0],
#                     'city': data['SHIPTOCITY'][0],
#                     'country': data['SHIPTOCOUNTRYNAME'][0],
#                     'state': data['SHIPTOSTATE'][0],
#                     'street': data['SHIPTOSTREET'][0],
#                     'zip': data['SHIPTOZIP'][0],
#                     'email': data['EMAIL'][0],
#                 }
# 
#     def do_express_checkout_payment(self, token, payerid, amount):
#         payload = PayPalPayload(self.request, 'DoExpressCheckoutPayment')
#         payload.add_payment_info(amount)
#         payload.add_token(token, payerid)
# 
#         response = requests.post(self.nvp_url, data=payload)
# 
#         if response.ok:
#             data = urlparse.parse_qs(response.text)
#             ack = data['ACK'][0]
#             if ack == 'Success':
#                 return True
# 
#         return False
# 
# Path: yithlibraryserver/i18n.py
# def deform_translator(term):
# def locale_negotiator(request):


# Path: yithlibraryserver/contributions/views.py
import random
from pyramid.httpexceptions import HTTPBadRequest, HTTPFound
from pyramid.i18n import get_locale_name
from pyramid.view import view_config
from yithlibraryserver.contributions.email import send_thankyou_email
from yithlibraryserver.contributions.email import send_notification_to_admins
from yithlibraryserver.contributions.models import create_donation
from yithlibraryserver.contributions.models import include_sticker
from yithlibraryserver.contributions.paypal import PayPalExpressCheckout
from yithlibraryserver.i18n import TranslationString as _

    return HTTPBadRequest('Wrong action or method')


@view_config(route_name='contributions_paypal_success_callback',
             renderer='templates/contributions_confirm.pt')
def contributions_paypal_success(request):
    error_msg = _('There was a problem in the confirmation process. Please start the checkout again')
    if request.method == 'POST':
        if 'submit' in request.POST:
            token = request.POST.get('token', None)
            payerid = request.POST.get('payerid', None)
            amount = request.POST.get('amount', None)

            success = False

            if token and payerid and amount:
                try:
                    amount = int(amount)
                except ValueError:
                    return HTTPBadRequest('Amount must be an integer')

                paypal = PayPalExpressCheckout(request)
                success = paypal.do_express_checkout_payment(token, payerid,
                                                             amount)

            if success:
                donation = create_donation(request, request.POST)
                send_thankyou_email(request, donation)
                send_notification_to_admins(request, donation)
                request.session.flash(

                    _('Thank you very much for your great contribution'),