-
Notifications
You must be signed in to change notification settings - Fork 12
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 13 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 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,8 @@ | ||
| //! Stripe-specific client implementations. | ||
| //! | ||
| //! Provides [`StripeProvider`] which implements [`PaymentProvider`] for | ||
| //! Stripe charge challenges using Shared Payment Tokens (SPTs). | ||
|
|
||
| mod provider; | ||
|
|
||
| pub use provider::StripeProvider; |
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,163 @@ | ||
| //! Stripe payment provider for client-side credential creation. | ||
|
|
||
| use std::future::Future; | ||
| use std::pin::Pin; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::client::PaymentProvider; | ||
| use crate::error::{MppError, ResultExt}; | ||
| use crate::protocol::core::{PaymentChallenge, PaymentCredential}; | ||
| use crate::protocol::intents::ChargeRequest; | ||
| use crate::protocol::methods::stripe::types::CreateTokenResult; | ||
| use crate::protocol::methods::stripe::{ | ||
| StripeCredentialPayload, StripeMethodDetails, INTENT_CHARGE, METHOD_NAME, | ||
| }; | ||
|
|
||
| /// Parameters passed to the `create_token` callback. | ||
| /// | ||
| /// Matches the mppx `OnChallengeParameters` shape. | ||
| #[derive(Debug, Clone, serde::Serialize)] | ||
| pub struct CreateTokenParams { | ||
| /// Payment amount in smallest currency unit. | ||
| pub amount: String, | ||
| /// Three-letter ISO currency code. | ||
| pub currency: String, | ||
| /// Stripe Business Network profile ID. | ||
| pub network_id: String, | ||
| /// SPT expiration as Unix timestamp (seconds). | ||
| pub expires_at: u64, | ||
| /// Optional metadata from the challenge's methodDetails. | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| pub metadata: Option<std::collections::HashMap<String, String>>, | ||
| /// The full challenge as JSON, for advanced use cases. | ||
| #[serde(skip)] | ||
| pub challenge: serde_json::Value, | ||
| } | ||
|
|
||
| /// Client-side Stripe payment provider. | ||
| /// | ||
| /// Handles 402 challenges with `method="stripe"` by creating an SPT via | ||
| /// the user-provided `create_token` callback and returning a credential. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ```ignore | ||
| /// use mpp::client::stripe::StripeProvider; | ||
| /// use mpp::protocol::methods::stripe::CreateTokenResult; | ||
| /// | ||
| /// let provider = StripeProvider::new(|params| { | ||
| /// Box::pin(async move { | ||
| /// let resp = reqwest::Client::new() | ||
| /// .post("https://my-server.com/api/create-spt") | ||
| /// .json(¶ms) | ||
| /// .send().await.map_err(|e| mpp::MppError::Http(e.to_string()))? | ||
| /// .json::<serde_json::Value>().await | ||
| /// .map_err(|e| mpp::MppError::Http(e.to_string()))?; | ||
| /// Ok(CreateTokenResult { | ||
| /// spt: resp["spt"].as_str().unwrap().to_string(), | ||
| /// external_id: None, | ||
| /// }) | ||
| /// }) | ||
| /// }); | ||
| /// ``` | ||
| type CreateTokenFn = dyn Fn( | ||
| CreateTokenParams, | ||
| ) -> Pin<Box<dyn Future<Output = Result<CreateTokenResult, MppError>> + Send>> | ||
| + Send | ||
| + Sync; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct StripeProvider { | ||
| create_token: Arc<CreateTokenFn>, | ||
| } | ||
|
|
||
| impl StripeProvider { | ||
| /// Create a new Stripe provider with the given SPT creation callback. | ||
| /// | ||
| /// The callback receives [`CreateTokenParams`] and should return a | ||
| /// [`CreateTokenResult`] containing the SPT and optional external ID. | ||
| pub fn new<F>(create_token: F) -> Self | ||
| where | ||
| F: Fn( | ||
| CreateTokenParams, | ||
| ) | ||
| -> Pin<Box<dyn Future<Output = Result<CreateTokenResult, MppError>> + Send>> | ||
| + Send | ||
| + Sync | ||
| + 'static, | ||
| { | ||
| Self { | ||
| create_token: Arc::new(create_token), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl PaymentProvider for StripeProvider { | ||
| fn supports(&self, method: &str, intent: &str) -> bool { | ||
| method == METHOD_NAME && intent == INTENT_CHARGE | ||
| } | ||
|
|
||
| async fn pay(&self, challenge: &PaymentChallenge) -> Result<PaymentCredential, MppError> { | ||
| let request: ChargeRequest = challenge | ||
| .request | ||
| .decode() | ||
| .mpp_config("failed to decode challenge request")?; | ||
|
|
||
| let details: StripeMethodDetails = request | ||
| .method_details | ||
| .as_ref() | ||
| .map(|v| serde_json::from_value(v.clone())) | ||
| .transpose() | ||
| .mpp_config("invalid methodDetails")? | ||
| .unwrap_or_default(); | ||
|
|
||
| let expires_at = challenge | ||
| .expires | ||
| .as_ref() | ||
| .and_then(|e| { | ||
| time::OffsetDateTime::parse(e, &time::format_description::well_known::Rfc3339).ok() | ||
| }) | ||
| .map(|dt| dt.unix_timestamp() as u64) | ||
| .unwrap_or_else(|| { | ||
| std::time::SystemTime::now() | ||
| .duration_since(std::time::UNIX_EPOCH) | ||
| .unwrap() | ||
| .as_secs() | ||
| + 3600 | ||
| }); | ||
|
|
||
| let params = CreateTokenParams { | ||
| amount: request.amount, | ||
| currency: request.currency, | ||
| network_id: details.network_id, | ||
| expires_at, | ||
| metadata: details.metadata, | ||
| challenge: serde_json::to_value(challenge).unwrap_or_default(), | ||
| }; | ||
|
|
||
| let result = (self.create_token)(params).await?; | ||
|
|
||
| let payload = StripeCredentialPayload { | ||
| spt: result.spt, | ||
| external_id: result.external_id, | ||
| }; | ||
|
|
||
| Ok(PaymentCredential::new(challenge.to_echo(), payload)) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_supports() { | ||
| let provider = StripeProvider::new(|_| { | ||
| Box::pin(async { Ok(CreateTokenResult::from("spt_test".to_string())) }) | ||
| }); | ||
|
|
||
| assert!(provider.supports("stripe", "charge")); | ||
| assert!(!provider.supports("tempo", "charge")); | ||
| assert!(!provider.supports("stripe", "session")); | ||
| } | ||
| } |
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
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.
👍