From b213274cfaed39ab36f88ca5871c73bdd77e191f Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 1 Apr 2023 14:32:04 +0200 Subject: [PATCH 01/16] Updated transaction start --- Helper/PayHelper.php | 41 +++ Model/PayPaymentCreate.php | 511 +++++++++++++++++++++++++- Model/Paymentmethod/PaymentMethod.php | 91 ++--- Model/PlaceMultiShippingOrder.php | 8 +- 4 files changed, 572 insertions(+), 79 deletions(-) diff --git a/Helper/PayHelper.php b/Helper/PayHelper.php index aebc5555..6c242c06 100644 --- a/Helper/PayHelper.php +++ b/Helper/PayHelper.php @@ -220,4 +220,45 @@ public function getHttpUserAgent() { return $this->httpHeader->getHttpUserAgent(); } + + /** + * @param $exceptionMessage + * @return \Magento\Framework\Phrase + */ + public static function getFriendlyMessage($exceptionMessage) + { + $exceptionMessage = strtolower(trim($exceptionMessage)); + + if (stripos($exceptionMessage, 'minimum amount') !== false) { + $strMessage = __('Unfortunately the order amount does not fit the requirements for this payment method.'); + } + elseif (stripos($exceptionMessage, 'not enabled for this service') !== false) { + $strMessage = __('The selected payment method is not enabled. Please select another payment method.'); + } else { + $strMessage = __('Unfortunately something went wrong.'); + } + + return $strMessage; + } + + /** + * @param string $gender + * @return string|null + */ + public static function genderConversion($gender) + { + switch ($gender) { + case '1': + $gender = 'M'; + break; + case '2': + $gender = 'F'; + break; + default: + $gender = null; + break; + } + return $gender; + } + } diff --git a/Model/PayPaymentCreate.php b/Model/PayPaymentCreate.php index b8126b50..f321f6f7 100644 --- a/Model/PayPaymentCreate.php +++ b/Model/PayPaymentCreate.php @@ -2,6 +2,12 @@ namespace Paynl\Payment\Model; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; +use Magento\InventoryInStorePickupShippingApi\Model\Carrier\InStorePickup; +use Magento\Sales\Model\Order; +use Paynl\Payment\Helper\PayHelper; +use Paynl\Payment\Model\Paymentmethod\PaymentMethod; + class PayPaymentCreate { /** @@ -15,30 +21,515 @@ class PayPaymentCreate private $finishURL; /** - * @param integer $amount Amounts - * @param string $exchangeURL + * @var string + */ + private $cocNumber; + + /** + * @var string + */ + private $vatNumber; + + /** + * @var string[] + */ + private $additionalData; + + /** + * @var integer + */ + private $paymentMethodId; + + /** + * @var integer + */ + private $testMode; + + /** + * @var \Paynl\Payment\Model\Config + */ + private $payConfig; + + /** + * @var \Magento\Framework\App\Config\ScopeConfigInterface */ - public function __construct($amount = null, $exchangeURL = null) + private $scopeConfig; + + /** + * @var PaymentMethod + */ + private $methodInstance; + + /** + * @var array + */ + private $paymentData = []; + + /** + * @var array + */ + private $endUserData = []; + + /** + * @var float|string|null + */ + private $orderId; + + /** + * @var Order + */ + private $order; + + /** + * @param Order $order + * @param PaymentMethod $methodInstance + * @throws \Exception + */ + public function __construct(Order $order, PaymentMethod $methodInstance) + { + $this->methodInstance = $methodInstance; + $this->payConfig = $methodInstance->paynlConfig; + $this->testMode = $this->payConfig->isTestMode(); + $this->scopeConfig = $methodInstance->getScopeConfig(); + $this->order = $order; + $this->orderId = $order->getIncrementId(); + $this->additionalData = $order->getPayment()->getAdditionalInformation(); + $this->setAmount($this->payConfig->isAlwaysBaseCurrency() ? $order->getBaseGrandTotal() : $order->getGrandTotal()); + $this->setCurrency($this->payConfig->isAlwaysBaseCurrency() ? $order->getBaseCurrencyCode() : $order->getOrderCurrencyCode()); + $this->setCocNumber($this->additionalData['kvknummer'] ?? ''); + $this->setVatNumber($this->additionalData['vatnumber'] ?? ''); + $this->setIssuer($this->additionalData['payment_option'] ?? ''); + $this->setExpireData((int)($this->additionalData['valid_days'] ?? '')); + $this->setFinishURL($this->additionalData['returnUrl'] ?? $order->getStore()->getBaseUrl() . 'paynl/checkout/finish/?entityid=' . $order->getEntityId()); + $this->setExchangeURL($this->additionalData['exchangeUrl'] ?? $order->getStore()->getBaseUrl() . 'paynl/checkout/exchange/'); + $this->setPaymentMethod($this->methodInstance->getPaymentOptionId()); + } + + /** + * @return \Paynl\Result\Transaction\Start + * @throws \Paynl\Error\Api + * @throws \Paynl\Error\Error + * @throws \Paynl\Error\Required\ApiToken + * @throws \Paynl\Error\Required\ServiceId + */ + public function create() { - $this->setAmount($amount); - $this->setFinishURL($exchangeURL); + $this->payConfig->configureSDK(); + + return \Paynl\Transaction::start($this->getData()); } /** - * @param string $url Finish URL + * @param integer $paymentMethodId Setting the method, like iDEAL or banktransfer... * @return void */ - public function setFinishURL($url) + public function setPaymentMethod($paymentMethodId) { - $this->finishURL = $url; + $this->paymentMethodId = $paymentMethodId; + return $this; } /** - * @param integer $amount + * @param $cocNumber * @return void */ + public function setCocNumber($cocNumber) + { + $this->cocNumber = $cocNumber; + return $this; + } + + /** + * @param $vatNumber + * @return $this + */ + public function setVatNumber($vatNumber) + { + $this->vatNumber = $vatNumber; + return $this; + } + + /** + * @param $issuer + * @return void + */ + public function setIssuer($issuer) + { + $this->paymentData['bank'] = $issuer ?? null; + return $this; + } + + /** + * @param string $finishUrl + * @return $this + */ + public function setFinishURL(string $finishUrl) + { + $this->paymentData['returnURL'] = $finishUrl; + return $this; + } + + /** + * @param string $exchangeURL + * @return $this + */ + public function setExchangeURL(string $exchangeURL) + { + $this->paymentData['exchangeURL'] = $exchangeURL; + return $this; + } + + /** + * @param int $valid_days + * @return $this + * @throws \Exception + */ + public function setExpireData(int $valid_days) + { + if (!empty($valid_days) && is_numeric($valid_days)) { + $this->paymentData['expireDate'] = new \DateTime('+' . $valid_days . ' days'); + } + return $this; + } + + /** + * @param $amount + * @return $this + */ public function setAmount($amount) { - $this->amount = $amount; + $this->paymentData['amount'] = $amount; + return $this; + } + + /** + * @param string currency Currency code + * @return $this + */ + public function setCurrency($currency) + { + $this->paymentData['currency'] = $currency; + return $this; + } + + private function getDescription() + { + $prefix = $this->scopeConfig->getValue('payment/paynl/order_description_prefix', 'store'); + return !empty($prefix) ? $prefix . $this->orderId : $this->orderId; + } + + public function getData() + { + $shippingAddress = $this->getShippingAddress(); + $endUserData = $this->getEnduserData(); + $invoiceAddress = $this->getInvoiceAddress(); + $productData = $this->getProductData(); + + $data = [ + 'amount' => $this->paymentData['amount'], + 'returnUrl' => $this->paymentData['returnURL'], + 'paymentMethod' => $this->paymentMethodId, + 'language' => $this->payConfig->getLanguage(), + 'bank' => $this->paymentData['bank'] ?? '', + 'orderNumber' => $this->orderId, + 'description' => $this->getDescription(), + 'extra1' => $this->orderId, + 'extra2' => $this->order->getQuoteId(), + 'extra3' => $this->order->getEntityId(), + 'transferData' => $this->methodInstance->getTransferData(), + 'exchangeUrl' => $this->paymentData['exchangeURL'], + 'currency' => $this->paymentData['currency'], + 'object' => $this->methodInstance->getVersion(), + ]; + + if (!empty($shippingAddress)) { + $data['address'] = $shippingAddress; + } + if (!empty($invoiceAddress)) { + $data['invoiceAddress'] = $invoiceAddress; + } + if (!empty($endUserData)) { + $data['enduser'] = $endUserData; + } + if (!empty($productData)) { + $data['products'] = $productData; + } + + $data['testmode'] = $this->testMode; + $data['ipaddress'] = $this->getIpAddress(); + + return $data; + } + + private function getIpAddress() + { + $ipAddress = $this->order->getRemoteIp(); + + # The ip address field in magento is too short, if the ip is invalid or ip is localhost get the ip myself + if (!filter_var($ipAddress, FILTER_VALIDATE_IP) || $ipAddress == '127.0.0.1') { + $ipAddress = \Paynl\Helper::getIp(); + } + return $ipAddress; + } + + private function useBillingAddressInstorePickup() + { + return $this->scopeConfig->getValue('payment/' . $this->methodInstance->getCode() . '/useBillingAddressInstorePickup', 'store') == 1; + } + + private function getShippingAddress() + { + $shippingAddress = null; + $orderShippingAddress = $this->order->getShippingAddress(); + + if (!empty($orderShippingAddress)) { + $arrShippingAddress = $orderShippingAddress->toArray(); + + if ($this->useBillingAddressInstorePickup() && class_exists('InStorePickup')) { + if ($this->order->getShippingMethod() === InStorePickup::DELIVERY_METHOD) { + $arrBillingAddress = $this->order->getBillingAddress(); + if (!empty($arrBillingAddress)) { + $arrShippingAddress = $arrBillingAddress->toArray(); + } + } + } + + $shippingAddress = [ + 'initials' => $arrShippingAddress['firstname'], + 'lastName' => $arrShippingAddress['lastname'], + ]; + $arrAddress2 = \Paynl\Helper::splitAddress($arrShippingAddress['street']); + $shippingAddress['streetName'] = $arrAddress2[0] ?? ''; + $shippingAddress['houseNumber'] = $arrAddress2[1] ?? ''; + $shippingAddress['zipCode'] = $arrShippingAddress['postcode']; + $shippingAddress['city'] = $arrShippingAddress['city']; + $shippingAddress['country'] = $arrShippingAddress['country_id']; + } + + return $shippingAddress; + } + + private function getEnduserData() + { + $arrBillingAddress = $this->order->getBillingAddress(); + $enduser = []; + + if ($arrBillingAddress) { + $arrBillingAddress = $arrBillingAddress->toArray(); + $enduser = [ + 'initials' => $arrBillingAddress['firstname'], + 'lastName' => $arrBillingAddress['lastname'], + 'phoneNumber' => $arrBillingAddress['telephone'], + 'emailAddress' => $arrBillingAddress['email'], + ]; + if (isset($this->additionalData['dob'])) { + $enduser['dob'] = $this->additionalData['dob']; + } + if (isset($this->additionalData['gender'])) { + $enduser['gender'] = $this->additionalData['gender']; + } + $enduser['gender'] = payHelper::genderConversion((empty($enduser['gender'])) ? $this->order->getCustomerGender($this->order) : $enduser['gender']); + if (!empty($arrBillingAddress['company'])) { + $enduser['company']['name'] = $arrBillingAddress['company']; + } + if (!empty($arrBillingAddress['country_id'])) { + $enduser['company']['countryCode'] = $arrBillingAddress['country_id']; + } + if (!empty($this->cocNumber)) { + $enduser['company']['cocNumber'] = $this->cocNumber; + } + if (!empty($arrBillingAddress['vat_id'])) { + $enduser['company']['vatNumber'] = $arrBillingAddress['vat_id']; + } elseif (!empty($this->vatNumber)) { + $enduser['company']['vatNumber'] = $this->vatNumber; + } + } + + return $enduser; + } + + private function getInvoiceAddress() + { + $arrBillingAddress = $this->order->getBillingAddress(); + $invoiceAddress = null; + + if ($arrBillingAddress) { + $arrBillingAddress = $arrBillingAddress->toArray(); + + $invoiceAddress = [ + 'initials' => $arrBillingAddress['firstname'] ?? '', + 'lastName' => $arrBillingAddress['lastname'] ?? '', + ]; + + $arrAddress = \Paynl\Helper::splitAddress($arrBillingAddress['street']); + $invoiceAddress['streetName'] = $arrAddress[0]; + $invoiceAddress['houseNumber'] = $arrAddress[1]; + $invoiceAddress['zipCode'] = $arrBillingAddress['postcode']; + $invoiceAddress['city'] = $arrBillingAddress['city']; + $invoiceAddress['country'] = $arrBillingAddress['country_id']; + } + + return $invoiceAddress; } + + private function getProductData() + { + $arrProducts = []; + $arrWeeeTax = []; + + foreach ($this->order->getAllVisibleItems() as $item) { + $arrItem = $item->toArray(); + if ($arrItem['price_incl_tax'] != null) { + // taxamount is not valid, because on discount it returns the taxamount after discount + $taxAmount = $arrItem['price_incl_tax'] - $arrItem['price']; + $price = $arrItem['price_incl_tax']; + + if ($this->payConfig->isAlwaysBaseCurrency()) { + $taxAmount = $arrItem['base_price_incl_tax'] - $arrItem['base_price']; + $price = $arrItem['base_price_incl_tax']; + } + + $productId = $arrItem['product_id']; + if ($this->payConfig->useSkuId()) { + $productId = $arrItem['sku']; + } + + $product = [ + 'id' => $productId, + 'name' => $arrItem['name'], + 'price' => $price, + 'qty' => $arrItem['qty_ordered'], + 'tax' => $taxAmount, + 'type' => \Paynl\Transaction::PRODUCT_TYPE_ARTICLE, + ]; + + # Product id's must be unique. Combinations of a "Configurable products" share the same product id. + # Each combination of a "configurable product" can be represented by a "simple product". + # The first and only child of the "configurable product" is the "simple product", or combination, chosen by the customer. + # Grab it and replace the product id to guarantee product id uniqueness. + if (isset($arrItem['product_type']) && $arrItem['product_type'] === Configurable::TYPE_CODE) { + $children = $item->getChildrenItems(); + $child = array_shift($children); + + if (!empty($child) && $child instanceof \Magento\Sales\Model\Order\Item && method_exists($child, 'getProductId')) { + $productIdChild = $child->getProductId(); + if ($this->payConfig->useSkuId() && method_exists($child, 'getSku')) { + $productIdChild = $child->getSku(); + } + $product['id'] = $productIdChild; + } + } + + $arrProducts[] = $product; + + # Check for Weee-tax + if (!empty($arrItem['weee_tax_applied'])) { + $weeeArr = json_decode($arrItem['weee_tax_applied']); + if (is_array($weeeArr)) { + foreach ($weeeArr as $weeeItem) { + if (!empty($weeeItem) && is_object($weeeItem)) { + $weeeTitle = $weeeItem->title; + $weeePrice = $weeeItem->row_amount_incl_tax; + $weeeTaxAmount = $weeeItem->row_amount_incl_tax - $weeeItem->row_amount; + + if ($this->payConfig->isAlwaysBaseCurrency()) { + $weeePrice = $weeeItem->base_row_amount_incl_tax; + $weeeTaxAmount = $weeeItem->base_row_amount_incl_tax - $weeeItem->base_row_amount; + } + + if (isset($arrWeeeTax[$weeeTitle])) { + $arrWeeeTax[$weeeTitle]['price'] += $weeePrice; + $arrWeeeTax[$weeeTitle]['tax'] += $weeeTaxAmount; + } else { + $arrWeeeTax[$weeeTitle] = array( + 'id' => 'weee', + 'name' => $weeeTitle, + 'price' => $weeePrice, + 'tax' => $weeeTaxAmount, + 'qty' => 1, + 'type' => \Paynl\Transaction::PRODUCT_TYPE_HANDLING, + ); + } + } + } + } + } + } + } + + # Shipping + $shippingCost = $this->order->getShippingInclTax(); + $shippingTax = $this->order->getShippingTaxAmount(); + + if ($this->payConfig->isAlwaysBaseCurrency()) { + $shippingCost = $this->order->getBaseShippingInclTax(); + $shippingTax = $this->order->getBaseShippingTaxAmount(); + } + + $shippingDescription = $this->order->getShippingDescription(); + + if ($shippingCost != 0) { + $arrProducts[] = [ + 'id' => 'shipping', + 'name' => empty($shippingDescription) ? 'Shipping' : $shippingDescription, + 'price' => $shippingCost, + 'qty' => 1, + 'tax' => $shippingTax, + 'type' => \Paynl\Transaction::PRODUCT_TYPE_SHIPPING, + ]; + } + + // Gift Wrapping + $gwCost = $this->order->getGwPriceInclTax(); + $gwTax = $this->order->getGwTaxAmount(); + + if ($this->payConfig->isAlwaysBaseCurrency()) { + $gwCost = $this->order->getGwBasePriceInclTax(); + $gwTax = $this->order->getGwBaseTaxAmount(); + } + + if ($gwCost != 0) { + $arrProducts[] = [ + 'id' => $this->order->getGwId(), + 'name' => 'Gift Wrapping', + 'price' => $gwCost, + 'qty' => 1, + 'tax' => $gwTax, + 'type' => \Paynl\Transaction::PRODUCT_TYPE_HANDLING, + ]; + } + + // kortingen + $discount = $this->order->getDiscountAmount(); + $discountTax = $this->order->getDiscountTaxCompensationAmount() * -1; + + if ($this->payConfig->isAlwaysBaseCurrency()) { + $discount = $this->order->getBaseDiscountAmount(); + $discountTax = $this->order->getBaseDiscountTaxCompensationAmount() * -1; + } + + if ($this->payConfig->isSendDiscountTax() == 0) { + $discountTax = 0; + } + + $discountDescription = __('Discount'); + + if ($discount != 0) { + $arrProducts[] = [ + 'id' => 'discount', + 'name' => $discountDescription, + 'price' => $discount, + 'qty' => 1, + 'tax' => $discountTax, + 'type' => \Paynl\Transaction::PRODUCT_TYPE_DISCOUNT, + ]; + } + + if (!empty($arrWeeeTax)) { + $arrProducts = array_merge($arrProducts, $arrWeeeTax); + } + + return $arrProducts; + } + } diff --git a/Model/Paymentmethod/PaymentMethod.php b/Model/Paymentmethod/PaymentMethod.php index 147f7877..41fe6e77 100644 --- a/Model/Paymentmethod/PaymentMethod.php +++ b/Model/Paymentmethod/PaymentMethod.php @@ -38,7 +38,7 @@ abstract class PaymentMethod extends AbstractMethod /** * @var Config */ - protected $paynlConfig; + public $paynlConfig; /** * @var OrderRepository @@ -124,6 +124,22 @@ public function __construct( $this->cookieManager = $objectManager->create('\Magento\Framework\Stdlib\CookieManagerInterface'); } + /** + * @return ScopeConfigInterface + */ + public function getScopeConfig() + { + return $this->_scopeConfig; + } + + /** + * @return mixed|string + */ + public function getCode() + { + return $this->_code; + } + /** * @param string $status * @return mixed @@ -315,26 +331,6 @@ public function getVersion() return $version; } - /** - * @param string $gender - * @return string|null - */ - public function genderConversion($gender) - { - switch ($gender) { - case '1': - $gender = 'M'; - break; - case '2': - $gender = 'F'; - break; - default: - $gender = null; - break; - } - return $gender; - } - /** * @param string $paymentAction * @param object $stateObject @@ -449,23 +445,14 @@ public function void(InfoInterface $payment) public function startTransaction(Order $order) { try { - $transaction = $this->doStartTransaction($order); - payHelper::logDebug('Transaction: ' . $transaction->getTransactionId()); + $transaction = (new PayPaymentCreate($order, $this))->create(); } catch (\Exception $e) { - $strMessage = $e->getMessage(); - payHelper::logDebug('Transactie start mislukt: ' . $strMessage . ' | ' . $e->getCode()); - - if (stripos($strMessage, 'minimum amount') !== false) { - $this->messageManager->addNoticeMessage(__('Unfortunately the order amount does not fit the requirements for this payment method.')); - } else { - $this->messageManager->addNoticeMessage(__('Unfortunately something went wrong')); - } - - $store = $order->getStore(); - - return $store->getBaseUrl() . 'checkout/cart/index'; + payHelper::logCritical('Transaction start failed: ' . $e->getMessage() . ' | ' . $e->getCode()); + $this->messageManager->addNoticeMessage(payHelper::getFriendlyMessage($e->getMessage())); + return $order->getStore()->getBaseUrl() . 'checkout/cart/index'; } + payHelper::logDebug('Transaction: ' . $transaction->getTransactionId()); $order->getPayment()->setAdditionalInformation('transactionId', $transaction->getTransactionId()); $this->paynlConfig->setStore($order->getStore()); @@ -478,38 +465,6 @@ public function startTransaction(Order $order) return $transaction->getRedirectUrl(); } - /** - * @param PayPaymentCreate $payment - * @return false|\Paynl\Result\Transaction\Start - */ - public function createPayPayment(PayPaymentCreate $payment) - { - $payTransction = false; - try { - $payTransction = \Paynl\Transaction::start($payment->getData()); - } catch (\Exception $exception) { - payHelper::logCritical('Transaction start failed: ' . $e->getMessage() . ' | ' . $e->getCode()); - $this->messageManager->addNoticeMessage(payHelper::getFriendlyMessage($e->getMessage())); - } - - return $payTransction; - } - - /** - * @param Order $order - * @param integer $multipleOrderAmount Total amount of orders - * @param string $finishUrl Optional finish URL - * @return \Paynl\Result\Transaction\Start - * @throws \Paynl\Error\Api - * @throws \Paynl\Error\Error - * @throws \Paynl\Error\Required\ApiToken - * @throws \Paynl\Error\Required\ServiceId - */ - public function startMultiShippingOrder(Order $order, $multipleOrderAmount = null, $finishUrl = null) - { - return $this->doStartTransaction($order, $multipleOrderAmount, $finishUrl); - } - /** * @param Order $order * @param integer $multipleOrderAmount @@ -520,7 +475,7 @@ public function startMultiShippingOrder(Order $order, $multipleOrderAmount = nul * @throws \Paynl\Error\Required\ApiToken * @throws \Paynl\Error\Required\ServiceId */ - protected function doStartTransaction(Order $order, $multipleOrderAmount = null, $finishUrl = null) + protected function doStartTransactionOld(Order $order, $multipleOrderAmount = null, $finishUrl = null) { $this->paynlConfig->setStore($order->getStore()); $this->paynlConfig->configureSDK(); diff --git a/Model/PlaceMultiShippingOrder.php b/Model/PlaceMultiShippingOrder.php index c32dc9cc..0db1961a 100755 --- a/Model/PlaceMultiShippingOrder.php +++ b/Model/PlaceMultiShippingOrder.php @@ -96,7 +96,13 @@ public function place(array $orderList): array if ($methodInstance instanceof \Paynl\Payment\Model\Paymentmethod\Paymentmethod) { payHelper::logNotice('Start new payment for multishipping order ' . $order->getId(), array(), $order->getStore()); - $transaction = $methodInstance->startMultiShippingOrder($order, $totalOrderAmount, $this->getRedirectUrl($orderList)); + + $newPayment = new PayPaymentCreate($order, $methodInstance); + $newPayment->setAmount($totalOrderAmount); + $newPayment->setFinishURL($this->getRedirectUrl($orderList)); + + $transaction = $newPayment->create(); + $payment->setAdditionalInformation('transactionId', $transaction->getTransactionId()); $payment->setAdditionalInformation('order_ids', json_encode($this->getOrderIds($orderList))); $this->orderRepository->save($order); From 5d00947812373d5ed3c98dd4249c7cfd49751ea6 Mon Sep 17 00:00:00 2001 From: woutse Date: Wed, 5 Apr 2023 11:19:11 +0200 Subject: [PATCH 02/16] Fixed classname --- Model/PayPayment.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Model/PayPayment.php b/Model/PayPayment.php index 65fa5ab9..83c90838 100644 --- a/Model/PayPayment.php +++ b/Model/PayPayment.php @@ -242,12 +242,12 @@ public function processPaidOrder(PayTransaction $transaction, Order $order) } /** - * @param Transaction $transaction + * @param PayTransaction $transaction * @param Order $order * @param Interceptor $payment * @return boolean */ - private function processB2BPayment(Transaction $transaction, Order $order, Interceptor $payment) + private function processB2BPayment(PayTransaction $transaction, Order $order, Interceptor $payment) { # Create transaction $formatedPrice = $order->getBaseCurrency()->formatTxt($order->getGrandTotal()); From ef15ee7083519f5853f6ec2aa8a37c03f0966937 Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 8 Apr 2023 21:09:16 +0200 Subject: [PATCH 03/16] Code polish --- Helper/PayHelper.php | 95 +++++++++++++++++++++++++++++++------- Model/PayPaymentCreate.php | 41 ++++++++++++---- 2 files changed, 110 insertions(+), 26 deletions(-) diff --git a/Helper/PayHelper.php b/Helper/PayHelper.php index 6c242c06..43a4e224 100644 --- a/Helper/PayHelper.php +++ b/Helper/PayHelper.php @@ -4,11 +4,10 @@ use Psr\Log\LoggerInterface; use Magento\Framework\App\ResourceConnection; -use \Paynl\Payment\Model\Config\Source\LogOptions; +use Paynl\Payment\Model\Config\Source\LogOptions; use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress; use Magento\Framework\HTTP\Header; - class PayHelper extends \Magento\Framework\App\Helper\AbstractHelper { const PAY_LOG_PREFIX = 'PAY.: '; @@ -19,16 +18,21 @@ class PayHelper extends \Magento\Framework\App\Helper\AbstractHelper private $remoteAddress; private $httpHeader; - public function __construct( - ResourceConnection $resource, - RemoteAddress $remoteAddress, - Header $httpHeader - ) { + /** + * @param ResourceConnection $resource + * @param RemoteAddress $remoteAddress + * @param Header $httpHeader + */ + public function __construct(ResourceConnection $resource, RemoteAddress $remoteAddress, Header $httpHeader) + { $this->remoteAddress = $remoteAddress; $this->httpHeader = $httpHeader; $this->resource = $resource; } + /** + * @return \Magento\Framework\App\ObjectManager + */ public static function getObjectManager() { if (empty(self::$objectManager)) { @@ -38,6 +42,10 @@ public static function getObjectManager() return self::$objectManager; } + /** + * @param Magento\Store\Model\Store $store + * @return mixed + */ public static function getStore($store) { if (empty($store)) { @@ -51,6 +59,11 @@ public static function getStore($store) return $store; } + /** + * @param integer $level + * @param string $type + * @return bool + */ public static function hasCorrectLevel($level, $type) { if ($level == LogOptions::LOG_ONLY_CRITICAL && $type == 'critical') { @@ -66,21 +79,45 @@ public static function hasCorrectLevel($level, $type) return false; } + /** + * @param string $text + * @param array $params + * @param Magento\Store\Model\Store $store + * @return void + */ public static function logCritical($text, $params = array(), $store = null) { self::writeLog($text, 'critical', $params, $store); } + /** + * @param $text + * @param $params + * @param Magento\Store\Model\Store $store + * @return void + */ public static function logNotice($text, $params = array(), $store = null) { self::writeLog($text, 'notice', $params, $store); } + /** + * @param string $text + * @param array $params + * @param Magento\Store\Model\Store $store + * @return void + */ public static function logInfo($text, $params = array(), $store = null) { self::writeLog($text, 'info', $params, $store); } + /** + * @param string $text + * @param array $params + * @param Magento\Store\Model\Store $store + * @return void + */ public static function logDebug($text, $params = array(), $store = null) { self::writeLog($text, 'debug', $params, $store); @@ -89,9 +126,9 @@ public static function logDebug($text, $params = array(), $store = null) /** * Logs while bypassing the loglevel setting. * - * @param $text + * @param string $text * @param array $params - * @param null $store + * @param Magento\Store\Model\Store|null $store */ public static function log($text, $params = array(), $store = null) { @@ -100,9 +137,15 @@ public static function log($text, $params = array(), $store = null) $logger->notice($text, $params); } + /** + * @param string $text + * @param string $type + * @param array $params + * @param Magento\Store\Model\Store $store + * @return void + */ public static function writeLog($text, $type, $params, $store) { - $objectManager = self::getObjectManager(); $store = self::getStore($store); $level = $store->getConfig('payment/paynl/logging_level'); @@ -110,6 +153,7 @@ public static function writeLog($text, $type, $params, $store) if (!is_array($params)) { $params = array(); } + $objectManager = self::getObjectManager(); $logger = $objectManager->get(\Paynl\Payment\Logging\Logger::class); switch ($type) { case 'critical': @@ -128,6 +172,11 @@ public static function writeLog($text, $type, $params, $store) } } + /** + * @param string $cookieName + * @param string $value + * @return void + */ public static function setCookie($cookieName, $value) { $objectManager = self::getObjectManager(); @@ -148,6 +197,10 @@ public static function setCookie($cookieName, $value) ); } + /** + * @param string $cookieName + * @return mixed + */ public static function getCookie($cookieName) { $objectManager = self::getObjectManager(); @@ -155,6 +208,10 @@ public static function getCookie($cookieName) return $cookieManager->getCookie($cookieName); } + /** + * @param string $cookieName + * @return void + */ public static function deleteCookie($cookieName) { $objectManager = self::getObjectManager(); @@ -170,8 +227,8 @@ public static function deleteCookie($cookieName) /** * Checks if new-ppt is already processing, mark as processing if not marked already * - * @param $payOrderId - * @return bool + * @param string $payOrderId + * @return boolean */ public function checkProcessing($payOrderId) { @@ -199,7 +256,7 @@ public function checkProcessing($payOrderId) /** * Removes processing mark after new-ppt is finished * - * @param $payOrderId + * @param string $payOrderId */ public function removeProcessing($payOrderId) { @@ -211,18 +268,24 @@ public function removeProcessing($payOrderId) ); } + /** + * @return false|string + */ public function getClientIp() { return $this->remoteAddress->getRemoteAddress(); } + /** + * @return string + */ public function getHttpUserAgent() { return $this->httpHeader->getHttpUserAgent(); } /** - * @param $exceptionMessage + * @param string $exceptionMessage * @return \Magento\Framework\Phrase */ public static function getFriendlyMessage($exceptionMessage) @@ -231,8 +294,7 @@ public static function getFriendlyMessage($exceptionMessage) if (stripos($exceptionMessage, 'minimum amount') !== false) { $strMessage = __('Unfortunately the order amount does not fit the requirements for this payment method.'); - } - elseif (stripos($exceptionMessage, 'not enabled for this service') !== false) { + } elseif (stripos($exceptionMessage, 'not enabled for this service') !== false) { $strMessage = __('The selected payment method is not enabled. Please select another payment method.'); } else { $strMessage = __('Unfortunately something went wrong.'); @@ -260,5 +322,4 @@ public static function genderConversion($gender) } return $gender; } - } diff --git a/Model/PayPaymentCreate.php b/Model/PayPaymentCreate.php index f321f6f7..a3d1de37 100644 --- a/Model/PayPaymentCreate.php +++ b/Model/PayPaymentCreate.php @@ -121,7 +121,7 @@ public function create() /** * @param integer $paymentMethodId Setting the method, like iDEAL or banktransfer... - * @return void + * @return $this */ public function setPaymentMethod($paymentMethodId) { @@ -130,8 +130,8 @@ public function setPaymentMethod($paymentMethodId) } /** - * @param $cocNumber - * @return void + * @param string $cocNumber + * @return $this */ public function setCocNumber($cocNumber) { @@ -140,7 +140,7 @@ public function setCocNumber($cocNumber) } /** - * @param $vatNumber + * @param string $vatNumber * @return $this */ public function setVatNumber($vatNumber) @@ -150,8 +150,8 @@ public function setVatNumber($vatNumber) } /** - * @param $issuer - * @return void + * @param string $issuer + * @return $this */ public function setIssuer($issuer) { @@ -180,7 +180,7 @@ public function setExchangeURL(string $exchangeURL) } /** - * @param int $valid_days + * @param integer $valid_days * @return $this * @throws \Exception */ @@ -193,7 +193,7 @@ public function setExpireData(int $valid_days) } /** - * @param $amount + * @param integer $amount * @return $this */ public function setAmount($amount) @@ -212,12 +212,18 @@ public function setCurrency($currency) return $this; } + /** + * @return float|string|null + */ private function getDescription() { $prefix = $this->scopeConfig->getValue('payment/paynl/order_description_prefix', 'store'); return !empty($prefix) ? $prefix . $this->orderId : $this->orderId; } + /** + * @return array + */ public function getData() { $shippingAddress = $this->getShippingAddress(); @@ -261,6 +267,9 @@ public function getData() return $data; } + /** + * @return float|string|null + */ private function getIpAddress() { $ipAddress = $this->order->getRemoteIp(); @@ -272,11 +281,17 @@ private function getIpAddress() return $ipAddress; } + /** + * @return bool + */ private function useBillingAddressInstorePickup() { return $this->scopeConfig->getValue('payment/' . $this->methodInstance->getCode() . '/useBillingAddressInstorePickup', 'store') == 1; } + /** + * @return array + */ private function getShippingAddress() { $shippingAddress = null; @@ -309,6 +324,9 @@ private function getShippingAddress() return $shippingAddress; } + /** + * @return array + */ private function getEnduserData() { $arrBillingAddress = $this->order->getBillingAddress(); @@ -348,6 +366,9 @@ private function getEnduserData() return $enduser; } + /** + * @return array + */ private function getInvoiceAddress() { $arrBillingAddress = $this->order->getBillingAddress(); @@ -372,6 +393,9 @@ private function getInvoiceAddress() return $invoiceAddress; } + /** + * @return array + */ private function getProductData() { $arrProducts = []; @@ -531,5 +555,4 @@ private function getProductData() return $arrProducts; } - } From 00fc07dce12886efed8df7833b2666fa48b9b3c3 Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 8 Apr 2023 21:17:04 +0200 Subject: [PATCH 04/16] Code polish --- Helper/PayHelper.php | 14 ++++++++------ Model/PayPaymentCreate.php | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Helper/PayHelper.php b/Helper/PayHelper.php index 43a4e224..7d85669f 100644 --- a/Helper/PayHelper.php +++ b/Helper/PayHelper.php @@ -10,7 +10,7 @@ class PayHelper extends \Magento\Framework\App\Helper\AbstractHelper { - const PAY_LOG_PREFIX = 'PAY.: '; + public const PAY_LOG_PREFIX = 'PAY.: '; private static $objectManager; private static $store; @@ -43,10 +43,10 @@ public static function getObjectManager() } /** - * @param Magento\Store\Model\Store $store + * @param \Magento\Store\Model\Store $store * @return mixed */ - public static function getStore($store) + public static function getStore(\Magento\Store\Model\Store $store) { if (empty($store)) { if (empty(self::$store)) { @@ -62,7 +62,7 @@ public static function getStore($store) /** * @param integer $level * @param string $type - * @return bool + * @return boolean */ public static function hasCorrectLevel($level, $type) { @@ -91,8 +91,8 @@ public static function logCritical($text, $params = array(), $store = null) } /** - * @param $text - * @param $params + * @param string $text + * @param array $params * @param Magento\Store\Model\Store $store * @return void */ @@ -129,6 +129,7 @@ public static function logDebug($text, $params = array(), $store = null) * @param string $text * @param array $params * @param Magento\Store\Model\Store|null $store + * @return void */ public static function log($text, $params = array(), $store = null) { @@ -257,6 +258,7 @@ public function checkProcessing($payOrderId) * Removes processing mark after new-ppt is finished * * @param string $payOrderId + * @return void */ public function removeProcessing($payOrderId) { diff --git a/Model/PayPaymentCreate.php b/Model/PayPaymentCreate.php index a3d1de37..6f24e55a 100644 --- a/Model/PayPaymentCreate.php +++ b/Model/PayPaymentCreate.php @@ -203,7 +203,7 @@ public function setAmount($amount) } /** - * @param string currency Currency code + * @param string $currency Currency code * @return $this */ public function setCurrency($currency) @@ -282,7 +282,7 @@ private function getIpAddress() } /** - * @return bool + * @return boolean */ private function useBillingAddressInstorePickup() { From c8bdf436208be62357194b383b434f4f564bb219 Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 8 Apr 2023 21:25:36 +0200 Subject: [PATCH 05/16] Code polish --- Helper/PayHelper.php | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/Helper/PayHelper.php b/Helper/PayHelper.php index 7d85669f..959acecb 100644 --- a/Helper/PayHelper.php +++ b/Helper/PayHelper.php @@ -82,10 +82,10 @@ public static function hasCorrectLevel($level, $type) /** * @param string $text * @param array $params - * @param Magento\Store\Model\Store $store + * @param \Magento\Store\Model\Store $store * @return void */ - public static function logCritical($text, $params = array(), $store = null) + public static function logCritical($text, array $params = array(), \Magento\Store\Model\Store $store = null) { self::writeLog($text, 'critical', $params, $store); } @@ -93,10 +93,10 @@ public static function logCritical($text, $params = array(), $store = null) /** * @param string $text * @param array $params - * @param Magento\Store\Model\Store $store + * @param \Magento\Store\Model\Store $store * @return void */ - public static function logNotice($text, $params = array(), $store = null) + public static function logNotice($text, $params = array(), \Magento\Store\Model\Store $store = null) { self::writeLog($text, 'notice', $params, $store); } @@ -104,10 +104,10 @@ public static function logNotice($text, $params = array(), $store = null) /** * @param string $text * @param array $params - * @param Magento\Store\Model\Store $store + * @param \Magento\Store\Model\Store $store * @return void */ - public static function logInfo($text, $params = array(), $store = null) + public static function logInfo($text, $params = array(), \Magento\Store\Model\Store $store = null) { self::writeLog($text, 'info', $params, $store); } @@ -115,10 +115,10 @@ public static function logInfo($text, $params = array(), $store = null) /** * @param string $text * @param array $params - * @param Magento\Store\Model\Store $store + * @param \Magento\Store\Model\Store $store * @return void */ - public static function logDebug($text, $params = array(), $store = null) + public static function logDebug($text, $params = array(), \Magento\Store\Model\Store $store = null) { self::writeLog($text, 'debug', $params, $store); } @@ -128,10 +128,10 @@ public static function logDebug($text, $params = array(), $store = null) * * @param string $text * @param array $params - * @param Magento\Store\Model\Store|null $store + * @param \Magento\Store\Model\Store |null $store * @return void */ - public static function log($text, $params = array(), $store = null) + public static function log($text, $params = array(), \Magento\Store\Model\Store $store = null) { $objectManager = self::getObjectManager(); $logger = $objectManager->get(\Paynl\Payment\Logging\Logger::class); @@ -142,10 +142,10 @@ public static function log($text, $params = array(), $store = null) * @param string $text * @param string $type * @param array $params - * @param Magento\Store\Model\Store $store + * @param \Magento\Store\Model\Store $store * @return void */ - public static function writeLog($text, $type, $params, $store) + public static function writeLog($text, $type, $params, \Magento\Store\Model\Store $store) { $store = self::getStore($store); $level = $store->getConfig('payment/paynl/logging_level'); @@ -213,6 +213,11 @@ public static function getCookie($cookieName) * @param string $cookieName * @return void */ + /** + * @param $cookieName + * @return void + * // phpcs:ignore + */ public static function deleteCookie($cookieName) { $objectManager = self::getObjectManager(); From d2636e4efe093c0cca2570eeb3243ce6f032bbc6 Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 8 Apr 2023 21:29:48 +0200 Subject: [PATCH 06/16] Code polish --- Helper/PayHelper.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Helper/PayHelper.php b/Helper/PayHelper.php index 959acecb..7218c923 100644 --- a/Helper/PayHelper.php +++ b/Helper/PayHelper.php @@ -96,7 +96,7 @@ public static function logCritical($text, array $params = array(), \Magento\Stor * @param \Magento\Store\Model\Store $store * @return void */ - public static function logNotice($text, $params = array(), \Magento\Store\Model\Store $store = null) + public static function logNotice($text, array $params = array(), \Magento\Store\Model\Store $store = null) { self::writeLog($text, 'notice', $params, $store); } @@ -107,7 +107,7 @@ public static function logNotice($text, $params = array(), \Magento\Store\Model\ * @param \Magento\Store\Model\Store $store * @return void */ - public static function logInfo($text, $params = array(), \Magento\Store\Model\Store $store = null) + public static function logInfo($text, array $params = array(), \Magento\Store\Model\Store $store = null) { self::writeLog($text, 'info', $params, $store); } @@ -118,7 +118,7 @@ public static function logInfo($text, $params = array(), \Magento\Store\Model\St * @param \Magento\Store\Model\Store $store * @return void */ - public static function logDebug($text, $params = array(), \Magento\Store\Model\Store $store = null) + public static function logDebug($text, array $params = array(), \Magento\Store\Model\Store $store = null) { self::writeLog($text, 'debug', $params, $store); } @@ -131,7 +131,7 @@ public static function logDebug($text, $params = array(), \Magento\Store\Model\S * @param \Magento\Store\Model\Store |null $store * @return void */ - public static function log($text, $params = array(), \Magento\Store\Model\Store $store = null) + public static function log($text, array $params = array(), \Magento\Store\Model\Store $store = null) { $objectManager = self::getObjectManager(); $logger = $objectManager->get(\Paynl\Payment\Logging\Logger::class); @@ -145,7 +145,7 @@ public static function log($text, $params = array(), \Magento\Store\Model\Store * @param \Magento\Store\Model\Store $store * @return void */ - public static function writeLog($text, $type, $params, \Magento\Store\Model\Store $store) + public static function writeLog($text, $type, array $params, \Magento\Store\Model\Store $store) { $store = self::getStore($store); $level = $store->getConfig('payment/paynl/logging_level'); @@ -212,10 +212,6 @@ public static function getCookie($cookieName) /** * @param string $cookieName * @return void - */ - /** - * @param $cookieName - * @return void * // phpcs:ignore */ public static function deleteCookie($cookieName) From 66b1e0779af3bcfba5e7b5dadf9f890b2f2e44bc Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 8 Apr 2023 21:39:38 +0200 Subject: [PATCH 07/16] Code polish --- Helper/PayHelper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Helper/PayHelper.php b/Helper/PayHelper.php index 7218c923..f348cde0 100644 --- a/Helper/PayHelper.php +++ b/Helper/PayHelper.php @@ -211,8 +211,8 @@ public static function getCookie($cookieName) /** * @param string $cookieName + * @phpcs:disable PSR12.Functions.ReturnTypeDeclaration * @return void - * // phpcs:ignore */ public static function deleteCookie($cookieName) { @@ -222,6 +222,7 @@ public static function deleteCookie($cookieName) if ($cookieManager->getCookie($cookieName)) { $metadata = $cookieMetadataFactory->createPublicCookieMetadata(); $metadata->setPath('/'); + // phpcs:ignore return $cookieManager->deleteCookie($cookieName, $metadata); } } From cb807f9d6e7012fb54d01855c60eac14019b7cbd Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 8 Apr 2023 21:43:39 +0200 Subject: [PATCH 08/16] Code polish --- Helper/PayHelper.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Helper/PayHelper.php b/Helper/PayHelper.php index f348cde0..5cfdddbc 100644 --- a/Helper/PayHelper.php +++ b/Helper/PayHelper.php @@ -212,7 +212,6 @@ public static function getCookie($cookieName) /** * @param string $cookieName * @phpcs:disable PSR12.Functions.ReturnTypeDeclaration - * @return void */ public static function deleteCookie($cookieName) { @@ -222,8 +221,7 @@ public static function deleteCookie($cookieName) if ($cookieManager->getCookie($cookieName)) { $metadata = $cookieMetadataFactory->createPublicCookieMetadata(); $metadata->setPath('/'); - // phpcs:ignore - return $cookieManager->deleteCookie($cookieName, $metadata); + return $cookieManager->deleteCookie($cookieName, $metadata); // phpcs:ignore } } From a3aafa8f552087a44a3ab5c22ce146cc856bf96e Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 8 Apr 2023 21:46:27 +0200 Subject: [PATCH 09/16] Code polish --- Helper/PayHelper.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Helper/PayHelper.php b/Helper/PayHelper.php index 5cfdddbc..7682789b 100644 --- a/Helper/PayHelper.php +++ b/Helper/PayHelper.php @@ -212,6 +212,7 @@ public static function getCookie($cookieName) /** * @param string $cookieName * @phpcs:disable PSR12.Functions.ReturnTypeDeclaration + * @phpcs:disable PEAR.Commenting.FunctionComment.MissingReturn */ public static function deleteCookie($cookieName) { From f1345c70655534941bcb8d7a9fd5a3a6fb9e8bd3 Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 8 Apr 2023 21:48:50 +0200 Subject: [PATCH 10/16] Code polish --- Helper/PayHelper.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Helper/PayHelper.php b/Helper/PayHelper.php index 7682789b..2cdca5ea 100644 --- a/Helper/PayHelper.php +++ b/Helper/PayHelper.php @@ -213,6 +213,7 @@ public static function getCookie($cookieName) * @param string $cookieName * @phpcs:disable PSR12.Functions.ReturnTypeDeclaration * @phpcs:disable PEAR.Commenting.FunctionComment.MissingReturn + * @return void|mixed */ public static function deleteCookie($cookieName) { From e5c7cff4da6582e4dd6ffac39a2e3b024f715aae Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 8 Apr 2023 23:02:53 +0200 Subject: [PATCH 11/16] Updated instore and paylink --- Helper/PayHelper.php | 12 ++++----- Model/Paymentmethod/Instore.php | 46 ++++++++++++++++++++++++++++++--- Model/Paymentmethod/Paylink.php | 26 ++++++++++++++++++- 3 files changed, 73 insertions(+), 11 deletions(-) diff --git a/Helper/PayHelper.php b/Helper/PayHelper.php index 2cdca5ea..c5bed95f 100644 --- a/Helper/PayHelper.php +++ b/Helper/PayHelper.php @@ -43,10 +43,10 @@ public static function getObjectManager() } /** - * @param \Magento\Store\Model\Store $store - * @return mixed + * @param \Magento\Store\Model\Store|null $store + * @return \Magento\Store\Model\Store|mixed */ - public static function getStore(\Magento\Store\Model\Store $store) + public static function getStore(\Magento\Store\Model\Store $store = null) { if (empty($store)) { if (empty(self::$store)) { @@ -115,7 +115,7 @@ public static function logInfo($text, array $params = array(), \Magento\Store\Mo /** * @param string $text * @param array $params - * @param \Magento\Store\Model\Store $store + * @param \Magento\Store\Model\Store|null $store * @return void */ public static function logDebug($text, array $params = array(), \Magento\Store\Model\Store $store = null) @@ -142,10 +142,10 @@ public static function log($text, array $params = array(), \Magento\Store\Model\ * @param string $text * @param string $type * @param array $params - * @param \Magento\Store\Model\Store $store + * @param \Magento\Store\Model\Store|null $store * @return void */ - public static function writeLog($text, $type, array $params, \Magento\Store\Model\Store $store) + public static function writeLog($text, $type, array $params, \Magento\Store\Model\Store $store = null) { $store = self::getStore($store); $level = $store->getConfig('payment/paynl/logging_level'); diff --git a/Model/Paymentmethod/Instore.php b/Model/Paymentmethod/Instore.php index 1504bc1e..0838b88d 100644 --- a/Model/Paymentmethod/Instore.php +++ b/Model/Paymentmethod/Instore.php @@ -3,7 +3,8 @@ namespace Paynl\Payment\Model\Paymentmethod; use Magento\Sales\Model\Order; -use \Paynl\Payment\Helper\PayHelper; +use Paynl\Payment\Helper\PayHelper; +use Paynl\Payment\Model\PayPaymentCreate; class Instore extends PaymentMethod { @@ -16,11 +17,20 @@ class Instore extends PaymentMethod */ protected $_formBlockType = \Paynl\Payment\Block\Form\Instore::class; + /** + * @return integer + */ protected function getDefaultPaymentOptionId() { return 1729; } + /** + * @param string $paymentAction + * @param object $stateObject + * @return object|void + * @throws \Magento\Framework\Exception\LocalizedException + */ public function initialize($paymentAction, $stateObject) { if ($paymentAction == 'order') { @@ -40,6 +50,12 @@ public function initialize($paymentAction, $stateObject) } } + /** + * @param Order $order + * @param boolean $fromAdmin + * @return string|void + * @throws \Exception + */ public function startTransaction(Order $order, $fromAdmin = false) { $store = $order->getStore(); @@ -56,7 +72,8 @@ public function startTransaction(Order $order, $fromAdmin = false) if (empty($terminalId)) { throw new \Exception(__('Please select a pin-terminal'), 201); } - $transaction = $this->doStartTransaction($order); + + $transaction = (new PayPaymentCreate($order, $this))->create(); $instorePayment = \Paynl\Instore::payment(['transactionId' => $transaction->getTransactionId(), 'terminalId' => $terminalId]); @@ -71,13 +88,13 @@ public function startTransaction(Order $order, $fromAdmin = false) } catch (\Exception $e) { payHelper::logCritical($e->getMessage(), [], $store); - if ($e->getCode() == 201) { + if ($e->getCode() == 201) { if ($fromAdmin) { throw new \Exception(__($e->getMessage())); } else { $this->messageManager->addNoticeMessage($e->getMessage()); } - } else { + } else { if ($fromAdmin) { throw new \Exception(__('Pin transaction could not be started')); } else { @@ -89,6 +106,11 @@ public function startTransaction(Order $order, $fromAdmin = false) return $url; } + /** + * @param \Magento\Framework\DataObject $data + * @return $this|object + * @throws \Magento\Framework\Exception\LocalizedException + */ public function assignData(\Magento\Framework\DataObject $data) { parent::assignData($data); @@ -105,6 +127,10 @@ public function assignData(\Magento\Framework\DataObject $data) return $this; } + /** + * @return array|false|mixed + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ public function getPaymentOptions() { $cache = $this->getCache(); @@ -148,6 +174,9 @@ public function getPaymentOptions() return $terminalsArr; } + /** + * @return integer|mixed + */ public function hidePaymentOptions() { if (!empty($this->getDefaultPaymentOption()) && $this->getDefaultPaymentOption() != '0') { @@ -156,6 +185,9 @@ public function hidePaymentOptions() return 0; } + /** + * @return mixed + */ public function getDefaultPaymentOption() { return $this->_scopeConfig->getValue('payment/' . $this->_code . '/default_terminal', 'store'); @@ -173,6 +205,9 @@ private function getCache() return $cache; } + /** + * @return boolean + */ public function isCurrentIpValid() { $onlyAllowedIPs = $this->_scopeConfig->getValue('payment/' . $this->_code . '/exclusiveforipaddress', 'store'); @@ -184,6 +219,9 @@ public function isCurrentIpValid() return in_array($this->helper->getClientIp(), explode(",", $onlyAllowedIPs)); } + /** + * @return boolean + */ public function isCurrentAgentValid() { $specifiedUserAgent = $this->_scopeConfig->getValue('payment/' . $this->_code . '/exclusiveforuseragent', 'store'); diff --git a/Model/Paymentmethod/Paylink.php b/Model/Paymentmethod/Paylink.php index eb1316d8..2b94c4a3 100644 --- a/Model/Paymentmethod/Paylink.php +++ b/Model/Paymentmethod/Paylink.php @@ -5,11 +5,15 @@ use Magento\Sales\Model\Order; use Magento\Framework\DataObject; use Paynl\Payment\Helper\PayHelper; +use Paynl\Payment\Model\PayPaymentCreate; class Paylink extends PaymentMethod { protected $_code = 'paynl_payment_paylink'; + /** + * @return integer + */ protected function getDefaultPaymentOptionId() { return 961; @@ -25,6 +29,15 @@ protected function getDefaultPaymentOptionId() // this is an admin only method protected $_canUseCheckout = false; + /** + * @param string $paymentAction + * @param object $stateObject + * @return false|void + * @throws \Magento\Framework\Exception\AlreadyExistsException + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ public function initialize($paymentAction, $stateObject) { if ($paymentAction == 'order') { @@ -32,7 +45,7 @@ public function initialize($paymentAction, $stateObject) $order = $this->getInfoInstance()->getOrder(); $this->orderRepository->save($order); - $transaction = $this->doStartTransaction($order); + $transaction = (new PayPaymentCreate($order, $this))->create(); $order->getPayment()->setAdditionalInformation('transactionId', $transaction->getTransactionId()); @@ -160,6 +173,12 @@ public function initialize($paymentAction, $stateObject) } } + /** + * @param string $order + * @param string $url + * @param string $status + * @return void + */ public function addPaylinkComment($order, $url, $status) { $paylinktext = __('PAY.: Here is your '); @@ -167,6 +186,11 @@ public function addPaylinkComment($order, $url, $status) $order->addStatusHistoryComment($paylinktext . 'PAY. Link. ' . $postText, $status)->save(); } + /** + * @param DataObject $data + * @return object + * @throws \Magento\Framework\Exception\LocalizedException + */ public function assignData(\Magento\Framework\DataObject $data) { $this->getInfoInstance()->setAdditionalInformation('valid_days', $data->getData('additional_data')['valid_days']); From be0e0d8462a7132647771c78a8a5f21618d95dc5 Mon Sep 17 00:00:00 2001 From: woutse Date: Sat, 8 Apr 2023 23:06:34 +0200 Subject: [PATCH 12/16] Code polish --- Model/Paymentmethod/Instore.php | 2 +- Model/Paymentmethod/Paylink.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Model/Paymentmethod/Instore.php b/Model/Paymentmethod/Instore.php index 0838b88d..9736ae18 100644 --- a/Model/Paymentmethod/Instore.php +++ b/Model/Paymentmethod/Instore.php @@ -31,7 +31,7 @@ protected function getDefaultPaymentOptionId() * @return object|void * @throws \Magento\Framework\Exception\LocalizedException */ - public function initialize($paymentAction, $stateObject) + public function initialize($paymentAction, object $stateObject) { if ($paymentAction == 'order') { /** @var Order $order */ diff --git a/Model/Paymentmethod/Paylink.php b/Model/Paymentmethod/Paylink.php index 2b94c4a3..7b762a5b 100644 --- a/Model/Paymentmethod/Paylink.php +++ b/Model/Paymentmethod/Paylink.php @@ -38,7 +38,7 @@ protected function getDefaultPaymentOptionId() * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException */ - public function initialize($paymentAction, $stateObject) + public function initialize($paymentAction, object $stateObject) { if ($paymentAction == 'order') { /** @var Order $order */ @@ -187,7 +187,7 @@ public function addPaylinkComment($order, $url, $status) } /** - * @param DataObject $data + * @param \Magento\Framework\DataObject $data * @return object * @throws \Magento\Framework\Exception\LocalizedException */ From f666d9237e411a07da2dda5d4b942ed27fbbc54e Mon Sep 17 00:00:00 2001 From: woutse Date: Thu, 20 Apr 2023 11:09:06 +0200 Subject: [PATCH 13/16] Removed typehint --- Model/Paymentmethod/Instore.php | 5 +++-- Model/Paymentmethod/Paylink.php | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Model/Paymentmethod/Instore.php b/Model/Paymentmethod/Instore.php index 9736ae18..629d8a9c 100644 --- a/Model/Paymentmethod/Instore.php +++ b/Model/Paymentmethod/Instore.php @@ -27,11 +27,12 @@ protected function getDefaultPaymentOptionId() /** * @param string $paymentAction - * @param object $stateObject + * @param object $stateObjects + * @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing * @return object|void * @throws \Magento\Framework\Exception\LocalizedException */ - public function initialize($paymentAction, object $stateObject) + public function initialize($paymentAction, $stateObject) { if ($paymentAction == 'order') { /** @var Order $order */ diff --git a/Model/Paymentmethod/Paylink.php b/Model/Paymentmethod/Paylink.php index 7b762a5b..270710e0 100644 --- a/Model/Paymentmethod/Paylink.php +++ b/Model/Paymentmethod/Paylink.php @@ -37,8 +37,9 @@ protected function getDefaultPaymentOptionId() * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException + * @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing */ - public function initialize($paymentAction, object $stateObject) + public function initialize($paymentAction, $stateObject) { if ($paymentAction == 'order') { /** @var Order $order */ From 46d4a07b2a4b7c2ee6a82345a4ee51ce56ef9c25 Mon Sep 17 00:00:00 2001 From: woutse Date: Thu, 20 Apr 2023 11:58:13 +0200 Subject: [PATCH 14/16] Removed typehint --- Model/Paymentmethod/Instore.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Paymentmethod/Instore.php b/Model/Paymentmethod/Instore.php index 629d8a9c..b9f64db5 100644 --- a/Model/Paymentmethod/Instore.php +++ b/Model/Paymentmethod/Instore.php @@ -27,7 +27,7 @@ protected function getDefaultPaymentOptionId() /** * @param string $paymentAction - * @param object $stateObjects + * @param object $stateObject * @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing * @return object|void * @throws \Magento\Framework\Exception\LocalizedException From 3e843f9eeb323625f7d50592554f5beacfca5139 Mon Sep 17 00:00:00 2001 From: woutse Date: Mon, 1 May 2023 12:12:29 +0200 Subject: [PATCH 15/16] Removed legacy --- Model/Paymentmethod/PaymentMethod.php | 339 -------------------------- 1 file changed, 339 deletions(-) diff --git a/Model/Paymentmethod/PaymentMethod.php b/Model/Paymentmethod/PaymentMethod.php index 41fe6e77..6bbfc043 100644 --- a/Model/Paymentmethod/PaymentMethod.php +++ b/Model/Paymentmethod/PaymentMethod.php @@ -465,345 +465,6 @@ public function startTransaction(Order $order) return $transaction->getRedirectUrl(); } - /** - * @param Order $order - * @param integer $multipleOrderAmount - * @param string $finishUrl - * @return \Paynl\Result\Transaction\Start - * @throws \Paynl\Error\Api - * @throws \Paynl\Error\Error - * @throws \Paynl\Error\Required\ApiToken - * @throws \Paynl\Error\Required\ServiceId - */ - protected function doStartTransactionOld(Order $order, $multipleOrderAmount = null, $finishUrl = null) - { - $this->paynlConfig->setStore($order->getStore()); - $this->paynlConfig->configureSDK(); - $additionalData = $order->getPayment()->getAdditionalInformation(); - $paymentOption = null; - $expireDate = null; - - if (isset($additionalData['kvknummer']) && is_numeric($additionalData['kvknummer'])) { - $kvknummer = $additionalData['kvknummer']; - } - if (isset($additionalData['vatnumber'])) { - $vatnumber = $additionalData['vatnumber']; - } - if (isset($additionalData['payment_option']) && is_numeric($additionalData['payment_option'])) { - $paymentOption = $additionalData['payment_option']; - } - if (isset($additionalData['valid_days']) && is_numeric($additionalData['valid_days'])) { - $expireDate = new \DateTime('+' . $additionalData['valid_days'] . ' days'); - } - - if ($this->paynlConfig->isAlwaysBaseCurrency()) { - $total = $order->getBaseGrandTotal(); - $currency = $order->getBaseCurrencyCode(); - } else { - $total = $order->getGrandTotal(); - $currency = $order->getOrderCurrencyCode(); - } - - if (!is_null($multipleOrderAmount)) { - $total = $multipleOrderAmount; - } - - $orderId = $order->getIncrementId(); - $quoteId = $order->getQuoteId(); - - $store = $order->getStore(); - $baseUrl = $store->getBaseUrl(); - - $returnUrl = $additionalData['returnUrl'] ?? $baseUrl . 'paynl/checkout/finish/?entityid=' . $order->getEntityId(); - $exchangeUrl = $additionalData['exchangeUrl'] ?? $baseUrl . 'paynl/checkout/exchange/'; - - if (!empty($finishUrl)) { - $returnUrl = $finishUrl; - } - - $paymentOptionId = $this->getPaymentOptionId(); - - $arrBillingAddress = $order->getBillingAddress(); - if ($arrBillingAddress) { - $arrBillingAddress = $arrBillingAddress->toArray(); - - $enduser = [ - 'initials' => $arrBillingAddress['firstname'], - 'lastName' => $arrBillingAddress['lastname'], - 'phoneNumber' => $arrBillingAddress['telephone'], - 'emailAddress' => $arrBillingAddress['email'], - ]; - - if (isset($additionalData['dob'])) { - $enduser['dob'] = $additionalData['dob']; - } - - if (isset($additionalData['gender'])) { - $enduser['gender'] = $additionalData['gender']; - } - $enduser['gender'] = $this->genderConversion((empty($enduser['gender'])) ? $order->getCustomerGender($order) : $enduser['gender']); - - if (!empty($arrBillingAddress['company'])) { - $enduser['company']['name'] = $arrBillingAddress['company']; - } - - if (!empty($arrBillingAddress['country_id'])) { - $enduser['company']['countryCode'] = $arrBillingAddress['country_id']; - } - - if (!empty($kvknummer)) { - $enduser['company']['cocNumber'] = $kvknummer; - } - - if (!empty($arrBillingAddress['vat_id'])) { - $enduser['company']['vatNumber'] = $arrBillingAddress['vat_id']; - } elseif (!empty($vatnumber)) { - $enduser['company']['vatNumber'] = $vatnumber; - } - - $invoiceAddress = [ - 'initials' => $arrBillingAddress['firstname'], - 'lastName' => $arrBillingAddress['lastname'], - ]; - - $arrAddress = \Paynl\Helper::splitAddress($arrBillingAddress['street']); - $invoiceAddress['streetName'] = $arrAddress[0]; - $invoiceAddress['houseNumber'] = $arrAddress[1]; - $invoiceAddress['zipCode'] = $arrBillingAddress['postcode']; - $invoiceAddress['city'] = $arrBillingAddress['city']; - $invoiceAddress['country'] = $arrBillingAddress['country_id']; - - if (!empty($arrShippingAddress['vat_id'])) { - $enduser['company']['vatNumber'] = $arrShippingAddress['vat_id']; - } - } - - $arrShippingAddress = $order->getShippingAddress(); - if (!empty($arrShippingAddress)) { - $arrShippingAddress = $arrShippingAddress->toArray(); - - if ($this->useBillingAddressInstorePickup() && class_exists('InStorePickup')) { - if ($order->getShippingMethod() === InStorePickup::DELIVERY_METHOD) { - $arrBillingAddress = $order->getBillingAddress(); - if (!empty($arrBillingAddress)) { - $arrShippingAddress = $arrBillingAddress->toArray(); - } - } - } - - $shippingAddress = [ - 'initials' => $arrShippingAddress['firstname'], - 'lastName' => $arrShippingAddress['lastname'], - ]; - $arrAddress2 = \Paynl\Helper::splitAddress($arrShippingAddress['street']); - $shippingAddress['streetName'] = $arrAddress2[0]; - $shippingAddress['houseNumber'] = $arrAddress2[1]; - $shippingAddress['zipCode'] = $arrShippingAddress['postcode']; - $shippingAddress['city'] = $arrShippingAddress['city']; - $shippingAddress['country'] = $arrShippingAddress['country_id']; - } - - $prefix = $this->_scopeConfig->getValue('payment/paynl/order_description_prefix', 'store'); - $description = !empty($prefix) ? $prefix . $orderId : $orderId; - - $data = [ - 'amount' => $total, - 'returnUrl' => $returnUrl, - 'paymentMethod' => $paymentOptionId, - 'language' => $this->paynlConfig->getLanguage(), - 'bank' => $paymentOption, - 'expireDate' => $expireDate, - 'orderNumber' => $orderId, - 'description' => $description, - 'extra1' => $orderId, - 'extra2' => $quoteId, - 'extra3' => $order->getEntityId(), - 'transferData' => $this->getTransferData(), - 'exchangeUrl' => $exchangeUrl, - 'currency' => $currency, - 'object' => $this->getVersion(), - ]; - if (isset($shippingAddress)) { - $data['address'] = $shippingAddress; - } - if (isset($invoiceAddress)) { - $data['invoiceAddress'] = $invoiceAddress; - } - if (isset($enduser)) { - $data['enduser'] = $enduser; - } - $arrProducts = []; - $arrWEEETax = []; - $items = $order->getAllVisibleItems(); - foreach ($items as $item) { - $arrItem = $item->toArray(); - if ($arrItem['price_incl_tax'] != null) { - // taxamount is not valid, because on discount it returns the taxamount after discount - $taxAmount = $arrItem['price_incl_tax'] - $arrItem['price']; - $price = $arrItem['price_incl_tax']; - - if ($this->paynlConfig->isAlwaysBaseCurrency()) { - $taxAmount = $arrItem['base_price_incl_tax'] - $arrItem['base_price']; - $price = $arrItem['base_price_incl_tax']; - } - - $productId = $arrItem['product_id']; - if ($this->paynlConfig->useSkuId()) { - $productId = $arrItem['sku']; - } - - $product = [ - 'id' => $productId, - 'name' => $arrItem['name'], - 'price' => $price, - 'qty' => $arrItem['qty_ordered'], - 'tax' => $taxAmount, - 'type' => \Paynl\Transaction::PRODUCT_TYPE_ARTICLE, - ]; - - # Product id's must be unique. Combinations of a "Configurable products" share the same product id. - # Each combination of a "configurable product" can be represented by a "simple product". - # The first and only child of the "configurable product" is the "simple product", or combination, chosen by the customer. - # Grab it and replace the product id to guarantee product id uniqueness. - if (isset($arrItem['product_type']) && $arrItem['product_type'] === Configurable::TYPE_CODE) { - $children = $item->getChildrenItems(); - $child = array_shift($children); - - if (!empty($child) && $child instanceof \Magento\Sales\Model\Order\Item && method_exists($child, 'getProductId')) { - $productIdChild = $child->getProductId(); - if ($this->paynlConfig->useSkuId() && method_exists($child, 'getSku')) { - $productIdChild = $child->getSku(); - } - $product['id'] = $productIdChild; - } - } - - $arrProducts[] = $product; - - # WEEE - if (!empty($arrItem['weee_tax_applied'])) { - $weeeArr = json_decode($arrItem['weee_tax_applied']); - if (is_array($weeeArr)) { - foreach ($weeeArr as $weee) { - if (!empty($weee) && is_object($weee)) { - $weee_title = $weee->title; - $weee_price = $weee->row_amount_incl_tax; - $weee_taxAmount = $weee->row_amount_incl_tax - $weee->row_amount; - - if ($this->paynlConfig->isAlwaysBaseCurrency()) { - $weee_price = $weee->base_row_amount_incl_tax; - $weee_taxAmount = $weee->base_row_amount_incl_tax - $weee->base_row_amount; - } - - if (isset($arrWEEETax[$weee_title])) { - $arrWEEETax[$weee_title]['price'] += $weee_price; - $arrWEEETax[$weee_title]['tax'] += $weee_taxAmount; - } else { - $arrWEEETax[$weee_title] = array( - 'id' => 'weee', - 'name' => $weee_title, - 'price' => $weee_price, - 'tax' => $weee_taxAmount, - 'qty' => 1, - 'type' => \Paynl\Transaction::PRODUCT_TYPE_HANDLING, - ); - } - } - } - } - } - } - } - - //shipping - $shippingCost = $order->getShippingInclTax(); - $shippingTax = $order->getShippingTaxAmount(); - - if ($this->paynlConfig->isAlwaysBaseCurrency()) { - $shippingCost = $order->getBaseShippingInclTax(); - $shippingTax = $order->getBaseShippingTaxAmount(); - } - - $shippingDescription = $order->getShippingDescription(); - - if ($shippingCost != 0) { - $arrProducts[] = [ - 'id' => 'shipping', - 'name' => empty($shippingDescription) ? 'Shipping' : $shippingDescription, - 'price' => $shippingCost, - 'qty' => 1, - 'tax' => $shippingTax, - 'type' => \Paynl\Transaction::PRODUCT_TYPE_SHIPPING, - ]; - } - - // Gift Wrapping - $gwCost = $order->getGwPriceInclTax(); - $gwTax = $order->getGwTaxAmount(); - - if ($this->paynlConfig->isAlwaysBaseCurrency()) { - $gwCost = $order->getGwBasePriceInclTax(); - $gwTax = $order->getGwBaseTaxAmount(); - } - - if ($gwCost != 0) { - $arrProducts[] = [ - 'id' => $order->getGwId(), - 'name' => 'Gift Wrapping', - 'price' => $gwCost, - 'qty' => 1, - 'tax' => $gwTax, - 'type' => \Paynl\Transaction::PRODUCT_TYPE_HANDLING, - ]; - } - - // kortingen - $discount = $order->getDiscountAmount(); - $discountTax = $order->getDiscountTaxCompensationAmount() * -1; - - if ($this->paynlConfig->isAlwaysBaseCurrency()) { - $discount = $order->getBaseDiscountAmount(); - $discountTax = $order->getBaseDiscountTaxCompensationAmount() * -1; - } - - if ($this->paynlConfig->isSendDiscountTax() == 0) { - $discountTax = 0; - } - - $discountDescription = __('Discount'); - - if ($discount != 0) { - $arrProducts[] = [ - 'id' => 'discount', - 'name' => $discountDescription, - 'price' => $discount, - 'qty' => 1, - 'tax' => $discountTax, - 'type' => \Paynl\Transaction::PRODUCT_TYPE_DISCOUNT, - ]; - } - - if (!empty($arrWEEETax)) { - $arrProducts = array_merge($arrProducts, $arrWEEETax); - } - - $data['products'] = $arrProducts; - - if ($this->paynlConfig->isTestMode()) { - $data['testmode'] = 1; - } - $ipAddress = $order->getRemoteIp(); - //The ip address field in magento is too short, if the ip is invalid or ip is localhost get the ip myself - if (!filter_var($ipAddress, FILTER_VALIDATE_IP) || $ipAddress == '127.0.0.1') { - $ipAddress = \Paynl\Helper::getIp(); - } - $data['ipaddress'] = $ipAddress; - - $transaction = \Paynl\Transaction::start($data); - - return $transaction; - } - /** * @return integer */ From bba4b5841e1ac4c91cdccc4542e6aeba5a3abaf9 Mon Sep 17 00:00:00 2001 From: woutse Date: Mon, 1 May 2023 13:41:36 +0200 Subject: [PATCH 16/16] Removed whitespace --- Model/Paymentmethod/PaymentMethod.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Paymentmethod/PaymentMethod.php b/Model/Paymentmethod/PaymentMethod.php index 8d40ca7e..6bbfc043 100644 --- a/Model/Paymentmethod/PaymentMethod.php +++ b/Model/Paymentmethod/PaymentMethod.php @@ -464,7 +464,7 @@ public function startTransaction(Order $order) return $transaction->getRedirectUrl(); } - + /** * @return integer */