-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add Stripe payment method support #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2acc05d
feat: scaffold Stripe payment method support
decofe 864356c
chore: add changelog
github-actions[bot] 01f5e62
feat: complete Stripe method implementation
grandizzy 789ddb9
Run stripe integration tests in CI
grandizzy 49173c8
docs: add Stripe examples to README
grandizzy dc73462
fix: align Stripe method with mppx TS SDK wire format
decofe 95af71b
fix: clippy lints and update README examples
decofe 80b1fea
refactor: deduplicate INTENT_CHARGE/INTENT_SESSION constants into pro…
grandizzy abe5748
refactor: use typed serde structs and ResultExt across Stripe code
grandizzy 7ae2af3
feat: add live Stripe integration tests + CI support
decofe 29dc990
fix: live Stripe test fallback for seller_details + suppress dead_cod…
decofe 237c00e
refactor: merge live Stripe tests into integration_stripe, runtime-gated
decofe b7b3927
refactor: extract server tempo/stripe config into submodules
grandizzy 55cd3af
docs: add Stripe example (server + client) (#147)
decofe fbcde07
fix: remove emoji from stripe example to pass no-emojis lint
decofe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| mpp: minor | ||
| --- | ||
|
|
||
| Added a Stripe Shared Payment Token (SPT) example demonstrating the full 402 → challenge → credential → retry flow using Stripe's payment method. Includes a server with SPT proxy endpoint and a headless client using a test card. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| mpp: minor | ||
| --- | ||
|
|
||
| Added Stripe payment method support (`method="stripe"`, `intent="charge"`) with client-side `StripeProvider` for SPT creation, server-side `ChargeMethod` for PaymentIntent verification, and `Mpp::create_stripe()` builder integration. Added `stripe` and `integration-stripe` feature flags backed by `reqwest`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| [package] | ||
| name = "stripe-example" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| publish = false | ||
|
|
||
| [[bin]] | ||
| name = "stripe-server" | ||
| path = "src/server.rs" | ||
|
|
||
| [[bin]] | ||
| name = "stripe-client" | ||
| path = "src/client.rs" | ||
|
|
||
| [dependencies] | ||
| mpp = { path = "../..", features = ["server", "client", "stripe"] } | ||
| axum = "0.7" | ||
| tokio = { version = "1", features = ["full"] } | ||
| reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] } | ||
| serde_json = "1" | ||
| serde = { version = "1", features = ["derive"] } | ||
| rand = "0.9" | ||
| base64 = "0.22" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Stripe Example | ||
|
|
||
| A pay-per-fortune API using Stripe's Shared Payment Token (SPT) flow. | ||
|
|
||
| ## What This Demonstrates | ||
|
|
||
| - Server-side payment protection with `Mpp::create_stripe()` and the Stripe method | ||
| - SPT proxy endpoint (secret key stays server-side) | ||
| - Headless client using a test card (`pm_card_visa`) | ||
| - Full 402 → challenge → credential → retry flow | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Rust 1.80+ | ||
| - A Stripe test-mode secret key (`sk_test_...`) | ||
|
|
||
| ## Running | ||
|
|
||
| **Start the server:** | ||
|
|
||
| ```bash | ||
| export STRIPE_SECRET_KEY=sk_test_... | ||
| cargo run --bin stripe-server | ||
| ``` | ||
|
|
||
| The server starts at http://localhost:3000. | ||
|
|
||
| **Run the client** (in another terminal): | ||
|
|
||
| ```bash | ||
| cargo run --bin stripe-client | ||
| # 🥠 A smooth long journey! Great expectations. | ||
| # Payment receipt: pi_3Q... | ||
| ``` | ||
|
|
||
| ## Testing Manually | ||
|
|
||
| **Without payment** (returns 402): | ||
|
|
||
| ```bash | ||
| curl -i http://localhost:3000/api/fortune | ||
| # HTTP/1.1 402 Payment Required | ||
| # WWW-Authenticate: Payment ... | ||
| ``` | ||
|
|
||
| ## How It Works | ||
|
|
||
| ``` | ||
| Client Server Stripe | ||
| │ │ │ | ||
| │ GET /api/fortune │ │ | ||
| ├──────────────────────────────> │ │ | ||
| │ │ │ | ||
| │ 402 + WWW-Authenticate │ │ | ||
| │<────────────────────────────── │ │ | ||
| │ │ │ | ||
| │ POST /api/create-spt │ │ | ||
| ├──────────────────────────────> │ Create SPT (test helper) │ | ||
| │ ├─────────────────────────────> │ | ||
| │ spt_... │ │ | ||
| │<────────────────────────────── │<───────────────────────────── │ | ||
| │ │ │ | ||
| │ GET /api/fortune │ │ | ||
| │ Authorization: Payment <cred> │ │ | ||
| ├──────────────────────────────> │ PaymentIntent (SPT + confirm)│ | ||
| │ ├─────────────────────────────> │ | ||
| │ │ pi_... succeeded │ | ||
| │ 200 + fortune + receipt │<───────────────────────────── │ | ||
| │<────────────────────────────── │ │ | ||
| ``` | ||
|
|
||
| 1. Client requests the fortune → server returns 402 with a payment challenge | ||
| 2. mpp client calls `create_token` → POSTs to `/api/create-spt` → server creates SPT via Stripe | ||
| 3. Client retries with a credential containing the SPT | ||
| 4. Server creates a PaymentIntent with `shared_payment_granted_token` and `confirm=true` | ||
| 5. On success, returns the fortune with a receipt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| //! # Stripe Fortune Teller CLI Client | ||
| //! | ||
| //! A CLI client that fetches a fortune from the payment-gated Fortune Teller API | ||
| //! using Stripe's Shared Payment Token (SPT) flow. | ||
| //! | ||
| //! Uses a test card (`pm_card_visa`) for headless operation — no browser needed. | ||
| //! | ||
| //! ## Running | ||
| //! | ||
| //! ```bash | ||
| //! # First start the server: | ||
| //! STRIPE_SECRET_KEY=sk_test_... cargo run --bin stripe-server | ||
| //! | ||
| //! # Then in another terminal: | ||
| //! cargo run --bin stripe-client | ||
| //! | ||
| //! # Or target a different server: | ||
| //! cargo run --bin stripe-client -- --server http://localhost:8000 | ||
| //! ``` | ||
|
|
||
| use mpp::client::{Fetch, StripeProvider}; | ||
| use mpp::protocol::methods::stripe::CreateTokenResult; | ||
| use mpp::{parse_receipt, MppError}; | ||
| use reqwest::Client; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| let server_url = std::env::args() | ||
| .skip_while(|a| a != "--server") | ||
| .nth(1) | ||
| .unwrap_or_else(|| "http://localhost:3000".to_string()); | ||
|
|
||
| let server_base = server_url.trim_end_matches('/').to_string(); | ||
| let spt_url = format!("{server_base}/api/create-spt"); | ||
|
|
||
| let provider = StripeProvider::new(move |params| { | ||
| let spt_url = spt_url.clone(); | ||
| Box::pin(async move { | ||
| let resp = Client::new() | ||
| .post(&spt_url) | ||
| .json(&serde_json::json!({ | ||
| "paymentMethod": "pm_card_visa", | ||
| "amount": params.amount, | ||
| "currency": params.currency, | ||
| "expiresAt": params.expires_at, | ||
| })) | ||
| .send() | ||
| .await | ||
| .map_err(|e| MppError::Http(e.to_string()))?; | ||
|
|
||
| if !resp.status().is_success() { | ||
| let body = resp.text().await.unwrap_or_default(); | ||
| return Err(MppError::Http(format!("SPT creation failed: {body}"))); | ||
| } | ||
|
|
||
| let json: serde_json::Value = resp | ||
| .json() | ||
| .await | ||
| .map_err(|e| MppError::Http(e.to_string()))?; | ||
|
|
||
| let spt = json["spt"] | ||
| .as_str() | ||
| .ok_or_else(|| MppError::Http("missing spt in response".to_string()))? | ||
| .to_string(); | ||
|
|
||
| Ok(CreateTokenResult::from(spt)) | ||
| }) | ||
| }); | ||
|
|
||
| let fortune_url = format!("{server_base}/api/fortune"); | ||
| println!("Fetching {fortune_url} ..."); | ||
|
|
||
| let resp = Client::new() | ||
| .get(&fortune_url) | ||
| .send_with_payment(&provider) | ||
| .await | ||
| .expect("request failed"); | ||
|
|
||
| println!("Status: {}", resp.status()); | ||
|
|
||
| if let Some(receipt_hdr) = resp.headers().get("payment-receipt") { | ||
| if let Ok(receipt_str) = receipt_hdr.to_str() { | ||
| if let Ok(receipt) = parse_receipt(receipt_str) { | ||
| println!("Payment receipt: {}", receipt.reference); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let body = resp.text().await.expect("failed to read response body"); | ||
|
|
||
| if let Ok(json) = serde_json::from_str::<serde_json::Value>(&body) { | ||
| if let Some(fortune) = json.get("fortune").and_then(|v| v.as_str()) { | ||
| println!("\nFortune: {fortune}"); | ||
| } else { | ||
| println!("\nResponse: {json}"); | ||
| } | ||
| } else { | ||
| println!("\nResponse: {body}"); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍