def test_non_200_response_raises_exception(self): response = self.create_mock_response(body='', status_code=500) with patch('requests.post') as post: post.return_value = response with self.assertRaises(exceptions.PayPalError): gateway.set_txn(self.basket, self.methods, 'GBP', 'http://localhost:8000/success', 'http://localhost:8000/error')
def test_forbids_zero_value_basket(self): basket = create_mock_basket(D('0.00')) shipping_methods = [FixedPrice(D('2.50'))] with patch('paypal.express.gateway._fetch_response') as mock_fetch: with self.assertRaises(InvalidBasket): gateway.set_txn(basket, shipping_methods, 'GBP', 'http://example.com', 'http://example.com')
def test_error_response_raises_exception(self): response_body = 'TIMESTAMP=2012%2d03%2d26T16%3a33%3a09Z&CORRELATIONID=3bea2076bb9c3&ACK=Failure&VERSION=0%2e000000&BUILD=2649250&L_ERRORCODE0=10002&L_SHORTMESSAGE0=Security%20error&L_LONGMESSAGE0=Security%20header%20is%20not%20valid&L_SEVERITYCODE0=Error' response = self.create_mock_response(response_body) with patch('requests.post') as post: post.return_value = response with self.assertRaises(exceptions.PayPalError): gateway.set_txn(self.basket, self.methods, 'GBP', 'http://localhost:8000/success', 'http://localhost:8000/error')
def test_not_forbid_if_shipping_not_zero(self): basket = create_mock_basket(D('0.00')) shipping_methods = [FixedPrice(D('2.50'), D('2.50'))] with patch('paypal.express.gateway._fetch_response') as mock_fetch: gateway.set_txn(basket, shipping_methods, 'GBP', 'http://example.com', 'http://example.com') args, __ = mock_fetch.call_args params = args[1] self.assertEqual(params['PAYMENTREQUEST_0_AMT'], D('2.50'))
def get_paypal_url(basket, shipping_methods, user=None, shipping_address=None, shipping_method=None, host=None, scheme='https', paypal_params=None): """ Return the URL for a PayPal Express transaction. This involves registering the txn with PayPal to get a one-time URL. If a shipping method and shipping address are passed, then these are given to PayPal directly - this is used within when using PayPal as a payment method. """ currency = getattr(settings, 'PAYPAL_CURRENCY', 'GBP') if host is None: host = Site.objects.get_current().domain return_url = '%s://%s%s' % (scheme, host, reverse('paypal-success-response', kwargs={'basket_id': basket.id})) cancel_url = '%s://%s%s' % (scheme, host, reverse('paypal-cancel-response', kwargs={'basket_id': basket.id})) # URL for updating shipping methods - we only use this if we have a set of # shipping methods to choose between. update_url = None if shipping_methods: update_url = '%s://%s%s' % (scheme, host, reverse('paypal-shipping-options', kwargs={'basket_id': basket.id})) # Determine whether a shipping address is required no_shipping = False if not basket.is_shipping_required(): no_shipping = True # Pass a default billing address is there is one. This means PayPal can # pre-fill the registration form. address = None if user: addresses = user.addresses.all().order_by('-is_default_for_billing') if len(addresses): address = addresses[0] return set_txn(basket=basket, shipping_methods=shipping_methods, currency=currency, return_url=return_url, cancel_url=cancel_url, update_url=update_url, action=_get_payment_action(), shipping_method=shipping_method, shipping_address=shipping_address, user=user, user_address=address, no_shipping=no_shipping, paypal_params=paypal_params)
def setUp(self): super(SuccessResponseTests, self).setUp() response_body = 'TOKEN=EC%2d6469953681606921P&TIMESTAMP=2012%2d03%2d26T17%3a19%3a38Z&CORRELATIONID=50a8d895e928f&ACK=Success&VERSION=60%2e0&BUILD=2649250' response = self.create_mock_response(response_body) with patch('requests.post') as post: post.return_value = response self.url = gateway.set_txn(self.basket, self.methods, 'GBP', 'http://localhost:8000/success', 'http://localhost:8000/error')