~linuxgoose/bocpress

ref: HEAD bocpress/main/templates/main/billing_subscribe.html -rw-r--r-- 3.8 KiB
7337de89Jordan Robinson update email host to be fetched from .env 8 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
{% extends 'main/layout.html' %}

{% block title %}Subscribe to Premium{% endblock %}

{% block head_extra %}
<script src="https://js.stripe.com/v3/?advancedFraudSignals=false"></script>
<style>
    [data-submitting] input[type="submit"] {
        opacity: 0.8;
    }
</style>
{% endblock head_extra %}

{% block content %}
<main>
    <h1>Subscribe to Premium</h1>

    <form id="payment-form" data-secret="{{ stripe_client_secret }}">

        <div id="payment-element">
            {# stripe.js will create stripe elements forms here #}
        </div>

        {% csrf_token %}
        <input id="submit" type="submit" value="Submit" style="margin-top: 16px;">
        <div id="loading-message" style="margin-top: 16px;">
            <!-- loading message for users here -->
        </div>
        <div id="error-message" style="margin-top: 16px; color: red;">
            <!-- error message for users here -->
        </div>
    </form>

    <p>
        <br>
        Once you click Submit:
    </p>
    <ol>
        <li>
            Your card details will be sent to Stripe (payment processor).
        </li>
        <li>
            We will charge your card immediately, through Stripe.
        </li>
        <li>
            We will store your card within Stripe, so that we can charge you
            next year.
        </li>
        <li>
            You will be able to cancel your subscription immediately as well,
            should you want to. You can cancel your subscription at any point,
            24/7/365, from this billing dashboard with one click.
        </li>
        <li>
            You can also get a refund, no questions asked.
        </li>
        <li>
            All terms of service can be found in our
            <a href="{% url 'methodology' %}">Methodology</a>
            page.
        </li>
    </ol>
</main>
{% endblock content %}

{% block scripts %}
<script>
    const stripe = Stripe('{{ stripe_public_key }}');
    const options = {
        clientSecret: '{{ stripe_client_secret }}',
    };
    const elements = stripe.elements(options);
    const paymentElement = elements.create('payment');
    paymentElement.mount('#payment-element');

    const form = document.getElementById('payment-form');
    form.addEventListener('submit', async (event) => {
        event.preventDefault();

        // if form is already submitting, do nothing
        if (form.hasAttribute('data-submitting')) {
            return;
        }

        // add [data-submitting] attribute to stop multiple submissions
        form.setAttribute('data-submitting', '');

        // add loading message
        const loadingContainer = document.querySelector('#loading-message');
        loadingContainer.textContent = 'Charging card using Stripe, this should take less than 10 seconds...';

        // clear error if exists
        const messageContainer = document.querySelector('#error-message');
        messageContainer.textContent = '';

        const { error } = await stripe.confirmPayment({
            elements,
            confirmParams: {
                return_url: '{{ stripe_return_url }}',
            },
        });

        if (error) {
            // This point will only be reached if there is an immediate error when
            // confirming the payment. Show error to your customer (for example, payment
            // details incomplete)
            messageContainer.textContent = error.message;
            loadingContainer.textContent = '';
        } else {
            // Your customer will be redirected to your `return_url`. For some payment
            // methods like iDEAL, your customer will be redirected to an intermediate
            // site first to authorize the payment, then redirected to the `return_url`.
        }

        // remove [data-submitting] attribute
        form.removeAttribute('data-submitting');
    });
</script>
{% endblock scripts %}