Skip to content

Commit

Permalink
Add CheckoutResponse types
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehenty committed Dec 4, 2024
1 parent 8265842 commit 900a387
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 34 deletions.
5 changes: 3 additions & 2 deletions cypress/pages/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CheckoutOptions, CheckoutPopupEvents } from '../../src';
import { CheckoutResponse } from '../../src/lib/contracts/CheckoutResponse';

export const initOptions: CheckoutOptions = {
product_id: Number.parseInt(
Expand Down Expand Up @@ -47,7 +48,7 @@ export const eventListeners: CheckoutPopupEvents = {
afterClose() {
updateEventText(EVENT_LOG.AFTER_CLOSE);
},
success() {
success(data: CheckoutResponse | null) {
updateEventText(EVENT_LOG.SUCCESS);
},
track(event) {
Expand All @@ -56,7 +57,7 @@ export const eventListeners: CheckoutPopupEvents = {
afterOpen() {
updateEventText(EVENT_LOG.AFTER_OPEN);
},
purchaseCompleted() {
purchaseCompleted(data: CheckoutResponse | null) {
updateEventText(EVENT_LOG.PURCHASE_COMPLETED);
},
onExitIntent() {
Expand Down
7 changes: 4 additions & 3 deletions src/demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Checkout, CheckoutOptions, CheckoutPopupOptions } from '.';
import { CheckoutResponse } from './lib/contracts/CheckoutResponse';

import './style.css';

Expand Down Expand Up @@ -65,13 +66,13 @@ document.addEventListener('DOMContentLoaded', () => {
cancel() {
log('cancel');
},
purchaseCompleted(data) {
purchaseCompleted(data: CheckoutResponse | null) {
log('purchaseCompleted', data);
},
success(data) {
success(data: CheckoutResponse | null) {
log('success', data);
},
track(event, data) {
track(event: string, data: Record<string, any> | null) {
log('track', event, data);
},
afterOpen() {
Expand Down
54 changes: 27 additions & 27 deletions src/lib/contracts/CheckoutPopupOptions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { CheckoutResponse } from './CheckoutResponse';

/**
* Supported "locale" option for the checkout.
* @see https://freemius.com/help/documentation/selling-with-freemius/freemius-checkout-buy-button/
* 1. `auto` - The system will try to guess the language of your user by looking into the browser and then the geo-location respectively. However, this wont select languages that are marked as AI-translated or beta for the time being.
* 2. `auto-beta` - Same as above, but will also select a language marked as beta. When a language marked as beta is selected, the UI will also show a BETA tag near it.
* 1. `auto` - The system will try to guess the language of your user by looking into the browser and then the geo-location respectively. However, this won't select languages that are marked as AI-translated or beta for the time being.
* 2. `auto-beta` - Same as above, but will also select a language marked as beta. When a language marked as beta is selected, the UI will also show a "BETA" tag near it.
* 3. Full locale code (for eg - `en_US`, `de_DE`, `fr_FR`, etc.)
*/
export type CheckoutLocaleValue = 'auto' | 'auto-beta' | string;
Expand Down Expand Up @@ -33,7 +35,7 @@ export type CheckoutTrackingEvent =
*/
export interface CheckoutPopupParams {
/**
* Required product ID (whether its a plugin, theme, add-on, bundle, or SaaS).
* Required product ID (whether it's a plugin, theme, add-on, bundle, or SaaS).
*/
plugin_id: number | string;

Expand All @@ -43,7 +45,7 @@ export interface CheckoutPopupParams {
public_key: string;

/**
* An optional ID to set the id attribute of the checkouts <body> HTML element.
* An optional ID to set the id attribute of the checkout's <body> HTML element.
* This argument is particularly useful if you have multiple checkout instances
* that need to have a slightly different design or visibility of UI components.
* You can assign a unique ID for each instance and customize it differently
Expand All @@ -53,29 +55,29 @@ export interface CheckoutPopupParams {
id?: string;

/**
* An optional string to override the products title.
* An optional string to override the product's title.
*
* @default "Defaults to the products title set within the Freemius dashboard."
* @default "Defaults to the product's title set within the Freemius dashboard."
*/
name?: string;

/**
* An optional string to override the checkouts title when buying a new license.
* An optional string to override the checkout's title when buying a new license.
*
* @default "Great selection, {{ firstName }}!"
*/
title?: string;

/**
* An optional string to override the checkouts subtitle.
* An optional string to override the checkout's subtitle.
*
* @default "Youre one step closer to our {{ planTitle }} features"
* @default "You're one step closer to our {{ planTitle }} features"
* @deprecated
*/
subtitle?: string;

/**
* An optional icon that loads at the checkout and will override the products
* An optional icon that loads at the checkout and will override the product's
* icon uploaded to the Freemius Dashboard. Use a secure path to the image
* over HTTPS. While the checkout will remain PCI compliant,
* credit-card automatic prefill by the browser will not work.
Expand Down Expand Up @@ -114,7 +116,7 @@ export interface CheckoutPopupParams {
* Use the `licenses` param instead. An optional ID of the exact multi-site
* license prices that will load once the checkout opened.
*
* @default "plans single-site prices ID"
* @default "plan's single-site prices ID"
*/
pricing_id?: number | string;

Expand Down Expand Up @@ -172,9 +174,9 @@ export interface CheckoutPopupParams {

/**
* When set to true, it will open the checkout in a trial mode and the trial
* type (free vs. paid) will be based on the plans configuration. This will
* only work if youve activated the Free Trial functionality in the plan
* configuration. If you configured the plan to support a trial that doesnt
* type (free vs. paid) will be based on the plan's configuration. This will
* only work if you've activated the Free Trial functionality in the plan
* configuration. If you configured the plan to support a trial that doesn't
* require a payment method, you can also open the checkout in a trial mode
* that requires a payment method by setting the value to 'paid'.
*
Expand All @@ -198,17 +200,17 @@ export interface CheckoutPopupParams {
is_payment_method_update?: boolean;

/**
* An optional string to prefill the buyers email address.
* An optional string to prefill the buyer's email address.
*/
user_email?: string;

/**
* An optional string to prefill the buyers first name.
* An optional string to prefill the buyer's first name.
*/
user_firstname?: string;

/**
* An optional string to prefill the buyers last name.
* An optional string to prefill the buyer's last name.
*/
user_lastname?: string;

Expand All @@ -219,19 +221,19 @@ export interface CheckoutPopupParams {
affiliate_user_id?: number;

/**
* An optional locale to override the checkouts language.
* An optional locale to override the checkout's language.
*/
language?: CheckoutLocaleValue;

/**
* An optional locale to override the checkouts language.
* An optional locale to override the checkout's language.
*
* @see `language`
*/
locale?: CheckoutLocaleValue;

/**
* An optional token which if present, would pre-populate the checkout with users personal and billing data (for example, the name, email, country, vat ID etc).
* An optional token which if present, would pre-populate the checkout with user's personal and billing data (for example, the name, email, country, vat ID etc).
*
* @see https://freemius.com/help/documentation/selling-with-freemius/freemius-checkout-buy-button/#user_token_in_checkout_new
*/
Expand Down Expand Up @@ -377,20 +379,18 @@ export interface CheckoutPopupEvents {
* An after successful purchase/subscription completion callback handler.
*
* Notice: When the user subscribes to a recurring billing plan, this method
* will execute upon a successful subscription creation. It doesnt guarantee
* that the subscriptions initial payment was processed successfully as well.
* will execute upon a successful subscription creation. It doesn't guarantee
* that the subscription's initial payment was processed successfully as well.
*/
purchaseCompleted?: (data: Record<string, any> | null) => void;
purchaseCompleted?: (data: CheckoutResponse | null) => void;
/**
* An optional callback handler, similar to purchaseCompleted. The main
* difference is that this callback will only execute after the user clicks
* the Got It”” button that appears in the after purchase screen as a
* the "Got It"" button that appears in the after purchase screen as a
* declaration that they successfully received the after purchase email.
* This callback is obsolete when the checkout is running in a 'dashboard' mode.
*/
success?: (
data: { purchase: Record<string, any> } | Record<string, any> | null
) => void;
success?: (data: CheckoutResponse | null) => void;
/**
* An optional callback handler for advanced tracking, which will be called on
* multiple checkout events such as updates in the currency, billing cycle,
Expand Down
52 changes: 52 additions & 0 deletions src/lib/contracts/CheckoutResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export interface User {
email: string;
first: string;
last: string;
public_key: string;
id: string;
created: string;
resend_email_endpoint: string;
}

export interface Purchase {
tax_rate?: number;
total_gross?: number;
amount_per_cycle?: number;
initial_amount?: number;
renewal_amount?: number;
renewals_discount?: number;
renewals_discount_type?: string;
billing_cycle?: number;
outstanding_balance?: number;
failed_payments?: number;
trial_ends?: string | null;
next_payment?: string;
canceled_at?: string | null;
user_id?: string;
install_id?: string | null;
plan_id?: string;
pricing_id?: string;
license_id?: string;
ip?: string;
country_code?: string;
zip_postal_code?: number;
vat_id?: string | null;
coupon_id?: string | null;
user_card_id?: string;
source?: number;
plugin_id?: string;
external_id?: string;
gateway?: string;
environment?: number;
id?: string;
created?: string;
updated?: string | null;
currency?: string;
license_key?: string;
[key: string]: unknown;
}

export interface CheckoutResponse {
purchase?: Purchase;
user?: User;
}
5 changes: 3 additions & 2 deletions src/lib/services/checkout-popup/CheckoutIFrame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CheckoutPopupEvents } from '../../contracts/CheckoutPopupOptions';
import { buildFreemiusQueryFromOptions } from '../../utils/ops';
import { Logger } from '../logger';
import { IExitIntent } from '../../contracts/IExitIntent';
import { CheckoutResponse } from '../../contracts/CheckoutResponse';

type EventListener = () => void;

Expand Down Expand Up @@ -91,7 +92,7 @@ export class CheckoutIFrame {
'upgraded',
(data) => {
try {
success?.(data as any);
success?.(data as CheckoutResponse);
} catch (e) {
Logger.Error(e);
}
Expand All @@ -107,7 +108,7 @@ export class CheckoutIFrame {
'purchaseCompleted',
(data) => {
try {
purchaseCompleted?.(data as any);
purchaseCompleted?.(data as CheckoutResponse);
} catch (e) {
Logger.Error(e);
}
Expand Down

0 comments on commit 900a387

Please sign in to comment.