public function testAll_returnsAllPlans() { $newId = strval(rand()); $params = array("id" => $newId, "billingDayOfMonth" => "1", "billingFrequency" => "1", "currencyIsoCode" => "USD", "description" => "some description", "name" => "php test plan", "numberOfBillingCycles" => "1", "price" => "1.00", "trialPeriod" => "false"); $http = new Braintree\Http(Braintree\Configuration::$global); $path = Braintree\Configuration::$global->merchantPath() . '/plans/create_plan_for_tests'; $http->post($path, array("plan" => $params)); $addOnParams = array("kind" => "add_on", "plan_id" => $newId, "amount" => "1.00", "name" => "add_on_name"); $http = new Braintree\Http(Braintree\Configuration::$global); $path = Braintree\Configuration::$global->merchantPath() . '/modifications/create_modification_for_tests'; $http->post($path, array('modification' => $addOnParams)); $discountParams = array("kind" => "discount", "plan_id" => $newId, "amount" => "1.00", "name" => "discount_name"); $http = new Braintree\Http(Braintree\Configuration::$global); $path = Braintree\Configuration::$global->merchantPath() . '/modifications/create_modification_for_tests'; $http->post($path, array("modification" => $discountParams)); $plans = Braintree\Plan::all(); foreach ($plans as $plan) { if ($plan->id == $newId) { $actualPlan = $plan; } } $this->assertNotNull($actualPlan); $this->assertEquals($params["billingDayOfMonth"], $actualPlan->billingDayOfMonth); $this->assertEquals($params["billingFrequency"], $actualPlan->billingFrequency); $this->assertEquals($params["currencyIsoCode"], $actualPlan->currencyIsoCode); $this->assertEquals($params["description"], $actualPlan->description); $this->assertEquals($params["name"], $actualPlan->name); $this->assertEquals($params["numberOfBillingCycles"], $actualPlan->numberOfBillingCycles); $this->assertEquals($params["price"], $actualPlan->price); $addOn = $actualPlan->addOns[0]; $this->assertEquals($addOnParams["name"], $addOn->name); $discount = $actualPlan->discounts[0]; $this->assertEquals($discountParams["name"], $discount->name); }
/** * Get the Braintree plan that has the given ID. * * @param string $id * @return \Braintree\Plan */ public static function findPlan($id) { $plans = BraintreePlan::all(); foreach ($plans as $plan) { if ($plan->id === $id) { return $plan; } } throw new Exception("Unable to find Braintree plan with ID [{$id}]."); }
/** * Find Braintree plan object by id. * * @param int $id * @return \Braintree\Plan * @throws \Exception */ public function findPlanById($id) { $braintreePlans = BraintreePlan::all(); $plan = null; foreach ($braintreePlans as $braintreePlan) { if ($braintreePlan->id == $id) { return $braintreePlan; } } throw new Exception("Plan with id '{$id}' does not exist in your Braintree account."); }
/** * @param string $plan_id * @param array $addOns * @param array $discounts * @param array $removeAddOns * @param array $removeDiscounts * @return mixed */ public function getPlanSummary($plan_id, $addOns = [], $discounts = [], $removeAddOns = [], $removeDiscounts = []) { $summary = []; $plans = Braintree_Plan::all(); foreach ($plans as $plan) { if ($plan->id == $plan_id) { $summary = []; $summary['price'] = $plan->price; $summary['summary'] = $plan->price; //add all default add-ons if (!empty($plan->addOns)) { foreach ($plan->addOns as $planAddOn) { if (!in_array($planAddOn->id, $removeAddOns)) { $summary['addOns'][] = ['name' => $planAddOn->name, 'description' => $planAddOn->description, 'amount' => $planAddOn->amount]; $summary['summary'] += $planAddOn->amount; } } } //add all default discounts if (!empty($plan->discounts)) { foreach ($plan->discounts as $planDiscount) { if (!in_array($planDiscount->id, $removeDiscounts)) { $summary['discounts'][] = ['name' => $planDiscount->name, 'description' => $planDiscount->description, 'amount' => $planDiscount->amount]; $summary['summary'] -= $planDiscount->amount; } } } break; } } //add all manually added add-ons $SystemAddOns = Braintree_AddOn::all(); foreach ($addOns as $addOn) { foreach ($SystemAddOns as $SystemAddOn) { if ($SystemAddOn->id == $addOn) { $summary['addOns'][] = ['name' => $SystemAddOn->name, 'description' => $SystemAddOn->description, 'amount' => $SystemAddOn->amount]; $summary['summary'] += $SystemAddOn->amount; } } } //add all manually added discounts $SystemDiscounts = Braintree_Discount::all(); foreach ($discounts as $discount) { foreach ($SystemDiscounts as $SystemDiscount) { if ($SystemDiscount->id == $discount) { $summary['discounts'][] = ['name' => $SystemDiscount->name, 'description' => $SystemDiscount->description, 'amount' => $SystemDiscount->amount]; $summary['summary'] -= $SystemDiscount->amount; } } } return $summary; }
/** * @param boolean $allowCaching whether to allow caching the result of retrieving of data from Braintree; * when this parameter is true (default), if data was retrieved before, * result will be directly returned when calling this method; * if this parameter is false, this method will always perform request to Braintree to obtain the up-to-date data; * note that this caching is effective only within the same HTTP request * @return Plan[] */ public function getAllPlans($allowCaching = true) { if (!$allowCaching) { return Plan::all(); } if (!isset($this->plans)) { $this->plans = Plan::all(); } return $this->plans; }