/** * Magento will consider a transaction for voiding only if it is an authorization * Braintree allows voiding captures too * * Lookup an authorization transaction using parent transaction id, if set * * @param Payment $subject * @param callable $proceed * * @return \Magento\Sales\Model\Order\Payment\Transaction|false * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function aroundGetAuthorizationTransaction(Payment $subject, \Closure $proceed) { if ($subject->getMethodInstance()->getCode() != PaymentMethod::METHOD_CODE) { return $proceed(); } $invoice = $this->registry->registry('current_invoice'); if ($invoice && $invoice->getId()) { $transactionId = $this->paymentHelper->clearTransactionId($invoice->getTransactionId()); $collection = $this->salesTransactionCollectionFactory->create()->addFieldToFilter('txn_id', ['eq' => $transactionId]); if ($collection->getSize() < 1) { return $proceed(); } else { return $collection->getFirstItem(); } } return $proceed(); }
/** * Import payment data to billing agreement * * $payment->getBillingAgreementData() contains array with following structure : * [billing_agreement_id] => string * [method_code] => string * * @param Payment $payment * @return $this */ public function importOrderPayment(Payment $payment) { $baData = $payment->getBillingAgreementData(); $this->_paymentMethodInstance = isset($baData['method_code']) ? $this->_paymentData->getMethodInstance($baData['method_code']) : $payment->getMethodInstance(); $this->_paymentMethodInstance->setStore($payment->getMethodInstance()->getStore()); $this->setCustomerId($payment->getOrder()->getCustomerId())->setMethodCode($this->_paymentMethodInstance->getCode())->setReferenceId($baData['billing_agreement_id'])->setStatus(self::STATUS_ACTIVE)->setAgreementLabel($this->_paymentMethodInstance->getTitle()); return $this; }
/** * Add status comment * * @param \Magento\Sales\Model\Order\Payment $payment * @return $this */ protected function addStatusComment(\Magento\Sales\Model\Order\Payment $payment) { try { $transactionId = $this->getResponse()->getXTransId(); $data = $payment->getMethodInstance()->getTransactionDetails($transactionId); $transactionStatus = (string) $data->transaction->transactionStatus; $fdsFilterAction = (string) $data->transaction->FDSFilterAction; if ($payment->getIsTransactionPending()) { $message = 'Amount of %1 is pending approval on the gateway.<br/>' . 'Transaction "%2" status is "%3".<br/>' . 'Transaction FDS Filter Action is "%4"'; $message = __($message, $payment->getOrder()->getBaseCurrency()->formatTxt($this->getResponse()->getXAmount()), $transactionId, $this->dataHelper->getTransactionStatusLabel($transactionStatus), $this->dataHelper->getFdsFilterActionLabel($fdsFilterAction)); $payment->getOrder()->addStatusHistoryComment($message); } } catch (\Exception $e) { //this request is optional } return $this; }
/** * {@inheritdoc} */ public function getMethodInstance() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getMethodInstance'); if (!$pluginInfo) { return parent::getMethodInstance(); } else { return $this->___callPlugins('getMethodInstance', func_get_args(), $pluginInfo); } }
/** * Process fraud status * * @param \Magento\Sales\Model\Order\Payment $payment * @return $this */ protected function processPaymentFraudStatus(\Magento\Sales\Model\Order\Payment $payment) { try { $fraudDetailsResponse = $payment->getMethodInstance()->fetchTransactionFraudDetails($this->getResponse()->getXTransId()); $fraudData = $fraudDetailsResponse->getData(); if (empty($fraudData)) { $payment->setIsFraudDetected(false); return $this; } $payment->setIsFraudDetected(true); $payment->setAdditionalInformation('fraud_details', $fraudData); } catch (\Exception $e) { //this request is optional } return $this; }
/** * 2016-09-08 * 2016-03-27 * «How is an online refunding implemented?» https://mage2.pro/t/959 * Сначала хотел cделать по аналогии с @see \Magento\Paypal\Model\Ipn::_registerPaymentRefund() * https://github.com/magento/magento2/blob/9546277/app/code/Magento/Paypal/Model/Ipn.php#L467-L501 * Однако используемый там метод @see \Magento\Sales\Model\Order\Payment::registerRefundNotification() * нерабочий: «Invalid method Magento\Sales\Model\Order\Creditmemo::register» * https://mage2.pro/t/1029 * Поэтому делаю по аналогии с @see \Magento\Sales\Controller\Adminhtml\Order\Creditmemo\Save::execute() * * @param P $p * @param I $i * @param string|int|float|null $amount [optional] * @return int */ function dfp_refund(P $p, I $i, $amount = null) { /** @var M $m */ $m = $p->getMethodInstance(); /** @var O $o */ $o = $m->o(); /** @var CML $cml */ $cml = df_o(CML::class); $cml->setOrderId($o->getId()); $cml->setInvoiceId($i->getId()); if ($amount) { /** * 2016-09-08 * Обработка частичного возврата. * Делаем по аналогии с @see \Dfe\TwoCheckout\Handler\RefundIssued::cm() * * Произвожу расчёты в базовой валюте, чтобы не мешали курсовые колебания, * которые могли произойти в период между платежом и возвратом. */ /** @var float $refundAmountB */ $refundAmountB = $m->cToBase($m->amountParse($amount)); /** @var float $invoiceAmountB */ $invoiceAmountB = $i->getBaseGrandTotal(); /** @var float $diffB */ $diffB = $invoiceAmountB - $refundAmountB; if (!df_is0($diffB)) { /** * 2016-05-23 * https://mage2.pro/tags/credit-memo-adjustment * * Стек вызова: * 1) @used-by \Magento\Sales\Controller\Adminhtml\Order\CreditmemoLoader::load() * https://github.com/magento/magento2/blob/b366da/app/code/Magento/Sales/Controller/Adminhtml/Order/CreditmemoLoader.php#L186 * 2) @used-by \Magento\Sales\Model\Order\CreditmemoFactory::createByInvoice() * https://github.com/magento/magento2/blob/b366da/app/code/Magento/Sales/Model/Order/CreditmemoFactory.php#L155 * 3) @used-by \Magento\Sales\Model\Order\CreditmemoFactory::initData() * https://github.com/magento/magento2/blob/b366da/app/code/Magento/Sales/Model/Order/CreditmemoFactory.php#L244-L246 */ $cml->setCreditmemo(['adjustment_negative' => df_currency_convert($diffB, df_currency_base($o), $o->getOrderCurrency())]); } } /** @var CM $cm */ $cm = $cml->load(); df_assert($cm); /** * 2016-03-28 * Важно! Иначе order загрузит payment автоматически вместо нашего, * и флаг @see \Dfe\Stripe\Method::WEBHOOK_CASE будет утерян */ $cm->getOrder()->setData(O::PAYMENT, $p); /** @var ICMS|CMS $cms */ $cms = df_om()->create(ICMS::class); $cms->refund($cm, false); /** * 2016-03-28 * @todo Надо отослать покупателю письмо-оповещение о возврате оплаты. * 2016-05-15 * Что интересно, при возврате из административной части Magento 2 * покупатель тоже не получает уведомление. */ return $cm->getId(); }