MPP.Methods.Stripe (mpp v0.6.2)

Copy Markdown View Source

Stripe payment method — verifies payment via Stripe PaymentIntent with SPT.

The client creates a Shared Payment Granted Token (SPT) via Stripe, includes it in the credential payload, and the server creates a PaymentIntent with confirm: true to charge it immediately.

Configuration

Pass Stripe-specific config via :method_config in MPP.Plug opts:

plug MPP.Plug,
  secret_key: "hmac-secret",
  realm: "api.example.com",
  method: MPP.Methods.Stripe,
  amount: "5000",
  currency: "usd",
  method_config: %{
    "stripe_secret_key" => "sk_test_...",
    "network_id" => "profile_1Mqx...",
    "payment_method_types" => ["card"]
  }

Config Keys

  • "stripe_secret_key" — (required) Stripe secret key for PaymentIntent creation
  • "network_id" — (required) Stripe Business Network profile ID
  • "payment_method_types" — (optional) accepted payment methods, defaults to ["card"]
  • "realm" — (optional, injected by Plug) server realm for analytics metadata

Credential Payload

The credential payload map must contain:

  • "spt" — (required) Stripe Shared Payment Granted Token (e.g., "spt_1N4...")
  • "externalId" — (optional) caller-provided correlation ID, echoed in receipt

API Functions

FunctionArityDescriptionParam Kinds
challenge_method_details1Return Stripe-specific fields (networkId, paymentMethodTypes) for the 402 challenge.charge: value
verify2Verify a Stripe SPT credential by creating a PaymentIntent with confirm: true.payload: value, charge: value
validate_config!1Validate Stripe method_config at init time. Raises on missing stripe_secret_key or network_id.config: value
method_name0Return the payment method identifier for Stripe.-

Summary

Functions

Return Stripe-specific fields (networkId, paymentMethodTypes) for the 402 challenge.

Return the payment method identifier for Stripe.

Validate Stripe method_config at init time. Raises on missing stripe_secret_key or network_id.

Verify a Stripe SPT credential by creating a PaymentIntent with confirm: true.

Functions

challenge_method_details(charge)

@spec challenge_method_details(MPP.Intents.Charge.t()) :: map() | nil
@spec challenge_method_details(MPP.Intents.Charge.t()) :: map() | nil

Return Stripe-specific fields (networkId, paymentMethodTypes) for the 402 challenge.

Parameters

  • charge - Charge struct with method_details containing network_id and optionally payment_method_types (value)

Returns

Map with networkId and paymentMethodTypes keys, or nil if no network_id configured (map_or_nil)

# descripex:contract
%{
  params: %{
    charge: %{
      description: "Charge struct with method_details containing `network_id` and optionally `payment_method_types`",
      kind: :value
    }
  },
  returns: %{
    type: :map_or_nil,
    description: "Map with `networkId` and `paymentMethodTypes` keys, or `nil` if no `network_id` configured"
  }
}

method_name()

@spec method_name() :: String.t()

Return the payment method identifier for Stripe.

# descripex:contract
%{}

validate_config!(config)

@spec validate_config!(map()) :: :ok
@spec validate_config!(map()) :: :ok

Validate Stripe method_config at init time. Raises on missing stripe_secret_key or network_id.

Parameters

  • config - method_config map to validate (value)

Returns

:ok on success, raises ArgumentError on missing keys (atom)

# descripex:contract
%{
  params: %{
    config: %{description: "method_config map to validate", kind: :value}
  },
  returns: %{
    type: :atom,
    description: "`:ok` on success, raises `ArgumentError` on missing keys"
  }
}

verify(payload, charge)

@spec verify(map(), MPP.Intents.Charge.t()) ::
  {:ok, MPP.Receipt.t()} | {:error, MPP.Errors.t()}

Verify a Stripe SPT credential by creating a PaymentIntent with confirm: true.

Parameters

  • payload - Credential payload map containing "spt" (Stripe Shared Payment Granted Token) (value)
  • charge - Charge intent struct with amount, currency, and method_details (including stripe_secret_key) (value)

Returns

{:ok, receipt} on success, {:error, error} on failure (tagged_tuple)

Errors

  • :invalid_payload
  • :verification_failed
# descripex:contract
%{
  params: %{
    payload: %{
      description: "Credential payload map containing `\"spt\"` (Stripe Shared Payment Granted Token)",
      kind: :value
    },
    charge: %{
      description: "Charge intent struct with amount, currency, and method_details (including `stripe_secret_key`)",
      kind: :value
    }
  },
  errors: [:invalid_payload, :verification_failed],
  returns: %{
    type: :tagged_tuple,
    description: "`{:ok, receipt}` on success, `{:error, error}` on failure"
  }
}