/** * Create new invoice with maximum qty for invoice for each item * register this invoice and capture * * @param OrderPaymentInterface $payment * @return Invoice */ protected function invoice(OrderPaymentInterface $payment) { /** @var Invoice $invoice */ $invoice = $payment->getOrder()->prepareInvoice(); $invoice->register(); if ($payment->getMethodInstance()->canCapture()) { $invoice->capture(); } $payment->getOrder()->addRelatedObject($invoice); return $invoice; }
/** * Authorizes payment. * * @param OrderPaymentInterface $payment * @param bool $isOnline * @param string|float $amount * @return OrderPaymentInterface */ public function authorize(OrderPaymentInterface $payment, $isOnline, $amount) { // check for authorization amount to be equal to grand total /** * @var $payment Payment */ $payment->setShouldCloseParentTransaction(false); $isSameCurrency = $payment->isSameCurrency(); if (!$isSameCurrency || !$payment->isCaptureFinal($amount)) { $payment->setIsFraudDetected(true); } // update totals $amount = $payment->formatAmount($amount, true); $payment->setBaseAmountAuthorized($amount); // do authorization $order = $payment->getOrder(); if ($isOnline) { // invoke authorization on gateway $method = $payment->getMethodInstance(); $method->setStore($order->getStoreId()); $method->authorize($payment, $amount); } $message = $this->stateCommand->execute($payment, $amount, $order); // update transactions, order state and add comments $transaction = $payment->addTransaction(Transaction::TYPE_AUTH); $message = $payment->prependMessage($message); $payment->addTransactionCommentsToOrder($transaction, $message); return $payment; }
/** * Decrease captured amount for partial payments. * * @param \Magento\Sales\Model\Order\Payment\Processor $subject * @param \Closure $proceed * @param \Magento\Sales\Api\Data\OrderPaymentInterface $payment * @param \Magento\Sales\Api\Data\InvoiceInterface|null $invoice * @return \Magento\Sales\Api\Data\OrderPaymentInterface */ public function aroundCapture(\Magento\Sales\Model\Order\Payment\Processor $subject, \Closure $proceed, \Magento\Sales\Api\Data\OrderPaymentInterface $payment, \Magento\Sales\Api\Data\InvoiceInterface $invoice = null) { if ($invoice) { /* try to find related record in quote registry */ $order = $payment->getOrder(); $quoteId = $order->getQuoteId(); $found = $this->_repoPartialQuote->getById($quoteId); if ($found) { /* decrease amount in invoice */ $partialBase = $found->getBasePartialAmount(); $partial = $found->getPartialAmount(); $grandTotalBase = $invoice->getBaseGrandTotal(); $grandTotal = $invoice->getGrandTotal(); $grandTotalBase -= $partialBase; $grandTotal -= $partial; $invoice->setBaseGrandTotal($grandTotalBase); $invoice->setGrandTotal($grandTotal); } } $result = $proceed($payment, $invoice); return $result; }
/** * @param OrderPaymentInterface $payment * @param string|float $amount * @return OrderPaymentInterface */ public function order(OrderPaymentInterface $payment, $amount) { /** * @var $payment Payment */ // update totals $amount = $payment->formatAmount($amount, true); // do ordering $order = $payment->getOrder(); $method = $payment->getMethodInstance(); $method->setStore($order->getStoreId()); $method->order($payment, $amount); if ($payment->getSkipOrderProcessing()) { return $payment; } $message = $this->stateCommand->execute($payment, $amount, $order); // update transactions, order state and add comments $transaction = $payment->addTransaction(Transaction::TYPE_ORDER); $message = $payment->prependMessage($message); $payment->addTransactionCommentsToOrder($transaction, $message); return $payment; }
/** * Get transaction with type order * * @param OrderPaymentInterface $payment * * @return false|\Magento\Sales\Api\Data\TransactionInterface */ protected function getOrderTransaction($payment) { return $this->transactionRepository->getByTransactionType(Transaction::TYPE_ORDER, $payment->getId(), $payment->getOrder()->getId()); }
/** * Get vault payment token entity * * @param \Braintree\Transaction $transaction * @param OrderPaymentInterface $payment * @return PaymentTokenInterface|null */ protected function getVaultPaymentToken(Transaction $transaction, OrderPaymentInterface $payment) { // Check token existing in gateway response $token = $transaction->creditCardDetails->token; if (empty($token)) { return null; } $order = $payment->getOrder(); /** @var PaymentTokenInterface $paymentToken */ $paymentToken = $this->paymentTokenFactory->create(); $paymentToken->setGatewayToken($token); $paymentToken->setCustomerId($order->getCustomerId()); $paymentToken->setPaymentMethodCode($payment->getMethod()); $paymentToken->setCreatedAt($order->getCreatedAt()); $paymentToken->setTokenDetails($this->convertDetailsToJSON(['type' => $this->getCreditCardType($transaction->creditCardDetails->cardType), 'maskedCC' => $transaction->creditCardDetails->last4, 'expirationDate' => $transaction->creditCardDetails->expirationDate])); return $paymentToken; }