How to Retrieve the Stripe Subscription ID from Your Backend
Most applications store the Stripe subscription ID in their own database after a successful checkout. Here are examples for different backend frameworks:
Node.js/Express Example
// Example: Fetching Stripe subscription ID from your database (Node.js/Express)
app.get('/account', async (req, res) => {
// Assume you have a User model with a reference to a Subscription
const user = await User.findById(req.user.id).populate('subscription');
const subscriptionId = user.subscription?.stripeSubscriptionId;
res.json({ subscriptionId });
});
PHP (Laravel) Example
// Example: Fetching Stripe subscription ID from your database (Laravel)
public function showAccount() {
$user = Auth::user();
$subscription = $user->subscription;
$subscriptionId = $subscription ? $subscription->stripe_subscription_id : null;
return response()->json(['subscriptionId' => $subscriptionId]);
}
Ruby on Rails Example
# Example: Fetching Stripe subscription ID from your database (Rails)
class AccountController < ApplicationController
def show
@subscription_id = current_user.subscription&.stripe_subscription_id
render json: { subscriptionId: @subscription_id }
end
end
Python (Django) Example
# Example: Fetching Stripe subscription ID from your database (Django)
def account_view(request):
subscription = Subscription.objects.filter(user=request.user).first()
subscription_id = subscription.stripe_subscription_id if subscription else None
return JsonResponse({'subscriptionId': subscription_id})
Fetching from Stripe API (Node.js Example)
// Using Stripe Node.js SDK
const stripe = require('stripe')('sk_test_your_stripe_secret_key');
async function getCustomerSubscriptions(customerId) {
try {
const subscriptions = await stripe.subscriptions.list({
customer: customerId,
status: 'active',
limit: 1
});
if (subscriptions.data.length > 0) {
return subscriptions.data[0].id; // Returns the subscription ID
}
return null;
} catch (error) {
console.error('Error fetching from Stripe:', error);
throw error;
}
}
Note: Never expose your Stripe secret key in client-side code. Always fetch subscription IDs securely from your backend.
Opting Out of Automatic Cancellation
If you have your own cancellation flow and want to prevent Staysignal from automatically cancelling subscriptions, you can opt out in two ways:
Config option: Pass cancelOptOut: true to the widget init config in JavaScript.
Button attribute: Add data-ss-cancel-optout="true" to your cancel button element.
Precedence: If either method is set, cancellation is skipped and a message is logged to the console.
Advanced Flows After Cancellation
After a cancellation, you may want to trigger additional actions (e.g., send a survey, update your CRM, or notify your team). Use the onComplete callback in the widget config to run custom logic after a successful cancellation: