# Path: yithlibraryserver/contributions/paypal.py
# class PayPalPayload(dict):
# 
#     def __init__(self, request, method):
#         self['METHOD'] = method
#         self['VERSION'] = '72.0'
#         self['USER'] = request.registry.settings['paypal_user']
#         self['PWD'] = request.registry.settings['paypal_password']
#         self['SIGNATURE'] = request.registry.settings['paypal_signature']
# 
#     def add_payment_info(self, amount):
#         self['PAYMENTREQUEST_0_AMT'] = amount
#         self['PAYMENTREQUEST_0_ITEMAMT'] = amount
#         self['PAYMENTREQUEST_0_DESC'] = 'Donation'
#         self['PAYMENTREQUEST_0_CURRENCYCODE'] = 'USD'
#         self['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Sale'
#         self['LOCALECODE'] = 'ES'
# 
#     def add_callbacks(self, return_url, cancel_url):
#         self['RETURNURL'] = return_url
#         self['CANCELURL'] = cancel_url
# 
#     def add_token(self, token, payerid):
#         self['TOKEN'] = token
#         self['PAYERID'] = payerid
# 
# 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


import unittest
from mock import patch
from pyramid import testing
from yithlibraryserver.contributions.paypal import PayPalPayload
from yithlibraryserver.contributions.paypal import PayPalExpressCheckout



class PayPalPayloadTests(unittest.TestCase):

    def setUp(self):
        self.config = testing.setUp(settings={
                'paypal_user': 'paypal_user',
                'paypal_password': 'paypal_password',
                'paypal_signature': 'paypal_signature',
                })

    def tearDown(self):
        testing.tearDown()

    def test_basic_payload(self):
        request = testing.DummyRequest()

        payload = PayPalPayload(request, 'method1')
        self.assertEqual(payload, {
                'METHOD': 'method1',
                'VERSION': '72.0',
                'USER': 'paypal_user',
                'PWD': 'paypal_password',
                'SIGNATURE': 'paypal_signature',
                })

    def test_payment_info(self):
        request = testing.DummyRequest()


# Path: yithlibraryserver/contributions/paypal.py
# class PayPalPayload(dict):
# 
#     def __init__(self, request, method):
#         self['METHOD'] = method
#         self['VERSION'] = '72.0'
#         self['USER'] = request.registry.settings['paypal_user']
#         self['PWD'] = request.registry.settings['paypal_password']
#         self['SIGNATURE'] = request.registry.settings['paypal_signature']
# 
#     def add_payment_info(self, amount):
#         self['PAYMENTREQUEST_0_AMT'] = amount
#         self['PAYMENTREQUEST_0_ITEMAMT'] = amount
#         self['PAYMENTREQUEST_0_DESC'] = 'Donation'
#         self['PAYMENTREQUEST_0_CURRENCYCODE'] = 'USD'
#         self['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Sale'
#         self['LOCALECODE'] = 'ES'
# 
#     def add_callbacks(self, return_url, cancel_url):
#         self['RETURNURL'] = return_url
#         self['CANCELURL'] = cancel_url
# 
#     def add_token(self, token, payerid):
#         self['TOKEN'] = token
#         self['PAYERID'] = payerid
# 
# 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/contributions/tests/test_paypal.py
import unittest
from mock import patch
from pyramid import testing
from yithlibraryserver.contributions.paypal import PayPalPayload
from yithlibraryserver.contributions.paypal import PayPalExpressCheckout




class PayPalPayloadTests(unittest.TestCase):

    def setUp(self):
        self.config = testing.setUp(settings={
                'paypal_user': 'paypal_user',
                'paypal_password': 'paypal_password',
                'paypal_signature': 'paypal_signature',
                })

    def tearDown(self):
        testing.tearDown()

    def test_basic_payload(self):
        request = testing.DummyRequest()

        payload = PayPalPayload(request, 'method1')
        self.assertEqual(payload, {
                'METHOD': 'method1',
                'VERSION': '72.0',
                'USER': 'paypal_user',
                'PWD': 'paypal_password',
                'SIGNATURE': 'paypal_signature',
                })

    def test_payment_info(self):
        request = testing.DummyRequest()


        payload = PayPalPayload(request, 'method_with_info')