Skip to content

Commit

Permalink
Merge pull request #89 from paynl/feature/PLUG-1674
Browse files Browse the repository at this point in the history
PLUG-1674 : Converted API errors to userfriendly messages
  • Loading branch information
woutse authored Jan 6, 2023
2 parents c408da8 + 731242b commit a3e52c2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/linters/phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</rule>
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="175"/>
<property name="lineLimit" value="200"/>
</properties>
<type>warning</type>
</rule>
Expand Down
50 changes: 42 additions & 8 deletions Model/Paymentmethod/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ public function getCustomerGroup()
return $this->_scopeConfig->getValue('payment/' . $this->_code . '/showforgroup', 'store');
}

public function shouldHoldOrder()
{
return $this->_scopeConfig->getValue('payment/' . $this->_code . '/holded', 'store') == 1;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -291,14 +296,13 @@ public function refund(InfoInterface $payment, $amount)
try {
Transaction::refund($transactionId, $amount);
} catch (\Exception $e) {

$docsLink = 'https://docs.pay.nl/plugins#magento2-errordefinitions';

$message = strtolower($e->getMessage());
if (substr($message, 0, 19) == '403 - access denied') {
$message = 'PAY. could not authorize this refund. Errorcode: PAY-MAGENTO2-001. See for more information ' . $docsLink;
} else {
$message = 'PAY. could not process this refund (' . $message . '). Errorcode: PAY-MAGENTO2-002. Transaction: '.$transactionId.'. More info: ' . $docsLink;
$message = 'PAY. could not process this refund (' . $message . '). Errorcode: PAY-MAGENTO2-002. Transaction: ' . $transactionId . '. More info: ' . $docsLink;
}

throw new \Magento\Framework\Exception\LocalizedException(__($message));
Expand Down Expand Up @@ -335,21 +339,54 @@ public function void(InfoInterface $payment)
return $this;
}

/**
* @param Order $order
* @return string|void
* @throws \Magento\Framework\Exception\AlreadyExistsException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function startTransaction(Order $order)
{
$transaction = $this->doStartTransaction($order);
try {
$transaction = $this->doStartTransaction($order);
payHelper::logDebug('Transaction: ' . $transaction->getTransactionId());
} 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';
}

$order->getPayment()->setAdditionalInformation('transactionId', $transaction->getTransactionId());
$this->paynlConfig->setStore($order->getStore());

$holded = $this->_scopeConfig->getValue('payment/' . $this->_code . '/holded', 'store');
if ($holded) {
if ($this->shouldHoldOrder()) {
$order->hold();
}

$this->orderRepository->save($order);

return $transaction->getRedirectUrl();
}

/**
* @param Order $order
* @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 doStartTransaction(Order $order)
{
$this->paynlConfig->setStore($order->getStore());
Expand Down Expand Up @@ -470,7 +507,6 @@ protected function doStartTransaction(Order $order)
$shippingAddress['zipCode'] = $arrShippingAddress['postcode'];
$shippingAddress['city'] = $arrShippingAddress['city'];
$shippingAddress['country'] = $arrShippingAddress['country_id'];

}

$prefix = $this->_scopeConfig->getValue('payment/paynl/order_description_prefix', 'store');
Expand Down Expand Up @@ -555,7 +591,6 @@ protected function doStartTransaction(Order $order)
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;
Expand Down Expand Up @@ -701,7 +736,6 @@ public function assignData(\Magento\Framework\DataObject $data)
$this->getInfoInstance()->setAdditionalInformation('dob', $data['dob']);
}
} elseif ($data instanceof \Magento\Framework\DataObject) {

$additional_data = $data->getAdditionalData();

if (isset($additional_data['kvknummer'])) {
Expand Down

0 comments on commit a3e52c2

Please sign in to comment.