示例#1
0
    def test_access_denied_error(self):
        request = type('', (), dict(GET={'error': 'access_denied'}))()

        error = get_oauth_request_error(request)

        self.assertIsNotNone(error)
        self.assertEquals(error, StripeOAuthError.ACCESS_DENIED)
示例#2
0
    def test_not_handled_error(self):
        request = type('', (),
                       dict(GET={'error': 'unsupported_response_type'}))()

        error = get_oauth_request_error(request)

        self.assertIsNone(error)
示例#3
0
    def test_access_denied_error(self):
        request = type('', (), dict(GET={'error': 'access_denied'}))()

        error = get_oauth_request_error(request)

        self.assertIsNotNone(error)
        self.assertEquals(error, StripeOAuthError.ACCESS_DENIED)
示例#4
0
    def test_no_errors(self):
        request = type('', (), dict(GET={}))()

        error = get_oauth_request_error(request)

        self.assertIsNone(error)
示例#5
0
    def test_not_handled_error(self):
        request = type('', (), dict(GET={'error': 'unsupported_response_type'}))()

        error = get_oauth_request_error(request)

        self.assertIsNone(error)
示例#6
0
    def test_no_errors(self):
        request = type('', (), dict(GET={}))()

        error = get_oauth_request_error(request)

        self.assertIsNone(error)
示例#7
0
def stripe(request):
    error = get_oauth_request_error(request)
    if error and error == StripeOAuthError.ACCESS_DENIED:
        return redirect('/club/dashboard')

    exception_occurred = False
    club, club_entity = get_club_and_club_entity(request)
    message = None
    json_data = None
    try:
        code = request.GET.get('code', '')
        params = urllib.urlencode({
            'client_secret': settings.STRIPE_SECRET_KEY,
            'code': code,
            'grant_type': 'authorization_code'})
        url = '/oauth/token?%s' % params
        connection = httplib.HTTPSConnection('connect.stripe.com')
        connection.connect()
        connection.request('POST', url)
        resp = connection.getresponse()
        resp_data = resp.read()
        json_data = json.loads(resp_data)
        stripe_user = StripeUser(
            club=club,
            code=code,
            access_token=json_data['access_token'],
            refresh_token=json_data['refresh_token'],
            publishable_key=json_data['stripe_publishable_key'],
            user_id=json_data['stripe_user_id'],
            scope=json_data['scope'],
            token_type=json_data['token_type'])
        stripe_user.save()
        stripe_controller = StripeController(stripe_user)
        account_details = stripe_controller.get_account_details()
        if not account_details or not account_details.get('display_name'):
            message = 'It looks like you registered with Stripe but did not provide a valid business name for your ' \
                      'organization. Please contact [email protected] quoting "NO STRIPE NAME".'
            raise Exception
        club_display_name = account_details['display_name']
        club.name = club_display_name
        club.name_is_fixed = True
        club.save()
        messages.success(
            request,
            "Congratulations, you have successfully connected with Stripe and can now start fundraising on Spudder. "
            "Your organizations name has been changed to match the IRS records you provided to Stripe and is now set "
            "to '%s'." % club_display_name)
    except httplib.HTTPException:
        exception_occurred = True
        logging.error('Http connection exception while trying to contact Stripe API server')
    except KeyError:
        exception_occurred = True
        logging.error('Access token missing in JSON object. Probably Stripe keys are configured improperly')
        logging.error(json_data)
    except Exception as e:
        exception_occurred = True

    if exception_occurred:
        logging.error(traceback.format_exc())
        try:
            StripeUser.objects.get(club=club).delete()
        except StripeUser.DoesNotExist:
            pass
        if not message:
            message = "Sorry, there was an error connecting your stripe account. Please try again and contact " \
                      "[email protected] if the problem continues."
        messages.error(
            request,
            message)
    return redirect('/club/dashboard')