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 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')
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' )