Пример #1
0
    def test_paypal(self):
        # TODO: This checks the most simple and perfect payment that could
        # happen, but not errors or other/invalid IPN

        payment = Payment.objects.create(
            user=self.user,
            time=timedelta(days=30),
            backend='paypal',
            amount=300
        )

        settings = dict(
            TEST=True,
            TITLE='Test Title',
            CURRENCY='EUR',
            ADDRESS='*****@*****.**',
        )

        with self.settings(ROOT_URL='root'):
            backend = PaypalBackend(settings)
            redirect = backend.new_payment(payment)

        self.assertIsInstance(redirect, HttpResponseRedirect)

        host, params = redirect.url.split('?', 1)
        params = parse_qs(params)

        expected_notify_url = 'root/payments/callback/paypal/%d' % payment.id
        expected_return_url = 'root/payments/view/%d' % payment.id
        expected_cancel_url = 'root/payments/cancel/%d' % payment.id

        self.assertEqual(params['cmd'][0], '_xclick')
        self.assertEqual(params['notify_url'][0], expected_notify_url)
        self.assertEqual(params['return'][0], expected_return_url)
        self.assertEqual(params['cancel_return'][0], expected_cancel_url)
        self.assertEqual(params['business'][0], '*****@*****.**')
        self.assertEqual(params['currency_code'][0], 'EUR')
        self.assertEqual(params['amount'][0], '3.00')
        self.assertEqual(params['item_name'][0], 'Test Title')

        # Replace PaypalBackend.verify_ipn to not call the PayPal API
        # we will assume the IPN is authentic
        backend.verify_ipn = lambda request: True

        ipn_url = '/payments/callback/paypal/%d' % payment.id
        ipn_request = RequestFactory().post(
            ipn_url,
            content_type='application/x-www-form-urlencoded',
            data=PAYPAL_IPN_TEST)
        r = backend.callback(payment, ipn_request)

        self.assertTrue(r)
        self.assertEqual(payment.status, 'confirmed')
        self.assertEqual(payment.paid_amount, 300)
        self.assertEqual(payment.backend_extid, '61E67681CH3238416')
Пример #2
0
    def test_paypal(self):
        # TODO: This checks the most simple and perfect payment that could
        # happen, but not errors or other/invalid IPN

        payment = Payment.objects.create(user=self.user,
                                         time=timedelta(days=30),
                                         backend_id='paypal',
                                         amount=300)

        settings = dict(
            TEST=True,
            TITLE='Test Title',
            CURRENCY='EUR',
            ADDRESS='*****@*****.**',
        )

        with self.settings(ROOT_URL='root'):
            backend = PaypalBackend(settings)
            redirect = backend.new_payment(payment)

        self.assertIsInstance(redirect, HttpResponseRedirect)

        host, params = redirect.url.split('?', 1)
        params = parse_qs(params)

        expected_notify_url = 'root/payments/callback/paypal/%d' % payment.id
        expected_return_url = 'root/payments/view/%d' % payment.id
        expected_cancel_url = 'root/payments/cancel/%d' % payment.id

        self.assertEqual(params['cmd'][0], '_xclick')
        self.assertEqual(params['notify_url'][0], expected_notify_url)
        self.assertEqual(params['return'][0], expected_return_url)
        self.assertEqual(params['cancel_return'][0], expected_cancel_url)
        self.assertEqual(params['business'][0], '*****@*****.**')
        self.assertEqual(params['currency_code'][0], 'EUR')
        self.assertEqual(params['amount'][0], '3.00')
        self.assertEqual(params['item_name'][0], 'Test Title')

        # Replace PaypalBackend.verify_ipn to not call the PayPal API
        # we will assume the IPN is authentic
        backend.verify_ipn = lambda request: True

        ipn_url = '/payments/callback/paypal/%d' % payment.id
        ipn_request = RequestFactory().post(
            ipn_url,
            content_type='application/x-www-form-urlencoded',
            data=PAYPAL_IPN_TEST)
        r = backend.callback(payment, ipn_request)

        self.assertTrue(r)
        self.assertEqual(payment.status, 'confirmed')
        self.assertEqual(payment.paid_amount, 300)
        self.assertEqual(payment.backend_extid, '61E67681CH3238416')
Пример #3
0
    def test_paypal_ipn_error(self):
        payment = Payment.objects.create(
            user=self.user,
            time=timedelta(days=30),
            backend='paypal',
            amount=300
        )

        settings = dict(
            TEST=True,
            TITLE='Test Title',
            CURRENCY='EUR',
            ADDRESS='*****@*****.**',
        )

        with self.settings(ROOT_URL='root'):
            backend = PaypalBackend(settings)
            redirect = backend.new_payment(payment)

        self.assertIsInstance(redirect, HttpResponseRedirect)

        host, params = redirect.url.split('?', 1)
        params = parse_qs(params)

        # Replace PaypalBackend.verify_ipn to not call the PayPal API
        # we will assume the IPN is authentic
        backend.verify_ipn = lambda request: True

        ipn_url = '/payments/callback/paypal/%d' % payment.id
        ipn_request = RequestFactory().post(
            ipn_url,
            content_type='application/x-www-form-urlencoded',
            data=PAYPAL_IPN_TEST)
        r = backend.callback(payment, ipn_request)

        self.assertTrue(r)
        self.assertEqual(payment.status, 'confirmed')
        self.assertEqual(payment.paid_amount, 300)
        self.assertEqual(payment.backend_extid, '61E67681CH3238416')
Пример #4
0
    def test_paypal_ipn_error(self):
        payment = Payment.objects.create(user=self.user,
                                         time=timedelta(days=30),
                                         backend_id='paypal',
                                         amount=300)

        settings = dict(
            TEST=True,
            TITLE='Test Title',
            CURRENCY='EUR',
            ADDRESS='*****@*****.**',
        )

        with self.settings(ROOT_URL='root'):
            backend = PaypalBackend(settings)
            redirect = backend.new_payment(payment)

        self.assertIsInstance(redirect, HttpResponseRedirect)

        host, params = redirect.url.split('?', 1)
        params = parse_qs(params)

        # Replace PaypalBackend.verify_ipn to not call the PayPal API
        # we will assume the IPN is authentic
        backend.verify_ipn = lambda request: True

        ipn_url = '/payments/callback/paypal/%d' % payment.id
        ipn_request = RequestFactory().post(
            ipn_url,
            content_type='application/x-www-form-urlencoded',
            data=PAYPAL_IPN_TEST)
        r = backend.callback(payment, ipn_request)

        self.assertTrue(r)
        self.assertEqual(payment.status, 'confirmed')
        self.assertEqual(payment.paid_amount, 300)
        self.assertEqual(payment.backend_extid, '61E67681CH3238416')
Пример #5
0
    def test_paypal_subscr(self):
        subscription = Subscription.objects.create(
            user=self.user,
            backend='paypal',
            period='3m'
        )

        settings = dict(
            TEST=True,
            TITLE='Test Title',
            CURRENCY='EUR',
            ADDRESS='*****@*****.**',
        )

        with self.settings(ROOT_URL='root'):
            backend = PaypalBackend(settings)
            redirect = backend.new_subscription(subscription)

        self.assertIsInstance(redirect, HttpResponseRedirect)

        host, params = redirect.url.split('?', 1)
        params = parse_qs(params)

        expected_notify_url = 'root/payments/callback/paypal_subscr/%d' % subscription.id
        expected_return_url = 'root/payments/return_subscr/%d' % subscription.id
        expected_cancel_url = 'root/account/'

        self.assertEqual(params['cmd'][0], '_xclick-subscriptions')
        self.assertEqual(params['notify_url'][0], expected_notify_url)
        self.assertEqual(params['return'][0], expected_return_url)
        self.assertEqual(params['cancel_return'][0], expected_cancel_url)
        self.assertEqual(params['business'][0], '*****@*****.**')
        self.assertEqual(params['currency_code'][0], 'EUR')
        self.assertEqual(params['a3'][0], '9.00')
        self.assertEqual(params['p3'][0], '3')
        self.assertEqual(params['t3'][0], 'M')
        self.assertEqual(params['item_name'][0], 'Test Title')

        # Replace PaypalBackend.verify_ipn to not call the PayPal API
        # we will assume the IPN is authentic
        backend.verify_ipn = lambda request: True

        self.assertEqual(subscription.status, 'new')

        # 1. the subscr_payment IPN
        ipn_url = '/payments/callback/paypal_subscr/%d' % subscription.id
        ipn_request = RequestFactory().post(
            ipn_url,
            content_type='application/x-www-form-urlencoded',
            data=PAYPAL_IPN_SUBSCR_PAYMENT)
        r = backend.callback_subscr(subscription, ipn_request)

        self.assertTrue(r)
        self.assertEqual(subscription.status, 'active')
        self.assertEqual(subscription.backend_extid, 'I-1S262863X133')

        payments = Payment.objects.filter(subscription=subscription).all()
        self.assertEqual(len(payments), 1)
        self.assertEqual(payments[0].amount, 900)
        self.assertEqual(payments[0].paid_amount, 900)
        self.assertEqual(payments[0].backend_extid, '097872679P963871Y')

        # 2. the subscr_signup IPN
        # We don't expect anything to happen here
        ipn_url = '/payments/callback/paypal_subscr/%d' % subscription.id
        ipn_request = RequestFactory().post(
            ipn_url,
            content_type='application/x-www-form-urlencoded',
            data=PAYPAL_IPN_SUBSCR_SIGNUP)
        r = backend.callback_subscr(subscription, ipn_request)

        self.assertTrue(r)
        self.assertEqual(subscription.status, 'active')
        self.assertEqual(subscription.backend_extid, 'I-1S262863X133')

        payments = Payment.objects.filter(subscription=subscription).all()
        self.assertEqual(len(payments), 1)
        self.assertEqual(payments[0].amount, 900)
        self.assertEqual(payments[0].paid_amount, 900)
        self.assertEqual(payments[0].backend_extid, '097872679P963871Y')

        # 3. the subscr_cancel IPN
        ipn_url = '/payments/callback/paypal_subscr/%d' % subscription.id
        ipn_request = RequestFactory().post(
            ipn_url,
            content_type='application/x-www-form-urlencoded',
            data=PAYPAL_IPN_SUBSCR_CANCEL)
        r = backend.callback_subscr(subscription, ipn_request)

        self.assertTrue(r)
        self.assertEqual(subscription.status, 'cancelled')
        self.assertEqual(subscription.backend_extid, 'I-1S262863X133')

        payments = Payment.objects.filter(subscription=subscription).all()
        self.assertEqual(len(payments), 1)
Пример #6
0
    def test_paypal_subscr(self):
        subscription = Subscription.objects.create(user=self.user,
                                                   backend_id='paypal',
                                                   period='3m')

        settings = dict(
            TEST=True,
            TITLE='Test Title',
            CURRENCY='EUR',
            ADDRESS='*****@*****.**',
        )

        with self.settings(ROOT_URL='root'):
            backend = PaypalBackend(settings)
            redirect = backend.new_subscription(subscription)

        self.assertIsInstance(redirect, HttpResponseRedirect)

        host, params = redirect.url.split('?', 1)
        params = parse_qs(params)

        expected_notify_url = 'root/payments/callback/paypal_subscr/%d' % subscription.id
        expected_return_url = 'root/payments/return_subscr/%d' % subscription.id
        expected_cancel_url = 'root/account/'

        self.assertEqual(params['cmd'][0], '_xclick-subscriptions')
        self.assertEqual(params['notify_url'][0], expected_notify_url)
        self.assertEqual(params['return'][0], expected_return_url)
        self.assertEqual(params['cancel_return'][0], expected_cancel_url)
        self.assertEqual(params['business'][0], '*****@*****.**')
        self.assertEqual(params['currency_code'][0], 'EUR')
        self.assertEqual(params['a3'][0], '9.00')
        self.assertEqual(params['p3'][0], '3')
        self.assertEqual(params['t3'][0], 'M')
        self.assertEqual(params['item_name'][0], 'Test Title')

        # Replace PaypalBackend.verify_ipn to not call the PayPal API
        # we will assume the IPN is authentic
        backend.verify_ipn = lambda request: True

        self.assertEqual(subscription.status, 'new')

        # 1. the subscr_payment IPN
        ipn_url = '/payments/callback/paypal_subscr/%d' % subscription.id
        ipn_request = RequestFactory().post(
            ipn_url,
            content_type='application/x-www-form-urlencoded',
            data=PAYPAL_IPN_SUBSCR_PAYMENT)
        r = backend.callback_subscr(subscription, ipn_request)

        self.assertTrue(r)
        self.assertEqual(subscription.status, 'active')
        self.assertEqual(subscription.backend_extid, 'I-1S262863X133')

        payments = Payment.objects.filter(subscription=subscription).all()
        self.assertEqual(len(payments), 1)
        self.assertEqual(payments[0].amount, 900)
        self.assertEqual(payments[0].paid_amount, 900)
        self.assertEqual(payments[0].backend_extid, '097872679P963871Y')

        # 2. the subscr_signup IPN
        # We don't expect anything to happen here
        ipn_url = '/payments/callback/paypal_subscr/%d' % subscription.id
        ipn_request = RequestFactory().post(
            ipn_url,
            content_type='application/x-www-form-urlencoded',
            data=PAYPAL_IPN_SUBSCR_SIGNUP)
        r = backend.callback_subscr(subscription, ipn_request)

        self.assertTrue(r)
        self.assertEqual(subscription.status, 'active')
        self.assertEqual(subscription.backend_extid, 'I-1S262863X133')

        payments = Payment.objects.filter(subscription=subscription).all()
        self.assertEqual(len(payments), 1)
        self.assertEqual(payments[0].amount, 900)
        self.assertEqual(payments[0].paid_amount, 900)
        self.assertEqual(payments[0].backend_extid, '097872679P963871Y')

        # 3. the subscr_cancel IPN
        ipn_url = '/payments/callback/paypal_subscr/%d' % subscription.id
        ipn_request = RequestFactory().post(
            ipn_url,
            content_type='application/x-www-form-urlencoded',
            data=PAYPAL_IPN_SUBSCR_CANCEL)
        r = backend.callback_subscr(subscription, ipn_request)

        self.assertTrue(r)
        self.assertEqual(subscription.status, 'cancelled')
        self.assertEqual(subscription.backend_extid, 'I-1S262863X133')

        payments = Payment.objects.filter(subscription=subscription).all()
        self.assertEqual(len(payments), 1)