public static function makeRequiredEntities() { $useExistingOrMakeNew = function ($entityClass, $model) { $name = $model->name; try { if ($entityClass === Bf_ProductRatePlan::getClassName()) { $existing = Bf_ProductRatePlan::getByProductAndRatePlanID($model->productID, $name); } else { $existing = $entityClass::getByID($name); } if ($existing) { return $existing; } } catch (Bf_NoMatchingEntityException $e) { return $entityClass::create($model); } }; $models = array('account' => Models::Account(), 'uom' => array(Models::UnitOfMeasure(), Models::UnitOfMeasure2(), Models::UnitOfMeasure2()), 'product' => Models::MonthlyRecurringProduct(), 'pricingComponentTierLists' => array(Models::PricingComponentTiers(), Models::PricingComponentTiers2(), Models::PricingComponentTiers2())); $created = array('account' => Bf_Account::create($models['account']), 'uom' => array($useExistingOrMakeNew(Bf_UnitOfMeasure::getClassName(), $models['uom'][0]), $useExistingOrMakeNew(Bf_UnitOfMeasure::getClassName(), $models['uom'][1])), 'product' => $useExistingOrMakeNew(Bf_Product::getClassName(), $models['product'])); // having created product, make rate plan for it $models['pricingComponents'] = array(Models::PricingComponent($created['uom'][0], $models['pricingComponentTierLists'][0]), Models::PricingComponent2($created['uom'][1], $models['pricingComponentTierLists'][1]), Models::PricingComponent3($created['uom'][1], $models['pricingComponentTierLists'][1])); $models['ratePlan'] = Models::ProductRatePlan($created['product'], $models['pricingComponents']); $created['ratePlan'] = $useExistingOrMakeNew(Bf_ProductRatePlan::getClassName(), $models['ratePlan']); $models['subscription'] = Models::Subscription($created['ratePlan'], $created['account']); $created['subscription'] = Bf_Subscription::create($models['subscription']); return $created; }
/** * Returns a Bf_PriceRequest model with 'updatedPricingComponentValues' mapped to the input key-value pairs. * @param array List of pricing component properties; array(array('name' => 'Bandwidth usage'), array('name' => 'CPU usage')) * @param array List of values to assign to respective pricing components; array(103, 2) * @param string (option 1) The ID of the product rate plan for which to generate a price request * @param Bf_ProductRatePlan (option 2) The model of the product rate plan for which to generate a price request; provide this to avoid fetching from API * @return Bf_PriceRequest The constructed Bf_PriceRequest */ public static function forPricingComponentsByProperties(array $propertiesList, array $valuesList, $productRatePlanID = null, Bf_ProductRatePlan $productRatePlanModel = null) { if (!is_array($propertiesList)) { throw new Bf_MalformedInputException('Expected input to be an array (a list of entity property maps). Instead received: ' + $propertiesList); } if (!is_array($valuesList)) { throw new Bf_MalformedInputException('Expected input to be an array (a list of integer values). Instead received: ' + $valuesList); } $productRatePlan; if (is_null($productRatePlanModel)) { if (is_null($productRatePlanID)) { throw new Bf_EmptyArgumentException('Received null product rate plan, and null product rate plan ID.'); } // fetch from API $productRatePlan = Bf_ProductRatePlan::getByID($productRatePlanID); } else { $productRatePlan = $productRatePlanModel; } $updatedPricingComponentValues = array(); foreach ($propertiesList as $key => $value) { if (!is_array($value)) { throw new Bf_MalformedInputException('Expected each element of input array to be an array (a map of expected properties on entity, to values). Instead received: ' + $value); } $pricingComponent = $productRatePlan->getPricingComponentWithProperties($value); $updatedPricingComponentValue = new Bf_PricingComponentValue(array('pricingComponentID' => $pricingComponent->id, 'value' => $valuesList[$key])); array_push($updatedPricingComponentValues, $updatedPricingComponentValue); } $model = new Bf_PriceRequest(array('productRatePlanID' => $productRatePlan->id, 'productID' => $productRatePlan->productID, 'updatedPricingComponentValues' => $updatedPricingComponentValues)); return $model; }
public function testGetAll() { $productRatePlans = Bf_ProductRatePlan::getAll(); $firstRatePlan = $productRatePlans[0]; $expected = Bf_ProductRatePlan::getResourcePath()->getEntityName(); $actual = $firstRatePlan['@type']; $this->assertEquals($expected, $actual, "Type of any returned entity matches known value."); }
/** * Assigns to this Bf_Coupon a Bf_ProductRatePlan and Bf_Product. * @param union[string ($id | $name) | Bf_ProductRatePlan $ratePlan] The product discounted by the coupon. <string>: ID or name of the Bf_ProductRatePlan. <Bf_ProductRatePlan>: The Bf_ProductRatePlan. * @param union[string ($id | $name) | Bf_Product $entity | NULL] (Default: NULL) The product discounted by the coupon. <string>: ID or name of the Bf_Product. <Bf_Product>: The Bf_Product. <NULL>: Fetch first result for Bf_ProductRatePlan (if identifying rate plan by name, please ensure this rate plan's name is unique), and use its Bf_Product. * @return Bf_Coupon The modified coupon model. */ public function setRatePlan($ratePlan, $product = NULL) { if (is_null($product)) { // get rate plan $ratePlanEntity = Bf_ProductRatePlan::fetchIfNecessary($ratePlan); $product = $ratePlanEntity->productID; } $productIdentifier = Bf_Product::getIdentifier($product); $ratePlanIdentifier = Bf_ProductRatePlan::getIdentifier($ratePlan); $this->product = $productIdentifier; $this->productRatePlan = $ratePlanIdentifier; return $this; }
/** * Retrieves a quote for the price of the specified quantities of pricing components of the product rate plan * @param union[string | Bf_ProductRatePlan] Reference to rate plan <string>: ID of the Bf_ProductRatePlan. <Bf_ProductRatePlan>: The Bf_ProductRatePlan entity. * @param array[string => number] $namesToValues The map of pricing component names to quantities * Example: * array( * 'Bandwidth' => 102, * 'CPU' => 10 * ) * @param array $quoteOptions (Default: All keys set to their respective default values) Encapsulates the following optional parameters: * * @param array (Default: array()) $..['couponCodes'] List of coupon codes to be applied in the quote * * @param string_ENUM['InitialPeriod', 'RecurringPeriod', 'Upgrade'] (Default: 'InitialPeriod') $..['quoteFor'] * * * * <InitialPeriod> (Default) * * * * <RecurringPeriod> * * * * <Upgrade> * * * * @param boolean (Default: true) $..['prorated'] * * @param boolean (Default: false) $..['free'] * * @param union[string | Bf_Subscription] (Default: NULL) $..['subscription'] Reference to some subscription for whom the quote is producted. <string>: ID of the Bf_Subscription. <Bf_Subscription>: The Bf_Subscription entity. * @return Bf_APIQuote The price quote */ public static function getQuote($ratePlan, array $namesToValues, array $quoteOptions = array('couponCodes' => array(), 'quoteFor' => 'InitialPeriod', 'prorated' => true, 'free' => false, 'subscription' => NULL)) { $inputOptions = $quoteOptions; // $ratePlanFetched = Bf_ProductRatePlan::fetchIfNecessary($ratePlan); $ratePlanID = Bf_ProductRatePlan::getIdentifier($ratePlan); $mappings = array_map(function ($name, $value) { return new Bf_QuoteRequestValue(array('pricingComponent' => $name, 'quantity' => $value)); }, array_keys($namesToValues), $namesToValues); $stateParams = static::mergeUserArgsOverNonNullDefaults(__METHOD__, array('quantities' => $mappings, 'productRatePlan' => $ratePlanID), $inputOptions); static::mutateKeysByStaticLambdas($stateParams, array('subscription' => array('Bf_Subscription', 'getIdentifier'))); $requestEntity = new Bf_QuoteRequest($stateParams); $endpoint = ''; $responseEntity = Bf_APIQuote::getClassName(); $constructedEntity = static::postEntityAndGrabFirst($endpoint, $requestEntity, $responseEntity); return $constructedEntity; }
public static function initStatics() { self::$_resourcePath = new Bf_ResourcePath('product-rate-plans', 'productRatePlan'); }
/** * Fetches Bf_ProductRatePlans for this Bf_Product. * @return Bf_ProductRatePlan[] */ public function getRatePlans($options = NULL, $customClient = NULL) { return Bf_ProductRatePlan::getForProduct($this->id, $options, $customClient); }
/** * Synchronously migrates the subscription to the specified plan. * @see scheduleMigratePlan() * @return Bf_MigrationResponse The migration result. */ public function migratePlan(array $namesToValues, $newPlan, array $migrationOptions = array('renameSubscription' => NULL, 'pricingBehaviour' => 'DifferenceProRated', 'invoicingType' => 'Aggregated', 'dryRun' => false)) { $inputOptions = $migrationOptions; $planID = Bf_ProductRatePlan::getIdentifier($newPlan); $subscriptionID = Bf_Subscription::getIdentifier($this); $mappings = array_map(function ($name, $value) { return new Bf_PricingComponentMigrationValue(array('pricingComponent' => $name, 'value' => $value)); }, array_keys($namesToValues), $namesToValues); $stateParams = static::mergeUserArgsOverNonNullDefaults(__METHOD__, array('mappings' => $mappings, 'productRatePlan' => $planID), $inputOptions); static::renameKey($stateParams, 'renameSubscription', 'nextSubscriptionName'); $requestEntity = new Bf_MigrationRequest($stateParams); $endpoint = sprintf("%s/migrate", rawurlencode($subscriptionID)); $responseEntity = Bf_MigrationResponse::getClassName(); $constructedEntity = static::postEntityAndGrabFirst($endpoint, $requestEntity, $responseEntity); return $constructedEntity; }
//-- Make unit of measure $uom = new Bf_UnitOfMeasure(array('name' => 'Devices', 'displayedAs' => 'Devices', 'roundingScheme' => 'UP')); $createdUom = Bf_UnitOfMeasure::create($uom); $createdUomID = $createdUom->id; //-- Make product $product = new Bf_Product(array('productType' => 'non-recurring', 'state' => 'prod', 'name' => 'Month of Paracetamoxyfrusebendroneomycin', 'description' => 'It can cure the common cold, and being struck by lightning', 'durationPeriod' => 'days', 'duration' => 28)); $createdProduct = Bf_Product::create($product); $createdProductID = $createdProduct->id; //-- Make product rate plan //-- Make pricing components for product rate plan //-- Make tiers for pricing component $tier = new Bf_PricingComponentTier(array('lowerThreshold' => 1, 'upperThreshold' => 1, 'pricingType' => 'unit', 'price' => 1)); $tiers = array($tier); $pricingComponentsArray = array(new Bf_PricingComponent(array('@type' => 'flatPricingComponent', 'chargeModel' => 'flat', 'name' => 'Devices used', 'description' => 'How many devices you use, I guess', 'unitOfMeasureID' => $createdUomID, 'chargeType' => 'subscription', 'upgradeMode' => 'immediate', 'downgradeMode' => 'immediate', 'defaultQuantity' => 10, 'tiers' => $tiers))); $prp = new Bf_ProductRatePlan(array('currency' => 'USD', 'name' => getUsualPrpName(), 'pricingComponents' => $pricingComponentsArray, 'productID' => $createdProductID)); $createdPrp = Bf_ProductRatePlan::create($prp); $createdProductRatePlanID = $createdPrp->id; $createdPricingComponentID = $createdPrp->pricingComponents[0]->id; //-- Make pricing component value instance of pricing component $prc = new Bf_PricingComponentValue(array('pricingComponentID' => $createdPricingComponentID, 'value' => 2, 'crmID' => '')); $pricingComponentValuesArray = array($prc); //-- Make Bf_PaymentMethodSubscriptionLinks // refer by ID to our payment method. $paymentMethodReference = new Bf_PaymentMethod(array('id' => $createdPaymentMethodID)); $paymentMethodSubscriptionLink = new Bf_PaymentMethodSubscriptionLink(array('paymentMethod' => $paymentMethodReference, 'organizationID' => $firstOrgID)); $paymentMethodSubscriptionLinks = array($paymentMethodSubscriptionLink); //-- Provision subscription $sub = new Bf_Subscription(array('type' => 'Subscription', 'productID' => $createdProductID, 'productRatePlanID' => $createdProductRatePlanID, 'accountID' => $createdAccID, 'name' => 'Memorable Bf_Subscription', 'description' => 'Memorable Bf_Subscription Description', 'paymentMethodSubscriptionLinks' => $paymentMethodSubscriptionLinks, 'pricingComponentValues' => $pricingComponentValuesArray)); $createdSub = Bf_Subscription::create($sub); // activate provisioned subscription $createdSub->activate();