Skip to content

Commit

Permalink
Merge pull request #23 from pagantis/INT-539
Browse files Browse the repository at this point in the history
Int 539
  • Loading branch information
pgarcess authored Aug 9, 2019
2 parents e006209 + cdb0508 commit 73f0502
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 42 deletions.
124 changes: 103 additions & 21 deletions WC_Pagantis.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Pagantis
* Plugin URI: http://www.pagantis.com/
* Description: Financiar con Pagantis
* Version: 8.1.3
* Version: 8.2.0
* Author: Pagantis
*/

Expand Down Expand Up @@ -33,20 +33,21 @@ class WcPagantis
const ORDERS_TABLE = 'posts';

public $defaultConfigs = array('PAGANTIS_TITLE'=>'Instant Financing',
'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'pgSDK.simulator.types.SIMPLE',
'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'pgSDK.simulator.skins.BLUE',
'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons',
'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3,
'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12,
'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default',
'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'pgSDK.simulator.positions.INNER',
'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'a:3:{i:0;s:48:"div.summary *:not(del)>.woocommerce-Price-amount";i:1;s:54:"div.entry-summary *:not(del)>.woocommerce-Price-amount";i:2;s:36:"*:not(del)>.woocommerce-Price-amount";}',
'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'a:2:{i:0;s:22:"div.quantity input.qty";i:1;s:18:"div.quantity>input";}',
'PAGANTIS_FORM_DISPLAY_TYPE'=>0,
'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1,
'PAGANTIS_URL_OK'=>'',
'PAGANTIS_URL_KO'=>'',
'PAGANTIS_ALLOWED_COUNTRIES' => 'a:2:{i:0;s:2:"es";i:1;s:2:"it";}'
'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'pgSDK.simulator.types.SIMPLE',
'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'pgSDK.simulator.skins.BLUE',
'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons',
'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3,
'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12,
'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default',
'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'pgSDK.simulator.positions.INNER',
'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'a:3:{i:0;s:48:"div.summary *:not(del)>.woocommerce-Price-amount";i:1;s:54:"div.entry-summary *:not(del)>.woocommerce-Price-amount";i:2;s:36:"*:not(del)>.woocommerce-Price-amount";}',
'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'a:2:{i:0;s:22:"div.quantity input.qty";i:1;s:18:"div.quantity>input";}',
'PAGANTIS_FORM_DISPLAY_TYPE'=>0,
'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1,
'PAGANTIS_URL_OK'=>'',
'PAGANTIS_URL_KO'=>'',
'PAGANTIS_ALLOWED_COUNTRIES' => 'a:2:{i:0;s:2:"es";i:1;s:2:"it";}',
'PAGANTIS_PROMOTION_EXTRA' => '<p>Finance this product <span class="pmt-no-interest">without interest!</span></p>'
);

/** @var Array $extraConfig */
Expand All @@ -73,6 +74,73 @@ public function __construct()
add_action('rest_api_init', array($this, 'pagantisRegisterEndpoint')); //Endpoint
add_filter('load_textdomain_mofile', array($this, 'loadPagantisTranslation'), 10, 2);
register_activation_hook(__FILE__, array($this, 'pagantisActivation'));
add_action('woocommerce_product_options_general_product_data', array($this, 'pagantisPromotedProductTpl'));
add_action('woocommerce_process_product_meta', array($this, 'pagantisPromotedVarSave'));
add_action('woocommerce_product_bulk_edit_start', array($this,'pagantisPromotedBulkTemplate'));
add_action('woocommerce_product_bulk_edit_save', array($this,'pagantisPromotedBulkTemplateSave'));
}

/**
* Piece of html code to insert into BULK admin edit
*/
public function pagantisPromotedBulkTemplate()
{
echo '<div class="inline-edit-group">
<label class="alignleft">
<span class="title">Pagantis promoted</span>
<span class="input-text-wrap">
<input type="checkbox" id="pagantis_promoted" name="pagantis_promoted"/>
</span>
</label>
</div>';
}

/**
* Php code to save our meta after a bulk admin edit
* @param $product
*/
public function pagantisPromotedBulkTemplateSave($product)
{
$post_id = $product->get_id();
$pagantis_promoted_value = $_REQUEST['pagantis_promoted'];
if ($pagantis_promoted_value == 'on') {
$pagantis_promoted_value = 'yes';
} else {
$pagantis_promoted_value = 'no';
}

update_post_meta($post_id, 'custom_product_pagantis_promoted', esc_attr($pagantis_promoted_value));
}

/**
* Piece of html code to insert into PRODUCT admin edit
*/
public function pagantisPromotedProductTpl()
{
global $post;
$_product = get_post_meta($post->ID);
woocommerce_wp_checkbox(
array(
'id' => 'pagantis_promoted',
'label' => __('Pagantis promoted', 'woocommerce'),
'value' => $_product['custom_product_pagantis_promoted']['0'],
'cbvalue' => 'yes',
'echo' => true
)
);
}

/**
* Php code to save our meta after a PRODUCT admin edit
* @param $post_id
*/
public function pagantisPromotedVarSave($post_id)
{
$pagantis_promoted_value = $_POST['pagantis_promoted'];
if ($pagantis_promoted_value == null) {
$pagantis_promoted_value = 'no';
}
update_post_meta($post_id, 'custom_product_pagantis_promoted', esc_attr($pagantis_promoted_value));
}

/*
Expand Down Expand Up @@ -205,6 +273,7 @@ public function pagantisAddProductSimulator()
return;
}

$post_id = $product->get_id();
$template_fields = array(
'total' => is_numeric($product->price) ? $product->price : 0,
'public_key' => $cfg['pagantis_public_key'],
Expand All @@ -213,7 +282,9 @@ public function pagantisAddProductSimulator()
'quantitySelector' => unserialize($this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR']),
'priceSelector' => unserialize($this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR']),
'totalAmount' => is_numeric($product->price) ? $product->price : 0,
'locale' => $locale
'locale' => $locale,
'promoted' => $this->isPromoted($post_id),
'promotedMessage' => $this->extraConfig['PAGANTIS_PROMOTION_EXTRA']
);
wc_get_template('product_simulator.php', $template_fields, '', $this->template_path);
}
Expand Down Expand Up @@ -285,7 +356,7 @@ public function pagantisRowMeta($links, $file)
if ($file == plugin_basename(__FILE__)) {
$links[] = '<a href="'.WcPagantis::GIT_HUB_URL.'" target="_blank">'.__('Documentation', 'pagantis').'</a>';
$links[] = '<a href="'.WcPagantis::PAGANTIS_DOC_URL.'" target="_blank">'.
__('API documentation', 'pagantis').'</a>';
__('API documentation', 'pagantis').'</a>';
$links[] = '<a href="'.WcPagantis::SUPPORT_EML.'">'.__('Support', 'pagantis').'</a>';

return $links;
Expand Down Expand Up @@ -430,10 +501,10 @@ public function pagantisRegisterEndpoint()
'pagantis/v1',
'/logs/(?P<secret>\w+)/(?P<from>\d+)/(?P<to>\d+)',
array(
'methods' => 'GET',
'callback' => array(
$this,
'readLogs')
'methods' => 'GET',
'callback' => array(
$this,
'readLogs')
),
true
);
Expand Down Expand Up @@ -510,6 +581,17 @@ private function preparePriceSelector($css_price_selector)

return $css_price_selector;
}

/**
* @param $product_id
*
* @return string
*/
private function isPromoted($product_id)
{
$metaProduct = get_post_meta($product_id);
return ($metaProduct['custom_product_pagantis_promoted']['0'] === 'yes') ? 'true' : 'false';
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions controllers/notifyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,18 @@ private function saveOrder()
global $woocommerce;
$paymentResult = $this->woocommerceOrder->payment_complete();
if ($paymentResult) {
$metadataOrder = $this->pagantisOrder->getMetadata();
$metadataInfo = null;
foreach ($metadataOrder as $metadataKey => $metadataValue) {
if ($metadataKey == 'promotedProduct') {
$metadataInfo.= "/Producto promocionado = $metadataValue";
}
}

if ($metadataInfo != null) {
$this->woocommerceOrder->add_order_note($metadataInfo);
}

$this->woocommerceOrder->add_order_note("Notification received via $this->origin");
$this->woocommerceOrder->reduce_order_stock();
$this->woocommerceOrder->save();
Expand Down
70 changes: 58 additions & 12 deletions controllers/paymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Pagantis\OrdersApiClient\Model\Order\Configuration;
use Pagantis\OrdersApiClient\Client;
use Pagantis\OrdersApiClient\Model\Order;
use Pagantis\ModuleUtils\Model\Log\LogEntry;

if (!defined('ABSPATH')) {
exit;
Expand Down Expand Up @@ -243,10 +244,21 @@ public function pagantisReceiptPage($order_id)
$orderUser->addOrderHistory($orderHistory);
}

$metadataOrder = new Metadata();
$metadata = array(
'woocommerce' => WC()->version,
'pagantis' => $this->plugin_info['Version'],
'php' => phpversion()
);
foreach ($metadata as $key => $metadatum) {
$metadataOrder->addMetadata($key, $metadatum);
}

$details = new Details();
$shippingCost = $order->shipping_total;
$details->setShippingCost(intval(strval(100 * $shippingCost)));
$items = $woocommerce->cart->get_cart();
$promotedAmount = 0;
foreach ($items as $key => $item) {
$product = new Product();
$productDescription = sprintf(
Expand All @@ -258,15 +270,26 @@ public function pagantisReceiptPage($order_id)
$product
->setAmount(intval(100 * $item['line_total']))
->setQuantity($item['quantity'])
->setDescription($productDescription);
->setDescription($productDescription)
;
$details->addProduct($product);

$promotedProduct = $this->isPromoted($item['product_id']);
if ($promotedProduct == 'true') {
$promotedAmount+=$product->getAmount();
$promotedMessage = 'Promoted Item: ' . $product->getDescription() .
' Price: ' . $item['line_total'] .
' Qty: ' . $product->getQuantity() .
' Item ID: ' . $item['id_product'];
$metadataOrder->addMetadata('promotedProduct', $promotedMessage);
}
}

$orderShoppingCart = new ShoppingCart();
$orderShoppingCart
->setDetails($details)
->setOrderReference($order->get_id())
->setPromotedAmount(0)
->setPromotedAmount($promotedAmount)
->setTotalAmount(intval(strval(100 * $order->total)))
;
$orderConfigurationUrls = new Urls();
Expand Down Expand Up @@ -295,15 +318,7 @@ public function pagantisReceiptPage($order_id)
->setUrls($orderConfigurationUrls)
->setPurchaseCountry($this->language)
;
$metadataOrder = new Metadata();
$metadata = array(
'woocommerce' => WC()->version,
'pagantis' => $this->plugin_info['Version'],
'php' => phpversion()
);
foreach ($metadata as $key => $metadatum) {
$metadataOrder->addMetadata($key, $metadatum);
}

$orderApiClient = new Order();
$orderApiClient
->setConfiguration($orderConfiguration)
Expand Down Expand Up @@ -467,6 +482,7 @@ public function payment_fields()
$locale = strtolower(strstr(get_locale(), '_', true));
$allowedCountries = unserialize($this->extraConfig['PAGANTIS_ALLOWED_COUNTRIES']);
$allowedCountry = (in_array(strtolower($locale), $allowedCountries));
$promotedAmount = $this->getPromotedAmount();

$template_fields = array(
'public_key' => $this->pagantis_public_key,
Expand All @@ -476,7 +492,8 @@ public function payment_fields()
'simulator_enabled' => $this->settings['pagantis_simulator'],
'locale' => $locale,
'allowedCountry' => $allowedCountry,
'simulator_type' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE']
'simulator_type' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'],
'promoted_amount' => $promotedAmount
);
wc_get_template('checkout_description.php', $template_fields, '', $this->template_path);
}
Expand Down Expand Up @@ -811,4 +828,33 @@ private function checkDbLogTable()
}
return;
}

/**
* @param $product_id
*
* @return string
*/
private function isPromoted($product_id)
{
$metaProduct = get_post_meta($product_id);
return ($metaProduct['custom_product_pagantis_promoted']['0'] === 'yes') ? 'true' : 'false';
}

/**
* @return int
*/
private function getPromotedAmount()
{
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$promotedAmount = 0;
foreach ($items as $key => $item) {
$promotedProduct = $this->isPromoted($item['product_id']);
if ($promotedProduct == 'true') {
$promotedAmount+=$item['line_total'];
}
}

return $promotedAmount;
}
}
4 changes: 4 additions & 0 deletions generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ then
vendor/bin/phpunit --group woocommerce3-buy
fi
fi

containerPort=$(docker container port ${container})
PORT=$(sed -e 's/.*://' <<< $containerPort)
echo 'Build of Woocommerce complete: http://'${container}'.docker:'${PORT}
1 change: 1 addition & 0 deletions templates/checkout_description.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function loadSimulator()
publicKey: '<?php echo $public_key; ?>',
selector: '.pagantisSimulator',
totalAmount: '<?php echo $total; ?>',
totalPromotedAmount: '<?php echo $promoted_amount; ?>',
locale: locale
});
return false;
Expand Down
34 changes: 25 additions & 9 deletions templates/product_simulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,24 @@ function loadSimulatorPagantis()
}

var priceSelector = findPriceSelector();

var promotedProduct = '<?php echo $promoted;?>';
var quantitySelector = findQuantitySelector();

simulator_options = {
publicKey: '<?php echo $public_key; ?>',
type: <?php echo $simulator_type; ?>,
selector: positionSelector,
itemQuantitySelector: quantitySelector,
locale: locale,
itemAmountSelector: priceSelector
};

if (promotedProduct == 'true') {
simulator_options.itemPromotedAmountSelector = priceSelector;
}

if (typeof sdk != 'undefined') {
window.WCSimulatorId = sdk.simulator.init({
publicKey: '<?php echo $public_key; ?>',
type: <?php echo $simulator_type; ?>,
selector: positionSelector,
itemQuantitySelector: quantitySelector,
itemAmountSelector: priceSelector,
locale: locale
});
window.WCSimulatorId = sdk.simulator.init(simulator_options);
return false;
}
}
Expand All @@ -87,4 +93,14 @@ function loadSimulatorPagantis()
loadSimulatorPagantis();
}, 2000);
</script>
<style>
.pmt-no-interest{
color: #00c1d5
}
</style>
<?php
if ($promoted == 'true') {
echo $promotedMessage;
}
?>
<div class="pagantisSimulator"></div>

0 comments on commit 73f0502

Please sign in to comment.