Payment.php 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Pricing\PriceCurrencyInterface;
  9. use Magento\Sales\Api\Data\OrderPaymentInterface;
  10. use Magento\Sales\Api\OrderRepositoryInterface;
  11. use Magento\Sales\Model\Order;
  12. use Magento\Sales\Model\Order\Payment\Info;
  13. use Magento\Sales\Model\Order\Payment\Transaction;
  14. use Magento\Sales\Model\Order\Payment\Transaction\ManagerInterface;
  15. use Magento\Sales\Api\CreditmemoManagementInterface as CreditmemoManager;
  16. /**
  17. * Order payment information
  18. *
  19. * @api
  20. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  21. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. * @since 100.0.2
  24. */
  25. class Payment extends Info implements OrderPaymentInterface
  26. {
  27. /**
  28. * Actions for payment when it triggered review state
  29. *
  30. * @var string
  31. */
  32. const REVIEW_ACTION_ACCEPT = 'accept';
  33. const REVIEW_ACTION_DENY = 'deny';
  34. const REVIEW_ACTION_UPDATE = 'update';
  35. const PARENT_TXN_ID = 'parent_transaction_id';
  36. /**
  37. * Order model object
  38. *
  39. * @var Order
  40. */
  41. protected $_order;
  42. /**
  43. * Whether can void
  44. * @var string
  45. */
  46. protected $_canVoidLookup = null;
  47. /**
  48. * @var string
  49. */
  50. protected $_eventPrefix = 'sales_order_payment';
  51. /**
  52. * @var string
  53. */
  54. protected $_eventObject = 'payment';
  55. /**
  56. * Transaction additional information container
  57. *
  58. * @var array
  59. */
  60. protected $transactionAdditionalInfo = [];
  61. /**
  62. * @var \Magento\Sales\Model\Order\CreditmemoFactory
  63. */
  64. protected $creditmemoFactory;
  65. /**
  66. * @var PriceCurrencyInterface
  67. */
  68. protected $priceCurrency;
  69. /**
  70. * @var \Magento\Sales\Api\TransactionRepositoryInterface
  71. */
  72. protected $transactionRepository;
  73. /**
  74. * @var \Magento\Sales\Model\Order\Payment\Transaction\ManagerInterface
  75. */
  76. protected $transactionManager;
  77. /**
  78. * @var \Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface
  79. */
  80. protected $transactionBuilder;
  81. /**
  82. * @var Payment\Processor
  83. */
  84. protected $orderPaymentProcessor;
  85. /**
  86. * @var OrderRepositoryInterface
  87. */
  88. protected $orderRepository;
  89. /**
  90. * @var OrderStateResolverInterface
  91. */
  92. private $orderStateResolver;
  93. /**
  94. * @var CreditmemoManager
  95. */
  96. private $creditmemoManager = null;
  97. /**
  98. * @param \Magento\Framework\Model\Context $context
  99. * @param \Magento\Framework\Registry $registry
  100. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  101. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  102. * @param \Magento\Payment\Helper\Data $paymentData
  103. * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
  104. * @param \Magento\Sales\Model\Order\CreditmemoFactory $creditmemoFactory
  105. * @param PriceCurrencyInterface $priceCurrency
  106. * @param \Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository
  107. * @param \Magento\Sales\Model\Order\Payment\Transaction\ManagerInterface $transactionManager
  108. * @param Transaction\BuilderInterface $transactionBuilder
  109. * @param Payment\Processor $paymentProcessor
  110. * @param OrderRepositoryInterface $orderRepository
  111. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  112. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  113. * @param array $data
  114. * @param CreditmemoManager $creditmemoManager
  115. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  116. */
  117. public function __construct(
  118. \Magento\Framework\Model\Context $context,
  119. \Magento\Framework\Registry $registry,
  120. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  121. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  122. \Magento\Payment\Helper\Data $paymentData,
  123. \Magento\Framework\Encryption\EncryptorInterface $encryptor,
  124. \Magento\Sales\Model\Order\CreditmemoFactory $creditmemoFactory,
  125. PriceCurrencyInterface $priceCurrency,
  126. \Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository,
  127. ManagerInterface $transactionManager,
  128. \Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface $transactionBuilder,
  129. \Magento\Sales\Model\Order\Payment\Processor $paymentProcessor,
  130. OrderRepositoryInterface $orderRepository,
  131. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  132. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  133. array $data = [],
  134. CreditmemoManager $creditmemoManager = null
  135. ) {
  136. $this->priceCurrency = $priceCurrency;
  137. $this->creditmemoFactory = $creditmemoFactory;
  138. $this->transactionRepository = $transactionRepository;
  139. $this->transactionManager = $transactionManager;
  140. $this->transactionBuilder = $transactionBuilder;
  141. $this->orderPaymentProcessor = $paymentProcessor;
  142. $this->orderRepository = $orderRepository;
  143. $this->creditmemoManager = $creditmemoManager ?: ObjectManager::getInstance()->get(CreditmemoManager::class);
  144. parent::__construct(
  145. $context,
  146. $registry,
  147. $extensionFactory,
  148. $customAttributeFactory,
  149. $paymentData,
  150. $encryptor,
  151. $resource,
  152. $resourceCollection,
  153. $data
  154. );
  155. }
  156. /**
  157. * Initialize resource model
  158. *
  159. * @return void
  160. */
  161. protected function _construct()
  162. {
  163. $this->_init(\Magento\Sales\Model\ResourceModel\Order\Payment::class);
  164. }
  165. /**
  166. * Declare order model object
  167. *
  168. * @codeCoverageIgnore
  169. *
  170. * @param Order $order
  171. * @return $this
  172. */
  173. public function setOrder(Order $order)
  174. {
  175. $this->_order = $order;
  176. return $this;
  177. }
  178. /**
  179. * Retrieve order model object
  180. *
  181. * @codeCoverageIgnore
  182. * @return Order
  183. */
  184. public function getOrder()
  185. {
  186. if (!$this->_order && $this->getParentId()) {
  187. $this->_order = $this->orderRepository->get($this->getParentId());
  188. }
  189. return $this->_order;
  190. }
  191. /**
  192. * Sets transaction id for current payment
  193. *
  194. * @param string $transactionId
  195. * @return $this
  196. */
  197. public function setTransactionId($transactionId)
  198. {
  199. $this->setData('transaction_id', $transactionId);
  200. return $this;
  201. }
  202. /**
  203. * Return transaction id
  204. *
  205. * @return int
  206. */
  207. public function getTransactionId()
  208. {
  209. return $this->getData('transaction_id');
  210. }
  211. /**
  212. * Sets transaction close flag
  213. *
  214. * @param bool $isClosed
  215. * @return $this
  216. */
  217. public function setIsTransactionClosed($isClosed)
  218. {
  219. $this->setData('is_transaction_closed', (bool)$isClosed);
  220. return $this;
  221. }
  222. /**
  223. * Returns transaction parent
  224. *
  225. * @return string
  226. */
  227. public function getParentTransactionId()
  228. {
  229. return $this->getData(self::PARENT_TXN_ID);
  230. }
  231. /**
  232. * Returns transaction parent
  233. *
  234. * @param string $txnId
  235. * @return string
  236. * @since 100.1.0
  237. */
  238. public function setParentTransactionId($txnId)
  239. {
  240. return $this->setData(self::PARENT_TXN_ID, $txnId);
  241. }
  242. /**
  243. * Check order payment capture action availability
  244. *
  245. * @return bool
  246. */
  247. public function canCapture()
  248. {
  249. if (!$this->getMethodInstance()->canCapture()) {
  250. return false;
  251. }
  252. // Check Authorization transaction state
  253. $authTransaction = $this->getAuthorizationTransaction();
  254. if ($authTransaction && $authTransaction->getIsClosed()) {
  255. $orderTransaction = $this->transactionRepository->getByTransactionType(
  256. Transaction::TYPE_ORDER,
  257. $this->getId(),
  258. $this->getOrder()->getId()
  259. );
  260. if (!$orderTransaction) {
  261. return false;
  262. }
  263. }
  264. return true;
  265. }
  266. /**
  267. * Check refund availability
  268. *
  269. * @return bool
  270. */
  271. public function canRefund()
  272. {
  273. return $this->getMethodInstance()->canRefund();
  274. }
  275. /**
  276. * Check partial refund availability for invoice
  277. *
  278. * @return bool
  279. */
  280. public function canRefundPartialPerInvoice()
  281. {
  282. return $this->getMethodInstance()->canRefundPartialPerInvoice();
  283. }
  284. /**
  285. * Check partial capture availability
  286. *
  287. * @return bool
  288. */
  289. public function canCapturePartial()
  290. {
  291. return $this->getMethodInstance()->canCapturePartial();
  292. }
  293. /**
  294. * Authorize or authorize and capture payment on gateway, if applicable
  295. *
  296. * This method is supposed to be called only when order is placed
  297. *
  298. * @return $this
  299. * @SuppressWarnings(PHPMD.NPathComplexity)
  300. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  301. */
  302. public function place()
  303. {
  304. $this->_eventManager->dispatch('sales_order_payment_place_start', ['payment' => $this]);
  305. $order = $this->getOrder();
  306. $this->setAmountOrdered($order->getTotalDue());
  307. $this->setBaseAmountOrdered($order->getBaseTotalDue());
  308. $this->setShippingAmount($order->getShippingAmount());
  309. $this->setBaseShippingAmount($order->getBaseShippingAmount());
  310. $methodInstance = $this->getMethodInstance();
  311. $methodInstance->setStore($order->getStoreId());
  312. $orderState = Order::STATE_NEW;
  313. $orderStatus = $methodInstance->getConfigData('order_status');
  314. $isCustomerNotified = $order->getCustomerNoteNotify();
  315. // Do order payment validation on payment method level
  316. $methodInstance->validate();
  317. $action = $methodInstance->getConfigPaymentAction();
  318. if ($action) {
  319. if ($methodInstance->isInitializeNeeded()) {
  320. $stateObject = new \Magento\Framework\DataObject();
  321. // For method initialization we have to use original config value for payment action
  322. $methodInstance->initialize($methodInstance->getConfigData('payment_action'), $stateObject);
  323. $orderState = $stateObject->getData('state') ?: $orderState;
  324. $orderStatus = $stateObject->getData('status') ?: $orderStatus;
  325. $isCustomerNotified = $stateObject->hasData('is_notified')
  326. ? $stateObject->getData('is_notified')
  327. : $isCustomerNotified;
  328. } else {
  329. $orderState = Order::STATE_PROCESSING;
  330. $this->processAction($action, $order);
  331. $orderState = $order->getState() ? $order->getState() : $orderState;
  332. $orderStatus = $order->getStatus() ? $order->getStatus() : $orderStatus;
  333. }
  334. } else {
  335. $order->setState($orderState)
  336. ->setStatus($orderStatus);
  337. }
  338. $isCustomerNotified = $isCustomerNotified ?: $order->getCustomerNoteNotify();
  339. if (!array_key_exists($orderStatus, $order->getConfig()->getStateStatuses($orderState))) {
  340. $orderStatus = $order->getConfig()->getStateDefaultStatus($orderState);
  341. }
  342. $this->updateOrder($order, $orderState, $orderStatus, $isCustomerNotified);
  343. $this->_eventManager->dispatch('sales_order_payment_place_end', ['payment' => $this]);
  344. return $this;
  345. }
  346. /**
  347. * Set appropriate state to order or add status to order history
  348. *
  349. * @param Order $order
  350. * @param string $orderState
  351. * @param string $orderStatus
  352. * @param bool $isCustomerNotified
  353. * @return void
  354. */
  355. protected function updateOrder(Order $order, $orderState, $orderStatus, $isCustomerNotified)
  356. {
  357. // add message if order was put into review during authorization or capture
  358. $message = $order->getCustomerNote();
  359. $originalOrderState = $order->getState();
  360. $originalOrderStatus = $order->getStatus();
  361. switch (true) {
  362. case ($message && ($originalOrderState == Order::STATE_PAYMENT_REVIEW)):
  363. $order->addStatusToHistory($originalOrderStatus, $message, $isCustomerNotified);
  364. break;
  365. case ($message):
  366. case ($originalOrderState && $message):
  367. case ($originalOrderState != $orderState):
  368. case ($originalOrderStatus != $orderStatus):
  369. $order->setState($orderState)
  370. ->setStatus($orderStatus)
  371. ->addStatusHistoryComment($message)
  372. ->setIsCustomerNotified($isCustomerNotified);
  373. break;
  374. default:
  375. break;
  376. }
  377. }
  378. /**
  379. * Perform actions based on passed action name
  380. *
  381. * @param string $action
  382. * @param Order $order
  383. * @return void
  384. */
  385. protected function processAction($action, Order $order)
  386. {
  387. $totalDue = $order->getTotalDue();
  388. $baseTotalDue = $order->getBaseTotalDue();
  389. switch ($action) {
  390. case \Magento\Payment\Model\Method\AbstractMethod::ACTION_ORDER:
  391. $this->_order($baseTotalDue);
  392. break;
  393. case \Magento\Payment\Model\Method\AbstractMethod::ACTION_AUTHORIZE:
  394. $this->authorize(true, $baseTotalDue);
  395. // base amount will be set inside
  396. $this->setAmountAuthorized($totalDue);
  397. break;
  398. case \Magento\Payment\Model\Method\AbstractMethod::ACTION_AUTHORIZE_CAPTURE:
  399. $this->setAmountAuthorized($totalDue);
  400. $this->setBaseAmountAuthorized($baseTotalDue);
  401. $this->capture(null);
  402. break;
  403. default:
  404. break;
  405. }
  406. }
  407. /**
  408. * Capture the payment online
  409. * Requires an invoice. If there is no invoice specified, will automatically prepare an invoice for order
  410. * Updates transactions hierarchy, if required
  411. * Updates payment totals, updates order status and adds proper comments
  412. *
  413. * @param null|Invoice $invoice
  414. * @throws \Magento\Framework\Exception\LocalizedException
  415. * @return $this
  416. */
  417. public function capture($invoice = null)
  418. {
  419. return $this->orderPaymentProcessor->capture($this, $invoice);
  420. }
  421. /**
  422. * Process a capture notification from a payment gateway for specified amount
  423. * Creates an invoice automatically if the amount covers the order base grand total completely
  424. * Updates transactions hierarchy, if required
  425. * Prevents transaction double processing
  426. * Updates payment totals, updates order status and adds proper comments
  427. *
  428. * @param float $amount
  429. * @param bool $skipFraudDetection
  430. * @return $this
  431. */
  432. public function registerCaptureNotification($amount, $skipFraudDetection = false)
  433. {
  434. return $this->orderPaymentProcessor->registerCaptureNotification($this, $amount, $skipFraudDetection);
  435. }
  436. /**
  437. * Process authorization notification
  438. *
  439. * @param float $amount
  440. * @return $this
  441. * @see self::_authorize()
  442. */
  443. public function registerAuthorizationNotification($amount)
  444. {
  445. return $this->checkIfTransactionExists() ? $this : $this->authorize(false, $amount);
  446. }
  447. /**
  448. * Register payment fact: update self totals from the invoice
  449. *
  450. * @param Invoice $invoice
  451. * @return $this
  452. */
  453. public function pay($invoice)
  454. {
  455. $totals = $this->collectTotalAmounts(
  456. $this->getOrder(),
  457. ['grand_total', 'base_grand_total', 'shipping_amount', 'base_shipping_amount']
  458. );
  459. $this->setAmountPaid($totals['grand_total']);
  460. $this->setBaseAmountPaid($totals['base_grand_total']);
  461. $this->setShippingCaptured($totals['shipping_amount']);
  462. $this->setBaseShippingCaptured($totals['base_shipping_amount']);
  463. $this->_eventManager->dispatch('sales_order_payment_pay', ['payment' => $this, 'invoice' => $invoice]);
  464. return $this;
  465. }
  466. /**
  467. * Cancel specified invoice: update self totals from it
  468. *
  469. * @param Invoice $invoice
  470. * @return $this
  471. */
  472. public function cancelInvoice($invoice)
  473. {
  474. $this->_updateTotals(
  475. [
  476. 'amount_paid' => -1 * $invoice->getGrandTotal(),
  477. 'base_amount_paid' => -1 * $invoice->getBaseGrandTotal(),
  478. 'shipping_captured' => -1 * $invoice->getShippingAmount(),
  479. 'base_shipping_captured' => -1 * $invoice->getBaseShippingAmount(),
  480. ]
  481. );
  482. $this->_eventManager->dispatch(
  483. 'sales_order_payment_cancel_invoice',
  484. ['payment' => $this, 'invoice' => $invoice]
  485. );
  486. return $this;
  487. }
  488. /**
  489. * Create new invoice with maximum qty for invoice for each item
  490. *
  491. * Register this invoice and capture
  492. *
  493. * @return Invoice
  494. */
  495. protected function _invoice()
  496. {
  497. $invoice = $this->getOrder()->prepareInvoice();
  498. $invoice->register();
  499. if ($this->getMethodInstance()->canCapture()) {
  500. $invoice->capture();
  501. }
  502. $this->getOrder()->addRelatedObject($invoice);
  503. return $invoice;
  504. }
  505. /**
  506. * Check order payment void availability
  507. *
  508. * @return bool
  509. */
  510. public function canVoid()
  511. {
  512. if (null === $this->_canVoidLookup) {
  513. $this->_canVoidLookup = (bool)$this->getMethodInstance()->canVoid();
  514. if ($this->_canVoidLookup) {
  515. $authTransaction = $this->getAuthorizationTransaction();
  516. $this->_canVoidLookup = (bool)$authTransaction && !(int)$authTransaction->getIsClosed();
  517. }
  518. }
  519. return $this->_canVoidLookup;
  520. }
  521. /**
  522. * Void payment online
  523. *
  524. * @param \Magento\Framework\DataObject $document
  525. * @return $this
  526. * @see self::_void()
  527. */
  528. public function void(\Magento\Framework\DataObject $document)
  529. {
  530. $this->_void(true);
  531. $this->_eventManager->dispatch('sales_order_payment_void', ['payment' => $this, 'invoice' => $document]);
  532. return $this;
  533. }
  534. /**
  535. * Process void notification
  536. *
  537. * @param float $amount
  538. * @return $this
  539. * @see self::_void()
  540. */
  541. public function registerVoidNotification($amount = null)
  542. {
  543. if (!$this->hasMessage()) {
  544. $this->setMessage(__('Registered a Void notification.'));
  545. }
  546. return $this->_void(false, $amount);
  547. }
  548. /**
  549. * Sets creditmemo for current payment
  550. *
  551. * @param Creditmemo $creditmemo
  552. * @return $this
  553. */
  554. public function setCreditmemo(Creditmemo $creditmemo)
  555. {
  556. $this->setData('creditmemo', $creditmemo);
  557. return $this;
  558. }
  559. /**
  560. * Returns Creditmemo assigned for this payment
  561. *
  562. * @return Creditmemo|null
  563. */
  564. public function getCreditmemo()
  565. {
  566. return $this->getData('creditmemo') instanceof Creditmemo
  567. ? $this->getData('creditmemo')
  568. : null;
  569. }
  570. /**
  571. * Refund payment online or offline, depending on whether there is invoice set in the creditmemo instance
  572. * Updates transactions hierarchy, if required
  573. * Updates payment totals, updates order status and adds proper comments
  574. *
  575. * @param Creditmemo $creditmemo
  576. * @return $this
  577. * @throws \Exception
  578. * @throws \Magento\Framework\Exception\LocalizedException
  579. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  580. * @SuppressWarnings(PHPMD.NPathComplexity)
  581. */
  582. public function refund($creditmemo)
  583. {
  584. $baseAmountToRefund = $this->formatAmount($creditmemo->getBaseGrandTotal());
  585. $this->setTransactionId(
  586. $this->transactionManager->generateTransactionId($this, Transaction::TYPE_REFUND)
  587. );
  588. $isOnline = false;
  589. $gateway = $this->getMethodInstance();
  590. $invoice = null;
  591. if ($gateway->canRefund()) {
  592. $this->setCreditmemo($creditmemo);
  593. if ($creditmemo->getDoTransaction()) {
  594. $invoice = $creditmemo->getInvoice();
  595. if ($invoice) {
  596. $isOnline = true;
  597. $captureTxn = $this->transactionRepository->getByTransactionId(
  598. $invoice->getTransactionId(),
  599. $this->getId(),
  600. $this->getOrder()->getId()
  601. );
  602. if ($captureTxn) {
  603. $this->setTransactionIdsForRefund($captureTxn);
  604. }
  605. $this->setShouldCloseParentTransaction(true);
  606. // TODO: implement multiple refunds per capture
  607. try {
  608. $gateway->setStore(
  609. $this->getOrder()->getStoreId()
  610. );
  611. $this->setRefundTransactionId($invoice->getTransactionId());
  612. $gateway->refund($this, $baseAmountToRefund);
  613. $creditmemo->setTransactionId($this->getLastTransId());
  614. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  615. if (!$captureTxn) {
  616. throw new \Magento\Framework\Exception\LocalizedException(
  617. __('If the invoice was created offline, try creating an offline credit memo.'),
  618. $e
  619. );
  620. }
  621. throw $e;
  622. }
  623. }
  624. } elseif ($gateway->isOffline()) {
  625. $gateway->setStore(
  626. $this->getOrder()->getStoreId()
  627. );
  628. $gateway->refund($this, $baseAmountToRefund);
  629. }
  630. }
  631. // update self totals from creditmemo
  632. $this->_updateTotals(
  633. [
  634. 'amount_refunded' => $creditmemo->getGrandTotal(),
  635. 'base_amount_refunded' => $baseAmountToRefund,
  636. 'base_amount_refunded_online' => $isOnline ? $baseAmountToRefund : null,
  637. 'shipping_refunded' => $creditmemo->getShippingAmount(),
  638. 'base_shipping_refunded' => $creditmemo->getBaseShippingAmount(),
  639. ]
  640. );
  641. // update transactions and order state
  642. $transaction = $this->addTransaction(
  643. Transaction::TYPE_REFUND,
  644. $creditmemo,
  645. $isOnline
  646. );
  647. if ($invoice) {
  648. $message = __('We refunded %1 online.', $this->formatPrice($baseAmountToRefund));
  649. } else {
  650. $message = $this->hasMessage() ? $this->getMessage() : __(
  651. 'We refunded %1 offline.',
  652. $this->formatPrice($baseAmountToRefund)
  653. );
  654. }
  655. $message = $message = $this->prependMessage($message);
  656. $message = $this->_appendTransactionToMessage($transaction, $message);
  657. $orderState = $this->getOrderStateResolver()->getStateForOrder($this->getOrder());
  658. $this->getOrder()
  659. ->addStatusHistoryComment(
  660. $message,
  661. $this->getOrder()->getConfig()->getStateDefaultStatus($orderState)
  662. )->setIsCustomerNotified($creditmemo->getOrder()->getCustomerNoteNotify());
  663. $this->_eventManager->dispatch(
  664. 'sales_order_payment_refund',
  665. ['payment' => $this, 'creditmemo' => $creditmemo]
  666. );
  667. return $this;
  668. }
  669. /**
  670. * Process payment refund notification
  671. * Updates transactions hierarchy, if required
  672. * Prevents transaction double processing
  673. * Updates payment totals, updates order status and adds proper comments
  674. * TODO: potentially a full capture can be refunded. In this case if there was only one invoice for that transaction
  675. * then we should create a creditmemo from invoice and also refund it offline
  676. * TODO: implement logic of chargebacks reimbursements (via negative amount)
  677. *
  678. * @param float $amount
  679. * @return $this
  680. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  681. */
  682. public function registerRefundNotification($amount)
  683. {
  684. $notificationAmount = $amount;
  685. $this->setTransactionId(
  686. $this->transactionManager->generateTransactionId(
  687. $this,
  688. Transaction::TYPE_REFUND,
  689. $this->transactionRepository->getByTransactionId(
  690. $this->getParentTransactionId(),
  691. $this->getId(),
  692. $this->getOrder()->getId()
  693. )
  694. )
  695. );
  696. if ($this->checkIfTransactionExists()) {
  697. return $this;
  698. }
  699. $order = $this->getOrder();
  700. $invoice = $this->_getInvoiceForTransactionId($this->getParentTransactionId());
  701. //choose where we get totals
  702. $salesEntity = $invoice ?: $order;
  703. $baseGrandTotal = $salesEntity->getBaseGrandTotal();
  704. $amountRefundLeft = $baseGrandTotal - $salesEntity->getBaseTotalRefunded();
  705. if ($amountRefundLeft < $amount) {
  706. $amount = $amountRefundLeft;
  707. }
  708. if ($amount != $baseGrandTotal) {
  709. $order->addStatusHistoryComment(
  710. __(
  711. 'IPN "Refunded". Refund issued by merchant. '
  712. . 'Registered notification about refunded amount of %1. Transaction ID: "%2". '
  713. . 'Credit Memo has not been created. Please create offline Credit Memo.',
  714. $this->formatPrice($notificationAmount),
  715. $this->getTransactionId()
  716. ),
  717. false
  718. );
  719. return $this;
  720. }
  721. $creditmemo = $this->prepareCreditMemo($amount, $baseGrandTotal, $invoice);
  722. $creditmemo->setPaymentRefundDisallowed(
  723. true
  724. )->setAutomaticallyCreated(
  725. true
  726. )->addComment(
  727. __('The credit memo has been created automatically.')
  728. );
  729. $this->creditmemoManager->refund($creditmemo, false);
  730. $this->_updateTotals(
  731. ['amount_refunded' => $creditmemo->getGrandTotal(), 'base_amount_refunded_online' => $amount]
  732. );
  733. $this->setCreatedCreditmemo($creditmemo);
  734. // update transactions and order state
  735. $transaction = $this->addTransaction(
  736. Transaction::TYPE_REFUND,
  737. $creditmemo
  738. );
  739. $message = $this->prependMessage(
  740. __('Registered notification about refunded amount of %1.', $this->formatPrice($amount))
  741. );
  742. $message = $this->_appendTransactionToMessage($transaction, $message);
  743. $this->setOrderStateProcessing($message);
  744. return $this;
  745. }
  746. /**
  747. * Cancel a creditmemo: substract its totals from the payment
  748. *
  749. * @param Creditmemo $creditmemo
  750. * @return $this
  751. */
  752. public function cancelCreditmemo($creditmemo)
  753. {
  754. $this->_updateTotals(
  755. [
  756. 'amount_refunded' => -1 * $creditmemo->getGrandTotal(),
  757. 'base_amount_refunded' => -1 * $creditmemo->getBaseGrandTotal(),
  758. 'shipping_refunded' => -1 * $creditmemo->getShippingAmount(),
  759. 'base_shipping_refunded' => -1 * $creditmemo->getBaseShippingAmount(),
  760. ]
  761. );
  762. $this->_eventManager->dispatch(
  763. 'sales_order_payment_cancel_creditmemo',
  764. ['payment' => $this, 'creditmemo' => $creditmemo]
  765. );
  766. return $this;
  767. }
  768. /**
  769. * Order cancellation hook for payment method instance
  770. *
  771. * Adds void transaction if needed
  772. *
  773. * @return $this
  774. */
  775. public function cancel()
  776. {
  777. $isOnline = true;
  778. if (!$this->canVoid()) {
  779. $isOnline = false;
  780. }
  781. if (!$this->hasMessage()) {
  782. $this->setMessage($isOnline ? __('Canceled order online') : __('Canceled order offline'));
  783. }
  784. if ($isOnline) {
  785. $this->_void($isOnline, null, 'cancel');
  786. }
  787. $this->_eventManager->dispatch('sales_order_payment_cancel', ['payment' => $this]);
  788. return $this;
  789. }
  790. /**
  791. * Check order payment review availability
  792. *
  793. * @return bool
  794. */
  795. public function canReviewPayment()
  796. {
  797. return (bool)$this->getMethodInstance()->canReviewPayment();
  798. }
  799. /**
  800. * Check fetch transaction info availability
  801. *
  802. * @return bool
  803. */
  804. public function canFetchTransactionInfo()
  805. {
  806. return (bool)$this->getMethodInstance()->canFetchTransactionInfo();
  807. }
  808. /**
  809. * Accept online a payment that is in review state
  810. *
  811. * @return $this
  812. */
  813. public function accept()
  814. {
  815. $transactionId = $this->getLastTransId();
  816. /** @var \Magento\Payment\Model\Method\AbstractMethod $method */
  817. $method = $this->getMethodInstance();
  818. $method->setStore($this->getOrder()->getStoreId());
  819. if ($method->acceptPayment($this)) {
  820. $invoice = $this->_getInvoiceForTransactionId($transactionId);
  821. $message = $this->_appendTransactionToMessage(
  822. $transactionId,
  823. $this->prependMessage(__('Approved the payment online.'))
  824. );
  825. $this->updateBaseAmountPaidOnlineTotal($invoice);
  826. $this->setOrderStateProcessing($message);
  827. } else {
  828. $message = $this->_appendTransactionToMessage(
  829. $transactionId,
  830. $this->prependMessage(__('There is no need to approve this payment.'))
  831. );
  832. $this->setOrderStatePaymentReview($message, $transactionId);
  833. }
  834. return $this;
  835. }
  836. /**
  837. * Accept order with payment method instance
  838. *
  839. * @param bool $isOnline
  840. * @return $this
  841. * @throws \Magento\Framework\Exception\LocalizedException
  842. */
  843. public function deny($isOnline = true)
  844. {
  845. $transactionId = $isOnline ? $this->getLastTransId() : $this->getTransactionId();
  846. if ($isOnline) {
  847. /** @var \Magento\Payment\Model\Method\AbstractMethod $method */
  848. $method = $this->getMethodInstance();
  849. $method->setStore($this->getOrder()->getStoreId());
  850. $result = $method->denyPayment($this);
  851. } else {
  852. $result = (bool)$this->getNotificationResult();
  853. }
  854. if ($result) {
  855. $invoice = $this->_getInvoiceForTransactionId($transactionId);
  856. $message = $this->_appendTransactionToMessage(
  857. $transactionId,
  858. $this->prependMessage(__('Denied the payment online'))
  859. );
  860. $this->cancelInvoiceAndRegisterCancellation($invoice, $message);
  861. } else {
  862. $txt = $isOnline ?
  863. 'There is no need to deny this payment.' : 'Registered notification about denied payment.';
  864. $message = $this->_appendTransactionToMessage(
  865. $transactionId,
  866. $this->prependMessage(__($txt))
  867. );
  868. $this->setOrderStatePaymentReview($message, $transactionId);
  869. }
  870. return $this;
  871. }
  872. /**
  873. * Performs registered payment update.
  874. *
  875. * @param bool $isOnline
  876. * @return $this
  877. * @throws \Magento\Framework\Exception\LocalizedException
  878. */
  879. public function update($isOnline = true)
  880. {
  881. $transactionId = $isOnline ? $this->getLastTransId() : $this->getTransactionId();
  882. $invoice = $this->_getInvoiceForTransactionId($transactionId);
  883. if ($isOnline) {
  884. $method = $this->getMethodInstance();
  885. $method->setStore($this->getOrder()->getStoreId());
  886. $method->fetchTransactionInfo($this, $transactionId);
  887. }
  888. if ($this->getIsTransactionApproved()) {
  889. $message = $this->_appendTransactionToMessage(
  890. $transactionId,
  891. $this->prependMessage(__('Registered update about approved payment.'))
  892. );
  893. $this->updateBaseAmountPaidOnlineTotal($invoice);
  894. $this->setOrderStateProcessing($message);
  895. } elseif ($this->getIsTransactionDenied()) {
  896. $message = $this->_appendTransactionToMessage(
  897. $transactionId,
  898. $this->prependMessage(__('Registered update about denied payment.'))
  899. );
  900. $this->cancelInvoiceAndRegisterCancellation($invoice, $message);
  901. } else {
  902. $message = $this->_appendTransactionToMessage(
  903. $transactionId,
  904. $this->prependMessage(__('There is no update for the payment.'))
  905. );
  906. $this->setOrderStatePaymentReview($message, $transactionId);
  907. }
  908. return $this;
  909. }
  910. /**
  911. * Triggers invoice pay and updates base_amount_paid_online total.
  912. *
  913. * @param \Magento\Sales\Model\Order\Invoice|false $invoice
  914. */
  915. protected function updateBaseAmountPaidOnlineTotal($invoice)
  916. {
  917. if ($invoice instanceof Invoice) {
  918. $invoice->pay();
  919. $totals = $this->collectTotalAmounts($this->getOrder(), ['base_grand_total']);
  920. $this->setBaseAmountPaidOnline($totals['base_grand_total']);
  921. $this->getOrder()->addRelatedObject($invoice);
  922. }
  923. }
  924. /**
  925. * Sets order state to 'processing' with appropriate message
  926. *
  927. * @param \Magento\Framework\Phrase|string $message
  928. */
  929. protected function setOrderStateProcessing($message)
  930. {
  931. $this->getOrder()->setState(Order::STATE_PROCESSING)
  932. ->setStatus($this->getOrder()->getConfig()->getStateDefaultStatus(Order::STATE_PROCESSING))
  933. ->addStatusHistoryComment($message);
  934. }
  935. /**
  936. * Cancel invoice and register order cancellation
  937. *
  938. * @param Invoice|false $invoice
  939. * @param string $message
  940. */
  941. protected function cancelInvoiceAndRegisterCancellation($invoice, $message)
  942. {
  943. if ($invoice instanceof Invoice) {
  944. $invoice->cancel();
  945. $this->getOrder()->addRelatedObject($invoice);
  946. }
  947. $this->getOrder()->registerCancellation($message, false);
  948. }
  949. /**
  950. * Sets order state status to 'payment_review' with appropriate message
  951. *
  952. * @param string $message
  953. * @param int|null $transactionId
  954. */
  955. protected function setOrderStatePaymentReview($message, $transactionId)
  956. {
  957. if ($this->getOrder()->getState() != Order::STATE_PAYMENT_REVIEW) {
  958. $this->getOrder()->setState(Order::STATE_PAYMENT_REVIEW)
  959. ->addStatusHistoryComment($message);
  960. if ($this->getIsFraudDetected()) {
  961. $this->getOrder()->setStatus(Order::STATUS_FRAUD);
  962. }
  963. if ($transactionId) {
  964. $this->setLastTransId($transactionId);
  965. }
  966. } else {
  967. $this->getOrder()->addStatusHistoryComment($message);
  968. }
  969. }
  970. /**
  971. * Triggers order payment command.
  972. *
  973. * This method can be used to support multi warehouse functionality
  974. * and notifies payment provider about the fact that order has been created.
  975. * 1. Updates order state and status to one of the following:
  976. * - processing, in case is_pending_payment flag is not set to a payment object
  977. * - payment_review, when is_pending_payment flag is set for payment object
  978. * - fraud, when is_fraud_detected flag is set to a payment object
  979. * 2. Sets corresponding comment for order and payment objects depending on order state value.
  980. * 3. Creates payment "order" transaction with corresponding comments
  981. *
  982. * For skipping 1,2,3 operations the skip_order_processing flag should be set in a payment object
  983. *
  984. * @param float $amount
  985. * @return $this
  986. */
  987. protected function _order($amount)
  988. {
  989. return $this->orderPaymentProcessor->order($this, $amount);
  990. }
  991. /**
  992. * Authorize payment either online or offline (process auth notification)
  993. * Updates transactions hierarchy, if required
  994. * Prevents transaction double processing
  995. * Updates payment totals, updates order status and adds proper comments
  996. *
  997. * @param bool $isOnline
  998. * @param float $amount
  999. *
  1000. * @return $this
  1001. */
  1002. public function authorize($isOnline, $amount)
  1003. {
  1004. return $this->orderPaymentProcessor->authorize($this, $isOnline, $amount);
  1005. }
  1006. /**
  1007. * Void payment either online or offline (process void notification)
  1008. * NOTE: that in some cases authorization can be voided after a capture. In such case it makes sense to use
  1009. * the amount void amount, for informational purposes.
  1010. * Updates payment totals, updates order status and adds proper comments
  1011. *
  1012. * @param bool $isOnline
  1013. * @param float $amount
  1014. * @param string $gatewayCallback
  1015. * @return $this
  1016. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  1017. * @SuppressWarnings(PHPMD.NPathComplexity)
  1018. */
  1019. protected function _void($isOnline, $amount = null, $gatewayCallback = 'void')
  1020. {
  1021. $order = $this->getOrder();
  1022. $authTransaction = $this->getAuthorizationTransaction();
  1023. $this->setTransactionId(
  1024. $this->transactionManager->generateTransactionId($this, Transaction::TYPE_VOID, $authTransaction)
  1025. );
  1026. $this->setShouldCloseParentTransaction(true);
  1027. // attempt to void
  1028. if ($isOnline) {
  1029. $method = $this->getMethodInstance();
  1030. $method->setStore($order->getStoreId());
  1031. $method->{$gatewayCallback}($this);
  1032. }
  1033. if ($this->checkIfTransactionExists()) {
  1034. return $this;
  1035. }
  1036. // if the authorization was untouched, we may assume voided amount = order grand total
  1037. // but only if the payment auth amount equals to order grand total
  1038. if ($authTransaction &&
  1039. $order->getBaseGrandTotal() == $this->getBaseAmountAuthorized() &&
  1040. 0 == $this->getBaseAmountCanceled()
  1041. ) {
  1042. if ($authTransaction->canVoidAuthorizationCompletely()) {
  1043. $amount = (double)$order->getBaseGrandTotal();
  1044. }
  1045. }
  1046. if ($amount) {
  1047. $amount = $this->formatAmount($amount);
  1048. }
  1049. // update transactions, order state and add comments
  1050. $transaction = $this->addTransaction(Transaction::TYPE_VOID, null, true);
  1051. $message = $this->hasMessage() ? $this->getMessage() : __('Voided authorization.');
  1052. $message = $this->prependMessage($message);
  1053. if ($amount) {
  1054. $message .= ' ' . __('Amount: %1.', $this->formatPrice($amount));
  1055. }
  1056. $message = $this->_appendTransactionToMessage($transaction, $message);
  1057. $this->setOrderStateProcessing($message);
  1058. $order->setDataChanges(true);
  1059. return $this;
  1060. }
  1061. /**
  1062. * Public access to _addTransaction method
  1063. *
  1064. * @param string $type
  1065. * @param \Magento\Sales\Model\AbstractModel $salesDocument
  1066. * @param bool $failSafe
  1067. * @return null|Transaction
  1068. */
  1069. public function addTransaction($type, $salesDocument = null, $failSafe = false)
  1070. {
  1071. $builder = $this->transactionBuilder->setPayment($this)
  1072. ->setOrder($this->getOrder())
  1073. ->setFailSafe($failSafe)
  1074. ->setTransactionId($this->getTransactionId())
  1075. ->setAdditionalInformation($this->transactionAdditionalInfo);
  1076. if ($salesDocument) {
  1077. $builder->setSalesDocument($salesDocument);
  1078. }
  1079. return $builder->build($type);
  1080. }
  1081. /**
  1082. * Add message to the specified transaction.
  1083. *
  1084. * @param Transaction|null $transaction
  1085. * @param string $message
  1086. * @return void
  1087. */
  1088. public function addTransactionCommentsToOrder($transaction, $message)
  1089. {
  1090. $order = $this->getOrder();
  1091. $message = $this->_appendTransactionToMessage($transaction, $message);
  1092. $order->addStatusHistoryComment($message);
  1093. }
  1094. /**
  1095. * Import details data of specified transaction
  1096. *
  1097. * @param Transaction $transactionTo
  1098. * @return $this
  1099. */
  1100. public function importTransactionInfo(Transaction $transactionTo)
  1101. {
  1102. $method = $this->getMethodInstance();
  1103. $method->setStore(
  1104. $this->getOrder()->getStoreId()
  1105. );
  1106. $data = $method->fetchTransactionInfo(
  1107. $this,
  1108. $transactionTo->getTxnId()
  1109. );
  1110. if ($data) {
  1111. $transactionTo->setAdditionalInformation(
  1112. Transaction::RAW_DETAILS,
  1113. $data
  1114. );
  1115. }
  1116. return $this;
  1117. }
  1118. /**
  1119. * Totals updater utility method
  1120. *
  1121. * Updates self totals by keys in data array('key' => $delta)
  1122. *
  1123. * @param array $data
  1124. * @return void
  1125. */
  1126. protected function _updateTotals($data)
  1127. {
  1128. foreach ($data as $key => $amount) {
  1129. if (null !== $amount) {
  1130. $was = $this->getDataUsingMethod($key);
  1131. $this->setDataUsingMethod($key, $was + $amount);
  1132. }
  1133. }
  1134. }
  1135. /**
  1136. * Append transaction ID (if any) message to the specified message
  1137. *
  1138. * @param Transaction|null $transaction
  1139. * @param string $message
  1140. * @return string
  1141. */
  1142. protected function _appendTransactionToMessage($transaction, $message)
  1143. {
  1144. if ($transaction) {
  1145. $txnId = is_object($transaction) ? $transaction->getHtmlTxnId() : $transaction;
  1146. $message .= ' ' . __('Transaction ID: "%1"', $txnId);
  1147. }
  1148. return $message;
  1149. }
  1150. /**
  1151. * Prepend a "prepared_message" that may be set to the payment instance before, to the specified message
  1152. *
  1153. * Prepends value to the specified string or to the comment of specified order status history item instance
  1154. *
  1155. * @param string|\Magento\Sales\Model\Order\Status\History $messagePrependTo
  1156. * @return string|\Magento\Sales\Model\Order\Status\History
  1157. */
  1158. public function prependMessage($messagePrependTo)
  1159. {
  1160. $preparedMessage = $this->getPreparedMessage();
  1161. if ($preparedMessage) {
  1162. if (is_string($preparedMessage)
  1163. || $preparedMessage instanceof \Magento\Framework\Phrase
  1164. ) {
  1165. return $preparedMessage . ' ' . $messagePrependTo;
  1166. } elseif (is_object($preparedMessage)
  1167. && $preparedMessage instanceof \Magento\Sales\Model\Order\Status\History
  1168. ) {
  1169. $comment = $preparedMessage->getComment() . ' ' . $messagePrependTo;
  1170. $preparedMessage->setComment($comment);
  1171. return $comment;
  1172. }
  1173. }
  1174. return $messagePrependTo;
  1175. }
  1176. /**
  1177. * Round up and cast specified amount to float or string
  1178. *
  1179. * @param string|float $amount
  1180. * @param bool $asFloat
  1181. * @return string|float
  1182. */
  1183. public function formatAmount($amount, $asFloat = false)
  1184. {
  1185. $amount = $this->priceCurrency->round($amount);
  1186. return !$asFloat ? (string)$amount : $amount;
  1187. }
  1188. /**
  1189. * Format price with currency sign
  1190. *
  1191. * @param float $amount
  1192. * @return string
  1193. */
  1194. public function formatPrice($amount)
  1195. {
  1196. return $this->getOrder()->getBaseCurrency()->formatTxt($amount);
  1197. }
  1198. /**
  1199. * Lookup an authorization transaction using parent transaction id, if set
  1200. *
  1201. * @return Transaction|false
  1202. */
  1203. public function getAuthorizationTransaction()
  1204. {
  1205. return $this->transactionManager->getAuthorizationTransaction(
  1206. $this->getParentTransactionId(),
  1207. $this->getId(),
  1208. $this->getOrder()->getId()
  1209. );
  1210. }
  1211. /**
  1212. * Decide whether authorization transaction may close (if the amount to capture will cover entire order)
  1213. *
  1214. * @param float $amountToCapture
  1215. * @return bool
  1216. */
  1217. public function isCaptureFinal($amountToCapture)
  1218. {
  1219. $total = $this->getOrder()->getBaseTotalDue();
  1220. return $this->formatAmount($total, true) == $this->formatAmount($amountToCapture, true);
  1221. }
  1222. /**
  1223. * Check whether payment currency corresponds to order currency
  1224. *
  1225. * @return bool
  1226. */
  1227. public function isSameCurrency()
  1228. {
  1229. return !$this->getCurrencyCode() || $this->getCurrencyCode() == $this->getOrder()->getBaseCurrencyCode();
  1230. }
  1231. /**
  1232. * Additional transaction info setter
  1233. *
  1234. * @param string $key
  1235. * @param string $value
  1236. * @return void
  1237. */
  1238. public function setTransactionAdditionalInfo($key, $value)
  1239. {
  1240. $this->transactionAdditionalInfo[$key] = $value;
  1241. }
  1242. /**
  1243. * Additional transaction info setter
  1244. *
  1245. * @return array
  1246. */
  1247. public function getTransactionAdditionalInfo()
  1248. {
  1249. return $this->transactionAdditionalInfo;
  1250. }
  1251. /**
  1252. * Reset transaction additional info property
  1253. *
  1254. * @return $this
  1255. */
  1256. public function resetTransactionAdditionalInfo()
  1257. {
  1258. $this->transactionAdditionalInfo = [];
  1259. return $this;
  1260. }
  1261. /**
  1262. * Prepare credit memo
  1263. *
  1264. * @param float $amount
  1265. * @param float $baseGrandTotal
  1266. * @param false|Invoice $invoice
  1267. * @return mixed
  1268. */
  1269. protected function prepareCreditMemo($amount, $baseGrandTotal, $invoice)
  1270. {
  1271. $entity = $invoice ?: $this->getOrder();
  1272. if ($entity->getBaseTotalRefunded() > 0) {
  1273. $adjustment = ['adjustment_positive' => $amount];
  1274. } else {
  1275. $adjustment = ['adjustment_negative' => $baseGrandTotal - $amount];
  1276. }
  1277. if ($invoice) {
  1278. $creditMemo = $this->creditmemoFactory->createByInvoice($invoice, $adjustment);
  1279. } else {
  1280. $creditMemo = $this->creditmemoFactory->createByOrder($this->getOrder(), $adjustment);
  1281. }
  1282. if ($creditMemo) {
  1283. $totalRefunded = $entity->getBaseTotalRefunded() + $creditMemo->getBaseGrandTotal();
  1284. $this->setShouldCloseParentTransaction($entity->getBaseGrandTotal() <= $totalRefunded);
  1285. }
  1286. return $creditMemo;
  1287. }
  1288. /**
  1289. * Checks if transaction exists
  1290. *
  1291. * @return bool
  1292. */
  1293. protected function checkIfTransactionExists()
  1294. {
  1295. return $this->transactionManager->isTransactionExists(
  1296. $this->getTransactionId(),
  1297. $this->getId(),
  1298. $this->getOrder()->getId()
  1299. );
  1300. }
  1301. /**
  1302. * Return invoice model for transaction
  1303. *
  1304. * @param string $transactionId
  1305. * @return Invoice|false
  1306. */
  1307. protected function _getInvoiceForTransactionId($transactionId)
  1308. {
  1309. foreach ($this->getOrder()->getInvoiceCollection() as $invoice) {
  1310. if ($invoice->getTransactionId() == $transactionId) {
  1311. $invoice->load($invoice->getId());
  1312. // to make sure all data will properly load (maybe not required)
  1313. return $invoice;
  1314. }
  1315. }
  1316. foreach ($this->getOrder()->getInvoiceCollection() as $invoice) {
  1317. if ($invoice->getState() == \Magento\Sales\Model\Order\Invoice::STATE_OPEN
  1318. && $invoice->load($invoice->getId())
  1319. ) {
  1320. $invoice->setTransactionId($transactionId);
  1321. return $invoice;
  1322. }
  1323. }
  1324. return false;
  1325. }
  1326. /**
  1327. * Get order state resolver instance.
  1328. *
  1329. * @deprecated 101.0.0
  1330. * @return OrderStateResolverInterface
  1331. */
  1332. private function getOrderStateResolver()
  1333. {
  1334. if ($this->orderStateResolver === null) {
  1335. $this->orderStateResolver = ObjectManager::getInstance()->get(OrderStateResolverInterface::class);
  1336. }
  1337. return $this->orderStateResolver;
  1338. }
  1339. //@codeCoverageIgnoreStart
  1340. /**
  1341. * Returns account_status
  1342. *
  1343. * @return string
  1344. */
  1345. public function getAccountStatus()
  1346. {
  1347. return $this->getData(OrderPaymentInterface::ACCOUNT_STATUS);
  1348. }
  1349. /**
  1350. * Returns additional_data
  1351. *
  1352. * @return string
  1353. */
  1354. public function getAdditionalData()
  1355. {
  1356. return $this->getData(OrderPaymentInterface::ADDITIONAL_DATA);
  1357. }
  1358. /**
  1359. * Returns address_status
  1360. *
  1361. * @return string
  1362. */
  1363. public function getAddressStatus()
  1364. {
  1365. return $this->getData(OrderPaymentInterface::ADDRESS_STATUS);
  1366. }
  1367. /**
  1368. * Returns amount_authorized
  1369. *
  1370. * @return float
  1371. */
  1372. public function getAmountAuthorized()
  1373. {
  1374. return $this->getData(OrderPaymentInterface::AMOUNT_AUTHORIZED);
  1375. }
  1376. /**
  1377. * Returns amount_canceled
  1378. *
  1379. * @return float
  1380. */
  1381. public function getAmountCanceled()
  1382. {
  1383. return $this->getData(OrderPaymentInterface::AMOUNT_CANCELED);
  1384. }
  1385. /**
  1386. * Returns amount_ordered
  1387. *
  1388. * @return float
  1389. */
  1390. public function getAmountOrdered()
  1391. {
  1392. return $this->getData(OrderPaymentInterface::AMOUNT_ORDERED);
  1393. }
  1394. /**
  1395. * Returns amount_paid
  1396. *
  1397. * @return float
  1398. */
  1399. public function getAmountPaid()
  1400. {
  1401. return $this->getData(OrderPaymentInterface::AMOUNT_PAID);
  1402. }
  1403. /**
  1404. * Returns amount_refunded
  1405. *
  1406. * @return float
  1407. */
  1408. public function getAmountRefunded()
  1409. {
  1410. return $this->getData(OrderPaymentInterface::AMOUNT_REFUNDED);
  1411. }
  1412. /**
  1413. * Returns anet_trans_method
  1414. *
  1415. * @return string
  1416. */
  1417. public function getAnetTransMethod()
  1418. {
  1419. return $this->getData(OrderPaymentInterface::ANET_TRANS_METHOD);
  1420. }
  1421. /**
  1422. * Returns base_amount_authorized
  1423. *
  1424. * @return float
  1425. */
  1426. public function getBaseAmountAuthorized()
  1427. {
  1428. return $this->getData(OrderPaymentInterface::BASE_AMOUNT_AUTHORIZED);
  1429. }
  1430. /**
  1431. * Returns base_amount_canceled
  1432. *
  1433. * @return float
  1434. */
  1435. public function getBaseAmountCanceled()
  1436. {
  1437. return $this->getData(OrderPaymentInterface::BASE_AMOUNT_CANCELED);
  1438. }
  1439. /**
  1440. * Returns base_amount_ordered
  1441. *
  1442. * @return float
  1443. */
  1444. public function getBaseAmountOrdered()
  1445. {
  1446. return $this->getData(OrderPaymentInterface::BASE_AMOUNT_ORDERED);
  1447. }
  1448. /**
  1449. * Returns base_amount_paid
  1450. *
  1451. * @return float
  1452. */
  1453. public function getBaseAmountPaid()
  1454. {
  1455. return $this->getData(OrderPaymentInterface::BASE_AMOUNT_PAID);
  1456. }
  1457. /**
  1458. * Returns base_amount_paid_online
  1459. *
  1460. * @return float
  1461. */
  1462. public function getBaseAmountPaidOnline()
  1463. {
  1464. return $this->getData(OrderPaymentInterface::BASE_AMOUNT_PAID_ONLINE);
  1465. }
  1466. /**
  1467. * Returns base_amount_refunded
  1468. *
  1469. * @return float
  1470. */
  1471. public function getBaseAmountRefunded()
  1472. {
  1473. return $this->getData(OrderPaymentInterface::BASE_AMOUNT_REFUNDED);
  1474. }
  1475. /**
  1476. * Returns base_amount_refunded_online
  1477. *
  1478. * @return float
  1479. */
  1480. public function getBaseAmountRefundedOnline()
  1481. {
  1482. return $this->getData(OrderPaymentInterface::BASE_AMOUNT_REFUNDED_ONLINE);
  1483. }
  1484. /**
  1485. * Returns base_shipping_amount
  1486. *
  1487. * @return float
  1488. */
  1489. public function getBaseShippingAmount()
  1490. {
  1491. return $this->getData(OrderPaymentInterface::BASE_SHIPPING_AMOUNT);
  1492. }
  1493. /**
  1494. * Returns base_shipping_captured
  1495. *
  1496. * @return float
  1497. */
  1498. public function getBaseShippingCaptured()
  1499. {
  1500. return $this->getData(OrderPaymentInterface::BASE_SHIPPING_CAPTURED);
  1501. }
  1502. /**
  1503. * Returns base_shipping_refunded
  1504. *
  1505. * @return float
  1506. */
  1507. public function getBaseShippingRefunded()
  1508. {
  1509. return $this->getData(OrderPaymentInterface::BASE_SHIPPING_REFUNDED);
  1510. }
  1511. /**
  1512. * Returns cc_approval
  1513. *
  1514. * @return string
  1515. */
  1516. public function getCcApproval()
  1517. {
  1518. return $this->getData(OrderPaymentInterface::CC_APPROVAL);
  1519. }
  1520. /**
  1521. * Returns cc_avs_status
  1522. *
  1523. * @return string
  1524. */
  1525. public function getCcAvsStatus()
  1526. {
  1527. return $this->getData(OrderPaymentInterface::CC_AVS_STATUS);
  1528. }
  1529. /**
  1530. * Returns cc_cid_status
  1531. *
  1532. * @return string
  1533. */
  1534. public function getCcCidStatus()
  1535. {
  1536. return $this->getData(OrderPaymentInterface::CC_CID_STATUS);
  1537. }
  1538. /**
  1539. * Returns cc_debug_request_body
  1540. *
  1541. * @return string
  1542. */
  1543. public function getCcDebugRequestBody()
  1544. {
  1545. return $this->getData(OrderPaymentInterface::CC_DEBUG_REQUEST_BODY);
  1546. }
  1547. /**
  1548. * Returns cc_debug_response_body
  1549. *
  1550. * @return string
  1551. */
  1552. public function getCcDebugResponseBody()
  1553. {
  1554. return $this->getData(OrderPaymentInterface::CC_DEBUG_RESPONSE_BODY);
  1555. }
  1556. /**
  1557. * Returns cc_debug_response_serialized
  1558. *
  1559. * @return string
  1560. */
  1561. public function getCcDebugResponseSerialized()
  1562. {
  1563. return $this->getData(OrderPaymentInterface::CC_DEBUG_RESPONSE_SERIALIZED);
  1564. }
  1565. /**
  1566. * Returns cc_exp_month
  1567. *
  1568. * @return string
  1569. */
  1570. public function getCcExpMonth()
  1571. {
  1572. return $this->getData(OrderPaymentInterface::CC_EXP_MONTH);
  1573. }
  1574. /**
  1575. * Returns cc_exp_year
  1576. *
  1577. * @return string
  1578. */
  1579. public function getCcExpYear()
  1580. {
  1581. return $this->getData(OrderPaymentInterface::CC_EXP_YEAR);
  1582. }
  1583. /**
  1584. * Returns cc_last_4
  1585. *
  1586. * @return string
  1587. */
  1588. public function getCcLast4()
  1589. {
  1590. return $this->getData(OrderPaymentInterface::CC_LAST_4);
  1591. }
  1592. /**
  1593. * Returns cc_number_enc
  1594. *
  1595. * @return string
  1596. */
  1597. public function getCcNumberEnc()
  1598. {
  1599. return $this->getData(OrderPaymentInterface::CC_NUMBER_ENC);
  1600. }
  1601. /**
  1602. * Returns cc_owner
  1603. *
  1604. * @return string
  1605. */
  1606. public function getCcOwner()
  1607. {
  1608. return $this->getData(OrderPaymentInterface::CC_OWNER);
  1609. }
  1610. /**
  1611. * Returns cc_secure_verify
  1612. *
  1613. * @return string
  1614. */
  1615. public function getCcSecureVerify()
  1616. {
  1617. return $this->getData(OrderPaymentInterface::CC_SECURE_VERIFY);
  1618. }
  1619. /**
  1620. * Returns cc_ss_issue
  1621. *
  1622. * @return string
  1623. * @deprecated 100.1.0 unused
  1624. */
  1625. public function getCcSsIssue()
  1626. {
  1627. return $this->getData(OrderPaymentInterface::CC_SS_ISSUE);
  1628. }
  1629. /**
  1630. * Returns cc_ss_start_month
  1631. *
  1632. * @return string
  1633. * @deprecated 100.1.0 unused
  1634. */
  1635. public function getCcSsStartMonth()
  1636. {
  1637. return $this->getData(OrderPaymentInterface::CC_SS_START_MONTH);
  1638. }
  1639. /**
  1640. * Returns cc_ss_start_year
  1641. *
  1642. * @return string
  1643. * @deprecated 100.1.0 unused
  1644. */
  1645. public function getCcSsStartYear()
  1646. {
  1647. return $this->getData(OrderPaymentInterface::CC_SS_START_YEAR);
  1648. }
  1649. /**
  1650. * Returns cc_status
  1651. *
  1652. * @return string
  1653. */
  1654. public function getCcStatus()
  1655. {
  1656. return $this->getData(OrderPaymentInterface::CC_STATUS);
  1657. }
  1658. /**
  1659. * Returns cc_status_description
  1660. *
  1661. * @return string
  1662. */
  1663. public function getCcStatusDescription()
  1664. {
  1665. return $this->getData(OrderPaymentInterface::CC_STATUS_DESCRIPTION);
  1666. }
  1667. /**
  1668. * Returns cc_trans_id
  1669. *
  1670. * @return string
  1671. */
  1672. public function getCcTransId()
  1673. {
  1674. return $this->getData(OrderPaymentInterface::CC_TRANS_ID);
  1675. }
  1676. /**
  1677. * Returns cc_type
  1678. *
  1679. * @return string
  1680. */
  1681. public function getCcType()
  1682. {
  1683. return $this->getData(OrderPaymentInterface::CC_TYPE);
  1684. }
  1685. /**
  1686. * Returns echeck_account_name
  1687. *
  1688. * @return string
  1689. */
  1690. public function getEcheckAccountName()
  1691. {
  1692. return $this->getData(OrderPaymentInterface::ECHECK_ACCOUNT_NAME);
  1693. }
  1694. /**
  1695. * Returns echeck_account_type
  1696. *
  1697. * @return string
  1698. */
  1699. public function getEcheckAccountType()
  1700. {
  1701. return $this->getData(OrderPaymentInterface::ECHECK_ACCOUNT_TYPE);
  1702. }
  1703. /**
  1704. * Returns echeck_bank_name
  1705. *
  1706. * @return string
  1707. */
  1708. public function getEcheckBankName()
  1709. {
  1710. return $this->getData(OrderPaymentInterface::ECHECK_BANK_NAME);
  1711. }
  1712. /**
  1713. * Returns echeck_routing_number
  1714. *
  1715. * @return string
  1716. */
  1717. public function getEcheckRoutingNumber()
  1718. {
  1719. return $this->getData(OrderPaymentInterface::ECHECK_ROUTING_NUMBER);
  1720. }
  1721. /**
  1722. * Returns echeck_type
  1723. *
  1724. * @return string
  1725. */
  1726. public function getEcheckType()
  1727. {
  1728. return $this->getData(OrderPaymentInterface::ECHECK_TYPE);
  1729. }
  1730. /**
  1731. * Returns last_trans_id
  1732. *
  1733. * @return string
  1734. */
  1735. public function getLastTransId()
  1736. {
  1737. return $this->getData(OrderPaymentInterface::LAST_TRANS_ID);
  1738. }
  1739. /**
  1740. * Returns method
  1741. *
  1742. * @return string
  1743. */
  1744. public function getMethod()
  1745. {
  1746. return $this->getData(OrderPaymentInterface::METHOD);
  1747. }
  1748. /**
  1749. * Returns parent_id
  1750. *
  1751. * @return int
  1752. */
  1753. public function getParentId()
  1754. {
  1755. return $this->getData(OrderPaymentInterface::PARENT_ID);
  1756. }
  1757. /**
  1758. * Returns po_number
  1759. *
  1760. * @return string
  1761. */
  1762. public function getPoNumber()
  1763. {
  1764. return $this->getData(OrderPaymentInterface::PO_NUMBER);
  1765. }
  1766. /**
  1767. * Returns protection_eligibility
  1768. *
  1769. * @return string
  1770. */
  1771. public function getProtectionEligibility()
  1772. {
  1773. return $this->getData(OrderPaymentInterface::PROTECTION_ELIGIBILITY);
  1774. }
  1775. /**
  1776. * Returns quote_payment_id
  1777. *
  1778. * @return int
  1779. */
  1780. public function getQuotePaymentId()
  1781. {
  1782. return $this->getData(OrderPaymentInterface::QUOTE_PAYMENT_ID);
  1783. }
  1784. /**
  1785. * Returns shipping_amount
  1786. *
  1787. * @return float
  1788. */
  1789. public function getShippingAmount()
  1790. {
  1791. return $this->getData(OrderPaymentInterface::SHIPPING_AMOUNT);
  1792. }
  1793. /**
  1794. * Returns shipping_captured
  1795. *
  1796. * @return float
  1797. */
  1798. public function getShippingCaptured()
  1799. {
  1800. return $this->getData(OrderPaymentInterface::SHIPPING_CAPTURED);
  1801. }
  1802. /**
  1803. * Returns shipping_refunded
  1804. *
  1805. * @return float
  1806. */
  1807. public function getShippingRefunded()
  1808. {
  1809. return $this->getData(OrderPaymentInterface::SHIPPING_REFUNDED);
  1810. }
  1811. /**
  1812. * @inheritdoc
  1813. */
  1814. public function setParentId($id)
  1815. {
  1816. return $this->setData(OrderPaymentInterface::PARENT_ID, $id);
  1817. }
  1818. /**
  1819. * @inheritdoc
  1820. */
  1821. public function setBaseShippingCaptured($baseShippingCaptured)
  1822. {
  1823. return $this->setData(OrderPaymentInterface::BASE_SHIPPING_CAPTURED, $baseShippingCaptured);
  1824. }
  1825. /**
  1826. * @inheritdoc
  1827. */
  1828. public function setShippingCaptured($shippingCaptured)
  1829. {
  1830. return $this->setData(OrderPaymentInterface::SHIPPING_CAPTURED, $shippingCaptured);
  1831. }
  1832. /**
  1833. * @inheritdoc
  1834. */
  1835. public function setAmountRefunded($amountRefunded)
  1836. {
  1837. return $this->setData(OrderPaymentInterface::AMOUNT_REFUNDED, $amountRefunded);
  1838. }
  1839. /**
  1840. * @inheritdoc
  1841. */
  1842. public function setBaseAmountPaid($baseAmountPaid)
  1843. {
  1844. return $this->setData(OrderPaymentInterface::BASE_AMOUNT_PAID, $baseAmountPaid);
  1845. }
  1846. /**
  1847. * @inheritdoc
  1848. */
  1849. public function setAmountCanceled($amountCanceled)
  1850. {
  1851. return $this->setData(OrderPaymentInterface::AMOUNT_CANCELED, $amountCanceled);
  1852. }
  1853. /**
  1854. * @inheritdoc
  1855. */
  1856. public function setBaseAmountAuthorized($baseAmountAuthorized)
  1857. {
  1858. return $this->setData(OrderPaymentInterface::BASE_AMOUNT_AUTHORIZED, $baseAmountAuthorized);
  1859. }
  1860. /**
  1861. * @inheritdoc
  1862. */
  1863. public function setBaseAmountPaidOnline($baseAmountPaidOnline)
  1864. {
  1865. return $this->setData(OrderPaymentInterface::BASE_AMOUNT_PAID_ONLINE, $baseAmountPaidOnline);
  1866. }
  1867. /**
  1868. * @inheritdoc
  1869. */
  1870. public function setBaseAmountRefundedOnline($baseAmountRefundedOnline)
  1871. {
  1872. return $this->setData(OrderPaymentInterface::BASE_AMOUNT_REFUNDED_ONLINE, $baseAmountRefundedOnline);
  1873. }
  1874. /**
  1875. * @inheritdoc
  1876. */
  1877. public function setBaseShippingAmount($amount)
  1878. {
  1879. return $this->setData(OrderPaymentInterface::BASE_SHIPPING_AMOUNT, $amount);
  1880. }
  1881. /**
  1882. * @inheritdoc
  1883. */
  1884. public function setShippingAmount($amount)
  1885. {
  1886. return $this->setData(OrderPaymentInterface::SHIPPING_AMOUNT, $amount);
  1887. }
  1888. /**
  1889. * @inheritdoc
  1890. */
  1891. public function setAmountPaid($amountPaid)
  1892. {
  1893. return $this->setData(OrderPaymentInterface::AMOUNT_PAID, $amountPaid);
  1894. }
  1895. /**
  1896. * @inheritdoc
  1897. */
  1898. public function setAmountAuthorized($amountAuthorized)
  1899. {
  1900. return $this->setData(OrderPaymentInterface::AMOUNT_AUTHORIZED, $amountAuthorized);
  1901. }
  1902. /**
  1903. * @inheritdoc
  1904. */
  1905. public function setBaseAmountOrdered($baseAmountOrdered)
  1906. {
  1907. return $this->setData(OrderPaymentInterface::BASE_AMOUNT_ORDERED, $baseAmountOrdered);
  1908. }
  1909. /**
  1910. * @inheritdoc
  1911. */
  1912. public function setBaseShippingRefunded($baseShippingRefunded)
  1913. {
  1914. return $this->setData(OrderPaymentInterface::BASE_SHIPPING_REFUNDED, $baseShippingRefunded);
  1915. }
  1916. /**
  1917. * @inheritdoc
  1918. */
  1919. public function setShippingRefunded($shippingRefunded)
  1920. {
  1921. return $this->setData(OrderPaymentInterface::SHIPPING_REFUNDED, $shippingRefunded);
  1922. }
  1923. /**
  1924. * @inheritdoc
  1925. */
  1926. public function setBaseAmountRefunded($baseAmountRefunded)
  1927. {
  1928. return $this->setData(OrderPaymentInterface::BASE_AMOUNT_REFUNDED, $baseAmountRefunded);
  1929. }
  1930. /**
  1931. * @inheritdoc
  1932. */
  1933. public function setAmountOrdered($amountOrdered)
  1934. {
  1935. return $this->setData(OrderPaymentInterface::AMOUNT_ORDERED, $amountOrdered);
  1936. }
  1937. /**
  1938. * @inheritdoc
  1939. */
  1940. public function setBaseAmountCanceled($baseAmountCanceled)
  1941. {
  1942. return $this->setData(OrderPaymentInterface::BASE_AMOUNT_CANCELED, $baseAmountCanceled);
  1943. }
  1944. /**
  1945. * @inheritdoc
  1946. */
  1947. public function setQuotePaymentId($id)
  1948. {
  1949. return $this->setData(OrderPaymentInterface::QUOTE_PAYMENT_ID, $id);
  1950. }
  1951. /**
  1952. * @inheritdoc
  1953. */
  1954. public function setAdditionalData($additionalData)
  1955. {
  1956. return $this->setData(OrderPaymentInterface::ADDITIONAL_DATA, $additionalData);
  1957. }
  1958. /**
  1959. * @inheritdoc
  1960. */
  1961. public function setCcExpMonth($ccExpMonth)
  1962. {
  1963. return $this->setData(OrderPaymentInterface::CC_EXP_MONTH, $ccExpMonth);
  1964. }
  1965. /**
  1966. * @inheritdoc
  1967. * @deprecated 100.1.0 unused
  1968. */
  1969. public function setCcSsStartYear($ccSsStartYear)
  1970. {
  1971. return $this->setData(OrderPaymentInterface::CC_SS_START_YEAR, $ccSsStartYear);
  1972. }
  1973. /**
  1974. * @inheritdoc
  1975. */
  1976. public function setEcheckBankName($echeckBankName)
  1977. {
  1978. return $this->setData(OrderPaymentInterface::ECHECK_BANK_NAME, $echeckBankName);
  1979. }
  1980. /**
  1981. * @inheritdoc
  1982. */
  1983. public function setMethod($method)
  1984. {
  1985. return $this->setData(OrderPaymentInterface::METHOD, $method);
  1986. }
  1987. /**
  1988. * @inheritdoc
  1989. */
  1990. public function setCcDebugRequestBody($ccDebugRequestBody)
  1991. {
  1992. return $this->setData(OrderPaymentInterface::CC_DEBUG_REQUEST_BODY, $ccDebugRequestBody);
  1993. }
  1994. /**
  1995. * @inheritdoc
  1996. */
  1997. public function setCcSecureVerify($ccSecureVerify)
  1998. {
  1999. return $this->setData(OrderPaymentInterface::CC_SECURE_VERIFY, $ccSecureVerify);
  2000. }
  2001. /**
  2002. * @inheritdoc
  2003. */
  2004. public function setProtectionEligibility($protectionEligibility)
  2005. {
  2006. return $this->setData(OrderPaymentInterface::PROTECTION_ELIGIBILITY, $protectionEligibility);
  2007. }
  2008. /**
  2009. * @inheritdoc
  2010. */
  2011. public function setCcApproval($ccApproval)
  2012. {
  2013. return $this->setData(OrderPaymentInterface::CC_APPROVAL, $ccApproval);
  2014. }
  2015. /**
  2016. * @inheritdoc
  2017. */
  2018. public function setCcLast4($ccLast4)
  2019. {
  2020. return $this->setData(OrderPaymentInterface::CC_LAST_4, $ccLast4);
  2021. }
  2022. /**
  2023. * @inheritdoc
  2024. */
  2025. public function setCcStatusDescription($description)
  2026. {
  2027. return $this->setData(OrderPaymentInterface::CC_STATUS_DESCRIPTION, $description);
  2028. }
  2029. /**
  2030. * @inheritdoc
  2031. */
  2032. public function setEcheckType($echeckType)
  2033. {
  2034. return $this->setData(OrderPaymentInterface::ECHECK_TYPE, $echeckType);
  2035. }
  2036. /**
  2037. * @inheritdoc
  2038. */
  2039. public function setCcDebugResponseSerialized($ccDebugResponseSerialized)
  2040. {
  2041. return $this->setData(OrderPaymentInterface::CC_DEBUG_RESPONSE_SERIALIZED, $ccDebugResponseSerialized);
  2042. }
  2043. /**
  2044. * @inheritdoc
  2045. * @deprecated 100.1.0 unused
  2046. */
  2047. public function setCcSsStartMonth($ccSsStartMonth)
  2048. {
  2049. return $this->setData(OrderPaymentInterface::CC_SS_START_MONTH, $ccSsStartMonth);
  2050. }
  2051. /**
  2052. * @inheritdoc
  2053. */
  2054. public function setEcheckAccountType($echeckAccountType)
  2055. {
  2056. return $this->setData(OrderPaymentInterface::ECHECK_ACCOUNT_TYPE, $echeckAccountType);
  2057. }
  2058. /**
  2059. * @inheritdoc
  2060. */
  2061. public function setLastTransId($id)
  2062. {
  2063. return $this->setData(OrderPaymentInterface::LAST_TRANS_ID, $id);
  2064. }
  2065. /**
  2066. * @inheritdoc
  2067. */
  2068. public function setCcCidStatus($ccCidStatus)
  2069. {
  2070. return $this->setData(OrderPaymentInterface::CC_CID_STATUS, $ccCidStatus);
  2071. }
  2072. /**
  2073. * @inheritdoc
  2074. */
  2075. public function setCcOwner($ccOwner)
  2076. {
  2077. return $this->setData(OrderPaymentInterface::CC_OWNER, $ccOwner);
  2078. }
  2079. /**
  2080. * @inheritdoc
  2081. */
  2082. public function setCcType($ccType)
  2083. {
  2084. return $this->setData(OrderPaymentInterface::CC_TYPE, $ccType);
  2085. }
  2086. /**
  2087. * @inheritdoc
  2088. */
  2089. public function setPoNumber($poNumber)
  2090. {
  2091. return $this->setData(OrderPaymentInterface::PO_NUMBER, $poNumber);
  2092. }
  2093. /**
  2094. * @inheritdoc
  2095. */
  2096. public function setCcExpYear($ccExpYear)
  2097. {
  2098. return $this->setData(OrderPaymentInterface::CC_EXP_YEAR, $ccExpYear);
  2099. }
  2100. /**
  2101. * @inheritdoc
  2102. */
  2103. public function setCcStatus($ccStatus)
  2104. {
  2105. return $this->setData(OrderPaymentInterface::CC_STATUS, $ccStatus);
  2106. }
  2107. /**
  2108. * @inheritdoc
  2109. */
  2110. public function setEcheckRoutingNumber($echeckRoutingNumber)
  2111. {
  2112. return $this->setData(OrderPaymentInterface::ECHECK_ROUTING_NUMBER, $echeckRoutingNumber);
  2113. }
  2114. /**
  2115. * @inheritdoc
  2116. */
  2117. public function setAccountStatus($accountStatus)
  2118. {
  2119. return $this->setData(OrderPaymentInterface::ACCOUNT_STATUS, $accountStatus);
  2120. }
  2121. /**
  2122. * @inheritdoc
  2123. */
  2124. public function setAnetTransMethod($anetTransMethod)
  2125. {
  2126. return $this->setData(OrderPaymentInterface::ANET_TRANS_METHOD, $anetTransMethod);
  2127. }
  2128. /**
  2129. * @inheritdoc
  2130. */
  2131. public function setCcDebugResponseBody($ccDebugResponseBody)
  2132. {
  2133. return $this->setData(OrderPaymentInterface::CC_DEBUG_RESPONSE_BODY, $ccDebugResponseBody);
  2134. }
  2135. /**
  2136. * @inheritdoc
  2137. * @deprecated 100.1.0 unused
  2138. */
  2139. public function setCcSsIssue($ccSsIssue)
  2140. {
  2141. return $this->setData(OrderPaymentInterface::CC_SS_ISSUE, $ccSsIssue);
  2142. }
  2143. /**
  2144. * @inheritdoc
  2145. */
  2146. public function setEcheckAccountName($echeckAccountName)
  2147. {
  2148. return $this->setData(OrderPaymentInterface::ECHECK_ACCOUNT_NAME, $echeckAccountName);
  2149. }
  2150. /**
  2151. * @inheritdoc
  2152. */
  2153. public function setCcAvsStatus($ccAvsStatus)
  2154. {
  2155. return $this->setData(OrderPaymentInterface::CC_AVS_STATUS, $ccAvsStatus);
  2156. }
  2157. /**
  2158. * @inheritdoc
  2159. */
  2160. public function setCcNumberEnc($ccNumberEnc)
  2161. {
  2162. return $this->setData(OrderPaymentInterface::CC_NUMBER_ENC, $ccNumberEnc);
  2163. }
  2164. /**
  2165. * @inheritdoc
  2166. */
  2167. public function setCcTransId($id)
  2168. {
  2169. return $this->setData(OrderPaymentInterface::CC_TRANS_ID, $id);
  2170. }
  2171. /**
  2172. * @inheritdoc
  2173. */
  2174. public function setAddressStatus($addressStatus)
  2175. {
  2176. return $this->setData(OrderPaymentInterface::ADDRESS_STATUS, $addressStatus);
  2177. }
  2178. /**
  2179. * @inheritdoc
  2180. *
  2181. * @return \Magento\Sales\Api\Data\OrderPaymentExtensionInterface|null
  2182. */
  2183. public function getExtensionAttributes()
  2184. {
  2185. return $this->_getExtensionAttributes();
  2186. }
  2187. /**
  2188. * @inheritdoc
  2189. *
  2190. * @param \Magento\Sales\Api\Data\OrderPaymentExtensionInterface $extensionAttributes
  2191. * @return $this
  2192. */
  2193. public function setExtensionAttributes(\Magento\Sales\Api\Data\OrderPaymentExtensionInterface $extensionAttributes)
  2194. {
  2195. return $this->_setExtensionAttributes($extensionAttributes);
  2196. }
  2197. /**
  2198. * Sets whether transaction is pending
  2199. *
  2200. * @param bool|int $flag
  2201. * @return $this
  2202. */
  2203. public function setIsTransactionPending($flag)
  2204. {
  2205. $this->setData('is_transaction_pending', (bool)$flag);
  2206. return $this;
  2207. }
  2208. /**
  2209. * Whether transaction is pending
  2210. *
  2211. * @return bool
  2212. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  2213. */
  2214. public function getIsTransactionPending()
  2215. {
  2216. return (bool)$this->getData('is_transaction_pending');
  2217. }
  2218. /**
  2219. * Sets whether fraud was detected
  2220. *
  2221. * @param bool|int $flag
  2222. * @return $this
  2223. */
  2224. public function setIsFraudDetected($flag)
  2225. {
  2226. $this->setData('is_fraud_detected', (bool)$flag);
  2227. return $this;
  2228. }
  2229. /**
  2230. * Whether fraud was detected
  2231. *
  2232. * @return bool
  2233. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  2234. */
  2235. public function getIsFraudDetected()
  2236. {
  2237. return (bool)$this->getData('is_fraud_detected');
  2238. }
  2239. /**
  2240. * Sets whether should close parent transaction
  2241. *
  2242. * @param int|bool $flag
  2243. * @return $this
  2244. */
  2245. public function setShouldCloseParentTransaction($flag)
  2246. {
  2247. $this->setData('should_close_parent_transaction', (bool)$flag);
  2248. return $this;
  2249. }
  2250. /**
  2251. * Whether should close parent transaction
  2252. *
  2253. * @return bool
  2254. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  2255. */
  2256. public function getShouldCloseParentTransaction()
  2257. {
  2258. return (bool)$this->getData('should_close_parent_transaction');
  2259. }
  2260. /**
  2261. * Set payment parent transaction id and current transaction id if it not set
  2262. *
  2263. * @param Transaction $transaction
  2264. * @return void
  2265. */
  2266. private function setTransactionIdsForRefund(Transaction $transaction)
  2267. {
  2268. if (!$this->getTransactionId()) {
  2269. $this->setTransactionId(
  2270. $this->transactionManager->generateTransactionId(
  2271. $this,
  2272. Transaction::TYPE_REFUND,
  2273. $transaction
  2274. )
  2275. );
  2276. }
  2277. $this->setParentTransactionId($transaction->getTxnId());
  2278. }
  2279. //@codeCoverageIgnoreEnd
  2280. /**
  2281. * Collects order invoices totals by provided keys.
  2282. *
  2283. * Returns result as {key: amount}.
  2284. *
  2285. * @param Order $order
  2286. * @param array $keys
  2287. * @return array
  2288. */
  2289. private function collectTotalAmounts(Order $order, array $keys)
  2290. {
  2291. $result = array_fill_keys($keys, 0.00);
  2292. $invoiceCollection = $order->getInvoiceCollection();
  2293. /** @var Invoice $invoice */
  2294. foreach ($invoiceCollection as $invoice) {
  2295. foreach ($keys as $key) {
  2296. $result[$key] += $invoice->getData($key);
  2297. }
  2298. }
  2299. return $result;
  2300. }
  2301. }