diff --git a/README.md b/README.md index bfbb413..df6841c 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ This repository contains the OpenCart PostFinance Checkout payment module that ## Documentation -* [English](https://plugin-documentation.postfinance-checkout.ch/pfpayments/opencart-2.3/1.0.24/docs/en/documentation.html) +* [English](https://plugin-documentation.postfinance-checkout.ch/pfpayments/opencart-2.3/1.0.26/docs/en/documentation.html) ## License -Please see the [license file](https://github.com/pfpayments/opencart-2.3/blob/1.0.24/LICENSE) for more information. \ No newline at end of file +Please see the [license file](https://github.com/pfpayments/opencart-2.3/blob/1.0.26/LICENSE) for more information. \ No newline at end of file diff --git a/docs/en/documentation.html b/docs/en/documentation.html index e87af06..4b474ef 100644 --- a/docs/en/documentation.html +++ b/docs/en/documentation.html @@ -21,7 +21,7 @@

Documentation

  • - + Source
  • @@ -48,7 +48,7 @@

    1. -

      Download the extension.

      +

      Download the extension.

    2. Extract the files and upload the content of the Upload directory into the root directory of your store using FTP/SSH.

      diff --git a/upload/admin/controller/extension/payment/postfinancecheckout.php b/upload/admin/controller/extension/payment/postfinancecheckout.php index 33a0373..6635b29 100644 --- a/upload/admin/controller/extension/payment/postfinancecheckout.php +++ b/upload/admin/controller/extension/payment/postfinancecheckout.php @@ -125,6 +125,8 @@ private function persistStoreSettings(array $global, array $store){ $newSettings['postfinancecheckout_download_invoice'] = isset($store['postfinancecheckout_download_invoice']); $newSettings['postfinancecheckout_download_packaging'] = isset($store['postfinancecheckout_download_packaging']); + $newSettings['postfinancecheckout_rounding_adjustment'] = isset($store['postfinancecheckout_rounding_adjustment']); + PostFinanceCheckoutVersionHelper::persistPluginStatus($this->registry, $newSettings); $this->model_setting_setting->editSetting('postfinancecheckout', $newSettings, $store['id']); @@ -240,6 +242,10 @@ private function getSettingsPageTranslatedVariables(){ $data['help_log_level'] = $this->language->get('help_log_level'); $data['log_levels'] = $this->getLogLevels(); + $data['title_rounding_adjustment'] = $this->language->get('title_rounding_adjustment'); + $data['entry_rounding_adjustment'] = $this->language->get('entry_rounding_adjustment'); + $data['description_rounding_adjustment'] = $this->language->get('description_rounding_adjustment'); + $data['entry_email'] = $this->language->get("entry_email"); $data['description_email'] = $this->language->get("description_email"); $data['entry_alerts'] = $this->language->get("entry_alerts"); @@ -431,6 +437,8 @@ private function getSettingsDefaults(){ "postfinancecheckout_notification_url" => null, + "postfinancecheckout_rounding_adjustment" => 0, + "postfinancecheckout_download_packaging" => 1, "postfinancecheckout_download_invoice" => 1, \PostFinanceCheckout\Service\ManualTask::CONFIG_KEY => 0 diff --git a/upload/admin/language/en-gb/extension/payment/postfinancecheckout.php b/upload/admin/language/en-gb/extension/payment/postfinancecheckout.php index c6ad75c..7d42318 100644 --- a/upload/admin/language/en-gb/extension/payment/postfinancecheckout.php +++ b/upload/admin/language/en-gb/extension/payment/postfinancecheckout.php @@ -36,6 +36,11 @@ $_['log_level_error'] = 'Error'; $_['log_level_debug'] = 'Debug'; +// Rounding +$_['title_rounding_adjustment'] = 'Rounding adjustments'; +$_['entry_rounding_adjustment'] = 'Send Item'; +$_['description_rounding_adjustment'] = 'If we cannot map the Opencart totals to PostFinance Checkout line items exactly, instead of disabling the payment methods you can instead choose to send an Adjustment Item which contains the difference. In this case tax amounts may no longer be exact, and features such as reconciliation in PostFinance Checkout and refunds from the Opencart backend may not fully work.'; + // Status $_['title_payment_status'] = "Status mapping"; $_['entry_processing_status'] = "Processing status"; @@ -250,6 +255,8 @@ $_['entry_sku'] = 'SKU'; $_['entry_id'] = 'Unique ID'; +$_['rounding_adjustment_item_name'] = 'Rounding Adjustment'; + $_['entry_restock'] = 'Restock products'; $_['button_reset'] = 'Reset'; $_['button_full'] = 'Full order'; diff --git a/upload/admin/model/extension/postfinancecheckout/migration.php b/upload/admin/model/extension/postfinancecheckout/migration.php index 8d047d9..80b12ed 100644 --- a/upload/admin/model/extension/postfinancecheckout/migration.php +++ b/upload/admin/model/extension/postfinancecheckout/migration.php @@ -1,4 +1,5 @@ registry)->log("Starting migration"); $currentVersion = '0.0.0'; if ($this->config->has('postfinancecheckout_migration_version')) { $currentVersion = $this->config->get('postfinancecheckout_migration_version'); + \PostFinanceCheckoutHelper::instance($this->registry)->log("Current version: $currentVersion"); } $startingVersion = $currentVersion; @@ -26,12 +29,14 @@ public function migrate(){ \PostFinanceCheckoutHelper::instance($this->registry)->dbTransactionStart(); try { if (version_compare($currentVersion, $migration['version']) === -1) { + \PostFinanceCheckoutHelper::instance($this->registry)->log("Running {$migration['name']}"); $this->{$migration['function']}(); \PostFinanceCheckoutHelper::instance($this->registry)->dbTransactionCommit(); $currentVersion = $migration['version']; } } catch (Exception $e) { + \PostFinanceCheckoutHelper::instance($this->registry)->log($e->getMessage()); \PostFinanceCheckoutHelper::instance($this->registry)->dbTransactionRollback(); break; } @@ -39,10 +44,12 @@ public function migrate(){ // update version if required if (version_compare($startingVersion, $currentVersion) !== 0) { + \PostFinanceCheckoutHelper::instance($this->registry)->log("Updating version"); $this->load->model('setting/setting'); $settings = $this->model_setting_setting->getSetting('postfinancecheckout'); $settings['postfinancecheckout_migration_version'] = self::$migrations[$currentVersion]['version']; $settings['postfinancecheckout_migration_name'] = self::$migrations[$currentVersion]['name']; + \PostFinanceCheckoutHelper::instance($this->registry)->log("Currently at ". self::$migrations[$currentVersion]['version'].": ".self::$migrations[$currentVersion]['name']); $this->model_setting_setting->editSetting('postfinancecheckout', $settings); } } diff --git a/upload/admin/view/template/extension/payment/postfinancecheckout.tpl b/upload/admin/view/template/extension/payment/postfinancecheckout.tpl index 9d19ed8..b406055 100644 --- a/upload/admin/view/template/extension/payment/postfinancecheckout.tpl +++ b/upload/admin/view/template/extension/payment/postfinancecheckout.tpl @@ -225,6 +225,35 @@
    +
    + +
    + + +
    + +
    +
    +
    +
    + +
    + + +
    + + checked value="1"/> +

    +
    +
    +
    diff --git a/upload/admin/view/template/extension/postfinancecheckout/refund.tpl b/upload/admin/view/template/extension/postfinancecheckout/refund.tpl index 7b12478..cf6fa40 100644 --- a/upload/admin/view/template/extension/postfinancecheckout/refund.tpl +++ b/upload/admin/view/template/extension/postfinancecheckout/refund.tpl @@ -110,7 +110,7 @@ function echoAmount($amount, $currency_decimals){ - getTaxes()) >= 1) : ?> + getTaxes())) : ?>
    getTaxes() as $tax_rate) :?>
    getTitle(); ?>
    diff --git a/upload/catalog/language/en-gb/extension/payment/postfinancecheckout.php b/upload/catalog/language/en-gb/extension/payment/postfinancecheckout.php index c0bd2fe..7ab730e 100644 --- a/upload/catalog/language/en-gb/extension/payment/postfinancecheckout.php +++ b/upload/catalog/language/en-gb/extension/payment/postfinancecheckout.php @@ -12,6 +12,8 @@ $_['error_not_pending'] = 'Transaction exists and is not in state pending.'; $_['error_confirmation'] = 'The transaction could not be confirmed. Please check to see if any payment has been made from your account, and retry if no charge has taken place.'; +$_['rounding_adjustment_item_name'] = 'Rounding Adjustment'; + // Order overview / download buttons $_['button_invoice'] = 'Invoice'; $_['button_packing_slip'] = 'Packing slip'; @@ -27,4 +29,4 @@ $_['message_webhook_voided'] = 'Transaction has been voided via webhook.'; $_['message_webhook_manual'] = 'A manual decision about whether to accept the payment is required.'; -$_['message_refund_successful'] = 'The refund \'%s\' over amount %s was successful.'; \ No newline at end of file +$_['message_refund_successful'] = 'The refund \'%s\' over amount %s was successful.'; diff --git a/upload/system/library/postfinancecheckout/dynamic/catalog/model.php b/upload/system/library/postfinancecheckout/dynamic/catalog/model.php index afce797..ad8f217 100644 --- a/upload/system/library/postfinancecheckout/dynamic/catalog/model.php +++ b/upload/system/library/postfinancecheckout/dynamic/catalog/model.php @@ -45,7 +45,7 @@ public function getMethod($address, $total){ } $available_methods = \PostFinanceCheckout\Service\Transaction::instance($this->registry)->getPaymentMethods($order_info); - $configuration_id = substr($this->getCode(), \PostFinanceCheckoutHelper::extractPaymentMethodId($this->getCode())); + $configuration_id = \PostFinanceCheckoutHelper::extractPaymentMethodId($this->getCode()); foreach ($available_methods as $method) { if ($method->getId() == $configuration_id) { diff --git a/upload/system/library/postfinancecheckout/service/line_item.php b/upload/system/library/postfinancecheckout/service/line_item.php index 8dbb07f..719e4dc 100644 --- a/upload/system/library/postfinancecheckout/service/line_item.php +++ b/upload/system/library/postfinancecheckout/service/line_item.php @@ -38,22 +38,22 @@ public function getReducedItemsFromOrder(array $order_info, $transaction_id, $sp $this->tax->setShippingAddress($order_info['shipping_country_id'], $order_info['shipping_zone_id']); $this->tax->setPaymentAddress($order_info['payment_country_id'], $order_info['payment_zone_id']); $this->coupon_total = 0; - + \PostFinanceCheckoutHelper::instance($this->registry)->xfeeproDisableIncVat(); $line_items = $this->getItemsFromOrder($order_info, $transaction_id, $space_id); \PostFinanceCheckoutHelper::instance($this->registry)->xfeeproRestoreIncVat(); - + // get all succesfully reduced items $refund_jobs = \PostFinanceCheckout\Entity\RefundJob::loadByOrder($this->registry, $order_info['order_id']); $reduction_items = array(); foreach ($refund_jobs as $refund) { if ($refund->getState() != \PostFinanceCheckout\Entity\RefundJob::STATE_FAILED_CHECK && - $refund->getState() != \PostFinanceCheckout\Entity\RefundJob::STATE_FAILED_DONE) { + $refund->getState() != \PostFinanceCheckout\Entity\RefundJob::STATE_FAILED_DONE) { foreach ($refund->getReductionItems() as $already_reduced) { if (!isset($reduction_items[$already_reduced->getLineItemUniqueId()])) { $reduction_items[$already_reduced->getLineItemUniqueId()] = array( 'quantity' => 0, - 'unit_price' => 0 + 'unit_price' => 0 ); } $reduction_items[$already_reduced->getLineItemUniqueId()]['quantity'] += $already_reduced->getQuantityReduction(); @@ -61,7 +61,7 @@ public function getReducedItemsFromOrder(array $order_info, $transaction_id, $sp } } } - + // remove them from available items foreach ($line_items as $key => $line_item) { if (isset($reduction_items[$line_item->getUniqueId()])) { @@ -83,13 +83,13 @@ public function getItemsFromOrder(array $order_info){ $this->tax = \PostFinanceCheckoutVersionHelper::newTax($this->registry); $this->tax->setShippingAddress($order_info['shipping_country_id'], $order_info['shipping_zone_id']); $this->tax->setPaymentAddress($order_info['payment_country_id'], $order_info['payment_zone_id']); - + $transaction_info = \PostFinanceCheckout\Entity\TransactionInfo::loadByOrderId($this->registry, $order_info['order_id']); $order_model = \PostFinanceCheckoutHelper::instance($this->registry)->getOrderModel(); - + $order_total = 0; $items = array(); - + $this->coupon_total = 0; $this->fixed_taxes = array(); $this->products = $order_model->getOrderProducts($order_info['order_id']); @@ -108,14 +108,14 @@ public function getItemsFromOrder(array $order_info){ 'title' => $order_info['shipping_method'], 'code' => $order_info['shipping_code'], 'cost' => $shipping_info->getCost(), - 'tax_class_id' => $shipping_info->getTaxClassId() + 'tax_class_id' => $shipping_info->getTaxClassId() ); } else { $this->shipping = false; } $this->total = $order_model->getOrderTotals($order_info['order_id']); - + $sub_total = 0; foreach ($this->total as $total) { if ($total['code'] == 'sub_total') { @@ -124,15 +124,15 @@ public function getItemsFromOrder(array $order_info){ } } $this->sub_total = $sub_total; - + $this->coupon = $this->getCoupon($transaction_info->getCouponCode(), $sub_total, $order_info['customer_id']); - + return $this->createLineItems($order_info['currency_code']); } public function getItemsFromSession(){ $this->tax = $this->registry->get('tax'); - + $session = $this->registry->get('session'); if (isset($session->data['shipping_country_id']) && isset($session->data['shipping_country_id'])) { $this->tax->setShippingAddress($session->data['shipping_country_id'], $session->data['shipping_zone_id']); @@ -141,7 +141,7 @@ public function getItemsFromSession(){ $this->tax->setPaymentAddress($session->data['payment_country_id'], $session->data['payment_zone_id']); } $this->products = $this->registry->get('cart')->getProducts(); - + if (!empty($this->registry->get('session')->data['vouchers'])) { $voucher = current($this->registry->get('session')->data['vouchers']); } @@ -151,18 +151,18 @@ public function getItemsFromSession(){ else { $this->voucher = false; } - + if (!empty($this->registry->get('session')->data['shipping_method'])) { $this->shipping = $this->registry->get('session')->data['shipping_method']; } else { $this->shipping = false; } - + \PostFinanceCheckoutHelper::instance($this->registry)->xfeeproDisableIncVat(); $this->total = \PostFinanceCheckoutVersionHelper::getSessionTotals($this->registry); \PostFinanceCheckoutHelper::instance($this->registry)->xfeeProRestoreIncVat(); - + $sub_total = 0; foreach ($this->total as $total) { if ($total['code'] == 'sub_total') { @@ -171,7 +171,7 @@ public function getItemsFromSession(){ } } $this->sub_total = $sub_total; - + if (isset($this->registry->get('session')->data['coupon']) && isset($this->registry->get('session')->data['customer_id'])) { $this->coupon = $this->getCoupon($this->registry->get('session')->data['coupon'], $sub_total, $this->registry->get('session')->data['customer_id']); @@ -179,36 +179,37 @@ public function getItemsFromSession(){ else { $this->coupon = false; } - + return $this->createLineItems(\PostFinanceCheckoutHelper::instance($this->registry)->getCurrency()); } private function createLineItems($currency_code){ + $items = array(); $calculated_total = 0; foreach ($this->products as $product) { $items[] = $item = $this->createLineItemFromProduct($product); $calculated_total += $item->getAmountIncludingTax(); } - + if ($this->voucher) { $items[] = $item = $this->createLineItemFromVoucher(); $calculated_total += $item->getAmountIncludingTax(); } - - if($this->coupon) { - $items[] = $item = $this->createLineItemFromCoupon(); + + if ($this->coupon) { + $items[] = $item = $this->createLineItemFromCoupon(); $calculated_total += $item->getAmountIncludingTax(); } - + if ($this->shipping) { $items[] = $item = $this->createLineItemFromShipping(); $calculated_total += $item->getAmountIncludingTax(); } - + $expected_total = 0; // attempt to add 3rd party totals foreach ($this->total as $total) { - + if (strncmp($total['code'], 'xfee', strlen('xfee')) === 0) { $items[] = $item = $this->createXFeeLineItem($total); $calculated_total += $item->getAmountIncludingTax(); @@ -218,7 +219,7 @@ private function createLineItems($currency_code){ 'shipping', 'sub_total', 'coupon', - 'tax' + 'tax' ))) { if ($total['value'] != 0) { $items[] = $item = $this->createLineItemFromTotal($total); @@ -229,72 +230,90 @@ private function createLineItems($currency_code){ $expected_total = $total['value']; } } - + foreach ($this->fixed_taxes as $key => $tax) { $items[] = $item = $this->createLineItemFromFee($tax, $key); $calculated_total += $item->getAmountIncludingTax(); } - + // only check amount if currency is base currency. Otherwise, rounding errors are expected to occur due to Opencart standard if ($this->registry->get('currency')->getValue($currency_code) == 1) { $expected_total = \PostFinanceCheckoutHelper::instance($this->registry)->formatAmount($expected_total); - + if (!\PostFinanceCheckoutHelper::instance($this->registry)->areAmountsEqual($calculated_total, $expected_total, $currency_code)) { - \PostFinanceCheckoutHelper::instance($this->registry)->log( - "Invalid order total calculated. Calculated total: $calculated_total, Expected total: $expected_total.", - \PostFinanceCheckoutHelper::LOG_ERROR); - \PostFinanceCheckoutHelper::instance($this->registry)->log(array( - 'Products' => $this->products - ), \PostFinanceCheckoutHelper::LOG_ERROR); - \PostFinanceCheckoutHelper::instance($this->registry)->log(array( - 'Voucher' => $this->voucher - ), \PostFinanceCheckoutHelper::LOG_ERROR); - \PostFinanceCheckoutHelper::instance($this->registry)->log(array( - 'Coupon' => $this->coupon - ), \PostFinanceCheckoutHelper::LOG_ERROR); - \PostFinanceCheckoutHelper::instance($this->registry)->log(array( - 'Totals' => $this->total - ), \PostFinanceCheckoutHelper::LOG_ERROR); - \PostFinanceCheckoutHelper::instance($this->registry)->log(array( - 'Fixed taxes' => $this->fixed_taxes - ), \PostFinanceCheckoutHelper::LOG_ERROR); - \PostFinanceCheckoutHelper::instance($this->registry)->log(array( - 'Shipping' => $this->shipping - ), \PostFinanceCheckoutHelper::LOG_ERROR); - \PostFinanceCheckoutHelper::instance($this->registry)->log(array( - 'postfinancecheckout Items' => $items - ), \PostFinanceCheckoutHelper::LOG_ERROR); - - throw new \Exception("Invalid order total."); + if ($this->registry->get('config')->get('postfinancecheckout_rounding_adjustment')) { + $items[] = $this->createRoundingAdjustmentLineItem($expected_total, $calculated_total); + } + else { + \PostFinanceCheckoutHelper::instance($this->registry)->log( + "Invalid order total calculated. Calculated total: $calculated_total, Expected total: $expected_total.", + \PostFinanceCheckoutHelper::LOG_ERROR); + \PostFinanceCheckoutHelper::instance($this->registry)->log(array( + 'Products' => $this->products + ), \PostFinanceCheckoutHelper::LOG_ERROR); + \PostFinanceCheckoutHelper::instance($this->registry)->log(array( + 'Voucher' => $this->voucher + ), \PostFinanceCheckoutHelper::LOG_ERROR); + \PostFinanceCheckoutHelper::instance($this->registry)->log(array( + 'Coupon' => $this->coupon + ), \PostFinanceCheckoutHelper::LOG_ERROR); + \PostFinanceCheckoutHelper::instance($this->registry)->log(array( + 'Totals' => $this->total + ), \PostFinanceCheckoutHelper::LOG_ERROR); + \PostFinanceCheckoutHelper::instance($this->registry)->log(array( + 'Fixed taxes' => $this->fixed_taxes + ), \PostFinanceCheckoutHelper::LOG_ERROR); + \PostFinanceCheckoutHelper::instance($this->registry)->log(array( + 'Shipping' => $this->shipping + ), \PostFinanceCheckoutHelper::LOG_ERROR); + \PostFinanceCheckoutHelper::instance($this->registry)->log(array( + 'postfinancecheckout Items' => $items + ), \PostFinanceCheckoutHelper::LOG_ERROR); + + throw new \Exception("Invalid order total."); + } } } - return $items; } + + private function createRoundingAdjustmentLineItem($expected, $calculated) { + $difference = $expected - $calculated; + $line_item = new LineItemCreate(); + + $line_item->setName(\PostFinanceCheckoutHelper::instance($this->registry)->getTranslation('rounding_adjustment_item_name')); + $line_item->setSku('rounding-adjustment'); + $line_item->setUniqueId('rounding-adjustment'); + $line_item->setQuantity(1); + $line_item->setType(LineItemType::FEE); + $line_item->setAmountIncludingTax(\PostFinanceCheckoutHelper::instance($this->registry)->formatAmount($difference)); + + return $this->cleanLineItem($line_item); + } private function createLineItemFromFee($fee, $id){ $line_item = new LineItemCreate(); - + $line_item->setName($fee['name']); $line_item->setSku($fee['code']); $line_item->setUniqueId($id); $line_item->setQuantity($fee['quantity']); $line_item->setType(LineItemType::FEE); $line_item->setAmountIncludingTax(\PostFinanceCheckoutHelper::instance($this->registry)->formatAmount($fee['amount'])); - + return $this->cleanLineItem($line_item); } private function createLineItemFromTotal($total){ $line_item = new LineItemCreate(); - + $line_item->setName($total['title']); $line_item->setSku($total['code']); $line_item->setUniqueId($total['code']); $line_item->setQuantity(1); $line_item->setType(LineItemType::FEE); $line_item->setAmountIncludingTax(\PostFinanceCheckoutHelper::instance($this->registry)->formatAmount($total['value'])); - + return $this->cleanLineItem($line_item); } @@ -346,9 +365,9 @@ private function getXfeeTaxClass($total){ } return null; } - - private function getXfeePro() { - if($this->xfeepro === null) { + + private function getXfeePro(){ + if ($this->xfeepro === null) { $config = $this->registry->get('config'); $this->xfeepro = $xfeepro = unserialize(base64_decode($config->get('xfeepro'))); } @@ -358,9 +377,9 @@ private function getXfeePro() { private function createLineItemFromProduct($product){ $line_item = new LineItemCreate(); $amount_excluding_tax = $product['total']; - + $product['tax_class_id'] = $this->getTaxClassByProductId($product['product_id']); - + if ($this->coupon && (!$this->coupon['product'] || in_array($product['product_id'], $this->coupon['product']))) { if ($this->coupon['type'] == 'F') { $discount = $this->coupon['discount'] * ($product['total'] / $this->sub_total); @@ -369,14 +388,16 @@ private function createLineItemFromProduct($product){ $discount = $product['total'] / 100 * $this->coupon['discount']; } $this->coupon_total -= $discount; - $line_item->setAttributes(array( - "coupon" => new \PostFinanceCheckout\Sdk\Model\LineItemAttributeCreate(array( - 'label' => $this->coupon['name'], - 'value' => $discount - )) - )); + $line_item->setAttributes( + array( + "coupon" => new \PostFinanceCheckout\Sdk\Model\LineItemAttributeCreate( + array( + 'label' => $this->coupon['name'], + 'value' => $discount + )) + )); } - + $line_item->setName($product['name']); $line_item->setQuantity($product['quantity']); $line_item->setShippingRequired(isset($product['shipping']) && $product['shipping']); @@ -388,16 +409,16 @@ private function createLineItemFromProduct($product){ } $line_item->setUniqueId($this->createUniqueIdFromProduct($product)); $line_item->setType(LineItemType::PRODUCT); - + $tax_amount = $this->addTaxesToLineItem($line_item, $amount_excluding_tax, $product['tax_class_id']); $line_item->setAmountIncludingTax(\PostFinanceCheckoutHelper::instance($this->registry)->formatAmount($amount_excluding_tax + $tax_amount)); - + return $this->cleanLineItem($line_item); } private function createUniqueIdFromProduct($product){ $id = $product['product_id']; - if(isset($product['option'])) { + if (isset($product['option'])) { foreach ($product['option'] as $option) { $hasValue = false; if (isset($option['product_option_id'])) { @@ -419,51 +440,51 @@ private function createUniqueIdFromProduct($product){ } return $id; } - + private function createLineItemFromCoupon(){ $line_item = new LineItemCreate(); - + $line_item->setName($this->coupon['name']); $line_item->setQuantity(1); $line_item->setType(LineItemType::DISCOUNT); $line_item->setSKU($this->coupon['code']); $line_item->setUniqueId($this->coupon['coupon_id']); $line_item->setAmountIncludingTax(\PostFinanceCheckoutHelper::instance($this->registry)->formatAmount($this->coupon_total)); - + return $this->cleanLineItem($line_item); } - + private function createLineItemFromVoucher(){ $line_item = new LineItemCreate(); - + $line_item->setName($this->voucher['name']); $line_item->setQuantity(1); $line_item->setType(LineItemType::DISCOUNT); $line_item->setSKU($this->voucher['code']); $line_item->setUniqueId($this->voucher['code']); $line_item->setAmountIncludingTax(\PostFinanceCheckoutHelper::instance($this->registry)->formatAmount($this->voucher['amount'])); - + return $this->cleanLineItem($line_item); } private function createLineItemFromShipping(){ $line_item = new LineItemCreate(); - + $amount_excluding_tax = $this->shipping['cost']; - + if ($this->coupon && $this->coupon['shipping']) { $amount_excluding_tax = 0; } - + $line_item->setName($this->shipping['title']); $line_item->setSku($this->shipping['code']); $line_item->setUniqueId($this->shipping['code']); $line_item->setType(LineItemType::SHIPPING); $line_item->setQuantity(1); - + $tax_amount = $this->addTaxesToLineItem($line_item, $amount_excluding_tax, $this->shipping['tax_class_id']); $line_item->setAmountIncludingTax(\PostFinanceCheckoutHelper::instance($this->registry)->formatAmount($amount_excluding_tax + $tax_amount)); - + return $this->cleanLineItem($line_item); } @@ -487,14 +508,14 @@ private function addTaxesToLineItem(LineItemCreate $line_item, $total, $tax_clas $tax_amount += $rate['amount']; $taxes[] = new TaxCreate(array( 'rate' => $rate['rate'], - 'title' => $rate['name'] + 'title' => $rate['name'] )); } // F = fixed else if ($rate['type'] == 'F') { $key = preg_replace("/[^\w_]/", "", $rate['name']); $amount = $rate['amount'] * $line_item->getQuantity(); - + if (isset($this->fixed_taxes[$key])) { $this->fixed_taxes[$key]['amount'] += $amount; $this->fixed_taxes[$key]['quantity'] += $line_item->getQuantity(); @@ -504,7 +525,7 @@ private function addTaxesToLineItem(LineItemCreate $line_item, $total, $tax_clas 'code' => $key, 'name' => $rate['name'], 'amount' => $amount, - 'quantity' => $line_item->getQuantity() + 'quantity' => $line_item->getQuantity() ); } } @@ -526,78 +547,78 @@ private function addTaxesToLineItem(LineItemCreate $line_item, $total, $tax_clas private function getCoupon($code, $sub_total, $customer_id){ $db = $this->registry->get('db'); $status = true; - + $coupon_query = $db->query( "SELECT * FROM `" . DB_PREFIX . "coupon` WHERE code = '" . $db->escape($code) . - "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND status = '1'"); - + "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND status = '1'"); + if ($coupon_query->num_rows) { if ($coupon_query->row['total'] > $sub_total) { $status = false; } - + $coupon_total = $this->getTotalCouponHistoriesByCoupon($code); - + if ($coupon_query->row['uses_total'] > 0 && ($coupon_total >= $coupon_query->row['uses_total'])) { $status = false; } - + if ($coupon_query->row['logged'] && !$customer_id) { $status = false; } - + if ($customer_id) { $customer_total = $this->getTotalCouponHistoriesByCustomerId($code, $customer_id); - + if ($coupon_query->row['uses_customer'] > 0 && ($customer_total >= $coupon_query->row['uses_customer'])) { $status = false; } } - + // Products $coupon_product_data = array(); - + $coupon_product_query = $db->query( "SELECT * FROM `" . DB_PREFIX . "coupon_product` WHERE coupon_id = '" . (int) $coupon_query->row['coupon_id'] . "'"); - + foreach ($coupon_product_query->rows as $product) { $coupon_product_data[] = $product['product_id']; } - + // Categories $coupon_category_data = array(); - + $coupon_category_query = $db->query( "SELECT * FROM `" . DB_PREFIX . "coupon_category` cc LEFT JOIN `" . DB_PREFIX . - "category_path` cp ON (cc.category_id = cp.path_id) WHERE cc.coupon_id = '" . (int) $coupon_query->row['coupon_id'] . "'"); - + "category_path` cp ON (cc.category_id = cp.path_id) WHERE cc.coupon_id = '" . (int) $coupon_query->row['coupon_id'] . "'"); + foreach ($coupon_category_query->rows as $category) { $coupon_category_data[] = $category['category_id']; } - + $product_data = array(); - + if ($coupon_product_data || $coupon_category_data) { foreach ($this->products as $product) { if (in_array($product['product_id'], $coupon_product_data)) { $product_data[] = $product['product_id']; - + continue; } - + foreach ($coupon_category_data as $category_id) { $coupon_category_query = $db->query( "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "product_to_category` WHERE `product_id` = '" . - (int) $product['product_id'] . "' AND category_id = '" . (int) $category_id . "'"); - + (int) $product['product_id'] . "' AND category_id = '" . (int) $category_id . "'"); + if ($coupon_category_query->row['total']) { $product_data[] = $product['product_id']; - + continue; } } } - + if (!$product_data) { $status = false; } @@ -606,7 +627,7 @@ private function getCoupon($code, $sub_total, $customer_id){ else { $status = false; } - + if ($status) { return array( 'coupon_id' => $coupon_query->row['coupon_id'], @@ -622,7 +643,7 @@ private function getCoupon($code, $sub_total, $customer_id){ 'uses_total' => $coupon_query->row['uses_total'], 'uses_customer' => $coupon_query->row['uses_customer'], 'status' => $coupon_query->row['status'], - 'date_added' => $coupon_query->row['date_added'] + 'date_added' => $coupon_query->row['date_added'] ); } else { @@ -633,17 +654,17 @@ private function getCoupon($code, $sub_total, $customer_id){ private function getTotalCouponHistoriesByCoupon($coupon){ $query = $this->registry->get('db')->query( "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "coupon_history` ch LEFT JOIN `" . DB_PREFIX . - "coupon` c ON (ch.coupon_id = c.coupon_id) WHERE c.code = '" . $this->registry->get('db')->escape($coupon) . "'"); - + "coupon` c ON (ch.coupon_id = c.coupon_id) WHERE c.code = '" . $this->registry->get('db')->escape($coupon) . "'"); + return $query->row['total']; } private function getTotalCouponHistoriesByCustomerId($coupon, $customer_id){ $query = $this->registry->get('db')->query( "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "coupon_history` ch LEFT JOIN `" . DB_PREFIX . - "coupon` c ON (ch.coupon_id = c.coupon_id) WHERE c.code = '" . $this->registry->get('db')->escape($coupon) . - "' AND ch.customer_id = '" . (int) $customer_id . "'"); - + "coupon` c ON (ch.coupon_id = c.coupon_id) WHERE c.code = '" . $this->registry->get('db')->escape($coupon) . "' AND ch.customer_id = '" . + (int) $customer_id . "'"); + return $query->row['total']; } diff --git a/upload/system/library/postfinancecheckout/service/token.php b/upload/system/library/postfinancecheckout/service/token.php index 7f46943..f15fc9f 100644 --- a/upload/system/library/postfinancecheckout/service/token.php +++ b/upload/system/library/postfinancecheckout/service/token.php @@ -44,7 +44,7 @@ public function updateToken($space_id, $token_id){ else { $info = \PostFinanceCheckout\Entity\TokenInfo::loadByToken($this->registry, $space_id, $token_id); if ($info->getId()) { - $info->delete(); + $info->delete($this->registry); } } } @@ -57,7 +57,7 @@ protected function updateInfo($space_id, \PostFinanceCheckout\Sdk\Model\TokenVer \PostFinanceCheckout\Sdk\Model\TokenVersionState::UNINITIALIZED ))) { if ($info->getId()) { - $info->delete(); + $info->delete($this->registry); } return; } @@ -65,8 +65,6 @@ protected function updateInfo($space_id, \PostFinanceCheckout\Sdk\Model\TokenVer $info->setCustomerId($token_version->getToken()->getCustomerId()); $info->setName($token_version->getName()); - /* @var PostFinanceCheckout_Payment_Model_Entity_PaymentMethodConfiguration $paymentMethod */ - $payment_method = \PostFinanceCheckout\Entity\MethodConfiguration::loadByConfiguration($this->registry, $space_id, $token_version->getPaymentConnectorConfiguration()->getPaymentMethodConfiguration()->getId()); $info->setPaymentMethodId($payment_method->getId());