-
Notifications
You must be signed in to change notification settings - Fork 5
Only consider US for apple external billing #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AnthonyRonning
wants to merge
4
commits into
master
Choose a base branch
from
apple-us-only-external
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8a01c1a
Only consider US for apple external billing
AnthonyRonning ef01fa5
Fix iOS simulator Swift library loading issues by targeting iOS 15 an…
AnthonyRonning 5784d2b
Remove unnecessary changes for build
AnthonyRonning ede3f69
First pass at IAP full implementation
AnthonyRonning File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,282 @@ | ||
| import { invoke } from "@tauri-apps/api/core"; | ||
| import { allowExternalBilling } from "../utils/region-gate"; | ||
|
|
||
| // Types for StoreKit | ||
| export type Product = { | ||
| id: string; | ||
| title: string; | ||
| description: string; | ||
| price: string; | ||
| priceValue: number; | ||
| currencyCode: string; | ||
| type: "consumable" | "non_consumable" | "auto_renewable_subscription" | "non_renewable_subscription"; | ||
| subscriptionPeriod?: { | ||
| unit: "day" | "week" | "month" | "year"; | ||
| value: number; | ||
| }; | ||
| introductoryOffer?: SubscriptionOffer; | ||
| promotionalOffers?: SubscriptionOffer[]; | ||
| }; | ||
|
|
||
| export type SubscriptionOffer = { | ||
| id: string; | ||
| displayPrice: string; | ||
| period: { | ||
| unit: "day" | "week" | "month" | "year"; | ||
| value: number; | ||
| }; | ||
| paymentMode: "pay_as_you_go" | "pay_up_front" | "free_trial"; | ||
| type: "introductory" | "promotional" | "prepaid" | "consumable"; | ||
| discountType?: "percentage" | "nominal"; | ||
| discountPrice?: string; | ||
| }; | ||
|
|
||
| export type Transaction = { | ||
| id: number; | ||
| originalId?: number; | ||
| productId: string; | ||
| purchaseDate: number; | ||
| expirationDate?: number; | ||
| webOrderLineItemId: string; | ||
| quantity: number; | ||
| type: "consumable" | "non_consumable" | "auto_renewable_subscription" | "non_renewable_subscription"; | ||
| ownershipType: "purchased" | "familyShared"; | ||
| signedDate: number; | ||
| }; | ||
|
|
||
| export type PurchaseResult = { | ||
| status: "success" | "pending"; | ||
| transactionId?: number; | ||
| originalTransactionId?: number; | ||
| productId?: string; | ||
| purchaseDate?: number; | ||
| expirationDate?: number; | ||
| webOrderLineItemId?: string; | ||
| quantity?: number; | ||
| type?: string; | ||
| ownershipType?: string; | ||
| signedDate?: number; | ||
| environment?: "sandbox" | "production"; | ||
| message?: string; | ||
| }; | ||
|
|
||
| export type VerificationResult = { | ||
| isValid: boolean; | ||
| expirationDate?: number; | ||
| purchaseDate?: number; | ||
| }; | ||
|
|
||
| export type RestorePurchasesResult = { | ||
| status: string; | ||
| transactions: Transaction[]; | ||
| }; | ||
|
|
||
| export type SubscriptionStatus = { | ||
| productId: string; | ||
| status: "subscribed" | "expired" | "in_billing_retry_period" | "in_grace_period" | "revoked" | "not_subscribed"; | ||
| willAutoRenew: boolean; | ||
| expirationDate?: number; | ||
| gracePeriodExpirationDate?: number; | ||
| }; | ||
|
|
||
| /** | ||
| * Maps between Stripe/server product IDs and Apple product IDs | ||
| * In a real app, this would be configured on the server and fetched during initialization | ||
| */ | ||
| const PRODUCT_ID_MAP: Record<string, string> = { | ||
| // Example: Stripe product ID -> Apple product ID (monthly plans only for Apple Pay) | ||
| "price_pro_monthly": "com.opensecret.maple.pro.monthly", | ||
| "price_starter_monthly": "com.opensecret.maple.starter.monthly" | ||
| }; | ||
|
|
||
| class ApplePayService { | ||
| private static instance: ApplePayService; | ||
| private cachedProducts: Map<string, Product> = new Map(); | ||
|
|
||
| // A mapping between system product IDs (e.g., Stripe) and Apple Store product IDs | ||
| private productIdMap: Record<string, string> = PRODUCT_ID_MAP; | ||
|
|
||
| private constructor() {} | ||
|
|
||
| public static getInstance(): ApplePayService { | ||
| if (!ApplePayService.instance) { | ||
| ApplePayService.instance = new ApplePayService(); | ||
| } | ||
| return ApplePayService.instance; | ||
| } | ||
|
|
||
| /** | ||
| * Set the product ID mapping | ||
| */ | ||
| public setProductIdMap(map: Record<string, string>): void { | ||
| this.productIdMap = { ...PRODUCT_ID_MAP, ...map }; | ||
| } | ||
|
|
||
| /** | ||
| * Convert a system product ID to an Apple product ID | ||
| */ | ||
| public getAppleProductId(systemProductId: string): string { | ||
| return this.productIdMap[systemProductId] || systemProductId; | ||
| } | ||
|
|
||
| /** | ||
| * Check if Apple Pay is available and permitted based on region | ||
| */ | ||
| public async isApplePayAvailable(): Promise<boolean> { | ||
| try { | ||
| // First check if we're on iOS | ||
| const { type } = await import("@tauri-apps/plugin-os"); | ||
| const platform = await type(); | ||
|
|
||
| if (platform !== "ios") { | ||
| console.log("[ApplePay] Not on iOS platform"); | ||
| return false; | ||
| } | ||
|
|
||
| // For regions where external billing is not allowed, Apple Pay is required | ||
| // For US regions, we can show both options | ||
| return true; | ||
| } catch (error) { | ||
| console.error("[ApplePay] Error checking availability:", error); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check if Apple Pay should be the required payment method (non-US) | ||
| */ | ||
| public async isApplePayRequired(): Promise<boolean> { | ||
| try { | ||
| // If not on iOS, it's never required | ||
| const { type } = await import("@tauri-apps/plugin-os"); | ||
| const platform = await type(); | ||
|
|
||
| if (platform !== "ios") { | ||
| return false; | ||
| } | ||
|
|
||
| // For non-US regions (where external billing is not allowed), Apple Pay is required | ||
| const isUSRegion = await allowExternalBilling(); | ||
| return !isUSRegion; | ||
| } catch (error) { | ||
| console.error("[ApplePay] Error checking if required:", error); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get products from the App Store | ||
| */ | ||
| public async getProducts(productIds: string[]): Promise<Product[]> { | ||
| try { | ||
| console.log("[ApplePay] Fetching products:", productIds); | ||
| const products = await invoke<Product[]>("plugin:store|get_products", { | ||
| productIds | ||
| }); | ||
|
|
||
| // Cache products for later use | ||
| products.forEach(product => { | ||
| this.cachedProducts.set(product.id, product); | ||
| }); | ||
|
|
||
| return products; | ||
| } catch (error) { | ||
| console.error("[ApplePay] Error fetching products:", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Purchase a product | ||
| */ | ||
| public async purchase(productId: string): Promise<PurchaseResult> { | ||
| try { | ||
| console.log("[ApplePay] Purchasing product:", productId); | ||
| return await invoke<PurchaseResult>("plugin:store|purchase", { | ||
| productId | ||
| }); | ||
| } catch (error) { | ||
| console.error("[ApplePay] Purchase error:", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Verify a purchase | ||
| */ | ||
| public async verifyPurchase(productId: string, transactionId: number): Promise<VerificationResult> { | ||
| try { | ||
| console.log("[ApplePay] Verifying purchase:", productId, transactionId); | ||
| return await invoke<VerificationResult>("plugin:store|verify_purchase", { | ||
| productId, | ||
| transactionId | ||
| }); | ||
| } catch (error) { | ||
| console.error("[ApplePay] Verification error:", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get all transactions, optionally filtered by product ID | ||
| */ | ||
| public async getTransactions(productId?: string): Promise<Transaction[]> { | ||
| try { | ||
| console.log("[ApplePay] Getting transactions"); | ||
| return await invoke<Transaction[]>("plugin:store|get_transactions", { | ||
| productId | ||
| }); | ||
| } catch (error) { | ||
| console.error("[ApplePay] Error getting transactions:", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Restore purchases | ||
| */ | ||
| public async restorePurchases(): Promise<RestorePurchasesResult> { | ||
| try { | ||
| console.log("[ApplePay] Restoring purchases"); | ||
| return await invoke<RestorePurchasesResult>("plugin:store|restore_purchases"); | ||
| } catch (error) { | ||
| console.error("[ApplePay] Restore error:", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get subscription status | ||
| */ | ||
| public async getSubscriptionStatus(productId: string): Promise<SubscriptionStatus> { | ||
| try { | ||
| console.log("[ApplePay] Getting subscription status:", productId); | ||
| return await invoke<SubscriptionStatus>("plugin:store|get_subscription_status", { | ||
| productId | ||
| }); | ||
| } catch (error) { | ||
| console.error("[ApplePay] Error getting subscription status:", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Format price to display currency with localization | ||
| */ | ||
| public formatPrice(product: Product): string { | ||
| return new Intl.NumberFormat(navigator.language, { | ||
| style: "currency", | ||
| currency: product.currencyCode | ||
| }).format(product.priceValue); | ||
| } | ||
|
|
||
| /** | ||
| * Get a product from cache by ID | ||
| */ | ||
| public getCachedProduct(productId: string): Product | undefined { | ||
| return this.cachedProducts.get(productId); | ||
| } | ||
| } | ||
|
|
||
| // Create singleton instance | ||
| export const applePayService = ApplePayService.getInstance(); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incomplete availability check logic.
The
isApplePayAvailablemethod only checks if the platform is iOS but doesn't consider region restrictions, making it potentially misleading.The method returns
truefor all iOS devices regardless of region, which contradicts the region-based gating purpose of this PR. This could lead to Apple Pay being shown in regions where it should be prohibited.