コード例 #1
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': '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)
コード例 #2
0
ファイル: views.py プロジェクト: srus/yith-library-server
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'))
コード例 #3
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)
コード例 #4
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"))
コード例 #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 = 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'))