Skip to content

Commit 83f818b

Browse files
authored
Merge pull request #1855 from oblien/fix/referral-provider-product-alias
Align referrals with Oblien SDK v4 and clarify checkout errors
2 parents d60e23f + dcb493e commit 83f818b

7 files changed

Lines changed: 129 additions & 38 deletions

File tree

‎apps/api/src/modules/billing/oblien-billing-api.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,27 @@ describe("Oblien billing SDK and transport contract", () => {
426426
expect(JSON.stringify(error)).not.toContain("ER_CANT_AGGREGATE_NCOLLATIONS");
427427
expect(fetcher).toHaveBeenCalledOnce();
428428
});
429+
it("explains self-referral restrictions when a referral is rejected at checkout", async () => {
430+
const { api, fetcher } = setup({ success: false, code: "affiliate_unavailable", message: "private provider detail" }, 400);
431+
const error = await api.createCheckout({
432+
namespace: "os-one", kind: "subscription", offer, metadata, billingInterval: "monthly",
433+
affiliateCode: "AF-0123456789",
434+
successUrl: "https://app.openship.io", cancelUrl: "https://app.openship.io", idempotencyKey: "attempt",
435+
}).catch(error => error);
436+
expect(error).toMatchObject({
437+
statusCode: 400,
438+
details: { providerCode: "affiliate_unavailable" },
439+
});
440+
expect(error.message).toContain("You can’t use your own code or a workspace member’s code.");
441+
expect(error.message).toContain("remove it before starting a new purchase.");
442+
expect(error.message).not.toContain("private provider detail");
443+
expect(fetcher).toHaveBeenCalledOnce();
444+
445+
await expect(api.referralRequest("GET", "/billing/affiliates/reseller/codes/AF-0123456789", {
446+
namespace: "openship",
447+
})).rejects.toThrow("This referral code is no longer available");
448+
});
449+
429450
it("retains actionable plan validation failures instead of treating them as checkout outages", async () => {
430451
const { api } = setup({ success: false, code: "invalid_plan", message: "private provider detail" }, 400);
431452
await expect(

‎apps/dashboard/src/components/billing/server-plan-picker.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ describe("Manage purchase tab", () => {
202202

203203
describe("referral checkout handoff", () => {
204204
const code = "AF-0123456789";
205-
const referral = { code, name: "Community", productId: "workspace_vm", planId: null,
205+
const referral = { code, name: "Community", productId: "vm_compute", planId: null,
206206
discountBps: 500, durationMonths: 12, expiresAt: null, minimumMonthlyCents: 1000 };
207207
const purchase = () => <ManagedServerPurchase requestedPlanId="starter" initialReferralCode={code} autoCheckout />;
208208

‎apps/dashboard/src/components/referrals/ReferralLanding.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const code = "AF-PARTNER";
2222
const offer: ReferralPublicOffer = {
2323
code,
2424
name: "Partner",
25-
productId: "workspace_vm",
25+
productId: "vm_compute",
2626
planId: null,
2727
discountBps: 500,
2828
durationMonths: 12,

‎packages/contracts/src/referrals.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ResourceOperationSchema, ScopedOperations } from "./resource-opera
33

44
const cents = Type.Integer({ minimum: 0, maximum: Number.MAX_SAFE_INTEGER });
55
const nullableString = Type.Union([Type.String(), Type.Null()]);
6+
const referralProduct = Type.Literal("vm_compute");
67
const id = Type.String({
78
pattern: "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$",
89
});
@@ -130,7 +131,7 @@ export const ReferralOfferSchema = Type.Object({
130131
code: ReferralCodeSchema,
131132
url: Type.String(),
132133
name: Type.String(),
133-
productId: Type.Literal("workspace_vm"),
134+
productId: referralProduct,
134135
planId: nullableString,
135136
commissionBps: Type.Integer({ minimum: 1, maximum: 10000 }),
136137
discountBps: Type.Integer({ minimum: 0, maximum: 10000 }),
@@ -150,7 +151,7 @@ export const ReferralCustomerSchema = Type.Object({
150151
id,
151152
offerId: id,
152153
customer: nullableString,
153-
productId: Type.Literal("workspace_vm"),
154+
productId: referralProduct,
154155
planId: nullableString,
155156
status: Type.Union([
156157
Type.Literal("pending"),
@@ -315,7 +316,7 @@ export const ReferralPublicOfferSchema = Type.Object(
315316
{
316317
code: ReferralCodeSchema,
317318
name: Type.String(),
318-
productId: Type.Literal("workspace_vm"),
319+
productId: referralProduct,
319320
planId: nullableString,
320321
discountBps: Type.Integer({ minimum: 0, maximum: 10000 }),
321322
eligiblePlans: Type.Optional(eligiblePlans),

‎packages/platform/src/engine/lib/oblien-billing-api.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,9 @@ export class OblienBillingApi {
385385
...diagnostic,
386386
});
387387
const known: Record<string, string> = {
388-
affiliate_unavailable: "This referral code is no longer available or does not apply to your purchase. Remove it or choose an eligible Cloud plan.",
388+
affiliate_unavailable: method === "POST" && path === "/billing/checkout"
389+
? "This referral code couldn’t be applied. You can’t use your own code or a workspace member’s code. Use an active code for your plan, or remove it before starting a new purchase."
390+
: "This referral code is no longer available or does not apply to your purchase. Remove it or choose an eligible Cloud plan.",
389391
affiliate_paused: "Referral access is paused. Contact support.",
390392
affiliate_not_found: "This referral record was not found for your account.",
391393
affiliate_payout_minimum: "The payout amount is below your minimum. Refresh your balance to see the current minimum.",

‎packages/platform/test/referral-public.test.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, beforeEach, expect, it, vi } from "vitest";
22
const h = vi.hoisted(() => ({
3-
env: { CLOUD_MODE: false, REFERRALS_ENABLED: false },
3+
env: { CLOUD_MODE: false, REFERRALS_ENABLED: false, REFERRAL_NAMESPACE: "openship" },
44
provider: vi.fn(),
55
}));
66
vi.mock("../src/engine/config/env", () => ({
@@ -17,7 +17,7 @@ const code = "AF-0123456789";
1717
const offer = {
1818
code,
1919
name: "Community",
20-
productId: "workspace_vm",
20+
productId: "vm_compute",
2121
planId: null,
2222
discountBps: 500,
2323
durationMonths: 12,
@@ -63,6 +63,23 @@ it("keeps Cloud feature-off unavailable and does not recurse into its public end
6363
expect(h.provider).not.toHaveBeenCalled();
6464
});
6565

66+
it("verifies v2 public terms and requires the configured provider namespace", async () => {
67+
h.env.CLOUD_MODE = true;
68+
h.env.REFERRALS_ENABLED = true;
69+
const scope = { namespace: h.env.REFERRAL_NAMESPACE };
70+
h.provider.mockResolvedValue({ success: true, scope, offer });
71+
await expect(readReferralOffer(code)).resolves.toEqual(offer);
72+
expect(h.provider).toHaveBeenCalledExactlyOnceWith(
73+
"GET",
74+
`/billing/affiliates/reseller/codes/${code}`,
75+
scope,
76+
);
77+
h.provider.mockResolvedValueOnce({ success: true, scope: { namespace: "other" }, offer });
78+
await expect(readReferralOffer(code)).rejects.toMatchObject({
79+
code: "AFFILIATE_PROVIDER_SCOPE_MISMATCH",
80+
});
81+
});
82+
6683
it("preserves existing participant ownership across the Referral rename", () => {
6784
expect(referralParticipantKey("alice")).toBe("9128fa57b97e7d6776411ffefba433b4e312abb60f221daedf2afec80a92d240");
6885
});

‎packages/platform/test/referrals.test.ts‎

Lines changed: 80 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -108,41 +108,91 @@ describe("provider boundary", () => {
108108
).rejects.toMatchObject({ code: "AFFILIATE_PROVIDER_SCOPE_MISMATCH" });
109109
}
110110
});
111-
it("projects commercial links without exposing provider wallet or beneficiary data", async () => {
111+
const providerOffer = {
112+
id,
113+
code: "AF-0123456789",
114+
url: "https://evil.test",
115+
name: "Share",
116+
productId: "vm_compute",
117+
planId: null,
118+
commissionBps: 2000,
119+
discountBps: 500,
120+
durationMonths: 12,
121+
status: "active",
122+
createdAt: "2026-11-19T00:00:00.000Z",
123+
expiresAt: null,
124+
revokedAt: null,
125+
user_id: "provider-secret",
126+
participant_key: "private",
127+
wallet: 1000000,
128+
};
129+
const dashboardUrl = "https://app.openship.test";
130+
131+
it.each(["createOffer", "revokeOffer"] as const)(
132+
"accepts the v2 product and keeps private fields out of %s",
133+
async (operation) => {
134+
const billing = { referralRequest: vi.fn(async () => envelope(providerOffer)) };
135+
const result = await callReferralProvider(billing, scope, dashboardUrl, operation, { id });
136+
expect(result).toMatchObject({
137+
productId: "vm_compute",
138+
url: "https://app.openship.test/r/AF-0123456789",
139+
});
140+
expect(result).not.toHaveProperty("user_id");
141+
expect(result).not.toHaveProperty("wallet");
142+
expect(result).not.toHaveProperty("participant_key");
143+
},
144+
);
145+
146+
it("lists v2 offers and rejects other product IDs", async () => {
112147
const billing = {
113-
referralRequest: vi.fn(async () =>
148+
referralRequest: vi.fn(async () => envelope({ total: 1, items: [providerOffer] })),
149+
};
150+
const result = await callReferralProvider(billing, scope, dashboardUrl, "offers");
151+
expect(result.items).toHaveLength(1);
152+
expect(result.items[0]).toMatchObject({
153+
productId: "vm_compute",
154+
url: "https://app.openship.test/r/AF-0123456789",
155+
});
156+
expect(result.items[0]).not.toHaveProperty("wallet");
157+
158+
for (const productId of ["workspace_vm", "unrelated_product"]) {
159+
billing.referralRequest.mockResolvedValueOnce(
160+
envelope({ total: 1, items: [{ ...providerOffer, productId }] }),
161+
);
162+
await expect(callReferralProvider(billing, scope, dashboardUrl, "offers")).rejects.toMatchObject({
163+
code: "AFFILIATE_PROVIDER_RESPONSE_INVALID",
164+
});
165+
}
166+
});
167+
168+
it("keeps referred customers visible after the provider product rename", async () => {
169+
const billing = {
170+
referralRequest: async () =>
114171
envelope({
115-
id,
116-
code: "AF-0123456789",
117-
url: "https://evil.test",
118-
name: "Share",
119-
productId: "workspace_vm",
120-
planId: null,
121-
commissionBps: 2000,
122-
discountBps: 500,
123-
durationMonths: 12,
124-
status: "active",
125-
createdAt: new Date().toISOString(),
126-
expiresAt: null,
127-
revokedAt: null,
128-
user_id: "provider-secret",
129-
participant_key: "private",
130-
wallet: 1000000,
172+
total: 1,
173+
items: [{
174+
id,
175+
offerId: id,
176+
customer: "Customer",
177+
productId: "vm_compute",
178+
planId: null,
179+
status: "active",
180+
paidThrough: null,
181+
autoRenew: true,
182+
nextRenewalAt: null,
183+
joinedAt: "2026-11-19T00:00:00.000Z",
184+
rewardsUntil: null,
185+
lastPaymentAt: null,
186+
paidCycles: 1,
187+
earned: 200,
188+
currency: "usd",
189+
}],
131190
}),
132-
),
133191
};
134-
const result = await callReferralProvider(
135-
billing,
136-
scope,
137-
"https://app.openship.test",
138-
"createOffer",
139-
{ policyId: id, name: "Share", discountBps: 500, idempotencyKey: "1234567890123456" },
140-
);
141-
expect(result.url).toBe("https://app.openship.test/r/AF-0123456789");
142-
expect(result).not.toHaveProperty("user_id");
143-
expect(result).not.toHaveProperty("wallet");
144-
expect(result).not.toHaveProperty("participant_key");
192+
const result = await callReferralProvider(billing, scope, dashboardUrl, "referrals");
193+
expect(result.items).toMatchObject([{ productId: "vm_compute", earned: 200 }]);
145194
});
195+
146196
it("fails closed on incomplete monetary totals", async () => {
147197
await expect(
148198
callReferralProvider(

0 commit comments

Comments
 (0)