Exemplo n.º 1
0
def join_post(id=None):
    data = request.json
    c = campaign.get(id)
    if c is None:
        return "Unknown campaign.", 404
    usr = current_user._get_current_object()
    token = data['stripeToken']
    full_name = data['fullName']
    stripe_private_key = app.config.get('STRIPE_SECRET_KEY')
    try:
        user.associate_stripe_token(usr, token, stripe_private_key, True)
    except (PreviousStripeAssociationError, ExternalAPIUsageError):
        return make_response(('We boned it, please try again later.', 500))
    except (ExternalAPIError, ExternalAPIUnavailableError):
        return make_response((json.dumps(
            {'error': 'Request failed due to an external dependency.'}), 424))

    charge = c.disburse_funds_when == 'immediately'
    amount = Decimal(data.get('amount'))
    curr = data.get('currency')
    curr = currency.get(curr)
    fees = fee.get(None, fee_names=['stripe-transaction', 'poold-transaction'])

    if charge:
        success = charge_user(usr, c, amount, curr, fees, full_name=full_name)
        if not success:
            return make_response(('We boned it, please try again later.', 500))
    try:
        campaign.associate_user(c,
                                usr,
                                'participant',
                                'participating',
                                pledge=amount)
    except DuplicateCampaignUserAssociationError:
        campaign.update_user_association(c, usr, role=None, pledge=amount)

    ledger = fee.calculate(fees, amount)
    ledger['date'] = datetime.utcnow().strftime('%m/%d/%Y')
    send_receipt_email(c, usr, ledger)

    ledger = sanitize_json(ledger)
    ledger['date'] = int(time.time()) * 1000

    return make_response(
        (json.dumps(ledger), 201, [('Content-Type', 'application/json')]))
Exemplo n.º 2
0
def join_post(id=None):
    data = request.json
    c = campaign.get(id)
    if c is None:
        return "Unknown campaign.", 404
    usr = current_user._get_current_object()
    token = data['stripeToken']
    full_name = data['fullName']
    stripe_private_key = app.config.get('STRIPE_SECRET_KEY')
    try:
        user.associate_stripe_token(usr, token, stripe_private_key, True)
    except (PreviousStripeAssociationError,
            ExternalAPIUsageError):
        return make_response(('We boned it, please try again later.', 500))
    except (ExternalAPIError,
            ExternalAPIUnavailableError):
        return make_response((json.dumps({'error': 'Request failed due to an external dependency.'}), 424))

    charge = c.disburse_funds_when == 'immediately'
    amount = Decimal(data.get('amount'))
    curr = data.get('currency')
    curr = currency.get(curr)
    fees = fee.get(None, fee_names=['stripe-transaction', 'poold-transaction'])

    if charge:
        success = charge_user(usr, c, amount, curr, fees, full_name=full_name)
        if not success:
            return make_response(('We boned it, please try again later.', 500))
    try:
        campaign.associate_user(c, usr, 'participant', 'participating', pledge=amount)
    except DuplicateCampaignUserAssociationError:
        campaign.update_user_association(c, usr, role=None, pledge=amount)

    ledger = fee.calculate(fees, amount)
    ledger['date'] = datetime.utcnow().strftime('%m/%d/%Y')
    send_receipt_email(c, usr, ledger)

    ledger = sanitize_json(ledger)
    ledger['date'] = int(time.time()) * 1000

    return make_response((json.dumps(ledger), 201, [('Content-Type', 'application/json')]))