def donation_successfull():

    provider = payment(
        provider='paypal',
        style='payment')

    bill = provider.confirm(request.args)
    if bill:
        pledge = donate.get_pledge({'name': bill.get('name')}).get()

        environment = int(provider.environment=='production')
        donate.add_payment().execute({
            'provider_id': provider.provider_id, 
            'pledge_id': pledge.get('id',''), 
            'reference': bill.get('reference'), 
            'amount': bill.get('amount'), 
            'environment': environment})
        
        web.page.create('Thanks for your donation')
        web.paragraph.create(
            """Thanks your payment has been recieved.""")
    else:
        web.page.create('Something went wrong')
        web.paragraph.create(
            """We could not confirm the payment something may have gone terribly wrong.""")

    web.template.create('Maidstone Hackspace')
    header('Maidstone Hackspace Donations')
    web.page.create('Thanks for your donation')
    web.paragraph.create(
        """Thanks your payment has been recieved.""")
    web.page.section(web.paragraph.render())
    web.template.body.append(web.page.render())
    return web.render()
def populate_by_name():
    web.template.create('Maidstone Hackspace')
    header('Maidstone Hackspace Donations')

    pledge = donate.get_pledge({'name': '#lair'}).get()

    import gocardless
    gocardless.environment = gocardless_environment
    gocardless.set_details(**gocardless_credentials)
    merchant = gocardless.client.merchant()

    web.template.body.append('Adding Badges')
    badges.create_badge().execute({'name': 'member'})
    badges.create_badge().execute({'name': 'backer'})
    badges.create_badge().execute({'name': 'teacher'})
    badges.create_badge().execute({'name': 'chairman'})
    badges.create_badge().execute({'name': 'treasurer'})
    badges.create_badge().execute({'name': 'secretary'})

    web.template.body.append('Populating users')
    user_list = {}
    #make sure we have all users in the system
    #~ users_emails = []
    for user in merchant.users():
        user_list[user.id] = user.email
        #~ users_emails.append(user.email)
        site_user.create_basic_user().execute({
            'email': user.email,
            'first_name': user.first_name,
            'last_name': user.last_name
        })

    #get the users ids and emails 
    #~ users = {}
    #~ for member in members.get_members():
        #~ for key, value in user_list.items():
            #~ if value == member.get('email'):
                #~ user_list[key] = member.get('user_id')

    badge_lookup = {badge.get('name'):badge.get('id') for badge in badges.fetch_badges()}

    web.template.body.append('Setting Donation Badges')
    environment = int(gocardless_environment=='production')
    for bill in merchant.bills():
        web.template.body.append(str(bill))

        matched_user = None
        for user_id, user_email in user_list.items():
            if user_email == user.email:
                matched_user = user_id

        donate.add_payment().execute({'user_id': matched_user,'pledge_id': pledge.get('id') , 'reference': bill.id, 'amount': bill.amount_minus_fees, 'environment': environment})

        if matched_user:
            badges.assign_badge().execute({'badge_id': badge_lookup.get('backer'), 'user_id': matched_user})

    return web.render()
def populate_by_name():
    pledge = donate.get_pledge({'name': '#lair'}).get()
    import gocardless
    gocardless.environment = gocardless_environment
    gocardless.set_details(**gocardless_credentials)
    merchant = gocardless.client.merchant()
    for bill in merchant.bills():
        environment = int(gocardless_environment=='production')
        donate.add_payment().execute({'pledge_id':pledge.get('id','') , 'reference': bill.id, 'amount': bill.amount_minus_fees, 'environment': environment})
    return abort()
def populate_by_name():
    pledge = donate.get_pledge({'name': '#lair'}).get()
    import gocardless
    gocardless.environment = gocardless_environment
    gocardless.set_details(**gocardless_credentials)
    merchant = gocardless.client.merchant()
    for bill in merchant.bills():
        environment = int(gocardless_environment == 'production')
        donate.add_payment().execute({
            'pledge_id': pledge.get('id', ''),
            'reference': bill.id,
            'amount': bill.amount_minus_fees,
            'environment': environment
        })
    return abort()
def success_donation():
    # confirm the payment

    bill_id = request.args.get('resource_id')
    try:
        import gocardless
        gocardless.environment = gocardless_environment
        gocardless.set_details(**gocardless_credentials)
        gocardless.client.confirm_resource(request.args)
        web.page.create('Thanks for your donation')
        web.paragraph.create("""Thanks your payment has been recieved.""")
    except:
        # TODO log what actually has gone wrong
        web.page.create('Something went wrong')
        web.paragraph.create(
            """We could not confirm the payment something may have gone terribly wrong."""
        )

    if bill_id:
        bill = gocardless.client.bill(bill_id)
        pledge = donate.get_pledge({'name': bill.name}).get()

        #~ print dir(bill)

        print bill.amount
        print bill.amount_minus_fees
        print bill.charge_customer_at
        print bill.created_at
        print bill.name
        print bill.payout
        print bill.status
        print bill.user
        environment = int(gocardless_environment == 'production')
        donate.add_payment().execute({
            'pledge_id': pledge.get('name', ''),
            'reference': bill_id,
            'amount': bill.amount_minus_fees,
            'environment': environment
        })

    web.template.create('Maidstone Hackspace')
    header('Maidstone Hackspace Donations')
    web.page.create('Thanks for your donation')
    web.paragraph.create("""Thanks your payment has been recieved.""")
    web.page.section(web.paragraph.render())
    web.template.body.append(web.page.render())
    return footer()
def success_donation():
    # confirm the payment
    
    bill_id = request.args.get('resource_id')
    try:
        import gocardless
        gocardless.environment = gocardless_environment
        gocardless.set_details(**gocardless_credentials)
        gocardless.client.confirm_resource(request.args)
        web.page.create('Thanks for your donation')
        web.paragraph.create(
            """Thanks your payment has been recieved.""")
    except:
        # TODO log what actually has gone wrong
        web.page.create('Something went wrong')
        web.paragraph.create(
            """We could not confirm the payment something may have gone terribly wrong.""")

    if bill_id:
        bill = gocardless.client.bill(bill_id)
        pledge = donate.get_pledge({'name': bill.name}).get()
        
        #~ print dir(bill)

        print bill.amount
        print bill.amount_minus_fees
        print bill.charge_customer_at
        print bill.created_at
        print bill.name
        print bill.payout
        print bill.status
        print bill.user
        environment = int(gocardless_environment=='production')
        donate.add_payment().execute({'pledge_id':pledge.get('name','') , 'reference': bill_id, 'amount': bill.amount_minus_fees, 'environment': environment})


    web.template.create('Maidstone Hackspace')
    header('Maidstone Hackspace Donations')
    web.page.create('Thanks for your donation')
    web.paragraph.create(
        """Thanks your payment has been recieved.""")
    web.page.section(web.paragraph.render())
    web.template.body.append(web.page.render())
    return footer()