feat(billing): add percentage discount coupons

Admins can create coupon codes that discount specific services (app
runtimes, optional services, managed products, custom-domain addon, or
all) and restrict them to specific users or make them public, with total
and per-user usage caps and an active date window.

Coupons apply in deploy, renewal, and upgrade flows: cost-breakdown lines
are tagged with a service key, the eligible portion is discounted and
capped to the payable amount, the invoice records discountAmount/
discountCode, and the redemption is recorded once when the invoice is
fully paid (covering wallet, gateway, and mixed payments).

- Discount + DiscountRedemption entities; invoice discount columns
- DiscountService (CRUD, validation, redemption) + admin/validate API
- Idempotent schema bootstrap on init so production (synchronize off)
  provisions the tables/columns without a migration runner
- Admin discounts UI, coupon entry in deploy/renewal, invoice discount line
- fa/en strings; discount.service unit spec

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
keyhan
2026-06-20 11:48:45 +03:30
parent b232129ee2
commit 49726f1dfd
21 changed files with 2013 additions and 20 deletions
@@ -123,6 +123,7 @@ export default function AppDetailPage() {
const [snapshotTab, setSnapshotTab] = useState<'revisions' | 'snapshots'>('revisions');
const [showRenewalModal, setShowRenewalModal] = useState(false);
const [selectedCycle, setSelectedCycle] = useState<'hourly' | 'monthly' | 'yearly'>('monthly');
const [renewCoupon, setRenewCoupon] = useState('');
const [showUpgradeConfirm, setShowUpgradeConfirm] = useState(false);
const [upgradeCostData, setUpgradeCostData] = useState<{
proratedAmount: number;
@@ -262,12 +263,17 @@ export default function AppDetailPage() {
});
const renewMutation = useMutation({
mutationFn: (cycle: string) => api.post(`/billing/applications/${appId}/renew`, { cycle }),
mutationFn: (cycle: string) =>
api.post(`/billing/applications/${appId}/renew`, {
cycle,
couponCode: renewCoupon.trim() || undefined,
}),
onSuccess: (res) => {
notify.success(res.data.message || 'Application renewed successfully!');
queryClient.invalidateQueries({ queryKey: ['application', appId] });
queryClient.invalidateQueries({ queryKey: ['wallet'] });
setShowRenewalModal(false);
setRenewCoupon('');
},
onError: (err: any) => {
notify.error(err, 'Failed to renew application');
@@ -276,7 +282,12 @@ export default function AppDetailPage() {
const createRenewalInvoiceMutation = useMutation({
mutationFn: (cycle: string) =>
api.post<Invoice>(`/billing/applications/${appId}/renew/invoice`, { cycle }).then((r) => r.data),
api
.post<Invoice>(`/billing/applications/${appId}/renew/invoice`, {
cycle,
couponCode: renewCoupon.trim() || undefined,
})
.then((r) => r.data),
onSuccess: (invoice) => {
notify.success(ad.invoiceCreated);
queryClient.invalidateQueries({ queryKey: ['invoices'] });
@@ -1299,10 +1310,23 @@ export default function AppDetailPage() {
})()
)}
{/* Coupon */}
<div className="mb-4">
<label className="text-sm font-medium text-gray-700">
{t.dashboard.billing.discounts.coupon.label}
</label>
<input
className="input-field w-full font-mono mt-1.5"
placeholder={t.dashboard.billing.discounts.coupon.placeholder}
value={renewCoupon}
onChange={(e) => setRenewCoupon(e.target.value.toUpperCase())}
/>
</div>
{/* Actions */}
<div className="flex gap-3">
<button
onClick={() => setShowRenewalModal(false)}
onClick={() => { setShowRenewalModal(false); setRenewCoupon(''); }}
className="flex-1 px-4 py-2.5 border border-gray-200 rounded-xl font-medium text-gray-700 hover:bg-gray-50 transition-all"
>{t.common.cancel}</button>
<button