getPaymentMethods
The
getPaymentMethods function retrieves a subscription’s payment methods.The getPaymentMethods Firebase Cloud Function retrieves a list of payment methods (cards) associated with the Stripe customer linked to a specific subscription. It also identifies which payment method is currently set as the default. This function is accessible only to the owner of the subscription.
Function Signature
export const getPaymentMethods = https.onCall(async (data: GetPaymentMethodsData, context) => { ... });
Parameters
The function expects the following data in the data object:
| Parameter | Type | Description | Required |
|---|---|---|---|
subscriptionId | string | The ID of the subscription for which to retrieve payment methods. | Yes |
GetPaymentMethodsData Interface:
interface GetPaymentMethodsData {
subscriptionId: string;
}
Context
The function requires an authenticated user context (context.auth). The authenticated user must also be the owner of the specified subscription.
Behavior
- Authentication Check: Verifies that the user calling the function is authenticated. If not, it throws an
unauthenticatederror. - Subscription Retrieval: Fetches the subscription document from Firestore using the provided
subscriptionId.- If the subscription does not exist, it throws a
not-founderror.
- If the subscription does not exist, it throws a
- Owner Verification: Checks if the authenticated user is the owner (
owner_id) of the subscription. If not, it throws apermission-deniederror. - Stripe Customer ID Retrieval: Retrieves the Stripe subscription to get the associated customer ID.
- If no customer is found for the subscription, it throws a
failed-preconditionerror.
- If no customer is found for the subscription, it throws a
- Default Payment Method Identification: Retrieves the Stripe customer details to determine their default payment method.
- Payment Methods Listing: Calls the Stripe API to list all card payment methods associated with the customer.
- Mark Default: Iterates through the retrieved payment methods and adds an
isDefault: trueflag to the one matching the customer’s default payment method. - Success: Returns an object containing
success: trueand an array ofpaymentMethods(each with anisDefaultflag).
Error Handling
The function throws HttpsError with specific codes for different failure scenarios:
unauthenticated: If the user is not authenticated.not-found: If the subscription is not found.permission-denied: If the user is not the subscription owner.failed-precondition: If no Stripe customer is found for the subscription.internal: For any Stripe API errors or other unexpected errors during execution.
Example Usage (Client-side)
import { getFunctions, httpsCallable } from 'firebase/functions';
import { getApp } from 'firebase/app';
const functions = getFunctions(getApp());
const getPaymentMethodsCallable = httpsCallable(functions, 'getPaymentMethods');
async function callGetPaymentMethods(subscriptionId: string) {
try {
const result = await getPaymentMethodsCallable({ subscriptionId });
console.log('Payment methods:', result.data.paymentMethods);
// Expected result.data.paymentMethods: [{ id: "pm_...", brand: "visa", last4: "4242", isDefault: true }, ...]
} catch (error) {
console.error('Error getting payment methods:', error.code, error.message);
}
}
// Example call
// callGetPaymentMethods('sub_your_stripe_subscription_id');
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.