Skip to content

Commit

Permalink
Version bump to 1.1.0: adds support for different installments plans
Browse files Browse the repository at this point in the history
  • Loading branch information
olance committed Apr 24, 2019
1 parent 97a35cf commit 957fdc9
Show file tree
Hide file tree
Showing 25 changed files with 2,317 additions and 237 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

v1.1.0
------

* Dependencies update
* Adds support for different installments plans: 2-, 3- and 4-installment plans, configurable in the Payment Methods
settings.

v1.0.1
------

Expand Down
56 changes: 56 additions & 0 deletions app/code/community/Alma/Installments/Block/PaymentForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* 2018-2019 Alma SAS
*
* THE MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @author Alma SAS <[email protected]>
* @copyright 2018-2019 Alma SAS
* @license https://opensource.org/licenses/MIT The MIT License
*/


class Alma_Installments_Block_PaymentForm extends Mage_Payment_Block_Form
{
/**
* @var Alma_Installments_Helper_Config
*/
private $config;

protected function _construct()
{
parent::_construct();
$this->setTemplate('alma/payment_form.phtml');

$this->config = Mage::helper('alma/config');
}

public function displayPnX($n)
{
if (!$this->config->isPnXEnabled($n)) {
return false;
}

/** @var Mage_Sales_Model_Quote $quote */
$quote = Mage::helper('checkout/cart')->getQuote();
if(!$quote) {
return false;
}

$cartTotal = Alma_Installments_Helper_Functions::priceToCents((float)$quote->getGrandTotal());
return $cartTotal >= $this->config->pnxMinAmount($n) && $cartTotal < $this->config->pnxMaxAmount($n);
}
}
27 changes: 27 additions & 0 deletions app/code/community/Alma/Installments/Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class Alma_Installments_Helper_Config extends Mage_Core_Helper_Abstract
const CONFIG_EXCLUDED_PRODUCT_TYPES = 'payment/alma_installments/excluded_product_types';
const CONFIG_EXCLUDED_PRODUCTS_MESSAGE = 'payment/alma_installments/excluded_products_message';

const CONFIG_PNX_ENABLED = 'payment/alma_installments/p%dx_enabled';
const CONFIG_PNX_MIN_AMOUNT = 'payment/alma_installments/p%dx_min_amount';
const CONFIG_PNX_MAX_AMOUNT = 'payment/alma_installments/p%dx_max_amount';
const CONFIG_PNX_MAX_N = 'payment/alma_installments/pnx_max_n';

const CONFIG_FULLY_CONFIGURED = 'payment/alma_installments/fully_configured';

public function get($field, $default = null, $storeId = null)
Expand Down Expand Up @@ -132,4 +137,26 @@ public function isFullyConfigured()
{
return !$this->needsAPIKeys() && (bool)(int)$this->get(self::CONFIG_FULLY_CONFIGURED, false);
}

public function isPnXEnabled($n)
{
return (bool)(int)$this->get(sprintf(self::CONFIG_PNX_ENABLED, $n), $n == 3);
}

public function pnxMinAmount($n, $merchant = null)
{
$min = $merchant ? $merchant->minimum_purchase_amount : 10000;
return (int)$this->get(sprintf(self::CONFIG_PNX_MIN_AMOUNT, $n), $min);
}

public function pnxMaxAmount($n, $merchant = null)
{
$max = $merchant ? $merchant->maximum_purchase_amount : 100000;
return (int)$this->get(sprintf(self::CONFIG_PNX_MAX_AMOUNT, $n), $max);
}

public function pnxMaxN()
{
return (int)$this->get(self::CONFIG_PNX_MAX_N, 3);
}
}
29 changes: 29 additions & 0 deletions app/code/community/Alma/Installments/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,33 @@

class Alma_Installments_Helper_Data extends Mage_Core_Helper_Abstract {

/** @var \Alma\API\Entities\Merchant */
private $merchant = null;
/** @var \Alma\API\Client $alma */
private $alma;
/** @var \Psr\Log\LoggerInterface $logger */
private $logger;

public function __construct()
{
$this->alma = Mage::helper('alma/AlmaClient')->getDefaultClient();
$this->logger = Mage::helper('alma/Logger')->getLogger();
}

/**
* @param bool $force
* @return \Alma\API\Entities\Merchant
*/
public function getMerchant($force = false)
{
if (!$this->merchant || $force) {
try {
$this->merchant = $this->alma->merchants->me();
} catch (\Exception $e) {
$this->logger->warning('Could not fetch merchant information for PNX min/max amounts');
}
}

return $this->merchant;
}
}
13 changes: 12 additions & 1 deletion app/code/community/Alma/Installments/Model/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Alma_Installments_Model_PaymentMethod extends Mage_Payment_Model_Method_Ab
protected $_canManageRecurringProfiles = false;
protected $_isInitializeNeeded = true;

protected $_formBlockType = 'alma/PaymentForm';

/** @var AlmaLogger */
private $logger;
/** @var \Alma\API\Client */
Expand All @@ -42,6 +44,12 @@ public function __construct()
$this->alma = Mage::helper('alma/AlmaClient')->getDefaultClient();
}

public function assignData($data)
{
$this->getInfoInstance()->setAdditionalInformation('installments_count', $data->getData('installments_count'));
return parent::assignData($data);
}

public function canUseForCurrency($currencyCode)
{
return $currencyCode == 'EUR';
Expand Down Expand Up @@ -72,6 +80,7 @@ public function initialize($paymentAction, $stateObject)
$data = array(
"payment" => array(
"return_url" => Mage::getUrl('alma/payment/return'),
"installments_count" => (int)$payment->getAdditionalInformation('installments_count'),
"ipn_callback_url" => Mage::getUrl('alma/payment/ipn'),
"customer_cancel_url" => Mage::getUrl('alma/payment/cancel'),
"purchase_amount" => Alma_Installments_Helper_Functions::priceToCents((float)$order->getTotalDue()),
Expand All @@ -96,7 +105,7 @@ public function initialize($paymentAction, $stateObject)
);

try {
$almaPayment = $this->alma->payments->createPayment($data);
$almaPayment = $this->alma->payments->create($data);
} catch (\Alma\API\RequestError $e) {
$this->logger->error("Error creating payment: {$e->getMessage()}");
$this->_cancelOrder();
Expand Down Expand Up @@ -143,6 +152,8 @@ public function isAvailable($quote = null)
$available = Mage::helper('alma/availability')->isAvailable();
$eligible = Mage::helper('alma/eligibility')->checkEligibility();

$this->toto = 'yay';

$isAvailable = $available && $eligible;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* 2018-2019 Alma SAS
*
* THE MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @author Alma SAS <[email protected]>
* @copyright 2018-2019 Alma SAS
* @license https://opensource.org/licenses/MIT The MIT License
*/

class Alma_Installments_Model_System_Config_Backend_PnXAmountBoundary extends Mage_Core_Model_Config_Data
{
protected $boundary = null;

public function _afterLoad()
{
/** @var \Alma\API\Entities\Merchant $merchant */
$merchant = Mage::helper('alma/Data')->getMerchant();

$defaults = array(
"min" => $merchant ? $merchant->minimum_purchase_amount : 10000,
"max" => $merchant ? $merchant->maximum_purchase_amount : 100000
);
$value = $this->getValue();

if (empty($value)) {
$value = $defaults[$this->boundary];
}

$this->setValue(Alma_Installments_Helper_Functions::priceFromCents($value));
}

public function _beforeSave()
{
$this->setValue(Alma_Installments_Helper_Functions::priceToCents($this->getValue()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* 2018-2019 Alma SAS
*
* THE MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @author Alma SAS <[email protected]>
* @copyright 2018-2019 Alma SAS
* @license https://opensource.org/licenses/MIT The MIT License
*/

class Alma_Installments_Model_System_Config_Backend_PnXMaxAmount extends Alma_Installments_Model_System_Config_Backend_PnXAmountBoundary
{
protected $boundary = 'max';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* 2018-2019 Alma SAS
*
* THE MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @author Alma SAS <[email protected]>
* @copyright 2018-2019 Alma SAS
* @license https://opensource.org/licenses/MIT The MIT License
*/

class Alma_Installments_Model_System_Config_Backend_PnXMinAmount extends Alma_Installments_Model_System_Config_Backend_PnXAmountBoundary
{
protected $boundary = 'min';
}
6 changes: 5 additions & 1 deletion app/code/community/Alma/Installments/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<config>
<modules>
<Alma_Installments>
<version>1.0.1</version>
<version>1.1.0</version>
</Alma_Installments>
</modules>
<default>
Expand Down Expand Up @@ -58,6 +58,10 @@

<title>Monthly Payments with Alma</title>
<description>Pay in 3 monthly payments with your credit card.</description>

<p2x_enabled>0</p2x_enabled>
<p3x_enabled>1</p3x_enabled>
<p4x_enabled>0</p4x_enabled>
</alma_installments>
</payment>
</default>
Expand Down
Loading

0 comments on commit 957fdc9

Please sign in to comment.