mirror of https://gitlab.com/tildes/tildes.git
Deimos
5 years ago
9 changed files with 124 additions and 27 deletions
-
24salt/salt/nginx/tildes.conf.jinja2
-
3tildes/production.ini.example
-
10tildes/static/js/behaviors/stripe-checkout.js
-
1tildes/tildes/lib/ratelimit.py
-
4tildes/tildes/metrics.py
-
4tildes/tildes/routes.py
-
22tildes/tildes/templates/donate_stripe.jinja2
-
15tildes/tildes/templates/donate_success.jinja2
-
68tildes/tildes/views/donate.py
@ -0,0 +1,10 @@ |
|||||
|
// Copyright (c) 2019 Tildes contributors <code@tildes.net>
|
||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
|
||||
|
$.onmount("[data-js-stripe-checkout]", function() { |
||||
|
/* eslint-disable-next-line no-undef */ |
||||
|
var stripe = Stripe($(this).attr("data-js-stripe-checkout")); |
||||
|
stripe.redirectToCheckout({ |
||||
|
sessionId: $(this).attr("data-js-stripe-checkout-session") |
||||
|
}); |
||||
|
}); |
@ -0,0 +1,15 @@ |
|||||
|
{# Copyright (c) 2019 Tildes contributors <code@tildes.net> #} |
||||
|
{# SPDX-License-Identifier: AGPL-3.0-or-later #} |
||||
|
|
||||
|
{% extends 'base_no_sidebar.jinja2' %} |
||||
|
|
||||
|
{% block title %}Thanks for donating!{% endblock %} |
||||
|
|
||||
|
{% block content %} |
||||
|
<div class="empty"> |
||||
|
<h2 class="empty-title">Thanks for donating to Tildes!</h2> |
||||
|
<p class="empty-subtitle">You should receive an email receipt. If you have any questions, please feel free to contact <a href="mailto:donate@tildes.net">donate@tildes.net</a></p> |
||||
|
|
||||
|
<div class="empty-action"><a href="/" class="btn btn-primary">Back to the home page</a></div> |
||||
|
</div> |
||||
|
{% endblock %} |
@ -0,0 +1,68 @@ |
|||||
|
# Copyright (c) 2018 Tildes contributors <code@tildes.net> |
||||
|
# SPDX-License-Identifier: AGPL-3.0-or-later |
||||
|
|
||||
|
"""The view for donating via Stripe.""" |
||||
|
|
||||
|
import stripe |
||||
|
from marshmallow.fields import Float, String |
||||
|
from marshmallow.validate import OneOf, Range |
||||
|
from pyramid.httpexceptions import HTTPInternalServerError |
||||
|
from pyramid.request import Request |
||||
|
from pyramid.security import NO_PERMISSION_REQUIRED |
||||
|
from pyramid.view import view_config |
||||
|
from webargs.pyramidparser import use_kwargs |
||||
|
|
||||
|
from tildes.metrics import incr_counter |
||||
|
|
||||
|
|
||||
|
@view_config( |
||||
|
route_name="donate_stripe", |
||||
|
request_method="POST", |
||||
|
renderer="donate_stripe.jinja2", |
||||
|
permission=NO_PERMISSION_REQUIRED, |
||||
|
require_csrf=False, |
||||
|
) |
||||
|
@use_kwargs( |
||||
|
{ |
||||
|
"amount": Float(required=True, validate=Range(min=1.0)), |
||||
|
"currency": String(required=True, validate=OneOf(("CAD", "USD"))), |
||||
|
} |
||||
|
) |
||||
|
def post_donate_stripe(request: Request, amount: int, currency: str) -> dict: |
||||
|
"""Process a Stripe donation.""" |
||||
|
try: |
||||
|
stripe.api_key = request.registry.settings["api_keys.stripe.secret"] |
||||
|
publishable_key = request.registry.settings["api_keys.stripe.publishable"] |
||||
|
except KeyError: |
||||
|
raise HTTPInternalServerError |
||||
|
|
||||
|
incr_counter("donation_initiations", type="stripe") |
||||
|
|
||||
|
session = stripe.checkout.Session.create( |
||||
|
payment_method_types=["card"], |
||||
|
line_items=[ |
||||
|
{ |
||||
|
"name": "One-time donation - tildes.net", |
||||
|
"amount": int(amount * 100), |
||||
|
"currency": currency, |
||||
|
"quantity": 1, |
||||
|
} |
||||
|
], |
||||
|
submit_type="donate", |
||||
|
success_url="https://tildes.net/donate_success", |
||||
|
cancel_url="https://docs.tildes.net/donate-stripe", |
||||
|
) |
||||
|
|
||||
|
return {"publishable_key": publishable_key, "session_id": session.id} |
||||
|
|
||||
|
|
||||
|
@view_config(route_name="donate_success", renderer="donate_success.jinja2") |
||||
|
def get_donate_success(request: Request) -> dict: |
||||
|
"""Display a message after a successful donation.""" |
||||
|
# pylint: disable=unused-argument |
||||
|
|
||||
|
# incrementing this metric on page-load and hard-coding Stripe isn't ideal, but it |
||||
|
# should do the job for now |
||||
|
incr_counter("donations", type="stripe") |
||||
|
|
||||
|
return {} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue