예제 #1
0
    def test_express_checkout_details(self):
        request = testing.DummyRequest()
        pec = PayPalExpressCheckout(request)
        self.assertEqual(pec.nvp_url, 'http://paypal.com/nvp')
        self.assertEqual(pec.express_checkout_url,
                         'http://paypal.com/express_checkout')

        with patch('requests.post') as fake:
            fake.return_value.ok = True
            fake.return_value.text = 'ACK=Success&AMT=5.00&FIRSTNAME=John&LASTNAME=Doe&SHIPTOCITY=ExampleCity&SHIPTOCOUNTRYNAME=ExampleCountry&SHIPTOSTATE=ExampleState&SHIPTOSTREET=ExampleStreet&SHIPTOZIP=123456&[email protected]'
            result = pec.get_express_checkout_details('123', '456')
            fake.assert_called_with('http://paypal.com/nvp',
                                    data={
                                        'METHOD': 'GetExpressCheckoutDetails',
                                        'VERSION': '72.0',
                                        'USER': '******',
                                        'PWD': 'paypal_password',
                                        'SIGNATURE': 'paypal_signature',
                                        'TOKEN': '123',
                                        'PAYERID': '456',
                                    })
            self.assertEqual(
                result, {
                    'amount': 5,
                    'firstname': 'John',
                    'lastname': 'Doe',
                    'city': 'ExampleCity',
                    'country': 'ExampleCountry',
                    'state': 'ExampleState',
                    'street': 'ExampleStreet',
                    'zip': '123456',
                    'email': '*****@*****.**',
                })
예제 #2
0
    def test_express_checkout_details(self):
        request = testing.DummyRequest()
        pec = PayPalExpressCheckout(request)
        self.assertEqual(pec.nvp_url, 'http://paypal.com/nvp')
        self.assertEqual(pec.express_checkout_url,
                         'http://paypal.com/express_checkout')

        with mock.patch('requests.post') as fake:
            fake.return_value.ok = True
            fake.return_value.text = 'ACK=Success&AMT=5.00&FIRSTNAME=John&LASTNAME=Doe&SHIPTOCITY=ExampleCity&SHIPTOCOUNTRYNAME=ExampleCountry&SHIPTOSTATE=ExampleState&SHIPTOSTREET=ExampleStreet&SHIPTOZIP=123456&[email protected]'
            result = pec.get_express_checkout_details('123', '456')
            fake.assert_called_with('http://paypal.com/nvp', data={
                'METHOD': 'GetExpressCheckoutDetails',
                'VERSION': '72.0',
                'USER': '******',
                'PWD': 'paypal_password',
                'SIGNATURE': 'paypal_signature',
                'TOKEN': '123',
                'PAYERID': '456',
            })
            self.assertEqual(result, {
                'amount': 5,
                'firstname': 'John',
                'lastname': 'Doe',
                'city': 'ExampleCity',
                'country': 'ExampleCountry',
                'state': 'ExampleState',
                'street': 'ExampleStreet',
                'zip': '123456',
                'email': '*****@*****.**',
            })
예제 #3
0
    def test_express_checkout_token(self):
        request = testing.DummyRequest()
        pec = PayPalExpressCheckout(request)
        self.assertEqual(pec.nvp_url, 'http://paypal.com/nvp')
        self.assertEqual(pec.express_checkout_url,
                         'http://paypal.com/express_checkout')

        with mock.patch('requests.post') as fake:
            fake.return_value.ok = True
            fake.return_value.text = 'ACK=Success&TOKEN=123'
            result = pec.get_express_checkout_token(5)
            fake.assert_called_with('http://paypal.com/nvp', data={
                'METHOD': 'SetExpressCheckout',
                'VERSION': '72.0',
                'USER': '******',
                'PWD': 'paypal_password',
                'SIGNATURE': 'paypal_signature',
                'LOCALECODE': 'EN',
                'PAYMENTREQUEST_0_ITEMAMT': 5,
                'PAYMENTREQUEST_0_PAYMENTACTION': 'Sale',
                'PAYMENTREQUEST_0_CURRENCYCODE': 'USD',
                'PAYMENTREQUEST_0_AMT': 5,
                'PAYMENTREQUEST_0_DESC': 'Donation',
                'L_PAYMENTREQUEST_0_NAME0': 'Donation of $5',
                'L_PAYMENTREQUEST_0_AMT0': 5,
                'BRANDNAME': 'Yith Library',
                'RETURNURL': 'http://example.com/contribute/paypal-success-callback',
                'CANCELURL': 'http://example.com/contribute/paypal-cancel-callback',
            })
            self.assertEqual(result, '123')

        self.assertEqual(
            pec.get_express_checkout_url('123'),
            'http://paypal.com/express_checkout?cmd=_express-checkout&token=123')
    def test_express_checkout_payment(self):
        request = testing.DummyRequest()
        pec = PayPalExpressCheckout(request)
        self.assertEqual(pec.nvp_url, 'http://paypal.com/nvp')
        self.assertEqual(pec.express_checkout_url,
                         'http://paypal.com/express_checkout')

        with patch('requests.post') as fake:
            fake.return_value.ok = True
            fake.return_value.text = 'ACK=Success'
            result = pec.do_express_checkout_payment('123', '456', 5)
            fake.assert_called_with('http://paypal.com/nvp', data={
                    'METHOD': 'DoExpressCheckoutPayment',
                    'VERSION': '72.0',
                    'USER': '******',
                    'PWD': 'paypal_password',
                    'SIGNATURE': 'paypal_signature',
                    'TOKEN': '123',
                    'PAYERID': '456',
                    'LOCALECODE': 'ES',
                    'PAYMENTREQUEST_0_ITEMAMT': 5,
                    'PAYMENTREQUEST_0_PAYMENTACTION': 'Sale',
                    'PAYMENTREQUEST_0_CURRENCYCODE': 'USD',
                    'PAYMENTREQUEST_0_AMT': 5,
                    'PAYMENTREQUEST_0_DESC': 'Donation',
                    })
            self.assertTrue(result)

            # Simulate a failure
            fake.return_value.ok = True
            fake.return_value.text = 'ACK=Failure'
            result = pec.do_express_checkout_payment('123', '456', 5)
            self.assertFalse(result)
예제 #5
0
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'),
                    'success',
                )
            else:
                request.session.flash(error_msg, 'error')

            return HTTPFound(location=request.route_path('contributions_index'))

        elif 'cancel' in request.POST:
            return HTTPFound(location=request.route_path('contributions_paypal_cancel_callback'))

        else:
            return HTTPBadRequest('Wrong action')

    else:
        token = request.GET.get('token', None)
        payerid = request.GET.get('PayerID', None)

        if token and payerid:
            paypal = PayPalExpressCheckout(request)
            details = paypal.get_express_checkout_details(token, payerid)
            details.update({
                'token': token,
                'payerid': payerid,
            })
            amount = details['amount']
            details['include_sticker'] = include_sticker(amount)
            return details
        else:
            request.session.flash(error_msg, 'error')
            return HTTPFound(location=request.route_path('contributions_index'))
예제 #6
0
def contributions_donate(request):
    if 'submit' in request.POST:
        paypal = PayPalExpressCheckout(request)
        amount = request.POST.get('amount', '1')
        try:
            amount = int(amount)
        except ValueError:
            return HTTPBadRequest('Amount must be an integer')
        token = paypal.get_express_checkout_token(amount)
        return HTTPFound(paypal.get_express_checkout_url(token))

    return HTTPBadRequest('Wrong action or method')
예제 #7
0
    def test_express_checkout_token(self):
        request = testing.DummyRequest()
        pec = PayPalExpressCheckout(request)
        self.assertEqual(pec.nvp_url, 'http://paypal.com/nvp')
        self.assertEqual(pec.express_checkout_url,
                         'http://paypal.com/express_checkout')

        with patch('requests.post') as fake:
            fake.return_value.ok = True
            fake.return_value.text = 'ACK=Success&TOKEN=123'
            result = pec.get_express_checkout_token(5)
            fake.assert_called_with(
                'http://paypal.com/nvp',
                data={
                    'METHOD':
                    'SetExpressCheckout',
                    'VERSION':
                    '72.0',
                    'USER':
                    '******',
                    'PWD':
                    'paypal_password',
                    'SIGNATURE':
                    'paypal_signature',
                    'LOCALECODE':
                    'EN',
                    'PAYMENTREQUEST_0_ITEMAMT':
                    5,
                    'PAYMENTREQUEST_0_PAYMENTACTION':
                    'Sale',
                    'PAYMENTREQUEST_0_CURRENCYCODE':
                    'USD',
                    'PAYMENTREQUEST_0_AMT':
                    5,
                    'PAYMENTREQUEST_0_DESC':
                    'Donation',
                    'L_PAYMENTREQUEST_0_NAME0':
                    'Donation of $5',
                    'L_PAYMENTREQUEST_0_AMT0':
                    5,
                    'BRANDNAME':
                    'Yith Library',
                    'RETURNURL':
                    'http://example.com/contribute/paypal-success-callback',
                    'CANCELURL':
                    'http://example.com/contribute/paypal-cancel-callback',
                })
            self.assertEqual(result, '123')

        self.assertEqual(
            pec.get_express_checkout_url('123'),
            'http://paypal.com/express_checkout?cmd=_express-checkout&token=123'
        )
예제 #8
0
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"), "success")
            else:
                request.session.flash(error_msg, "error")

            return HTTPFound(location=request.route_path("contributions_index"))

        elif "cancel" in request.POST:
            return HTTPFound(location=request.route_path("contributions_paypal_cancel_callback"))

        else:
            return HTTPBadRequest("Wrong action")

    else:
        token = request.GET.get("token", None)
        payerid = request.GET.get("PayerID", None)

        if token and payerid:
            paypal = PayPalExpressCheckout(request)
            details = paypal.get_express_checkout_details(token, payerid)
            details.update({"token": token, "payerid": payerid})
            amount = details["amount"]
            details["include_sticker"] = include_sticker(amount)
            return details
        else:
            request.session.flash(error_msg, "error")
            return HTTPFound(location=request.route_path("contributions_index"))
예제 #9
0
    def test_express_checkout_payment(self):
        request = testing.DummyRequest()
        pec = PayPalExpressCheckout(request)
        self.assertEqual(pec.nvp_url, 'http://paypal.com/nvp')
        self.assertEqual(pec.express_checkout_url,
                         'http://paypal.com/express_checkout')

        with patch('requests.post') as fake:
            fake.return_value.ok = True
            fake.return_value.text = 'ACK=Success'
            result = pec.do_express_checkout_payment('123', '456', 5)
            fake.assert_called_with('http://paypal.com/nvp',
                                    data={
                                        'METHOD': 'DoExpressCheckoutPayment',
                                        'VERSION': '72.0',
                                        'USER': '******',
                                        'PWD': 'paypal_password',
                                        'SIGNATURE': 'paypal_signature',
                                        'TOKEN': '123',
                                        'PAYERID': '456',
                                        'LOCALECODE': 'EN',
                                        'PAYMENTREQUEST_0_ITEMAMT': 5,
                                        'PAYMENTREQUEST_0_PAYMENTACTION':
                                        'Sale',
                                        'PAYMENTREQUEST_0_CURRENCYCODE': 'USD',
                                        'PAYMENTREQUEST_0_AMT': 5,
                                        'PAYMENTREQUEST_0_DESC': 'Donation',
                                        'L_PAYMENTREQUEST_0_NAME0':
                                        'Donation of $5',
                                        'L_PAYMENTREQUEST_0_AMT0': 5,
                                        'BRANDNAME': 'Yith Library',
                                    })
            self.assertTrue(result)

            # Simulate a failure
            fake.return_value.ok = True
            fake.return_value.text = 'ACK=Failure'
            result = pec.do_express_checkout_payment('123', '456', 5)
            self.assertFalse(result)
예제 #10
0
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 = Donation(
                    amount=amount,
                    first_name=request.POST['firstname'],
                    last_name=request.POST['lastname'],
                    city=request.POST['city'],
                    country=request.POST['country'],
                    state=request.POST['state'],
                    street=request.POST['street'],
                    zipcode=request.POST['zip'],
                    email=request.POST['email'],
                )
                if donation.should_include_sticker():
                    if 'no-sticker' in request.POST:
                        donation.send_sticker = False
                    else:
                        donation.send_sticker = True
                else:
                    donation.send_sticker = False

                donation.user = request.user

                Session.add(donation)
                Session.flush()

                send_thankyou_email(request, donation)
                send_notification_to_admins(request, donation)
                request.session.flash(
                    _('Thank you very much for your great contribution'),
                    'success',
                )
            else:
                request.session.flash(error_msg, 'error')

            return HTTPFound(location=request.route_path('contributions_index'))

        elif 'cancel' in request.POST:
            return HTTPFound(location=request.route_path('contributions_paypal_cancel_callback'))

        else:
            return HTTPBadRequest('Wrong action')

    else:
        token = request.GET.get('token', None)
        payerid = request.GET.get('PayerID', None)

        if token and payerid:
            paypal = PayPalExpressCheckout(request)
            details = paypal.get_express_checkout_details(token, payerid)
            details.update({
                'token': token,
                'payerid': payerid,
            })
            amount = details['amount']
            details['include_sticker'] = include_sticker(amount)
            return details
        else:
            request.session.flash(error_msg, 'error')
            return HTTPFound(location=request.route_path('contributions_index'))