0

On declined payment stop invoice generation and set order state in pending

public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
        {
          $order = $payment->getOrder();
           try {
              if($charge):
                $payment->setTransactionId($charge["id"]);
                if ($capture) {
                    $payment->setIsTransactionClosed(1);
                } else {
                    $payment->setIsTransactionClosed(0);
                } else:


                endif;
            } catch ( $e) {

                $error_code = $e->getErrorCode();
                if ($error_code === "card_declined") {
                    $errorMsg = 'Charge was declined. Please, contact you bank for more information or use a different card.';
                } else {
                    $errorMsg = $e->getMessage();
                }
$this->_logger->error(__($errorMsg));
$orderId=$order->getId();
            $payment->setSkipTransactionCreation(true);
                 //throw new \Magento\Framework\Validator\Exception(__($errorMsg));

            }
1

These two function did the trick for me

$payment->setIsTransactionPending(true);

$payment->setIsFraudDetected(true);

public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
        {
          $order = $payment->getOrder();
           try {
              if($charge):
                $payment->setTransactionId($charge["id"]);
                if ($capture) {
                    $payment->setIsTransactionClosed(1);
                } else {
                    $payment->setIsTransactionClosed(0);
                } else:


                endif;
            } catch ( $e) {

                $error_code = $e->getErrorCode();
                if ($error_code === "card_declined") {
                    $errorMsg = 'Charge was declined. Please, contact you bank for more information or use a different card.';
                } else {
                    $errorMsg = $e->getMessage();
                }
$this->_logger->error(__($errorMsg));
$orderId=$order->getId();
            $payment->setSkipTransactionCreation(true);
                 //throw new \Magento\Framework\Validator\Exception(__($errorMsg));
            $state = 'pending_payment';
            $status = 'pending_payment';
            $comment = $errorMsg;
            $isNotified = false;
            $order->setState($state);
            $order->setStatus($status);
            $order->addStatusToHistory($order->getStatus(), $comment);
            $order->save(); 
            $payment->setIsTransactionPending(true); 
            $payment->setIsFraudDetected(true);

            }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.