/**
* @param string $customer_id
* @param string $plan_id
* @param array $addOns
* @param array $discounts
* @param array $removeAddOns
* @param array $removeDiscounts
* @return bool | int
* @throws Exception
*/
public function createSubscription($customer_id, $plan_id, $addOns = [], $discounts = [], $removeAddOns = [], $removeDiscounts = [])
{
$customer = Braintree_Customer::find($customer_id);
$the_token = null;
if (!empty($customer)) {
$the_token = $customer->paymentMethods[0]->token;
} else {
throw new Exception("Customer not found \n");
}
$formattedAddOns = [];
foreach ($addOns as $addOn) {
$formattedAddOns[] = ['inheritedFromId' => $addOn];
}
$formattedDiscounts = [];
foreach ($discounts as $discount) {
$formattedDiscounts[] = ['inheritedFromId' => $discount];
}
$result = Braintree_Subscription::create(['paymentMethodToken' => $the_token, 'planId' => $plan_id, 'addOns' => ['add' => $formattedAddOns, 'remove' => $removeAddOns], 'discounts' => ['add' => $formattedDiscounts, 'remove' => $removeDiscounts]]);
if ($result->success) {
return $result->subscription->id;
} else {
foreach ($result->errors->deepAll() as $error) {
throw new Exception($error->code . ": " . $error->message . "\n");
}
}
return false;
}