lfs-stripe 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lfs_stripe-1.0.1/.gitignore +10 -0
- lfs_stripe-1.0.1/.python-version +1 -0
- lfs_stripe-1.0.1/LICENSE.txt +27 -0
- lfs_stripe-1.0.1/PKG-INFO +92 -0
- lfs_stripe-1.0.1/README.md +74 -0
- lfs_stripe-1.0.1/pyproject.toml +27 -0
- lfs_stripe-1.0.1/src/lfs_stripe/__init__.py +0 -0
- lfs_stripe-1.0.1/src/lfs_stripe/static/lfs_stripe/lfs_stripe.css +0 -0
- lfs_stripe-1.0.1/src/lfs_stripe/static/lfs_stripe/lfs_stripe.js +263 -0
- lfs_stripe-1.0.1/src/lfs_stripe/urls.py +10 -0
- lfs_stripe-1.0.1/src/lfs_stripe/views.py +73 -0
- lfs_stripe-1.0.1/tests/test_config.py +12 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Copyright (c) 2026, Kai Diefenbach
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
|
6
|
+
|
|
7
|
+
* Redistributions of source code must retain the above copyright notice,
|
|
8
|
+
this list of conditions and the following disclaimer.
|
|
9
|
+
|
|
10
|
+
* Redistributions in binary form must reproduce the above copyright
|
|
11
|
+
notice, this list of conditions and the following disclaimer in the
|
|
12
|
+
documentation and/or other materials provided with the distribution.
|
|
13
|
+
|
|
14
|
+
* Neither the name of the author nor the names of other contributors may
|
|
15
|
+
be used to endorse or promote products derived from this software
|
|
16
|
+
without specific prior written permission.
|
|
17
|
+
|
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
20
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
22
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
23
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
24
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
25
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
26
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: lfs-stripe
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: Stripe payments for LFS
|
|
5
|
+
Project-URL: Source, https://github.com/diefenbach/lfs-stripe
|
|
6
|
+
Author: Kai Diefenbach
|
|
7
|
+
License-Expression: BSD-3-Clause
|
|
8
|
+
License-File: LICENSE.txt
|
|
9
|
+
Classifier: Environment :: Web Environment
|
|
10
|
+
Classifier: Framework :: Django
|
|
11
|
+
Classifier: Framework :: Django :: 5.2
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Requires-Python: >=3.12
|
|
15
|
+
Requires-Dist: django-lfs<2,>=1.0
|
|
16
|
+
Requires-Dist: stripe<16,>=15.6
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# LFS Stripe
|
|
20
|
+
|
|
21
|
+
A Django Lightning Fast Shop (LFS) payment integration for Stripe.
|
|
22
|
+
|
|
23
|
+
## Overview
|
|
24
|
+
|
|
25
|
+
This package provides Stripe payment integration for LFS e-commerce platform. It allows customers to make secure credit card payments through Stripe's payment processing system.
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- Seamless integration with LFS checkout process
|
|
30
|
+
- Secure credit card processing via Stripe API
|
|
31
|
+
- Support for payment intents
|
|
32
|
+
- Client-side card validation
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
1. Install the package:
|
|
37
|
+
```
|
|
38
|
+
pip install lfs-stripe
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
2. Add to your INSTALLED_APPS in settings.py:
|
|
42
|
+
```python
|
|
43
|
+
INSTALLED_APPS = [
|
|
44
|
+
# ...
|
|
45
|
+
'lfs_stripe',
|
|
46
|
+
# ...
|
|
47
|
+
]
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
3. Configure your Stripe API keys in settings.py:
|
|
51
|
+
```python
|
|
52
|
+
STRIPE_SECRET_KEY = 'your_stripe_secret_key'
|
|
53
|
+
STRIPE_PUBLISHABLE_KEY = 'your_stripe_publishable_key'
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
4. Include the URLs in your project's urls.py:
|
|
57
|
+
```python
|
|
58
|
+
urlpatterns = [
|
|
59
|
+
# ...
|
|
60
|
+
path('stripe/', include('lfs_stripe.urls')),
|
|
61
|
+
# ...
|
|
62
|
+
]
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Request Flow
|
|
66
|
+
1. fetch to backend to: /stripe/create-payment-intent/
|
|
67
|
+
2. API Call to stripe to: confirmCardPayment
|
|
68
|
+
3. fetch to backend to: /stripe/create-order
|
|
69
|
+
4. Redirect to thank-you page
|
|
70
|
+
|
|
71
|
+
## Differences from LFS-PayPal
|
|
72
|
+
|
|
73
|
+
While both lfs-stripe and lfs-paypal provide payment integration for LFS, there are several key differences:
|
|
74
|
+
|
|
75
|
+
1. **Payment Processing**:
|
|
76
|
+
- Stripe processes credit card payments directly on your site (with client-side tokenization)
|
|
77
|
+
|
|
78
|
+
2. **API Integration**:
|
|
79
|
+
- Stripe uses a modern API with Payment Intents
|
|
80
|
+
|
|
81
|
+
3. **Payment Methods**:
|
|
82
|
+
- Stripe primarily handles credit card payments directly
|
|
83
|
+
|
|
84
|
+
4. **User Experience**:
|
|
85
|
+
- Stripe allows customers to stay on your site throughout the checkout process
|
|
86
|
+
|
|
87
|
+
## Notes:
|
|
88
|
+
- create payment intent muss immer im BE aufgerufen werden. Das verlangt Stripe zur Sicherheit, weil so keine Daten im Browser manipuliert werden können. Außerdem liegt nur dort der private key.
|
|
89
|
+
- Im BE wird also intent = stripe.PaymentIntent.create(...) aufgerufen mit dem Betrag und dem private key
|
|
90
|
+
- Das gibt wiederum ein client secret zurück
|
|
91
|
+
- Mit diesem client secrect wird this.stripe.confirmCardPayment(clientSecret, {...}) aufgerufen
|
|
92
|
+
- Wenn das erfolgreich ist
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# LFS Stripe
|
|
2
|
+
|
|
3
|
+
A Django Lightning Fast Shop (LFS) payment integration for Stripe.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package provides Stripe payment integration for LFS e-commerce platform. It allows customers to make secure credit card payments through Stripe's payment processing system.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Seamless integration with LFS checkout process
|
|
12
|
+
- Secure credit card processing via Stripe API
|
|
13
|
+
- Support for payment intents
|
|
14
|
+
- Client-side card validation
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
1. Install the package:
|
|
19
|
+
```
|
|
20
|
+
pip install lfs-stripe
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
2. Add to your INSTALLED_APPS in settings.py:
|
|
24
|
+
```python
|
|
25
|
+
INSTALLED_APPS = [
|
|
26
|
+
# ...
|
|
27
|
+
'lfs_stripe',
|
|
28
|
+
# ...
|
|
29
|
+
]
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
3. Configure your Stripe API keys in settings.py:
|
|
33
|
+
```python
|
|
34
|
+
STRIPE_SECRET_KEY = 'your_stripe_secret_key'
|
|
35
|
+
STRIPE_PUBLISHABLE_KEY = 'your_stripe_publishable_key'
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
4. Include the URLs in your project's urls.py:
|
|
39
|
+
```python
|
|
40
|
+
urlpatterns = [
|
|
41
|
+
# ...
|
|
42
|
+
path('stripe/', include('lfs_stripe.urls')),
|
|
43
|
+
# ...
|
|
44
|
+
]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Request Flow
|
|
48
|
+
1. fetch to backend to: /stripe/create-payment-intent/
|
|
49
|
+
2. API Call to stripe to: confirmCardPayment
|
|
50
|
+
3. fetch to backend to: /stripe/create-order
|
|
51
|
+
4. Redirect to thank-you page
|
|
52
|
+
|
|
53
|
+
## Differences from LFS-PayPal
|
|
54
|
+
|
|
55
|
+
While both lfs-stripe and lfs-paypal provide payment integration for LFS, there are several key differences:
|
|
56
|
+
|
|
57
|
+
1. **Payment Processing**:
|
|
58
|
+
- Stripe processes credit card payments directly on your site (with client-side tokenization)
|
|
59
|
+
|
|
60
|
+
2. **API Integration**:
|
|
61
|
+
- Stripe uses a modern API with Payment Intents
|
|
62
|
+
|
|
63
|
+
3. **Payment Methods**:
|
|
64
|
+
- Stripe primarily handles credit card payments directly
|
|
65
|
+
|
|
66
|
+
4. **User Experience**:
|
|
67
|
+
- Stripe allows customers to stay on your site throughout the checkout process
|
|
68
|
+
|
|
69
|
+
## Notes:
|
|
70
|
+
- create payment intent muss immer im BE aufgerufen werden. Das verlangt Stripe zur Sicherheit, weil so keine Daten im Browser manipuliert werden können. Außerdem liegt nur dort der private key.
|
|
71
|
+
- Im BE wird also intent = stripe.PaymentIntent.create(...) aufgerufen mit dem Betrag und dem private key
|
|
72
|
+
- Das gibt wiederum ein client secret zurück
|
|
73
|
+
- Mit diesem client secrect wird this.stripe.confirmCardPayment(clientSecret, {...}) aufgerufen
|
|
74
|
+
- Wenn das erfolgreich ist
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "lfs-stripe"
|
|
3
|
+
version = "1.0.1"
|
|
4
|
+
description = "Stripe payments for LFS"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "BSD-3-Clause"
|
|
7
|
+
license-files = ["LICENSE.txt"]
|
|
8
|
+
authors = [{ name = "Kai Diefenbach" }]
|
|
9
|
+
requires-python = ">=3.12"
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Environment :: Web Environment",
|
|
12
|
+
"Framework :: Django",
|
|
13
|
+
"Framework :: Django :: 5.2",
|
|
14
|
+
"Operating System :: OS Independent",
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
]
|
|
17
|
+
dependencies = [
|
|
18
|
+
"django-lfs>=1.0,<2",
|
|
19
|
+
"stripe>=15.6,<16",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Source = "https://github.com/diefenbach/lfs-stripe"
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["hatchling>=1.27,<2"]
|
|
27
|
+
build-backend = "hatchling.build"
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
class StripeCheckoutHandler {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.stripe = null;
|
|
4
|
+
this.elements = null;
|
|
5
|
+
this.cardNumberElement = null;
|
|
6
|
+
this.cardExpiryElement = null;
|
|
7
|
+
this.cardCvcElement = null;
|
|
8
|
+
this.creditCardForm = null;
|
|
9
|
+
|
|
10
|
+
this.initialize();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async initialize() {
|
|
14
|
+
const style = {
|
|
15
|
+
base: {
|
|
16
|
+
fontSize: '16px',
|
|
17
|
+
color: '#495057',
|
|
18
|
+
lineHeight: '1.5',
|
|
19
|
+
'::placeholder': {
|
|
20
|
+
color: '#6c757d',
|
|
21
|
+
},
|
|
22
|
+
backgroundColor: '#fff',
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Only initialize if we have a credit card form
|
|
27
|
+
this.creditCardForm = document.querySelector('.creditcard-form');
|
|
28
|
+
if (!this.creditCardForm) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// The publishable key comes from the shop's STRIPE_PUBLISHABLE_KEY setting.
|
|
33
|
+
const response = await fetch('/stripe/config/');
|
|
34
|
+
const config = await response.json();
|
|
35
|
+
this.stripe = Stripe(config.publishableKey);
|
|
36
|
+
this.elements = this.stripe.elements();
|
|
37
|
+
|
|
38
|
+
this.cardNumberElement = this.elements.create('cardNumber', {
|
|
39
|
+
style: style,
|
|
40
|
+
disableLink: true
|
|
41
|
+
});
|
|
42
|
+
this.cardExpiryElement = this.elements.create('cardExpiry', {style});
|
|
43
|
+
this.cardCvcElement = this.elements.create('cardCvc', {style});
|
|
44
|
+
|
|
45
|
+
this.cardNumberElement.mount('#card-number-element');
|
|
46
|
+
this.cardExpiryElement.mount('#card-expiry-element');
|
|
47
|
+
this.cardCvcElement.mount('#card-cvc-element');
|
|
48
|
+
|
|
49
|
+
// Error handling stripe elements
|
|
50
|
+
[this.cardNumberElement, this.cardExpiryElement, this.cardCvcElement].forEach(element => {
|
|
51
|
+
element.on('change', ({error}) => {
|
|
52
|
+
this.displayError(error ? error.message : '');
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Error handling cardholder name (which is not a stripe element)
|
|
57
|
+
const cardholderNameInput = document.querySelector('input[name="cardholder_name"]');
|
|
58
|
+
if (cardholderNameInput) {
|
|
59
|
+
cardholderNameInput.addEventListener('blur', (event) => {
|
|
60
|
+
if (!event.target.value.trim()) {
|
|
61
|
+
this.displayError('Bitte geben Sie den Namen des Karteninhabers ein.');
|
|
62
|
+
} else {
|
|
63
|
+
this.displayError('');
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Clear error when user starts typing
|
|
68
|
+
cardholderNameInput.addEventListener('input', () => {
|
|
69
|
+
this.displayError('');
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Form submit
|
|
74
|
+
this.creditCardForm.addEventListener('submit', (event) => this.handleFormSubmit(event));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async handleFormSubmit(event) {
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
|
|
80
|
+
// Show spinner and disable button
|
|
81
|
+
this.showSpinner();
|
|
82
|
+
|
|
83
|
+
const valid_customer = document.querySelector('#id_valid_customer')
|
|
84
|
+
if (!valid_customer.checked) {
|
|
85
|
+
// Hide spinner and enable button
|
|
86
|
+
this.hideSpinner();
|
|
87
|
+
|
|
88
|
+
// Display error message for unchecked terms
|
|
89
|
+
const errorElement = document.createElement('div');
|
|
90
|
+
errorElement.className = 'age-error text-danger mb-2';
|
|
91
|
+
errorElement.textContent = 'Bitte bestätigen sie dieses Feld.';
|
|
92
|
+
|
|
93
|
+
// Insert the error message before the checkbox
|
|
94
|
+
const existingError = valid_customer.parentElement.querySelector('.age-error');
|
|
95
|
+
if (!existingError) {
|
|
96
|
+
valid_customer.parentElement.insertBefore(errorElement, valid_customer);
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Get form data to send to the backend
|
|
102
|
+
const formData = new FormData(this.creditCardForm);
|
|
103
|
+
|
|
104
|
+
// Convert FormData to JSON object
|
|
105
|
+
const formDataObj = Object.fromEntries(formData.entries());
|
|
106
|
+
const csrfToken = getCsrfToken();
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
// Payment Intent vom Backend holen
|
|
110
|
+
const clientSecret = await this.createPaymentIntent(formData, csrfToken);
|
|
111
|
+
|
|
112
|
+
// Validate cardholder name before submission
|
|
113
|
+
const cardholderName = document.querySelector('input[name="cardholder_name"]').value.trim();
|
|
114
|
+
if (!cardholderName) {
|
|
115
|
+
this.hideSpinner();
|
|
116
|
+
this.displayError('Bitte geben Sie den Namen des Karteninhabers ein.');
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (clientSecret) {
|
|
121
|
+
const paymentResult = await this.confirmPayment(clientSecret);
|
|
122
|
+
|
|
123
|
+
if (paymentResult.success) {
|
|
124
|
+
// Add payment intent ID to form data
|
|
125
|
+
formDataObj.payment_intent_id = paymentResult.paymentIntentId;
|
|
126
|
+
await this.createOrder(formDataObj, csrfToken);
|
|
127
|
+
} else {
|
|
128
|
+
this.hideSpinner();
|
|
129
|
+
this.displayError(paymentResult.error);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
} catch (error) {
|
|
133
|
+
console.error('Error:', error);
|
|
134
|
+
this.hideSpinner();
|
|
135
|
+
this.displayError('Ein Fehler ist aufgetreten.');
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async createPaymentIntent(formData, csrfToken) {
|
|
140
|
+
try {
|
|
141
|
+
const response = await fetch('/stripe/create-payment-intent/', {
|
|
142
|
+
method: 'POST',
|
|
143
|
+
headers: {
|
|
144
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
145
|
+
'X-CSRFToken': csrfToken
|
|
146
|
+
},
|
|
147
|
+
body: new URLSearchParams(formData)
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
if (response.ok) {
|
|
151
|
+
const {clientSecret} = await response.json();
|
|
152
|
+
return clientSecret;
|
|
153
|
+
} else {
|
|
154
|
+
const error = await response.json();
|
|
155
|
+
console.error('Payment Intent creation failed:', error);
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
} catch (error) {
|
|
159
|
+
console.error('Error creating payment intent:', error);
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async confirmPayment(clientSecret) {
|
|
165
|
+
const customerFirstName = this.getCustomerData('first_name');
|
|
166
|
+
const customerLastName = this.getCustomerData('last_name');
|
|
167
|
+
|
|
168
|
+
const {error, paymentIntent} = await this.stripe.confirmCardPayment(clientSecret, {
|
|
169
|
+
payment_method: {
|
|
170
|
+
card: this.cardNumberElement,
|
|
171
|
+
billing_details: {
|
|
172
|
+
name: `${customerFirstName} ${customerLastName}`,
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
success: !error,
|
|
179
|
+
error: error ? error.message : null,
|
|
180
|
+
paymentIntentId: paymentIntent ? paymentIntent.id : null
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async createOrder(formDataObj, csrfToken) {
|
|
185
|
+
try {
|
|
186
|
+
const response = await fetch('/stripe/create-order/', {
|
|
187
|
+
method: 'POST',
|
|
188
|
+
headers: {
|
|
189
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
190
|
+
'X-CSRFToken': csrfToken
|
|
191
|
+
},
|
|
192
|
+
body: new URLSearchParams(formDataObj)
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
if (response.ok) {
|
|
196
|
+
window.location.href = '/thank-you/';
|
|
197
|
+
} else {
|
|
198
|
+
const errorData = await response.json();
|
|
199
|
+
console.error('Order creation failed:', errorData);
|
|
200
|
+
this.hideSpinner();
|
|
201
|
+
this.displayError('Bestellerstellung fehlgeschlagen: ' + (errorData.error || 'Unbekannter Fehler'));
|
|
202
|
+
}
|
|
203
|
+
} catch (err) {
|
|
204
|
+
console.error('Error creating order:', err);
|
|
205
|
+
this.hideSpinner();
|
|
206
|
+
this.displayError('Ein Fehler ist aufgetreten.');
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
displayError(message) {
|
|
211
|
+
const displayError = document.getElementById('card-errors');
|
|
212
|
+
if (displayError) {
|
|
213
|
+
displayError.textContent = message;
|
|
214
|
+
|
|
215
|
+
// Scroll to card form when error occurs
|
|
216
|
+
if (message) {
|
|
217
|
+
const creditCardSection = document.getElementById('payment-method-title');
|
|
218
|
+
if (creditCardSection) {
|
|
219
|
+
creditCardSection.scrollIntoView({
|
|
220
|
+
behavior: 'smooth',
|
|
221
|
+
block: 'start'
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
getCustomerData(field) {
|
|
229
|
+
const customerElement = document.querySelector(`[data-customer-${field}]`);
|
|
230
|
+
if (customerElement) {
|
|
231
|
+
return customerElement.getAttribute(`data-customer-${field}`);
|
|
232
|
+
}
|
|
233
|
+
return '';
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
showSpinner() {
|
|
237
|
+
const payButton = document.getElementById('pay-button');
|
|
238
|
+
const cartIcon = document.getElementById('cart-icon');
|
|
239
|
+
const spinner = document.getElementById('loading-spinner');
|
|
240
|
+
|
|
241
|
+
if (payButton && cartIcon && spinner) {
|
|
242
|
+
payButton.disabled = true;
|
|
243
|
+
cartIcon.style.display = 'none';
|
|
244
|
+
spinner.style.display = 'inline-block';
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
hideSpinner() {
|
|
249
|
+
const payButton = document.getElementById('pay-button');
|
|
250
|
+
const cartIcon = document.getElementById('cart-icon');
|
|
251
|
+
const spinner = document.getElementById('loading-spinner');
|
|
252
|
+
|
|
253
|
+
if (payButton && cartIcon && spinner) {
|
|
254
|
+
payButton.disabled = false;
|
|
255
|
+
cartIcon.style.display = 'inline';
|
|
256
|
+
spinner.style.display = 'none';
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
document.addEventListener('DOMContentLoaded', function() {
|
|
262
|
+
new StripeCheckoutHandler();
|
|
263
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from django.urls import path
|
|
2
|
+
from . import views
|
|
3
|
+
|
|
4
|
+
app_name = "lfs-stripe"
|
|
5
|
+
|
|
6
|
+
urlpatterns = [
|
|
7
|
+
path("config/", views.config, name="config"),
|
|
8
|
+
path("create-payment-intent/", views.create_payment_intent, name="create_payment_intent"),
|
|
9
|
+
path("create-order/", views.create_order, name="create_order"),
|
|
10
|
+
]
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from django.conf import settings
|
|
4
|
+
from django.http import HttpResponseRedirect, JsonResponse
|
|
5
|
+
from django.urls import reverse
|
|
6
|
+
import stripe
|
|
7
|
+
|
|
8
|
+
import lfs.core.utils
|
|
9
|
+
from lfs.cart.utils import get_cart
|
|
10
|
+
from lfs.core.signals import order_paid, order_submitted
|
|
11
|
+
from lfs.order.settings import PAID
|
|
12
|
+
from lfs.order.utils import add_order
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger("lfs.stripe")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def config(request):
|
|
18
|
+
"""Return the shop's Stripe publishable key, which the checkout JavaScript needs to start Stripe."""
|
|
19
|
+
return JsonResponse({"publishableKey": settings.STRIPE_PUBLISHABLE_KEY})
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def create_payment_intent(request):
|
|
23
|
+
cart = lfs.cart.utils.get_cart(request)
|
|
24
|
+
if cart is None:
|
|
25
|
+
return HttpResponseRedirect(reverse("lfs_cart"))
|
|
26
|
+
|
|
27
|
+
# Set Stripe API key
|
|
28
|
+
stripe.api_key = settings.STRIPE_SECRET_KEY
|
|
29
|
+
|
|
30
|
+
try:
|
|
31
|
+
# Get cart and total amount
|
|
32
|
+
cart = get_cart(request)
|
|
33
|
+
|
|
34
|
+
# Convert to cents
|
|
35
|
+
amount = cart.get_total_price_gross(request) * 100
|
|
36
|
+
|
|
37
|
+
intent = stripe.PaymentIntent.create(
|
|
38
|
+
amount=int(amount),
|
|
39
|
+
currency="eur",
|
|
40
|
+
payment_method_types=["card"],
|
|
41
|
+
metadata={"cart_id": f"{settings.STRIPE_SHOP_NAME}, {cart.id}"},
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
return JsonResponse({"clientSecret": intent.client_secret})
|
|
45
|
+
|
|
46
|
+
except Exception as e:
|
|
47
|
+
return JsonResponse({"error": str(e)}, status=400)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def create_order(request):
|
|
51
|
+
cart = get_cart(request)
|
|
52
|
+
order = add_order(request)
|
|
53
|
+
logger.info(f"Order created: {order.id}")
|
|
54
|
+
order.state = PAID
|
|
55
|
+
order.save()
|
|
56
|
+
|
|
57
|
+
# Update Stripe PaymentIntent with order ID
|
|
58
|
+
stripe.api_key = settings.STRIPE_SECRET_KEY
|
|
59
|
+
payment_intent_id = request.POST.get("payment_intent_id")
|
|
60
|
+
if payment_intent_id:
|
|
61
|
+
try:
|
|
62
|
+
stripe.PaymentIntent.modify(
|
|
63
|
+
payment_intent_id, metadata={"order_number": f"{settings.STRIPE_SHOP_NAME}, {order.number}"}
|
|
64
|
+
)
|
|
65
|
+
logger.info(f"Updated PaymentIntent {payment_intent_id} with order_id: {order.id}")
|
|
66
|
+
except Exception as e:
|
|
67
|
+
logger.error(f"Failed to update PaymentIntent metadata: {e}")
|
|
68
|
+
|
|
69
|
+
# Notify the system
|
|
70
|
+
order_submitted.send(sender=order, request=request)
|
|
71
|
+
order_paid.send(sender=order, request=request)
|
|
72
|
+
|
|
73
|
+
return JsonResponse({"message": "Payment confirmed"})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""The checkout gets the Stripe publishable key from the shop's settings, not from the package."""
|
|
2
|
+
|
|
3
|
+
from django.test import override_settings
|
|
4
|
+
from django.urls import reverse
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@override_settings(STRIPE_PUBLISHABLE_KEY="pk_test_from_settings")
|
|
8
|
+
def test_config_returns_the_publishable_key_from_the_settings(client):
|
|
9
|
+
response = client.get(reverse("lfs-stripe:config"))
|
|
10
|
+
|
|
11
|
+
assert response.status_code == 200
|
|
12
|
+
assert response.json() == {"publishableKey": "pk_test_from_settings"}
|