Skip to content

Commit

Permalink
Fix error message purchasing already purchased non-consumables (#200)
Browse files Browse the repository at this point in the history
## Motivation / Description
Fix error message when trying to purcase already purchased
non-consumables
<img width="680" alt="image"
src="https://github.com/user-attachments/assets/5535d468-ae66-42ea-8d23-0cda2a6bcecb">

## Changes introduced

## Linear ticket (if any)

## Additional comments
  • Loading branch information
tonidero authored Oct 8, 2024
1 parent 5e1e832 commit 540b45e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/rcbilling-demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/entities/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class ErrorCodeUtils {
return ErrorCode.StoreProblemError;
case PurchaseFlowErrorCode.UnknownError:
return ErrorCode.UnknownError;
case PurchaseFlowErrorCode.AlreadySubscribedError:
case PurchaseFlowErrorCode.AlreadyPurchasedError:
return ErrorCode.ProductAlreadyPurchasedError;
}
}
Expand Down
24 changes: 15 additions & 9 deletions src/helpers/purchase-operation-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import {
CheckoutStatusErrorCodes,
type CheckoutStatusResponse,
} from "../networking/responses/checkout-status-response";
import type {
PresentedOfferingContext,
PurchaseOption,
import {
type PresentedOfferingContext,
type Product,
ProductType,
type PurchaseOption,
} from "../entities/offerings";
import { Logger } from "./logger";

Expand All @@ -24,7 +26,7 @@ export enum PurchaseFlowErrorCode {
NetworkError = 3,
StripeError = 4,
MissingEmailError = 5,
AlreadySubscribedError = 6,
AlreadyPurchasedError = 6,
}

export class PurchaseFlowError extends Error {
Expand All @@ -45,14 +47,14 @@ export class PurchaseFlowError extends Error {
return true;
case PurchaseFlowErrorCode.ErrorSettingUpPurchase:
case PurchaseFlowErrorCode.ErrorChargingPayment:
case PurchaseFlowErrorCode.AlreadySubscribedError:
case PurchaseFlowErrorCode.AlreadyPurchasedError:
case PurchaseFlowErrorCode.StripeError:
case PurchaseFlowErrorCode.UnknownError:
return false;
}
}

getPublicErrorMessage(): string {
getPublicErrorMessage(productDetails: Product | null): string {
const errorCode =
this.extra?.backendErrorCode ?? this.purchasesErrorCode ?? this.errorCode;
switch (this.errorCode) {
Expand All @@ -70,8 +72,12 @@ export class PurchaseFlowError extends Error {
return this.message;
case PurchaseFlowErrorCode.MissingEmailError:
return "Email is required to complete the purchase.";
case PurchaseFlowErrorCode.AlreadySubscribedError:
return "You are already subscribed to this product.";
case PurchaseFlowErrorCode.AlreadyPurchasedError:
if (productDetails?.productType === ProductType.Subscription) {
return "You are already subscribed to this product.";
} else {
return "You have already purchased this product.";
}
}
}

Expand All @@ -81,7 +87,7 @@ export class PurchaseFlowError extends Error {
): PurchaseFlowError {
let errorCode: PurchaseFlowErrorCode;
if (e.errorCode === ErrorCode.ProductAlreadyPurchasedError) {
errorCode = PurchaseFlowErrorCode.AlreadySubscribedError;
errorCode = PurchaseFlowErrorCode.AlreadyPurchasedError;
} else if (e.errorCode === ErrorCode.InvalidEmailError) {
errorCode = PurchaseFlowErrorCode.MissingEmailError;
} else if (e.errorCode === ErrorCode.NetworkError) {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/helpers/purchase-operation-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe("PurchaseOperationHelper", () => {
},
),
new PurchaseFlowError(
PurchaseFlowErrorCode.AlreadySubscribedError,
PurchaseFlowErrorCode.AlreadyPurchasedError,
"This product is already active for the user.",
"This subscriber is already subscribed to the requested product.",
),
Expand Down
2 changes: 2 additions & 0 deletions src/ui/rcb-ui.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import BrandingInfoUI from "./branding-info-ui.svelte";
import SandboxBanner from "./sandbox-banner.svelte";
import { mapStyleOverridesToStyleVariables } from "../helpers/process-style-overrides";
export let asModal = true;
export let customerEmail: string | undefined;
export let appUserId: string;
Expand Down Expand Up @@ -249,6 +250,7 @@
"Unknown error without state set.",
)}
supportEmail={brandingInfo?.seller_company_support_email}
productDetails={productDetails}
onContinue={closeWithError}
/>
{/if}
Expand Down
17 changes: 10 additions & 7 deletions src/ui/states/state-error.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<script lang="ts">
import {
PurchaseFlowError,
PurchaseFlowErrorCode,
} from "../../helpers/purchase-operation-helper";
import { PurchaseFlowError, PurchaseFlowErrorCode } from "../../helpers/purchase-operation-helper";
import IconError from "../assets/icon-error.svelte";
import { onMount } from "svelte";
import { type BrandingInfoResponse } from "../../networking/responses/branding-response";
import { Logger } from "../../helpers/logger.js";
import MessageLayout from "../layout/message-layout.svelte";
import { type Product, ProductType } from "../../entities/offerings";
export let brandingInfo: BrandingInfoResponse | null = null;
export let lastError: PurchaseFlowError;
export let supportEmail: string | null = null;
export let productDetails: Product | null = null;
export let onContinue: () => void;
onMount(() => {
Expand All @@ -22,8 +21,12 @@
function getErrorTitle(): string {
switch (lastError.errorCode) {
case PurchaseFlowErrorCode.AlreadySubscribedError:
return "Already subscribed";
case PurchaseFlowErrorCode.AlreadyPurchasedError:
if (productDetails?.productType === ProductType.Subscription) {
return "Already subscribed";
} else {
return "Already purchased";
}
default:
return "Something went wrong";
}
Expand All @@ -37,7 +40,7 @@
type="error"
>
<IconError slot="icon" />
{lastError.getPublicErrorMessage()}
{lastError.getPublicErrorMessage(productDetails)}
{#if supportEmail}
<br>If this error persists, please contact <a href="mailto:{supportEmail}"
>{supportEmail}</a
Expand Down

0 comments on commit 540b45e

Please sign in to comment.