def get_token_url(self, amount):
        """
        Get the URL, including token, to use for accessing Paypal.
        
        @param amount     Amount of sale
        
        @return           URL
        """

        data = self.credentials.copy()
        data.update({
            'METHOD': 'SetExpressCheckout',
            'RETURNURL': self.settings.return_url,
            'CANCELURL': self.settings.cancel_url,
            'AMT': amount,
            'NOSHIPPING': '1',
        })

        response = self.get_url(urllib.urlencode(data))
        if 'TOKEN' not in response:
            raise invalid_response_exception(str(response) + str(data))

        token = paypal_ec_token(token=response['TOKEN'],
                                amount=amount,
                                time=datetime.utcnow())
        token.save()

        # the 'useraction=commit' makes the paypal site appear to complete the transaction with
        # something like a "Pay Now" button, instead of sending the user back to us to review
        # the details before submitting a payment
        return token.token, '%s&useraction=commit&token=%s' % (
            settings.express_checkout_url, token.token)
    def get_token_url(self, amount):
        """
        Get the URL, including token, to use for accessing Paypal.
        
        @param amount     Amount of sale
        
        @return           URL
        """

        data = self.credentials.copy()
        data.update({
            'METHOD' : 'SetExpressCheckout',
            'RETURNURL' : self.settings.return_url,
            'CANCELURL' : self.settings.cancel_url,
            'AMT' : amount,
            'NOSHIPPING' : '1',
            })

        response = self.get_url(urllib.urlencode(data))
        if 'TOKEN' not in response:
            raise invalid_response_exception(str(response) + str(data))

        token = paypal_ec_token(token = response['TOKEN'], amount = amount,
                                time=datetime.utcnow())
        token.save()

        # the 'useraction=commit' makes the paypal site appear to complete the transaction with
        # something like a "Pay Now" button, instead of sending the user back to us to review
        # the details before submitting a payment
        return token.token, '%s&useraction=commit&token=%s' % (settings.express_checkout_url,
                                                               token.token)
    def test_cleanup_paypal_ec_tokens(self):
        # make some Paypal express checkout tokens in the db
        ec_tokens = [
            models.paypal_ec_token(amount='$100.00',
                                   token='1',
                                   time=(datetime.utcnow() -
                                         timedelta(hours=4))),
            models.paypal_ec_token(amount='$110.00',
                                   token='2',
                                   time=(datetime.utcnow() -
                                         timedelta(hours=3, minutes=30))),
            models.paypal_ec_token(amount='$120.00',
                                   token='3',
                                   time=(datetime.utcnow() -
                                         timedelta(hours=3))),
            models.paypal_ec_token(amount='$130.00',
                                   token='4',
                                   time=(datetime.utcnow() -
                                         timedelta(hours=2, minutes=30))),
            models.paypal_ec_token(amount='$140.00',
                                   token='5',
                                   time=(datetime.utcnow() -
                                         timedelta(hours=1))),
            models.paypal_ec_token(amount='$150.00',
                                   token='6',
                                   time=datetime.utcnow())
        ]

        for ec_token in ec_tokens:
            ec_token.save()

        self.pu.cleanup_paypal_ec_tokens()
        self.assertEquals(models.paypal_ec_token.objects.count(), 3)
        self.assertEquals(
            models.paypal_ec_token.objects.filter(token='4').count(), 1)
        self.assertEquals(
            models.paypal_ec_token.objects.filter(token='5').count(), 1)
        self.assertEquals(
            models.paypal_ec_token.objects.filter(token='6').count(), 1)
 def test_cleanup_paypal_ec_tokens(self):
     # make some Paypal express checkout tokens in the db
     ec_tokens = [models.paypal_ec_token(amount='$100.00', token='1',
                      time=(datetime.utcnow() - timedelta(hours=4))),
                  models.paypal_ec_token(amount='$110.00', token='2',
                      time=(datetime.utcnow() - timedelta(hours=3, minutes=30))),
                  models.paypal_ec_token(amount='$120.00', token='3',
                      time=(datetime.utcnow() - timedelta(hours=3))),
                  models.paypal_ec_token(amount='$130.00', token='4',
                      time=(datetime.utcnow() - timedelta(hours=2, minutes=30))),
                  models.paypal_ec_token(amount='$140.00', token='5',
                      time=(datetime.utcnow() - timedelta(hours=1))),
                  models.paypal_ec_token(amount='$150.00', token='6',
                      time=datetime.utcnow())]
     
     for ec_token in ec_tokens:
         ec_token.save()
 
     self.pu.cleanup_paypal_ec_tokens()
     self.assertEquals(models.paypal_ec_token.objects.count(), 3)
     self.assertEquals(models.paypal_ec_token.objects.filter(token='4').count(), 1)
     self.assertEquals(models.paypal_ec_token.objects.filter(token='5').count(), 1)
     self.assertEquals(models.paypal_ec_token.objects.filter(token='6').count(), 1)