123456789101112131415161718192021222324252627282930import UIKit// + your subscription SDK's import (provides Purchases)enum AppThunderRef { static let apiBase = URL(string: "API_BASE")! // dashboard /// Fresh installs: from your onboarding code field. static func applyCode( _ code: String) { Purchases.shared.attribution.setAttributes(["at_ref": code]) } /// Checks a code before "applied" UI. Public key. static func validateCode( _ code: String, done: @escaping (Bool) -> Void) { var url = URLComponents(url: apiBase.appendingPathComponent("codes/\(code)"), resolvingAgainstBaseURL: false)! url.queryItems = [URLQueryItem(name: "public_key", value: "PUBLIC_KEY")] URLSession.shared.dataTask(with: url.url!) { data, _, _ in let json = data.flatMap { try? JSONSerialization.jsonObject(with: $0) as? [String: Any] } done(json?["valid"] as? Bool ?? false) }.resume() } /// The user's OWN code, stored by your backend. static func share( code: String, from vc: UIViewController) { let text = "Use my code \(code): https://yourapp.com/?ref=\(code)" let sheet = UIActivityViewController(activityItems: [text], applicationActivities: nil) sheet.popoverPresentationController?.sourceView = vc.view // iPad vc.present(sheet, animated: true) }}
123456789101112131415161718192021222324252627282930import android.content.* // Context, Intentimport java.net.* // URL, HttpURLConnectionimport kotlinx.coroutines.* // Dispatchers, withContextimport org.json.JSONObject// + your subscription SDK's import (provides Purchases)object AppThunderRef { /** Fresh installs: from your onboarding code field. */ fun applyCode( code: String) = Purchases.sharedInstance.setAttributes(mapOf("at_ref" to code)) /** Checks a code before "applied" UI. Public key. */ suspend fun validateCode( code: String): Boolean = withContext(Dispatchers.IO) { try { val q = URLEncoder.encode(code, "UTF-8") + "?public_key=PUBLIC_KEY" val conn = URL("API_BASE/codes/$q").openConnection() as HttpURLConnection conn.connectTimeout = 5_000; conn.readTimeout = 5_000 JSONObject(conn.inputStream.bufferedReader().use { it.readText() }).optBoolean("valid") } catch (_: Exception) { false } } /** The user's OWN code, stored by your backend. */ fun share( context: Context, code: String) { val text = "Use my code $code: https://yourapp.com/?ref=$code" val send = Intent(Intent.ACTION_SEND).setType("text/plain").putExtra(Intent.EXTRA_TEXT, text) context.startActivity(Intent.createChooser(send, null).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) }}
12345678910111213141516171819202122232425262728import { Share } from 'react-native';import Purchases from 'react-native-purchases';const API_BASE = 'API_BASE'; // dashboardconst PUBLIC_KEY = 'PUBLIC_KEY';export const AppThunderRef = { /** Fresh installs: from your onboarding code field. */ applyCode(code: string) { return Purchases.setAttributes({ at_ref: code }); }, /** Checks a code before "applied" UI. Public key. */ validateCode(code: string) { const url = `${API_BASE}/codes/${encodeURIComponent(code)}?public_key=${PUBLIC_KEY}`; return fetch(url) .then((res) => res.json()) .then((json: { valid?: boolean }) => json.valid === true) .catch(() => false); }, /** The user's OWN code, stored by your backend. */ share(code: string) { const message = `Use my code ${code}: https://yourapp.com/?ref=${code}`; // message only: same text on iOS, iPad, Android return Share.share({ message }); },};
123456789101112131415161718192021222324252627282930import 'dart:convert';import 'package:flutter/material.dart';import 'package:http/http.dart' as http;import 'package:purchases_flutter/purchases_flutter.dart';import 'package:share_plus/share_plus.dart';class AppThunderRef { /// Fresh installs: from your onboarding code field. static Future<void> applyCode( String code) => Purchases.setAttributes({'at_ref': code}); /// Checks a code before "applied" UI. Public key. static Future<bool> validateCode( String code) async { try { final uri = Uri.parse('API_BASE/codes/${Uri.encodeComponent(code)}?public_key=PUBLIC_KEY'); final res = await http.get(uri).timeout(const Duration(seconds: 5)); return (jsonDecode(res.body) as Map<String, dynamic>)['valid'] == true; } catch (_) { return false; } } /// The user's OWN code, stored by your backend. static Future<void> share( String code, BuildContext context) { final box = context.findRenderObject() as RenderBox; return SharePlus.instance.share(ShareParams( text: 'Use my code $code: https://yourapp.com/?ref=$code', sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size)); // iPad }}
1234567891011121314151617181920212223242526272829// ── browser, after the snippet from your dashboardconst ref = window.AppThunderRef; // shareBtn, onValid: yoursconst code = ref.getCode(); // from ?ref=, 90 days, first touchref.validateCode(code, onValid); // show "code applied"// …post `code` as referredBy with your signup form.shareBtn.addEventListener('click', async () => { const { myCode } = await (await fetch('/api/my-code')).json(); const text = `Use my code ${myCode}:`; ref.share(myCode, { text }); // share sheet or clipboard});// ── your server (Node) · app, db, stripe: yoursconst at = (path, body) => fetch('API_BASE' + path, { method: 'POST', body: JSON.stringify(body), headers: { Authorization: `Bearer ${process.env.AT_SECRET_KEY}`, 'Content-Type': 'application/json' },}).then((r) => r.json());app.post('/signup', async (req, res) => { const user = await db.createUser(req.body); const { code } = await at('/codes', { external_user_id: user.id }); // once; 402 w/o card await db.saveReferralCode(user.id, code); const session = await stripe.checkout.sessions.create({ mode: 'subscription', /* … */ subscription_data: { metadata: { at_ref: req.body.referredBy } } }); res.json({ url: session.url });});app.get('/api/my-code', async (req, res) => (req.user ? res.json({ myCode: await db.referralCode(req.user.id) }) : res.sendStatus(401)));// No Stripe? /referrals at signup, /events per payment.