|
| 1 | +import Product from "./Product"; |
| 2 | +import {PurchaseUpdatePolicy} from "./enums"; |
| 3 | +import ProductOfferDetails from "./storeProducts/ProductOfferDetails"; |
| 4 | +import PurchaseOptions from "./PurchaseOptions"; |
| 5 | + |
| 6 | +class PurchaseOptionsBuilder { |
| 7 | + private offerId: string | null = null; |
| 8 | + private applyOffer: boolean = true; |
| 9 | + private oldProduct: Product | null = null; |
| 10 | + private updatePolicy: PurchaseUpdatePolicy | null = null; |
| 11 | + private contextKeys: string[] | null = null; |
| 12 | + private quantity: number = 1; |
| 13 | + |
| 14 | + /** |
| 15 | + * iOS only. |
| 16 | + * Set quantity of product purchasing. Use for consumable in-app products. |
| 17 | + * @param quantity of product purchasing. |
| 18 | + * @return builder instance for chain calls. |
| 19 | + */ |
| 20 | + setQuantity(quantity: number): PurchaseOptionsBuilder { |
| 21 | + this.quantity = quantity; |
| 22 | + return this; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Android only. |
| 27 | + * Set offer for the purchase. |
| 28 | + * If offer is not specified, then the default offer will be applied. To know how we choose |
| 29 | + * the default offer, see {@link ProductStoreDetails.defaultSubscriptionOfferDetails}. |
| 30 | + * @param offer concrete offer which you'd like to purchase. |
| 31 | + * @return builder instance for chain calls. |
| 32 | + */ |
| 33 | + setOffer(offer: ProductOfferDetails) { |
| 34 | + this.offerId = offer.offerId; |
| 35 | + return this; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Android only. |
| 40 | + * Set the offer Id to the purchase. |
| 41 | + * If {@link offerId} is not specified, then the default offer will be applied. To know how we choose |
| 42 | + * the default offer, see {@link ProductStoreDetails.defaultSubscriptionOfferDetails}. |
| 43 | + * @param offerId concrete offer Id which you'd like to purchase. |
| 44 | + * @return builder instance for chain calls. |
| 45 | + */ |
| 46 | + setOfferId(offerId: string): PurchaseOptionsBuilder { |
| 47 | + this.offerId = offerId; |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Android only. |
| 53 | + * Call this function to remove any intro/trial offer from the purchase (use only a bare base plan). |
| 54 | + * @return builder instance for chain calls. |
| 55 | + */ |
| 56 | + removeOffer(): PurchaseOptionsBuilder { |
| 57 | + this.applyOffer = false; |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Android only. |
| 63 | + * Set Qonversion product from which the upgrade/downgrade will be initialized. |
| 64 | + * |
| 65 | + * @param oldProduct Qonversion product from which the upgrade/downgrade |
| 66 | + * will be initialized. |
| 67 | + * @return builder instance for chain calls. |
| 68 | + */ |
| 69 | + setOldProduct(oldProduct: Product): PurchaseOptionsBuilder { |
| 70 | + this.oldProduct = oldProduct; |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Android only. |
| 76 | + * Set the update policy for the purchase. |
| 77 | + * If the {@link updatePolicy} is not provided, then default one |
| 78 | + * will be selected - {@link PurchaseUpdatePolicy.WITH_TIME_PRORATION}. |
| 79 | + * @param updatePolicy update policy for the purchase. |
| 80 | + * @return builder instance for chain calls. |
| 81 | + */ |
| 82 | + setUpdatePolicy(updatePolicy: PurchaseUpdatePolicy): PurchaseOptionsBuilder { |
| 83 | + this.updatePolicy = updatePolicy; |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Set the context keys associated with a purchase. |
| 89 | + * |
| 90 | + * @param contextKeys context keys for the purchase. |
| 91 | + * @return builder instance for chain calls. |
| 92 | + */ |
| 93 | + setContextKeys(contextKeys: string[]): PurchaseOptionsBuilder { |
| 94 | + this.contextKeys = contextKeys; |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Generate {@link PurchaseOptions} instance with all the provided options. |
| 100 | + * @return the complete {@link PurchaseOptions} instance. |
| 101 | + */ |
| 102 | + build(): PurchaseOptions { |
| 103 | + return new PurchaseOptions(this.offerId, this.applyOffer, this.oldProduct, this.updatePolicy, this.contextKeys, this.quantity) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +export default PurchaseOptionsBuilder; |
0 commit comments