Skip to content
Permalink
Browse files

Batch subscriptions

  • Loading branch information...
jbelelieu committed Dec 2, 2017
1 parent 6ca8626 commit e586b071a8a3499ede113ed52b54cb51810c71ef
@@ -0,0 +1,159 @@
<?php
class subupdown {
private $cart;
private $sub;
private $subscription;
private $price_difference;
private $options;
private $id;
private $direction;
private $newOption;
public $error = false;
public $errorMsg;
public $charge;
public $final_price;
public $new_level;
public function __construct($id, $direction)
{
$this->cart = new cart();
$this->sub = new subscription();
$this->id = $id;
$this->direction = $direction;
$this->run();
}
protected function run()
{
$get = $this->subscription = $this->sub->get_subscription($this->id);
// $getProduct = $this->cart->get_product($get['data']['product']);
$options = $this->options = $this->cart->get_product_options_all($get['data']['product']);
$selectedOptions = json_decode($get['data']['product_options'], true);
$totalOptions = sizeof($options);
$changes = array();
$selectedOption = 0;
$currentSelected = 1;
$options = array_reverse($options);
foreach ($options as $aOption) {
$price = $get['product']['price'] + $aOption['price_adjust'];
if ($aOption['id'] == $selectedOptions['id']) {
$selected = true;
$selectedOption = $currentSelected;
} else {
$selected = false;
}
$changes[] = array(
'selected' => $selected,
'price' => $price,
'option' => $currentSelected,
'price_adjust' => $aOption['price_adjust'],
'raw' => $aOption,
);
$currentSelected++;
}
$selectedOption--;
if ($selectedOption == 0) {
$upgradeOption = $changes['1'];
$downgradeOption = array();
} elseif ($selectedOption == $totalOptions-1) {
$upgradeOption = array();
$downgradeOption = $changes[$totalOptions-2];
} else {
$upgradeOption = $changes[$selectedOption+1];
$downgradeOption = $changes[$selectedOption-1];
}
if ($this->direction == 'down') {
if ($currentSelected == 1) {
$this->error = true;
$this->errorMsg = 'Subscription cannot be downgraded.';
return $this;
} else {
$this->new_level = $newLevel = $currentSelected - 1;
}
} else {
if ($currentSelected == $totalOptions) {
$this->error = true;
$this->errorMsg = 'Subscription cannot be upgraded.';
return $this;
} else {
$this->new_level = $newLevel = $currentSelected + 1;
}
}
$arrayIndex = $newLevel - 1;
$new_price_adjust = $options[$arrayIndex]['price_adjust'];
if ($this->direction == 'down') {
$new_price = $this->price_difference = $downgradeOption['price_adjust'] - $selectedOptions['price_adjust'];
$this->final_price = $final_price = $downgradeOption['price'];
$this->newOption = $downgradeOption['raw'];
} else {
$new_price = $this->price_difference = $upgradeOption['price_adjust'] - $selectedOptions['price_adjust'];
$this->final_price = $final_price = $upgradeOption['price'];
$this->newOption = $upgradeOption['raw'];
// Charge the difference
$card = $this->cart->get_card($get['data']['card_id']);
if (! empty($card['gateway'])) {
$gateway = new $card['gateway']($new_price, $card);
$this->charge = $gateway->charge();
}
}
$this->cart->general_edit('ppSD_subscriptions', array(
'price' => $final_price,
'product_options' => json_encode($this->newOption),
), $get['data']['id']);
$this->sendEmail();
return $this;
}
protected function sendEmail()
{
if ($this->direction == 'down') {
$use_difference = $this->price_difference * -1;
} else {
$use_difference = $this->price_difference;
}
$changes = array(
'subscription' => $this->subscription['data'],
'product' => $this->subscription['product'],
'order_price' => $use_difference,
'new_price' => place_currency($this->final_price),
'options' => $this->newOption,
);
$email = new email(
'',
$this->subscription['data']['member_id'],
$this->subscription['data']['member_type'],
'',
$changes,
'cart_subscription_changed'
);
return true;
}
public function getNewPrice()
{
return $this->final_price;
}
}
@@ -0,0 +1,29 @@
<?php
/**
* Upgrades/downgrades a subscription based on the
* product option, not the subscription package.
*/
require "../sd-system/config.php";
$admin = new admin;
$task = 'sub_updown';
// Check permissions and employee
$employee = $admin->check_employee($task);
$updown = new subupdown($_POST['id'], $_POST['direction']);
if ($updown->error) {
echo "0+++" . $updown->errorMsg;
exit;
} else {
$return = array(
'show_saved' => 'Done. ' . $updown->charge['zen_order_id'],
'refresh_slider' => '1',
);
echo "1+++" . json_encode($return);
exit;
}
@@ -0,0 +1,45 @@
<?php
/**
*
*
* Zenbership Membership Software
* Copyright (C) 2013-2016 Castlamp, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Castlamp
* @link http://www.castlamp.com/
* @link http://www.zenbership.com/
* @copyright (c) 2013-2016 Castlamp
* @license http://www.gnu.org/licenses/gpl-3.0.en.html
* @project Zenbership Membership Software
*/
require "../sd-system/config.php";
$admin = new admin;
$employee = $admin->check_employee();
$cart = new cart();
$options = $cart->get_product_options_all($_POST['product']);
$out = '';
foreach ($options as $item) {
$out .= '<input type="radio" name="product_option" value="' . $item['id'] . '" /> ' . $item['option1'] . ' (' . place_currency($item['price_adjust']) . ')<br />';
}
echo "1+++" . $out;
exit;
@@ -0,0 +1,3 @@
magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off
@@ -0,0 +1,67 @@
<?php
/**
*
*
* Zenbership Membership Software
* Copyright (C) 2013-2016 Castlamp, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Castlamp
* @link http://www.castlamp.com/
* @link http://www.zenbership.com/
* @copyright (c) 2013-2016 Castlamp
* @license http://www.gnu.org/licenses/gpl-3.0.en.html
* @project Zenbership Membership Software
*/
require "../admin/sd-system/config.php";
$cart = new cart;
$subscription = new subscription;
if (! empty($_GET['id'])) {
$sub = $subscription->get_subscription($_GET['id']);
if (! empty($sub['data'])) {
if (empty($_GET['s']) || $sub['data']['salt'] != $_GET['s']) {
$db->show_error_page('S042');
exit;
}
} else {
$db->show_error_page('S042');
exit;
}
} else {
$db->show_error_page('S042');
exit;
}
$updown = new subupdown($_GET['id'], $_GET['d']);
// getNewPrice
$url = PP_URL . '/pp-cart/manage_subscription.php?id=' . $sub['data']['id'] . '&s=' . $sub['data']['salt'];
if ($updown->error) {
header('Location: ' . $url . '&code=S071');
exit;
} else {
$return = array(
'show_saved' => 'Done. ' . $updown->charge['zen_order_id'],
'refresh_slider' => '1',
);
header('Location: ' . $url . '&scode=S072');
exit;
}
@@ -0,0 +1,33 @@
<h1>Subscriptions Renewed</h1>
<div class="box">
<div class="pad">
<p>%first_name%,</p>

<p>We attempted to renew your subscriptions. Here's the outcome:</p>

<h2>Renewal Status?</h2>
<p>%success%</p>

<h2>What Renewed?</h2>
<table border="0" cellspacing=0 cellpadding=0 class=basic>
<thead>
<tr>
<th>Product</th>
<th>Options</th>
<th>Next Renewal</th>
<th>Price</th>
</tr>
</thead>
<tbody>
%entries%
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>%total%</td>
</tr>
</tbody>
</table>

</div>
</div>
@@ -0,0 +1,37 @@
<h1>Subscription No. %subscription:id% Has Been Updated!</h1>

<div class="box">
<div class="pad">
<p>%first_name%,</p>

<p>We are happy to report that the options on your subscription have been successfully updated. Here is your subscription's new information:</p>

<h2>Subscription Overview</h2>
<table border="0" cellspacing=0 cellpadding=0 class=basic>
<tr>
<td class="right" width="130"><b>Name</b></th>
<td>%product:name%</td>
</tr>
<tr>
<td class="right" width="130"><b>Option</b></th>
<td>%options:option1%</td>
</tr>
<tr>
<td class="right" width="130"><b>Description</b></th>
<td>%product:tagline%</td>
</tr>
<tr>
<td class="right" width="130"><b>Price</b></th>
<td>%subscription:format_next_price%</td>
</tr>
<tr>
<td class="right" width="130"><b>Manage</b></th>
<td><a href="%subscription:update_link%">Manage this subscription</a></td>
</tr>
</tbody>
</table>

<p>Please feel free to contact us with any questions you may have regarding this subscription.</p>

</div>
</div>
@@ -0,0 +1,6 @@
<tr>
<td><a href="%data:update_link%">%product:name%</a></td>
<td>%options:option1%</td>
<td>%data:next_renew%</td>
<td>%data:format_price%</td>
</tr>

0 comments on commit e586b07

Please sign in to comment.
You can’t perform that action at this time.