Skip to content

Commit

Permalink
Pay in 15 days
Browse files Browse the repository at this point in the history
Add new payment method: Pay in 15 days
  • Loading branch information
alexhernandezord committed Oct 24, 2019
1 parent 94fdd02 commit 11bf7ae
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 78 deletions.
217 changes: 177 additions & 40 deletions aplazame/aplazame.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public function __construct()
$this->name = 'aplazame';
$this->tab = 'payments_gateways';
$this->version = '5.4.8';
$this->author = 'Aplazame';
$this->author = 'Aplazame SL';
$this->author_uri = 'https://aplazame.com';
$this->module_key = '64b13ea3527b4df3fe2e3fc1526ce515';

parent::__construct();

$this->displayName = $this->l('Aplazame: buy now, pay later');
$this->displayName = $this->l('Aplazame');
$this->description = $this->l('Boost sales by 50% with Aplazame, a risk free payment method that offers instant credit for online purchases.');
$this->confirmUninstall = $this->l('Are you sure about removing these details?');

Expand Down Expand Up @@ -90,14 +90,23 @@ public function install()
Configuration::updateValue('APLAZAME_WIDGET_PROD', '0');

Configuration::updateValue('APLAZAME_CART_WIDGET_ENABLED', true);
Configuration::updateValue('APLAZAME_BUTTON_IMAGE', 'https://aplazame.com/static/img/buttons/white-148x46.png');
if (_PS_VERSION_ >= 1.7) {
Configuration::updateValue('APLAZAME_BUTTON', "div.payment-option:has(input[data-module-name='{$this->name}'])");
Configuration::updateValue('APLAZAME_BUTTON_IMAGE', '');
Configuration::updateValue('APLAZAME_BUTTON_IMAGE_PAY_LATER', '');
Configuration::updateValue('APLAZAME_BUTTON_INSTALMENTS', "div.payment-option:has(input[data-module-name='{$this->name}'])");
Configuration::updateValue('APLAZAME_BUTTON_PAY_LATER', "div.payment-option:has(input[data-module-name='{$this->name}_pay_later'])");
} else {
Configuration::updateValue('APLAZAME_BUTTON', '#aplazame_payment_button');
Configuration::updateValue('APLAZAME_BUTTON_IMAGE', 'https://aplazame.com/static/img/buttons/white-148x46.png');
Configuration::updateValue('APLAZAME_BUTTON_IMAGE_PAY_LATER', 'https://aplazame.com/static/img/buttons/pay-later-227x46.png');
Configuration::updateValue('APLAZAME_BUTTON_INSTALMENTS', '#aplazame_payment_button');
Configuration::updateValue('APLAZAME_BUTTON_PAY_LATER', '#aplazame_pay_later_payment_button');
}

Configuration::updateValue('APLAZAME_PAYMENT_INSTALMENTS', true);
Configuration::updateValue('APLAZAME_PAYMENT_PAY_LATER', false);

return ($this->registerHook('actionOrderSlipAdd')
&& $this->registerHook('actionOrderStatusUpdate')
&& $this->registerHook('displayAdminProductsExtra')
&& $this->registerHook('displayHeader')
&& $this->registerHook('displayPayment')
Expand Down Expand Up @@ -204,11 +213,15 @@ public function getContent()
$settingsKeys = array(
'APLAZAME_SANDBOX',
'APLAZAME_SECRET_KEY',
'APLAZAME_PAYMENT_INSTALMENTS',
'APLAZAME_PAYMENT_PAY_LATER',
'APLAZAME_PRODUCT_WIDGET_ENABLED',
'APLAZAME_WIDGET_PROD',
'APLAZAME_CART_WIDGET_ENABLED',
'APLAZAME_BUTTON',
'APLAZAME_BUTTON_INSTALMENTS',
'APLAZAME_BUTTON_PAY_LATER',
'APLAZAME_BUTTON_IMAGE',
'APLAZAME_BUTTON_IMAGE_PAY_LATER',
);

if (Tools::isSubmit('submitAplazameModule')) {
Expand Down Expand Up @@ -341,6 +354,50 @@ protected function getConfigForm()
),
),
),
array(
'form' => array(
'legend' => array(
'title' => $this->l('Payment methods'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => (_PS_VERSION_ >= 1.6) ? 'switch' : 'radio',
'label' => $this->l('Flexible financing'),
'name' => 'APLAZAME_PAYMENT_INSTALMENTS',
'is_bool' => true,
'desc' => $this->l('Enable "Flexible financing" payment method'),
'values' => array(
array(
'id' => 'active_on',
'value' => true,
),
array(
'id' => 'active_off',
'value' => false,
),
),
),
array(
'type' => (_PS_VERSION_ >= 1.6) ? 'switch' : 'radio',
'label' => $this->l('Pay in 15 days'),
'name' => 'APLAZAME_PAYMENT_PAY_LATER',
'is_bool' => true,
'desc' => $this->l('Enable "Pay in 15 days" payment method'),
'values' => array(
array(
'id' => 'active_on',
'value' => true,
),
array(
'id' => 'active_off',
'value' => false,
),
),
),
),
),
),
array(
'form' => array(
'legend' => array(
Expand Down Expand Up @@ -418,17 +475,33 @@ protected function getConfigForm()
'col' => 4,
'type' => 'text',
'prefix' => '<i class="icon icon-code"></i>',
'desc' => $this->l('Aplazame Button CSS Selector'),
'name' => 'APLAZAME_BUTTON',
'label' => $this->l('Button'),
'desc' => $this->l('Aplazame "Flexible financing" Button CSS Selector'),
'name' => 'APLAZAME_BUTTON_INSTALMENTS',
'label' => $this->l('"Flexible financing" Button'),
),
array(
'col' => 4,
'type' => 'text',
'prefix' => '<i class="icon icon-code"></i>',
'desc' => $this->l('Aplazame Button Image that you want to show'),
'desc' => $this->l('Aplazame "Pay in 15 days" Button CSS Selector'),
'name' => 'APLAZAME_BUTTON_PAY_LATER',
'label' => $this->l('"Pay in 15 days" Button'),
),
array(
'col' => 4,
'type' => 'text',
'prefix' => '<i class="icon icon-code"></i>',
'desc' => $this->l('Aplazame "Flexible financing" Button Image that you want to show'),
'name' => 'APLAZAME_BUTTON_IMAGE',
'label' => $this->l('Button Image'),
'label' => $this->l('"Flexible financing" Button Image'),
),
array(
'col' => 4,
'type' => 'text',
'prefix' => '<i class="icon icon-code"></i>',
'desc' => $this->l('Aplazame "Pay in 15 days" Button Image that you want to show'),
'name' => 'APLAZAME_BUTTON_IMAGE_PAY_LATER',
'label' => $this->l('"Pay in 15 days" Button Image'),
),
),
'submit' => array(
Expand Down Expand Up @@ -470,6 +543,23 @@ public function hookActionOrderSlipAdd($params)
return $this->refundAmount($order, $lastOrderSlip->total_products_tax_incl + $lastOrderSlip->total_shipping_tax_incl);
}

public function hookActionOrderStatusUpdate($params)
{
$shipped = $params['newOrderStatus']->shipped;

if ($shipped) {

/** @var Order $order */
$order = new Order($params['id_order']);

if ($order->module == $this->name) {
return $this->captureOrder($order);
}
}

return false;
}

public function hookDisplayAdminProductsExtra($params)
{
if ($params && isset($params['id_product'])) {
Expand Down Expand Up @@ -508,11 +598,6 @@ public function hookDisplayHeader()
return $this->display(__FILE__, 'header.tpl');
}

public function hookDisplayPayment($params)
{
return $this->hookPayment($params);
}

public function hookDisplayProductButtons($params)
{
$displayWidget = (int) Configuration::get('APLAZAME_WIDGET_PROD');
Expand Down Expand Up @@ -548,7 +633,7 @@ public function hookDisplayRightColumn($params)

public function hookDisplayShoppingCart($params)
{
if (!$this->isAvailable() || !Configuration::get('APLAZAME_CART_WIDGET_ENABLED')) {
if (!$this->isAvailable() || !Configuration::get('APLAZAME_CART_WIDGET_ENABLED') || !Configuration::get('APLAZAME_PAYMENT_INSTALMENTS')) {
return false;
}

Expand All @@ -569,7 +654,7 @@ public function hookDisplayShoppingCart($params)
* This method is used to render the payment button,
* Take care if the button should be displayed or not.
*/
public function hookPayment($params)
public function hookDisplayPayment($params)
{
if (!$this->isAvailable()) {
return false;
Expand All @@ -578,8 +663,18 @@ public function hookPayment($params)
/** @var Cart $cart */
$cart = $params['cart'];

$this->context->smarty->assign($this->getButtonTemplateVars($cart));
$this->context->smarty->assign(array('aplazame_button_image_uri' => Configuration::get('APLAZAME_BUTTON_IMAGE')));
$this->context->smarty->assign(array(
'aplazame_instalments' => array(
'is_enabled' => Configuration::get('APLAZAME_PAYMENT_INSTALMENTS'),
'button_image' => Configuration::get('APLAZAME_BUTTON_IMAGE'),
'button' => $this->getButtonTemplateVars($cart, 'instalments'),
),
'aplazame_pay_later' => array(
'is_enabled' => Configuration::get('APLAZAME_PAYMENT_PAY_LATER'),
'button_image' => Configuration::get('APLAZAME_BUTTON_IMAGE_PAY_LATER'),
'button' => $this->getButtonTemplateVars($cart, 'pay_later'),
),
));

return $this->display(__FILE__, 'payment_1.5.tpl');
}
Expand All @@ -594,18 +689,34 @@ public function hookPaymentOptions($params)
$cart = $params['cart'];

$link = $this->context->link;
$this->context->smarty->assign($this->getButtonTemplateVars($cart));

$newOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption();
$newOption->setCallToActionText($this->l('Pay with Aplazame'))
->setModuleName($this->name)
->setAction($link->getModuleLink($this->name, 'redirect'))
->setAdditionalInformation($this->fetch('module:aplazame/views/templates/hook/payment_1.7.tpl'))
;
$options = array();

return array(
$newOption,
);
if (Configuration::get('APLAZAME_PAYMENT_INSTALMENTS')) {
$this->context->smarty->assign(array('aplazame_button' => $this->getButtonTemplateVars($cart, 'instalments')));
$newOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption();
$newOption->setCallToActionText($this->l('Aplazame - Flexible financing'))
->setModuleName($this->name)
->setAction($link->getModuleLink($this->name, 'redirect', array('type' => 'instalments')))
->setAdditionalInformation($this->fetch('module:aplazame/views/templates/hook/payment_1.7.tpl'))
->setLogo(Configuration::get('APLAZAME_BUTTON_IMAGE'))
;
$options[] = $newOption;
}

if (Configuration::get('APLAZAME_PAYMENT_PAY_LATER')) {
$this->context->smarty->assign(array('aplazame_button' => $this->getButtonTemplateVars($cart, 'pay_later')));
$newOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption();
$newOption->setCallToActionText($this->l('Aplazame - Pay in 15 days'))
->setModuleName($this->name . '_pay_later')
->setAction($link->getModuleLink($this->name, 'redirect', array('type' => 'pay_later')))
->setAdditionalInformation($this->fetch('module:aplazame/views/templates/hook/payment_1.7.tpl'))
->setLogo(Configuration::get('APLAZAME_BUTTON_IMAGE_PAY_LATER'))
;
$options[] = $newOption;
}

return $options;
}

public function hookPaymentReturn($params)
Expand Down Expand Up @@ -636,7 +747,7 @@ public function hookPaymentReturn($params)

public function getWidget($params)
{
if (!$this->isAvailable() || !Configuration::get('APLAZAME_PRODUCT_WIDGET_ENABLED')) {
if (!$this->isAvailable() || !Configuration::get('APLAZAME_PRODUCT_WIDGET_ENABLED') || !Configuration::get('APLAZAME_PAYMENT_INSTALMENTS')) {
return false;
}

Expand Down Expand Up @@ -707,7 +818,7 @@ public function doRefund(Order $order, $amount)
{
$refund = function ($mid) use ($amount) {
$decimal = Aplazame_Sdk_Serializer_Decimal::fromFloat($amount)->value;
$this->callToRest('POST', '/orders/' . $mid . '/refund', array('amount' => $decimal));
$this->callToRest('POST', '/orders/' . $mid . '/refund-extended', array('amount' => $decimal));
};

try {
Expand All @@ -730,14 +841,41 @@ public function refundAmount(Order $order, $amount)
return $order->addOrderPayment(-$amount, $this->displayName);
}

public function captureOrder(Order $order)
{
$reference = $order->reference;

try {
$payload = $this->callToRest('GET', '/orders/' . $reference . '/captures');
} catch (Exception $e) {
$this->log(self::LOG_CRITICAL, 'Cannot retrieve capture. Detail ' . $e->getMessage(), $reference);

return false;
}

if ($payload['remaining_capture_amount'] != 0) {
try {
$response = $this->callToRest('POST', '/orders/' . $reference . '/captures', array('amount' => $payload['remaining_capture_amount']));
} catch (Exception $e) {
$this->log(self::LOG_CRITICAL, 'Cannot do capture. Detail ' . $e->getMessage(), $reference);

return false;
}

return $response;
}

return false;
}

/**
* @return array
*
* @throws Exception
*/
public function createCheckoutOnAplazame(Cart $cart)
public function createCheckoutOnAplazame(Cart $cart, $type)
{
$checkout = Aplazame_Aplazame_BusinessModel_Checkout::createFromCart($cart, (int) $this->id, $this->currentOrder);
$checkout = Aplazame_Aplazame_BusinessModel_Checkout::createFromCart($cart, (int) $this->id, $this->currentOrder, $type);

$response = $this->callToRest('POST', '/checkout', Aplazame_Sdk_Serializer_JsonSerializer::serializeValue($checkout));

Expand All @@ -763,16 +901,15 @@ private function registerController($className, $name)
return (boolean) $tab->add();
}

private function getButtonTemplateVars(Cart $cart)
private function getButtonTemplateVars(Cart $cart, $type)
{
$currency = new Currency((int) ($cart->id_currency));

return array(
'aplazame_button' => array(
'selector' => Configuration::get('APLAZAME_BUTTON'),
'currency' => $currency->iso_code,
'amount' => Aplazame_Sdk_Serializer_Decimal::fromFloat($cart->getOrderTotal())->value,
),
'selector' => Configuration::get('APLAZAME_BUTTON_' . strtoupper($type)),
'currency' => $currency->iso_code,
'amount' => Aplazame_Sdk_Serializer_Decimal::fromFloat($cart->getOrderTotal())->value,
'product' => array('type' => $type),
);
}

Expand Down
2 changes: 1 addition & 1 deletion aplazame/controllers/front/redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function postProcess()
}

try {
$payload = $this->module->createCheckoutOnAplazame($cart);
$payload = $this->module->createCheckoutOnAplazame($cart, Tools::getValue('type'));
} catch (Aplazame_Sdk_Api_ApiClientException $e) {
$this->errors[] = 'Aplazame Error: ' . $e->getMessage();

Expand Down
4 changes: 3 additions & 1 deletion aplazame/lib/Aplazame/Aplazame/BusinessModel/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Aplazame_Aplazame_BusinessModel_Checkout
{
public static function createFromCart(Cart $cart, $moduleId, $orderId)
public static function createFromCart(Cart $cart, $moduleId, $orderId, $type)
{
/** @var Aplazame $aplazame */
$aplazame = ModuleCore::getInstanceByName('aplazame');
Expand Down Expand Up @@ -52,6 +52,8 @@ public static function createFromCart(Cart $cart, $moduleId, $orderId)
'version' => _PS_VERSION_,
);

$checkout->product = array('type' => $type);

return $checkout;
}
}
Loading

0 comments on commit 11bf7ae

Please sign in to comment.