swarmauri_billing_stripe 0.11.0.dev2__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.
- swarmauri_billing_stripe-0.11.0.dev2/LICENSE +201 -0
- swarmauri_billing_stripe-0.11.0.dev2/PKG-INFO +222 -0
- swarmauri_billing_stripe-0.11.0.dev2/README.md +186 -0
- swarmauri_billing_stripe-0.11.0.dev2/pyproject.toml +102 -0
- swarmauri_billing_stripe-0.11.0.dev2/swarmauri_billing_stripe/__init__.py +5 -0
- swarmauri_billing_stripe-0.11.0.dev2/swarmauri_billing_stripe/provider.py +429 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [2025] [Jacob Stewart @ Swarmauri]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: swarmauri_billing_stripe
|
|
3
|
+
Version: 0.11.0.dev2
|
|
4
|
+
Summary: Stripe-backed Swarmauri billing provider for checkout, payments, subscriptions, invoices, refunds, disputes, Connect, and webhooks.
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: swarmauri,sdk,billing,stripe,stripe billing provider,stripe payments,standards,payments,payment gateway,checkout sessions,payment intents,refunds,recurring billing,subscription management,invoices,stripe connect,webhook verification,dispute management,merchant services
|
|
8
|
+
Author: Jacob Stewart
|
|
9
|
+
Author-email: jacob@swarmauri.com
|
|
10
|
+
Requires-Python: >=3.10,<3.15
|
|
11
|
+
Classifier: Development Status :: 1 - Planning
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: Information Technology
|
|
14
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Framework :: AsyncIO
|
|
17
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
18
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
19
|
+
Classifier: Topic :: Office/Business :: Financial :: Point-Of-Sale
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Classifier: Programming Language :: Python
|
|
24
|
+
Classifier: Programming Language :: Python :: 3
|
|
25
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
28
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
29
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
30
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
31
|
+
Requires-Dist: stripe (>=15.0.0,<16)
|
|
32
|
+
Requires-Dist: swarmauri_base
|
|
33
|
+
Requires-Dist: swarmauri_core
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+

|
|
37
|
+
|
|
38
|
+
<p align="center">
|
|
39
|
+
<a href="https://pepy.tech/project/swarmauri_billing_stripe/">
|
|
40
|
+
<img src="https://static.pepy.tech/badge/swarmauri_billing_stripe/month" alt="PyPI - Downloads"/></a>
|
|
41
|
+
<a href="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/standards/swarmauri_billing_stripe/">
|
|
42
|
+
<img alt="Hits" src="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/standards/swarmauri_billing_stripe.svg"/></a>
|
|
43
|
+
<a href="https://pypi.org/project/swarmauri_billing_stripe/">
|
|
44
|
+
<img src="https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13%20%7C%203.14-blue" alt="PyPI - Python Version"/></a>
|
|
45
|
+
<a href="https://pypi.org/project/swarmauri_billing_stripe/">
|
|
46
|
+
<img src="https://img.shields.io/pypi/l/swarmauri_billing_stripe" alt="PyPI - License"/></a>
|
|
47
|
+
<a href="https://pypi.org/project/swarmauri_billing_stripe/">
|
|
48
|
+
<img src="https://img.shields.io/pypi/v/swarmauri_billing_stripe?label=swarmauri_billing_stripe&color=green" alt="PyPI - swarmauri_billing_stripe"/></a>
|
|
49
|
+
<a href="https://discord.gg/N4UpBuQv8T">
|
|
50
|
+
<img src="https://img.shields.io/badge/Discord-Join%20Chat-5865F2?logo=discord&logoColor=white" alt="Discord"/></a></p>
|
|
51
|
+
|
|
52
|
+
# Swarmauri Billing Stripe
|
|
53
|
+
|
|
54
|
+
`swarmauri_billing_stripe` provides a Stripe-backed billing provider for the Swarmauri SDK. It connects Swarmauri billing interfaces to the official Stripe Python SDK for products, prices, Checkout Sessions, Payment Intents, captures, cancellations, refunds, subscriptions, invoices, Stripe Connect destination charges, disputes, and webhook signature verification.
|
|
55
|
+
|
|
56
|
+
## Why Swarmauri Billing Stripe?
|
|
57
|
+
|
|
58
|
+
Use `swarmauri_billing_stripe` when a Swarmauri application needs production Stripe billing workflows while keeping application code behind provider-neutral billing interfaces. The provider maps Stripe Billing, Checkout, Payment Intents, Refunds, Invoicing, Subscriptions, Connect, and Disputes into Swarmauri refs and specs.
|
|
59
|
+
|
|
60
|
+
## FAQ
|
|
61
|
+
|
|
62
|
+
### Q: Does this package call live Stripe APIs?
|
|
63
|
+
|
|
64
|
+
A: Yes. The provider configures the official `stripe` Python SDK with `api_key` and calls Stripe resources such as `Product`, `Price`, `checkout.Session`, `PaymentIntent`, `Refund`, `Subscription`, `Invoice`, and `Dispute`.
|
|
65
|
+
|
|
66
|
+
### Q: Which Stripe workflows are supported?
|
|
67
|
+
|
|
68
|
+
A: It supports product and price creation, hosted checkout, payment intent creation, manual capture, payment intent cancellation, refund create/retrieve, subscription create/cancel, invoice create/finalize/void/mark-uncollectible, Connect destination charges with application fees, webhook signature validation, and dispute listing.
|
|
69
|
+
|
|
70
|
+
### Q: What credential is required?
|
|
71
|
+
|
|
72
|
+
A: Pass a Stripe secret key as `api_key`, such as a test key beginning with `sk_test_`. Production keys should be provided through secure deployment secrets.
|
|
73
|
+
|
|
74
|
+
## Features
|
|
75
|
+
|
|
76
|
+
- Stripe-backed provider class registered as `StripeBillingProvider`.
|
|
77
|
+
- Product and price creation with metadata support.
|
|
78
|
+
- Hosted Checkout Session creation for one-time payments.
|
|
79
|
+
- Payment Intent creation with automatic or manual capture.
|
|
80
|
+
- Payment capture and cancellation through Stripe Payment Intents.
|
|
81
|
+
- Refund creation and lookup through Stripe Refunds.
|
|
82
|
+
- Subscription creation and cancellation with Stripe prices.
|
|
83
|
+
- Invoice item creation plus invoice lifecycle operations.
|
|
84
|
+
- Stripe Connect destination-charge support with application fees.
|
|
85
|
+
- Webhook signature verification through `stripe.Webhook.construct_event`.
|
|
86
|
+
- Dispute listing for operational review workflows.
|
|
87
|
+
- Python 3.10, 3.11, 3.12, 3.13, and 3.14 support.
|
|
88
|
+
|
|
89
|
+
## Installation
|
|
90
|
+
|
|
91
|
+
Install with `uv`:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
uv add swarmauri_billing_stripe
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Install with `pip`:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
pip install swarmauri_billing_stripe
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Usage
|
|
104
|
+
|
|
105
|
+
Create a product, price, and Stripe Checkout Session:
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from swarmauri_billing_stripe import StripeBillingProvider
|
|
109
|
+
from swarmauri_base.billing import CheckoutRequest, PriceSpec, ProductSpec
|
|
110
|
+
|
|
111
|
+
provider = StripeBillingProvider(api_key="sk_test_123")
|
|
112
|
+
|
|
113
|
+
product = provider.create_product(
|
|
114
|
+
ProductSpec(name="Starter", description="Starter subscription"),
|
|
115
|
+
idempotency_key="stripe-prod-001",
|
|
116
|
+
)
|
|
117
|
+
price = provider.create_price(
|
|
118
|
+
product,
|
|
119
|
+
PriceSpec(currency="USD", unit_amount_minor=1000, nickname="Starter monthly"),
|
|
120
|
+
idempotency_key="stripe-price-001",
|
|
121
|
+
)
|
|
122
|
+
checkout = provider.create_checkout(
|
|
123
|
+
price,
|
|
124
|
+
CheckoutRequest(
|
|
125
|
+
quantity=1,
|
|
126
|
+
success_url="https://example.com/success",
|
|
127
|
+
cancel_url="https://example.com/cancel",
|
|
128
|
+
customer_email="buyer@example.com",
|
|
129
|
+
idempotency_key="stripe-checkout-001",
|
|
130
|
+
),
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
print(product.id, price.id, checkout.url)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Create and capture a manual Payment Intent:
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
from swarmauri_billing_stripe import StripeBillingProvider
|
|
140
|
+
from swarmauri_base.billing import PaymentIntentRequest
|
|
141
|
+
|
|
142
|
+
provider = StripeBillingProvider(api_key="sk_test_123")
|
|
143
|
+
|
|
144
|
+
payment = provider.create_payment_intent(
|
|
145
|
+
PaymentIntentRequest(
|
|
146
|
+
amount_minor=2500,
|
|
147
|
+
currency="USD",
|
|
148
|
+
payment_method_id="pm_card_visa",
|
|
149
|
+
confirm=True,
|
|
150
|
+
capture=False,
|
|
151
|
+
idempotency_key="stripe-payment-001",
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
captured = provider.capture_payment(payment.id, idempotency_key="stripe-capture-001")
|
|
155
|
+
|
|
156
|
+
print(payment.status, captured.status)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Verify a Stripe webhook signature:
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
from swarmauri_billing_stripe import StripeBillingProvider
|
|
163
|
+
|
|
164
|
+
provider = StripeBillingProvider(api_key="sk_test_123")
|
|
165
|
+
|
|
166
|
+
valid = provider.verify_webhook_signature(
|
|
167
|
+
raw_body=b'{"id":"evt_test","object":"event"}',
|
|
168
|
+
headers={"Stripe-Signature": "t=...,v1=..."},
|
|
169
|
+
secret="whsec_...",
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
print(valid)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Important Scope Notes
|
|
176
|
+
|
|
177
|
+
This package uses live Stripe SDK calls. Runtime behavior depends on Stripe account settings, API version, enabled payment methods, test or live mode, Checkout configuration, Connect account setup, and webhook endpoint secrets.
|
|
178
|
+
|
|
179
|
+
## Entry Point
|
|
180
|
+
|
|
181
|
+
The package exposes a Swarmauri billing provider entry point:
|
|
182
|
+
|
|
183
|
+
```toml
|
|
184
|
+
[project.entry-points.'swarmauri.billing_providers']
|
|
185
|
+
StripeBillingProvider = "swarmauri_billing_stripe.provider:StripeBillingProvider"
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Related Packages
|
|
189
|
+
|
|
190
|
+
Billing provider packages:
|
|
191
|
+
|
|
192
|
+
- [swarmauri_billing_adyen](https://pypi.org/project/swarmauri_billing_adyen/)
|
|
193
|
+
- [swarmauri_billing_authorize_net](https://pypi.org/project/swarmauri_billing_authorize_net/)
|
|
194
|
+
- [swarmauri_billing_braintree](https://pypi.org/project/swarmauri_billing_braintree/)
|
|
195
|
+
- [swarmauri_billing_mock](https://pypi.org/project/swarmauri_billing_mock/)
|
|
196
|
+
- [swarmauri_billing_paypal](https://pypi.org/project/swarmauri_billing_paypal/)
|
|
197
|
+
- [swarmauri_billing_paystack](https://pypi.org/project/swarmauri_billing_paystack/)
|
|
198
|
+
- [swarmauri_billing_razorpay](https://pypi.org/project/swarmauri_billing_razorpay/)
|
|
199
|
+
- [swarmauri_billing_square](https://pypi.org/project/swarmauri_billing_square/)
|
|
200
|
+
|
|
201
|
+
Foundational packages:
|
|
202
|
+
|
|
203
|
+
- [swarmauri_core](https://pypi.org/project/swarmauri_core/) defines billing capabilities and interfaces.
|
|
204
|
+
- [swarmauri_base](https://pypi.org/project/swarmauri_base/) provides billing specs, refs, mixins, and `BillingProviderBase`.
|
|
205
|
+
- [swarmauri](https://pypi.org/project/swarmauri/) provides namespace imports and plugin discovery.
|
|
206
|
+
|
|
207
|
+
## Documentation
|
|
208
|
+
|
|
209
|
+
- [Stripe provider source](swarmauri_billing_stripe/provider.py)
|
|
210
|
+
- [Swarmauri SDK repository](https://github.com/swarmauri/swarmauri-sdk)
|
|
211
|
+
- [Stripe API documentation](https://docs.stripe.com/api)
|
|
212
|
+
|
|
213
|
+
## License
|
|
214
|
+
|
|
215
|
+
Apache-2.0
|
|
216
|
+
|
|
217
|
+
## Contributing
|
|
218
|
+
|
|
219
|
+
When expanding Stripe coverage, keep each Swarmauri billing method aligned with the official Stripe Python SDK, document required Stripe account configuration, and add tests for each changed runtime path.
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://pepy.tech/project/swarmauri_billing_stripe/">
|
|
5
|
+
<img src="https://static.pepy.tech/badge/swarmauri_billing_stripe/month" alt="PyPI - Downloads"/></a>
|
|
6
|
+
<a href="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/standards/swarmauri_billing_stripe/">
|
|
7
|
+
<img alt="Hits" src="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/standards/swarmauri_billing_stripe.svg"/></a>
|
|
8
|
+
<a href="https://pypi.org/project/swarmauri_billing_stripe/">
|
|
9
|
+
<img src="https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13%20%7C%203.14-blue" alt="PyPI - Python Version"/></a>
|
|
10
|
+
<a href="https://pypi.org/project/swarmauri_billing_stripe/">
|
|
11
|
+
<img src="https://img.shields.io/pypi/l/swarmauri_billing_stripe" alt="PyPI - License"/></a>
|
|
12
|
+
<a href="https://pypi.org/project/swarmauri_billing_stripe/">
|
|
13
|
+
<img src="https://img.shields.io/pypi/v/swarmauri_billing_stripe?label=swarmauri_billing_stripe&color=green" alt="PyPI - swarmauri_billing_stripe"/></a>
|
|
14
|
+
<a href="https://discord.gg/N4UpBuQv8T">
|
|
15
|
+
<img src="https://img.shields.io/badge/Discord-Join%20Chat-5865F2?logo=discord&logoColor=white" alt="Discord"/></a></p>
|
|
16
|
+
|
|
17
|
+
# Swarmauri Billing Stripe
|
|
18
|
+
|
|
19
|
+
`swarmauri_billing_stripe` provides a Stripe-backed billing provider for the Swarmauri SDK. It connects Swarmauri billing interfaces to the official Stripe Python SDK for products, prices, Checkout Sessions, Payment Intents, captures, cancellations, refunds, subscriptions, invoices, Stripe Connect destination charges, disputes, and webhook signature verification.
|
|
20
|
+
|
|
21
|
+
## Why Swarmauri Billing Stripe?
|
|
22
|
+
|
|
23
|
+
Use `swarmauri_billing_stripe` when a Swarmauri application needs production Stripe billing workflows while keeping application code behind provider-neutral billing interfaces. The provider maps Stripe Billing, Checkout, Payment Intents, Refunds, Invoicing, Subscriptions, Connect, and Disputes into Swarmauri refs and specs.
|
|
24
|
+
|
|
25
|
+
## FAQ
|
|
26
|
+
|
|
27
|
+
### Q: Does this package call live Stripe APIs?
|
|
28
|
+
|
|
29
|
+
A: Yes. The provider configures the official `stripe` Python SDK with `api_key` and calls Stripe resources such as `Product`, `Price`, `checkout.Session`, `PaymentIntent`, `Refund`, `Subscription`, `Invoice`, and `Dispute`.
|
|
30
|
+
|
|
31
|
+
### Q: Which Stripe workflows are supported?
|
|
32
|
+
|
|
33
|
+
A: It supports product and price creation, hosted checkout, payment intent creation, manual capture, payment intent cancellation, refund create/retrieve, subscription create/cancel, invoice create/finalize/void/mark-uncollectible, Connect destination charges with application fees, webhook signature validation, and dispute listing.
|
|
34
|
+
|
|
35
|
+
### Q: What credential is required?
|
|
36
|
+
|
|
37
|
+
A: Pass a Stripe secret key as `api_key`, such as a test key beginning with `sk_test_`. Production keys should be provided through secure deployment secrets.
|
|
38
|
+
|
|
39
|
+
## Features
|
|
40
|
+
|
|
41
|
+
- Stripe-backed provider class registered as `StripeBillingProvider`.
|
|
42
|
+
- Product and price creation with metadata support.
|
|
43
|
+
- Hosted Checkout Session creation for one-time payments.
|
|
44
|
+
- Payment Intent creation with automatic or manual capture.
|
|
45
|
+
- Payment capture and cancellation through Stripe Payment Intents.
|
|
46
|
+
- Refund creation and lookup through Stripe Refunds.
|
|
47
|
+
- Subscription creation and cancellation with Stripe prices.
|
|
48
|
+
- Invoice item creation plus invoice lifecycle operations.
|
|
49
|
+
- Stripe Connect destination-charge support with application fees.
|
|
50
|
+
- Webhook signature verification through `stripe.Webhook.construct_event`.
|
|
51
|
+
- Dispute listing for operational review workflows.
|
|
52
|
+
- Python 3.10, 3.11, 3.12, 3.13, and 3.14 support.
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
55
|
+
|
|
56
|
+
Install with `uv`:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
uv add swarmauri_billing_stripe
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Install with `pip`:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install swarmauri_billing_stripe
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Usage
|
|
69
|
+
|
|
70
|
+
Create a product, price, and Stripe Checkout Session:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from swarmauri_billing_stripe import StripeBillingProvider
|
|
74
|
+
from swarmauri_base.billing import CheckoutRequest, PriceSpec, ProductSpec
|
|
75
|
+
|
|
76
|
+
provider = StripeBillingProvider(api_key="sk_test_123")
|
|
77
|
+
|
|
78
|
+
product = provider.create_product(
|
|
79
|
+
ProductSpec(name="Starter", description="Starter subscription"),
|
|
80
|
+
idempotency_key="stripe-prod-001",
|
|
81
|
+
)
|
|
82
|
+
price = provider.create_price(
|
|
83
|
+
product,
|
|
84
|
+
PriceSpec(currency="USD", unit_amount_minor=1000, nickname="Starter monthly"),
|
|
85
|
+
idempotency_key="stripe-price-001",
|
|
86
|
+
)
|
|
87
|
+
checkout = provider.create_checkout(
|
|
88
|
+
price,
|
|
89
|
+
CheckoutRequest(
|
|
90
|
+
quantity=1,
|
|
91
|
+
success_url="https://example.com/success",
|
|
92
|
+
cancel_url="https://example.com/cancel",
|
|
93
|
+
customer_email="buyer@example.com",
|
|
94
|
+
idempotency_key="stripe-checkout-001",
|
|
95
|
+
),
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
print(product.id, price.id, checkout.url)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Create and capture a manual Payment Intent:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from swarmauri_billing_stripe import StripeBillingProvider
|
|
105
|
+
from swarmauri_base.billing import PaymentIntentRequest
|
|
106
|
+
|
|
107
|
+
provider = StripeBillingProvider(api_key="sk_test_123")
|
|
108
|
+
|
|
109
|
+
payment = provider.create_payment_intent(
|
|
110
|
+
PaymentIntentRequest(
|
|
111
|
+
amount_minor=2500,
|
|
112
|
+
currency="USD",
|
|
113
|
+
payment_method_id="pm_card_visa",
|
|
114
|
+
confirm=True,
|
|
115
|
+
capture=False,
|
|
116
|
+
idempotency_key="stripe-payment-001",
|
|
117
|
+
)
|
|
118
|
+
)
|
|
119
|
+
captured = provider.capture_payment(payment.id, idempotency_key="stripe-capture-001")
|
|
120
|
+
|
|
121
|
+
print(payment.status, captured.status)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Verify a Stripe webhook signature:
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
from swarmauri_billing_stripe import StripeBillingProvider
|
|
128
|
+
|
|
129
|
+
provider = StripeBillingProvider(api_key="sk_test_123")
|
|
130
|
+
|
|
131
|
+
valid = provider.verify_webhook_signature(
|
|
132
|
+
raw_body=b'{"id":"evt_test","object":"event"}',
|
|
133
|
+
headers={"Stripe-Signature": "t=...,v1=..."},
|
|
134
|
+
secret="whsec_...",
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
print(valid)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Important Scope Notes
|
|
141
|
+
|
|
142
|
+
This package uses live Stripe SDK calls. Runtime behavior depends on Stripe account settings, API version, enabled payment methods, test or live mode, Checkout configuration, Connect account setup, and webhook endpoint secrets.
|
|
143
|
+
|
|
144
|
+
## Entry Point
|
|
145
|
+
|
|
146
|
+
The package exposes a Swarmauri billing provider entry point:
|
|
147
|
+
|
|
148
|
+
```toml
|
|
149
|
+
[project.entry-points.'swarmauri.billing_providers']
|
|
150
|
+
StripeBillingProvider = "swarmauri_billing_stripe.provider:StripeBillingProvider"
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Related Packages
|
|
154
|
+
|
|
155
|
+
Billing provider packages:
|
|
156
|
+
|
|
157
|
+
- [swarmauri_billing_adyen](https://pypi.org/project/swarmauri_billing_adyen/)
|
|
158
|
+
- [swarmauri_billing_authorize_net](https://pypi.org/project/swarmauri_billing_authorize_net/)
|
|
159
|
+
- [swarmauri_billing_braintree](https://pypi.org/project/swarmauri_billing_braintree/)
|
|
160
|
+
- [swarmauri_billing_mock](https://pypi.org/project/swarmauri_billing_mock/)
|
|
161
|
+
- [swarmauri_billing_paypal](https://pypi.org/project/swarmauri_billing_paypal/)
|
|
162
|
+
- [swarmauri_billing_paystack](https://pypi.org/project/swarmauri_billing_paystack/)
|
|
163
|
+
- [swarmauri_billing_razorpay](https://pypi.org/project/swarmauri_billing_razorpay/)
|
|
164
|
+
- [swarmauri_billing_square](https://pypi.org/project/swarmauri_billing_square/)
|
|
165
|
+
|
|
166
|
+
Foundational packages:
|
|
167
|
+
|
|
168
|
+
- [swarmauri_core](https://pypi.org/project/swarmauri_core/) defines billing capabilities and interfaces.
|
|
169
|
+
- [swarmauri_base](https://pypi.org/project/swarmauri_base/) provides billing specs, refs, mixins, and `BillingProviderBase`.
|
|
170
|
+
- [swarmauri](https://pypi.org/project/swarmauri/) provides namespace imports and plugin discovery.
|
|
171
|
+
|
|
172
|
+
## Documentation
|
|
173
|
+
|
|
174
|
+
- [Stripe provider source](swarmauri_billing_stripe/provider.py)
|
|
175
|
+
- [Swarmauri SDK repository](https://github.com/swarmauri/swarmauri-sdk)
|
|
176
|
+
- [Stripe API documentation](https://docs.stripe.com/api)
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
Apache-2.0
|
|
181
|
+
|
|
182
|
+
## Contributing
|
|
183
|
+
|
|
184
|
+
When expanding Stripe coverage, keep each Swarmauri billing method aligned with the official Stripe Python SDK, document required Stripe account configuration, and add tests for each changed runtime path.
|
|
185
|
+
|
|
186
|
+
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "swarmauri_billing_stripe"
|
|
3
|
+
version = "0.11.0.dev2"
|
|
4
|
+
description = "Stripe-backed Swarmauri billing provider for checkout, payments, subscriptions, invoices, refunds, disputes, Connect, and webhooks."
|
|
5
|
+
license = "Apache-2.0"
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
repository = "https://github.com/swarmauri/swarmauri-sdk"
|
|
8
|
+
requires-python = ">=3.10,<3.15"
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Development Status :: 1 - Planning",
|
|
11
|
+
"Intended Audience :: Developers",
|
|
12
|
+
"Intended Audience :: Information Technology",
|
|
13
|
+
"License :: OSI Approved :: Apache Software License",
|
|
14
|
+
"Operating System :: OS Independent",
|
|
15
|
+
"Framework :: AsyncIO",
|
|
16
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
17
|
+
"Topic :: Office/Business :: Financial",
|
|
18
|
+
"Topic :: Office/Business :: Financial :: Point-Of-Sale",
|
|
19
|
+
"Topic :: Software Development :: Libraries",
|
|
20
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
|
21
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
22
|
+
"Programming Language :: Python",
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
25
|
+
"Programming Language :: Python :: 3.10",
|
|
26
|
+
"Programming Language :: Python :: 3.11",
|
|
27
|
+
"Programming Language :: Python :: 3.12",
|
|
28
|
+
"Programming Language :: Python :: 3.13",
|
|
29
|
+
"Programming Language :: Python :: 3.14",
|
|
30
|
+
]
|
|
31
|
+
authors = [{ name = "Jacob Stewart", email = "jacob@swarmauri.com" }]
|
|
32
|
+
dependencies = [
|
|
33
|
+
"swarmauri_core",
|
|
34
|
+
"swarmauri_base",
|
|
35
|
+
"stripe>=15.0.0,<16",
|
|
36
|
+
]
|
|
37
|
+
keywords = [
|
|
38
|
+
"swarmauri",
|
|
39
|
+
"sdk",
|
|
40
|
+
"billing",
|
|
41
|
+
"stripe",
|
|
42
|
+
"stripe billing provider",
|
|
43
|
+
"stripe payments",
|
|
44
|
+
"standards",
|
|
45
|
+
"payments",
|
|
46
|
+
"payment gateway",
|
|
47
|
+
"checkout sessions",
|
|
48
|
+
"payment intents",
|
|
49
|
+
"refunds",
|
|
50
|
+
"recurring billing",
|
|
51
|
+
"subscription management",
|
|
52
|
+
"invoices",
|
|
53
|
+
"stripe connect",
|
|
54
|
+
"webhook verification",
|
|
55
|
+
"dispute management",
|
|
56
|
+
"merchant services",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
[tool.uv.sources]
|
|
60
|
+
swarmauri_core = { workspace = true }
|
|
61
|
+
swarmauri_base = { workspace = true }
|
|
62
|
+
|
|
63
|
+
[tool.pytest.ini_options]
|
|
64
|
+
norecursedirs = ["combined", "scripts"]
|
|
65
|
+
markers = [
|
|
66
|
+
"test: standard test",
|
|
67
|
+
"unit: Unit tests",
|
|
68
|
+
"i9n: Integration tests",
|
|
69
|
+
"r8n: Regression tests",
|
|
70
|
+
"timeout: mark test to timeout after X seconds",
|
|
71
|
+
"xpass: Expected passes",
|
|
72
|
+
"xfail: Expected failures",
|
|
73
|
+
"acceptance: Acceptance tests",
|
|
74
|
+
"perf: Performance tests that measure execution time and resource usage",
|
|
75
|
+
]
|
|
76
|
+
timeout = 300
|
|
77
|
+
log_cli = true
|
|
78
|
+
log_cli_level = "INFO"
|
|
79
|
+
log_cli_format = "%(asctime)s [%(levelname)s] %(message)s"
|
|
80
|
+
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
|
|
81
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
82
|
+
|
|
83
|
+
[project.entry-points.'swarmauri.billing_providers']
|
|
84
|
+
StripeBillingProvider = "swarmauri_billing_stripe.provider:StripeBillingProvider"
|
|
85
|
+
|
|
86
|
+
[build-system]
|
|
87
|
+
requires = ["poetry-core>=1.0.0"]
|
|
88
|
+
build-backend = "poetry.core.masonry.api"
|
|
89
|
+
|
|
90
|
+
[dependency-groups]
|
|
91
|
+
dev = [
|
|
92
|
+
"pytest>=8.0",
|
|
93
|
+
"pytest-asyncio>=0.24.0",
|
|
94
|
+
"pytest-xdist>=3.6.1",
|
|
95
|
+
"pytest-json-report>=1.5.0",
|
|
96
|
+
"python-dotenv",
|
|
97
|
+
"httpx>=0.27",
|
|
98
|
+
"flake8>=7.0",
|
|
99
|
+
"pytest-timeout>=2.3.1",
|
|
100
|
+
"ruff>=0.9.9",
|
|
101
|
+
"pytest-benchmark>=4.0.0",
|
|
102
|
+
]
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
"""Production-ready Stripe billing provider implementation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Mapping, Optional, Sequence, cast
|
|
6
|
+
|
|
7
|
+
from pydantic import Field, PrivateAttr, SecretStr
|
|
8
|
+
|
|
9
|
+
from swarmauri_base.billing import (
|
|
10
|
+
BillingProviderBase,
|
|
11
|
+
BillingSpec,
|
|
12
|
+
CheckoutIntentRef,
|
|
13
|
+
CheckoutRequest,
|
|
14
|
+
HostedCheckoutMixin,
|
|
15
|
+
InvoicingMixin,
|
|
16
|
+
InvoiceSpec,
|
|
17
|
+
MarketplaceMixin,
|
|
18
|
+
OnlinePaymentsMixin,
|
|
19
|
+
PaymentIntentRequest,
|
|
20
|
+
PaymentRef,
|
|
21
|
+
PriceRef,
|
|
22
|
+
PriceSpec,
|
|
23
|
+
ProductRef,
|
|
24
|
+
ProductSpec,
|
|
25
|
+
ProductsPricesMixin,
|
|
26
|
+
RefundRequest,
|
|
27
|
+
RefundsMixin,
|
|
28
|
+
RiskMixin,
|
|
29
|
+
SplitSpec,
|
|
30
|
+
SubscriptionSpec,
|
|
31
|
+
SubscriptionsMixin,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class StripeBillingProvider(
|
|
36
|
+
ProductsPricesMixin,
|
|
37
|
+
HostedCheckoutMixin,
|
|
38
|
+
OnlinePaymentsMixin,
|
|
39
|
+
SubscriptionsMixin,
|
|
40
|
+
InvoicingMixin,
|
|
41
|
+
MarketplaceMixin,
|
|
42
|
+
RefundsMixin,
|
|
43
|
+
RiskMixin,
|
|
44
|
+
BillingProviderBase,
|
|
45
|
+
):
|
|
46
|
+
"""Stripe billing provider backed by the official Stripe SDK."""
|
|
47
|
+
|
|
48
|
+
component_name: str = "stripe"
|
|
49
|
+
api_key: SecretStr = Field(repr=False)
|
|
50
|
+
_stripe: Any = PrivateAttr(default=None)
|
|
51
|
+
|
|
52
|
+
# ------------------------------------------------------------------ Utils
|
|
53
|
+
def _client(self) -> Any:
|
|
54
|
+
if self._stripe is None:
|
|
55
|
+
import stripe
|
|
56
|
+
|
|
57
|
+
stripe.api_key = self.api_key.get_secret_value()
|
|
58
|
+
self._stripe = stripe
|
|
59
|
+
return self._stripe
|
|
60
|
+
|
|
61
|
+
@staticmethod
|
|
62
|
+
def _require(spec: BillingSpec, field: str) -> Any:
|
|
63
|
+
value = spec.resolve(field)
|
|
64
|
+
if value is None or (isinstance(value, str) and not value.strip()):
|
|
65
|
+
raise ValueError(
|
|
66
|
+
f"{field} is required for {spec.__class__.__name__}"
|
|
67
|
+
)
|
|
68
|
+
return value
|
|
69
|
+
|
|
70
|
+
# ---------------------------------------------------------------- Products
|
|
71
|
+
# & Prices
|
|
72
|
+
def _create_product(
|
|
73
|
+
self, product_spec: ProductSpec, *, idempotency_key: str
|
|
74
|
+
) -> ProductRef:
|
|
75
|
+
stripe = self._client()
|
|
76
|
+
name = self._require(product_spec, "name")
|
|
77
|
+
product = stripe.Product.create(
|
|
78
|
+
name=name,
|
|
79
|
+
description=product_spec.resolve("description") or None,
|
|
80
|
+
metadata=product_spec.resolve("metadata") or None,
|
|
81
|
+
idempotency_key=idempotency_key,
|
|
82
|
+
)
|
|
83
|
+
ref = ProductRef(
|
|
84
|
+
id=product["id"],
|
|
85
|
+
provider=self.component_name,
|
|
86
|
+
name=product.get("name"),
|
|
87
|
+
description=product.get("description"),
|
|
88
|
+
metadata=product.get("metadata"),
|
|
89
|
+
raw=product,
|
|
90
|
+
)
|
|
91
|
+
return ref
|
|
92
|
+
|
|
93
|
+
def _create_price(
|
|
94
|
+
self,
|
|
95
|
+
product: ProductRef,
|
|
96
|
+
price_spec: PriceSpec,
|
|
97
|
+
*,
|
|
98
|
+
idempotency_key: str,
|
|
99
|
+
) -> PriceRef:
|
|
100
|
+
stripe = self._client()
|
|
101
|
+
currency = self._require(price_spec, "currency")
|
|
102
|
+
amount = int(self._require(price_spec, "unit_amount_minor"))
|
|
103
|
+
price = stripe.Price.create(
|
|
104
|
+
product=product.id,
|
|
105
|
+
currency=str(currency).lower(),
|
|
106
|
+
unit_amount=amount,
|
|
107
|
+
nickname=price_spec.resolve("nickname"),
|
|
108
|
+
metadata=price_spec.resolve("metadata") or None,
|
|
109
|
+
idempotency_key=idempotency_key,
|
|
110
|
+
)
|
|
111
|
+
ref = PriceRef(
|
|
112
|
+
id=price["id"],
|
|
113
|
+
product_id=price.get("product"),
|
|
114
|
+
currency=price.get("currency"),
|
|
115
|
+
unit_amount_minor=price.get("unit_amount"),
|
|
116
|
+
provider=self.component_name,
|
|
117
|
+
raw=price,
|
|
118
|
+
)
|
|
119
|
+
return ref
|
|
120
|
+
|
|
121
|
+
# ---------------------------------------------------------------- Hosted
|
|
122
|
+
# Checkout
|
|
123
|
+
def _create_checkout(
|
|
124
|
+
self, price: PriceRef, request: CheckoutRequest
|
|
125
|
+
) -> CheckoutIntentRef:
|
|
126
|
+
stripe = self._client()
|
|
127
|
+
success_url = self._require(request, "success_url")
|
|
128
|
+
cancel_url = request.resolve("cancel_url") or success_url
|
|
129
|
+
session = stripe.checkout.Session.create(
|
|
130
|
+
mode="payment",
|
|
131
|
+
line_items=[{"price": price.id, "quantity": request.quantity}],
|
|
132
|
+
success_url=success_url,
|
|
133
|
+
cancel_url=cancel_url,
|
|
134
|
+
customer_email=request.resolve("customer_email"),
|
|
135
|
+
metadata=request.resolve("metadata") or None,
|
|
136
|
+
idempotency_key=request.resolve("idempotency_key"),
|
|
137
|
+
)
|
|
138
|
+
intent = CheckoutIntentRef(
|
|
139
|
+
id=session["id"],
|
|
140
|
+
url=session.get("url"),
|
|
141
|
+
provider=self.component_name,
|
|
142
|
+
raw=session,
|
|
143
|
+
)
|
|
144
|
+
return intent
|
|
145
|
+
|
|
146
|
+
# ---------------------------------------------------------------- Online
|
|
147
|
+
# Payments
|
|
148
|
+
def _create_payment_intent(self, req: PaymentIntentRequest) -> PaymentRef:
|
|
149
|
+
stripe = self._client()
|
|
150
|
+
amount = int(self._require(req, "amount_minor"))
|
|
151
|
+
currency = self._require(req, "currency")
|
|
152
|
+
pi = stripe.PaymentIntent.create(
|
|
153
|
+
amount=amount,
|
|
154
|
+
currency=str(currency).lower(),
|
|
155
|
+
payment_method=req.resolve("payment_method_id"),
|
|
156
|
+
confirm=req.confirm,
|
|
157
|
+
capture_method="automatic" if req.capture else "manual",
|
|
158
|
+
metadata=req.resolve("metadata") or None,
|
|
159
|
+
idempotency_key=req.resolve("idempotency_key"),
|
|
160
|
+
)
|
|
161
|
+
ref = PaymentRef(
|
|
162
|
+
id=pi["id"],
|
|
163
|
+
status=pi.get("status"),
|
|
164
|
+
amount_minor=pi.get("amount"),
|
|
165
|
+
currency=pi.get("currency"),
|
|
166
|
+
provider=self.component_name,
|
|
167
|
+
raw=pi,
|
|
168
|
+
)
|
|
169
|
+
return ref
|
|
170
|
+
|
|
171
|
+
def _capture_payment(
|
|
172
|
+
self, payment_id: str, *, idempotency_key: Optional[str] = None
|
|
173
|
+
) -> PaymentRef:
|
|
174
|
+
stripe = self._client()
|
|
175
|
+
pi = stripe.PaymentIntent.capture(
|
|
176
|
+
payment_id,
|
|
177
|
+
idempotency_key=idempotency_key,
|
|
178
|
+
)
|
|
179
|
+
ref = PaymentRef(
|
|
180
|
+
id=pi["id"],
|
|
181
|
+
status=pi.get("status"),
|
|
182
|
+
amount_minor=pi.get("amount_received"),
|
|
183
|
+
currency=pi.get("currency"),
|
|
184
|
+
provider=self.component_name,
|
|
185
|
+
raw=pi,
|
|
186
|
+
)
|
|
187
|
+
return ref
|
|
188
|
+
|
|
189
|
+
def _cancel_payment(
|
|
190
|
+
self,
|
|
191
|
+
payment_id: str,
|
|
192
|
+
*,
|
|
193
|
+
reason: Optional[str] = None,
|
|
194
|
+
idempotency_key: Optional[str] = None,
|
|
195
|
+
) -> PaymentRef:
|
|
196
|
+
stripe = self._client()
|
|
197
|
+
pi = stripe.PaymentIntent.cancel(
|
|
198
|
+
payment_id,
|
|
199
|
+
cancellation_reason=reason or None,
|
|
200
|
+
idempotency_key=idempotency_key,
|
|
201
|
+
)
|
|
202
|
+
ref = PaymentRef(
|
|
203
|
+
id=pi["id"],
|
|
204
|
+
status=pi.get("status"),
|
|
205
|
+
amount_minor=pi.get("amount"),
|
|
206
|
+
currency=pi.get("currency"),
|
|
207
|
+
provider=self.component_name,
|
|
208
|
+
raw=pi,
|
|
209
|
+
)
|
|
210
|
+
return ref
|
|
211
|
+
|
|
212
|
+
# ----------------------------------------------------------------
|
|
213
|
+
# Subscriptions
|
|
214
|
+
def _create_subscription(
|
|
215
|
+
self, spec: SubscriptionSpec, *, idempotency_key: str
|
|
216
|
+
) -> Mapping[str, Any]:
|
|
217
|
+
stripe = self._client()
|
|
218
|
+
customer_id = self._require(spec, "customer_id")
|
|
219
|
+
items = [
|
|
220
|
+
{"price": item.price_id, "quantity": item.quantity}
|
|
221
|
+
for item in spec.items
|
|
222
|
+
]
|
|
223
|
+
if not items:
|
|
224
|
+
raise ValueError(
|
|
225
|
+
"SubscriptionSpec.items must contain at least one item"
|
|
226
|
+
)
|
|
227
|
+
sub = stripe.Subscription.create(
|
|
228
|
+
customer=customer_id,
|
|
229
|
+
items=items,
|
|
230
|
+
trial_end=spec.resolve("trial_end"),
|
|
231
|
+
collection_method=spec.resolve("collection_method"),
|
|
232
|
+
metadata=spec.resolve("metadata") or None,
|
|
233
|
+
idempotency_key=idempotency_key,
|
|
234
|
+
)
|
|
235
|
+
result = {
|
|
236
|
+
"subscription_id": sub["id"],
|
|
237
|
+
"status": sub.get("status"),
|
|
238
|
+
"provider": self.component_name,
|
|
239
|
+
"raw": sub,
|
|
240
|
+
}
|
|
241
|
+
return result
|
|
242
|
+
|
|
243
|
+
def _cancel_subscription(
|
|
244
|
+
self, subscription_id: str, *, at_period_end: bool = True
|
|
245
|
+
) -> Mapping[str, Any]:
|
|
246
|
+
stripe = self._client()
|
|
247
|
+
if at_period_end:
|
|
248
|
+
sub = stripe.Subscription.modify(
|
|
249
|
+
subscription_id,
|
|
250
|
+
cancel_at_period_end=True,
|
|
251
|
+
)
|
|
252
|
+
else:
|
|
253
|
+
sub = stripe.Subscription.delete(subscription_id)
|
|
254
|
+
result = {
|
|
255
|
+
"subscription_id": subscription_id,
|
|
256
|
+
"status": sub.get("status"),
|
|
257
|
+
"provider": self.component_name,
|
|
258
|
+
"raw": sub,
|
|
259
|
+
}
|
|
260
|
+
return result
|
|
261
|
+
|
|
262
|
+
# ----------------------------------------------------------------
|
|
263
|
+
# Invoicing
|
|
264
|
+
def _create_invoice(
|
|
265
|
+
self, spec: InvoiceSpec, *, idempotency_key: str
|
|
266
|
+
) -> Mapping[str, Any]:
|
|
267
|
+
stripe = self._client()
|
|
268
|
+
customer_id = self._require(spec, "customer_id")
|
|
269
|
+
for item in spec.line_items:
|
|
270
|
+
if item.price_id:
|
|
271
|
+
stripe.InvoiceItem.create(
|
|
272
|
+
customer=customer_id,
|
|
273
|
+
price=item.price_id,
|
|
274
|
+
quantity=item.quantity,
|
|
275
|
+
)
|
|
276
|
+
else:
|
|
277
|
+
stripe.InvoiceItem.create(
|
|
278
|
+
customer=customer_id,
|
|
279
|
+
amount=item.amount_minor or 0,
|
|
280
|
+
currency=(item.currency or "usd").lower(),
|
|
281
|
+
description=item.description or "",
|
|
282
|
+
quantity=item.quantity,
|
|
283
|
+
)
|
|
284
|
+
invoice = stripe.Invoice.create(
|
|
285
|
+
customer=customer_id,
|
|
286
|
+
collection_method=spec.resolve("collection_method"),
|
|
287
|
+
days_until_due=spec.resolve("days_until_due"),
|
|
288
|
+
metadata=spec.resolve("metadata") or None,
|
|
289
|
+
idempotency_key=idempotency_key,
|
|
290
|
+
)
|
|
291
|
+
result = {
|
|
292
|
+
"invoice_id": invoice["id"],
|
|
293
|
+
"status": invoice.get("status"),
|
|
294
|
+
"provider": self.component_name,
|
|
295
|
+
"raw": invoice,
|
|
296
|
+
}
|
|
297
|
+
return result
|
|
298
|
+
|
|
299
|
+
def _finalize_invoice(self, invoice_id: str) -> Mapping[str, Any]:
|
|
300
|
+
stripe = self._client()
|
|
301
|
+
invoice = stripe.Invoice.finalize_invoice(invoice_id)
|
|
302
|
+
result = {
|
|
303
|
+
"invoice_id": invoice["id"],
|
|
304
|
+
"status": invoice.get("status"),
|
|
305
|
+
"provider": self.component_name,
|
|
306
|
+
"raw": invoice,
|
|
307
|
+
}
|
|
308
|
+
return result
|
|
309
|
+
|
|
310
|
+
def _void_invoice(self, invoice_id: str) -> Mapping[str, Any]:
|
|
311
|
+
stripe = self._client()
|
|
312
|
+
invoice = stripe.Invoice.void_invoice(invoice_id)
|
|
313
|
+
result = {
|
|
314
|
+
"invoice_id": invoice["id"],
|
|
315
|
+
"status": invoice.get("status"),
|
|
316
|
+
"provider": self.component_name,
|
|
317
|
+
"raw": invoice,
|
|
318
|
+
}
|
|
319
|
+
return result
|
|
320
|
+
|
|
321
|
+
def _mark_uncollectible(self, invoice_id: str) -> Mapping[str, Any]:
|
|
322
|
+
stripe = self._client()
|
|
323
|
+
invoice = stripe.Invoice.mark_uncollectible(invoice_id)
|
|
324
|
+
result = {
|
|
325
|
+
"invoice_id": invoice["id"],
|
|
326
|
+
"status": invoice.get("status"),
|
|
327
|
+
"provider": self.component_name,
|
|
328
|
+
"raw": invoice,
|
|
329
|
+
}
|
|
330
|
+
return result
|
|
331
|
+
|
|
332
|
+
# ----------------------------------------------------------------
|
|
333
|
+
# Marketplace
|
|
334
|
+
def _create_split(
|
|
335
|
+
self, spec: SplitSpec, *, idempotency_key: str
|
|
336
|
+
) -> Mapping[str, Any]:
|
|
337
|
+
result = {
|
|
338
|
+
"split": spec.model_dump(mode="json"),
|
|
339
|
+
"provider": self.component_name,
|
|
340
|
+
}
|
|
341
|
+
return result
|
|
342
|
+
|
|
343
|
+
def _charge_with_split(
|
|
344
|
+
self,
|
|
345
|
+
amount_minor: int,
|
|
346
|
+
currency: str,
|
|
347
|
+
*,
|
|
348
|
+
split_code_or_params: Mapping[str, Any],
|
|
349
|
+
idempotency_key: str,
|
|
350
|
+
) -> Mapping[str, Any]:
|
|
351
|
+
stripe = self._client()
|
|
352
|
+
pi = stripe.PaymentIntent.create(
|
|
353
|
+
amount=amount_minor,
|
|
354
|
+
currency=currency.lower(),
|
|
355
|
+
transfer_data={"destination": split_code_or_params["destination"]},
|
|
356
|
+
application_fee_amount=split_code_or_params[
|
|
357
|
+
"application_fee_amount"
|
|
358
|
+
],
|
|
359
|
+
idempotency_key=idempotency_key,
|
|
360
|
+
)
|
|
361
|
+
result = {
|
|
362
|
+
"payment_intent_id": pi["id"],
|
|
363
|
+
"status": pi.get("status"),
|
|
364
|
+
"provider": self.component_name,
|
|
365
|
+
"raw": pi,
|
|
366
|
+
}
|
|
367
|
+
return result
|
|
368
|
+
|
|
369
|
+
# ---------------------------------------------------------------- Refunds
|
|
370
|
+
def _create_refund(
|
|
371
|
+
self, payment: PaymentRef, req: RefundRequest, *, idempotency_key: str
|
|
372
|
+
) -> Mapping[str, Any]:
|
|
373
|
+
stripe = self._client()
|
|
374
|
+
payload: dict[str, Any] = {}
|
|
375
|
+
if payment.id.startswith("pi_"):
|
|
376
|
+
payload["payment_intent"] = payment.id
|
|
377
|
+
elif payment.id.startswith("ch_"):
|
|
378
|
+
payload["charge"] = payment.id
|
|
379
|
+
else:
|
|
380
|
+
payload["payment_intent"] = payment.id
|
|
381
|
+
refund = stripe.Refund.create(
|
|
382
|
+
**payload,
|
|
383
|
+
amount=req.amount_minor,
|
|
384
|
+
reason=req.reason,
|
|
385
|
+
metadata=req.resolve("metadata") or None,
|
|
386
|
+
idempotency_key=idempotency_key,
|
|
387
|
+
)
|
|
388
|
+
return {
|
|
389
|
+
"id": refund["id"],
|
|
390
|
+
"status": refund.get("status"),
|
|
391
|
+
"provider": self.component_name,
|
|
392
|
+
"raw": refund,
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
def _get_refund(self, refund_id: str) -> Mapping[str, Any]:
|
|
396
|
+
stripe = self._client()
|
|
397
|
+
refund = stripe.Refund.retrieve(refund_id)
|
|
398
|
+
return {
|
|
399
|
+
"id": refund["id"],
|
|
400
|
+
"status": refund.get("status"),
|
|
401
|
+
"provider": self.component_name,
|
|
402
|
+
"raw": refund,
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
# ---------------------------------------------------------------- Risk
|
|
406
|
+
def _verify_webhook_signature(
|
|
407
|
+
self, raw_body: bytes, headers: Mapping[str, str], secret: str
|
|
408
|
+
) -> bool:
|
|
409
|
+
webhook = self._client().Webhook
|
|
410
|
+
try:
|
|
411
|
+
webhook.construct_event(
|
|
412
|
+
payload=raw_body,
|
|
413
|
+
sig_header=headers.get("Stripe-Signature", ""),
|
|
414
|
+
secret=secret,
|
|
415
|
+
)
|
|
416
|
+
result = True
|
|
417
|
+
except Exception: # pragma: no cover - Stripe raises rich subclasses
|
|
418
|
+
result = False
|
|
419
|
+
return result
|
|
420
|
+
|
|
421
|
+
def _list_disputes(
|
|
422
|
+
self, *, limit: int = 50
|
|
423
|
+
) -> Sequence[Mapping[str, Any]]:
|
|
424
|
+
stripe = self._client()
|
|
425
|
+
disputes = stripe.Dispute.list(limit=limit)
|
|
426
|
+
data = (
|
|
427
|
+
disputes.get("data", []) if isinstance(disputes, Mapping) else []
|
|
428
|
+
)
|
|
429
|
+
return cast(Sequence[Mapping[str, Any]], data)
|