Skip to content

Commit

Permalink
Now calculating the tax class for products
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Pieters committed Nov 24, 2016
1 parent 32c95c2 commit 23ddcfd
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/addons/paynl_addon/addon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<id>paynl_addon</id>
<name>pay.nl</name>
<description>Pay.nl Gateway addon</description>
<version>1.0.7</version>
<version>1.0.8</version>
<priority>1000</priority>
<settings layout="separate">
<sections>
Expand Down
129 changes: 95 additions & 34 deletions app/addons/paynl_addon/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,32 @@

require_once(dirname(__FILE__) . '/paynl/classes/Autoload.php');

function fn_getCredential($var) {
function fn_getCredential($var)
{
$paynl_setting = Registry::get('addons.paynl_addon');
return array('token_api' => $paynl_setting['token_api'],
'service_id' => $paynl_setting['service_id']);
}

function fn_get_ideal_banks($processor_data) {

function fn_get_ideal_banks($processor_data)
{

$service = new Pay_Api_Getservice();
$service->setApiToken($processor_data['processor_params']['token_api']);
$service->setServiceId($processor_data['processor_params']['service_id']);
try {
$result = $service->doRequest();
$banks = $result['paymentOptions'][$processor_data['processor_params']['optionId']]['paymentOptionSubList'];

return $banks;
} catch (Exception $ex) {
fn_set_notification('E', __('error'), $ex->getMessage());

}
}

function fn_getPaynlOptionId($var) {
function fn_getPaynlOptionId($var)
{
$paynl_setting = Registry::get('addons.paynl_addon');
$service = new Pay_Api_Getservice();
$service->setApiToken($paynl_setting['token_api']);
Expand All @@ -40,7 +43,9 @@ function fn_getPaynlOptionId($var) {

return $result['paymentOptions'];
}
function fn_paynl_getInfo($payNLTransactionID, $processor_data){

function fn_paynl_getInfo($payNLTransactionID, $processor_data)
{
$payApiInfo = new Pay_Api_Info();
$payApiInfo->setApiToken($processor_data['processor_params']['token_api']);
$payApiInfo->setServiceId($processor_data['processor_params']['service_id']);
Expand All @@ -53,7 +58,9 @@ function fn_paynl_getInfo($payNLTransactionID, $processor_data){
}
return $result;
}
function fn_paynl_getState($payNLTransactionID, $processor_data) {

function fn_paynl_getState($payNLTransactionID, $processor_data)
{
$payApiInfo = new Pay_Api_Info();
$payApiInfo->setApiToken($processor_data['processor_params']['token_api']);
$payApiInfo->setServiceId($processor_data['processor_params']['service_id']);
Expand All @@ -68,50 +75,57 @@ function fn_paynl_getState($payNLTransactionID, $processor_data) {
return $state;
}

function fn_paynl_startTransaction($order_id, $order_info, $processor_data, $exchangeUrl, $finishUrl, $paymentOptionSubId = null) {
function fn_paynl_startTransaction($order_id, $order_info, $processor_data, $exchangeUrl, $finishUrl, $paymentOptionSubId = null)
{
$paynl_setting = Registry::get('addons.paynl_addon');
$currency = CART_PRIMARY_CURRENCY;
$payNL = new Pay_Api_Start();
$payNL->setApiToken($processor_data['processor_params']['token_api']);
$payNL->setServiceId($processor_data['processor_params']['service_id']);
$payNL->setAmount(floatval($order_info['total']) * 100);
$payNL->setPaymentOptionId($processor_data['processor_params']['optionId']);
if(!empty($paymentOptionSubId)){

if (!empty($paymentOptionSubId)) {
$payNL->setPaymentOptionSubId($paymentOptionSubId);
}

$payNL->setExchangeUrl($exchangeUrl);
$payNL->setCurrency($currency);
$payNL->setFinishUrl($finishUrl);
$payNL->setDescription($order_info['order_id']);

$s_address = splitAddress(trim($order_info['s_address'] . ' ' . $order_info['s_address_2']));
$b_address = splitAddress(trim($order_info['b_address'] . ' ' . $order_info['b_address_2']));
$payNL->setEnduser(array('accessCode' => $order_info['user_id'],
'language' => $order_info['lang_code'],
'initials' => $order_info['s_firstname'],
'lastName' => $order_info['s_lastname'],
'phoneNumber' => $order_info['s_phone'],
'dob' => $order_info['birthday'],
'emailAddress' => $order_info['email'],
'address' => array('streetName' => $s_address[0],
'streetNumber' => substr($s_address[1], 0, 4),
'zipCode' => $order_info['s_zipcode'],
'city' => $order_info['s_city'],
'countryCode' => $order_info['s_country']),
'invoiceAddress' => array('initials' => $order_info['b_firstname'],
'lastname' => $order_info['b_lastname'],
'streetName' => $b_address[0],
'streetNumber' => substr($b_address[1], 0, 4),
'zipCode' => $order_info['b_zipcode'],
'city' => $order_info['b_city'],
'countryCode' => $order_info['b_country']))
'language' => $order_info['lang_code'],
'initials' => $order_info['s_firstname'],
'lastName' => $order_info['s_lastname'],
'phoneNumber' => $order_info['s_phone'],
'dob' => $order_info['birthday'],
'emailAddress' => $order_info['email'],
'address' => array('streetName' => $s_address[0],
'streetNumber' => substr($s_address[1], 0, 4),
'zipCode' => $order_info['s_zipcode'],
'city' => $order_info['s_city'],
'countryCode' => $order_info['s_country']),
'invoiceAddress' => array('initials' => $order_info['b_firstname'],
'lastname' => $order_info['b_lastname'],
'streetName' => $b_address[0],
'streetNumber' => substr($b_address[1], 0, 4),
'zipCode' => $order_info['b_zipcode'],
'city' => $order_info['b_city'],
'countryCode' => $order_info['b_country']))
);
$payNL->setExtra1($order_id);

foreach ($order_info['products'] as $key => $product) {
$payNL->addProduct($product['product_id'], $product['product'], floatval($product['price']) * 100, $product['amount'], 'H');
$payNL->addProduct(
$product['product_id'],
$product['product'],
floatval($product['price']) * 100,
$product['amount'],
paynl_getTaxClassForItem($order_info, $key)
);
}

if (isset($order_info['subtotal_discount']) && $order_info['subtotal_discount'] > 0)
Expand Down Expand Up @@ -151,7 +165,8 @@ function fn_paynl_startTransaction($order_id, $order_info, $processor_data, $exc
}
}

function splitAddress($strAddress) {
function splitAddress($strAddress)
{
$strAddress = trim($strAddress);
$a = preg_split('/([0-9]+)/', $strAddress, 2, PREG_SPLIT_DELIM_CAPTURE);
$strStreetName = trim(array_shift($a));
Expand All @@ -167,4 +182,50 @@ function splitAddress($strAddress) {
return array($strStreetName, $strStreetNumber);
}

?>
function paynl_getTaxClassForItem($order_info, $item_id)
{
foreach ($order_info['taxes'] as $tax_rule) {
if (
array_key_exists($item_id, $tax_rule['applies']['items']['P']) &&
$tax_rule['applies']['items']['P'][$item_id] === true
) {
$percentage = floatval($tax_rule['rate_value']);
return paynl_getTaxClass($percentage);
}
return null;
}
}

function paynl_getTaxClass($percentage)
{
$taxClasses = array(
0 => 'N',
6 => 'L',
21 => 'H'
);
$nearestTaxRate = paynl_nearest($percentage, array_keys($taxClasses));
return ($taxClasses[$nearestTaxRate]);
}

/**
* Get the nearest number
*
* @param int $number
* @param array $numbers
* @return int|bool nearest number false on error
*/
function paynl_nearest($number, $numbers)
{
$output = FALSE;
$number = intval($number);
if (is_array($numbers) && count($numbers) >= 1) {
$NDat = array();
foreach ($numbers as $n) {
$NDat[abs($number - $n)] = $n;
}
ksort($NDat);
$NDat = array_values($NDat);
$output = $NDat[0];
}
return $output;
}
Binary file removed cscart-pluginv1.0.7.zip
Binary file not shown.

0 comments on commit 23ddcfd

Please sign in to comment.