/**
  * Create capture request
  * 
  * @param array $buildSubject
  * @return array
  */
 public function build(array $buildSubject)
 {
     /** @var \Magento\Payment\Gateway\Data\PaymentDataObject $paymentDataObject */
     $paymentDataObject = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject);
     $amount = \Magento\Payment\Gateway\Helper\SubjectReader::readAmount($buildSubject);
     $payment = $paymentDataObject->getPayment();
     $pspReference = $payment->getCcTransId();
     $currency = $payment->getOrder()->getOrderCurrencyCode();
     //format the amount to minor units
     $amount = $this->adyenHelper->formatAmount($amount, $currency);
     $modificationAmount = ['currency' => $currency, 'value' => $amount];
     return ["modificationAmount" => $modificationAmount, "reference" => $payment->getOrder()->getIncrementId(), "originalReference" => $pspReference];
 }
示例#2
0
 /**
  * Reads amount from subject
  *
  * @param array $subject
  * @return mixed
  */
 public function readAmount(array $subject)
 {
     return Helper\SubjectReader::readAmount($subject);
 }
 /**
  * @param array $buildSubject
  * @return array
  */
 public function build(array $buildSubject)
 {
     /** @var \Magento\Payment\Gateway\Data\PaymentDataObject $paymentDataObject */
     $paymentDataObject = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject);
     $amount = \Magento\Payment\Gateway\Helper\SubjectReader::readAmount($buildSubject);
     $order = $paymentDataObject->getOrder();
     $payment = $paymentDataObject->getPayment();
     $pspReference = $payment->getCcTransId();
     $currency = $payment->getOrder()->getOrderCurrencyCode();
     $storeId = $order->getStoreId();
     $merchantAccount = $this->adyenHelper->getAdyenAbstractConfigData("merchant_account", $storeId);
     $grandTotal = $payment->getOrder()->getGrandTotal();
     // check if it contains a split payment
     $orderPaymentCollection = $this->orderPaymentCollectionFactory->create()->addFieldToFilter('payment_id', $payment->getId());
     // partial refund if multiple payments check refund strategy
     if ($orderPaymentCollection->getSize() > 1) {
         $refundStrategy = $this->adyenHelper->getAdyenAbstractConfigData('split_payments_refund_strategy', $order->getStoreId());
         $ratio = null;
         if ($refundStrategy == "1") {
             // Refund in ascending order
             $orderPaymentCollection->addPaymentFilterAscending($payment->getId());
         } elseif ($refundStrategy == "2") {
             // Refund in descending order
             $orderPaymentCollection->addPaymentFilterDescending($payment->getId());
         } elseif ($refundStrategy == "3") {
             // refund based on ratio
             $ratio = $amount / $grandTotal;
             $orderPaymentCollection->addPaymentFilterAscending($payment->getId());
         }
         // loop over payment methods and refund them all
         $result = [];
         foreach ($orderPaymentCollection as $splitPayment) {
             // could be that not all the split payments need a refund
             if ($amount > 0) {
                 if ($ratio) {
                     // refund based on ratio calculate refund amount
                     $modificationAmount = $ratio * ($splitPayment->getAmount() - $splitPayment->getTotalRefunded());
                 } else {
                     // total authorised amount of the split payment
                     $splitPaymentAmount = $splitPayment->getAmount() - $splitPayment->getTotalRefunded();
                     // if rest amount is zero go to next payment
                     if (!$splitPaymentAmount > 0) {
                         continue;
                     }
                     // if refunded amount is greather then split payment amount do a full refund
                     if ($amount >= $splitPaymentAmount) {
                         $modificationAmount = $splitPaymentAmount;
                     } else {
                         $modificationAmount = $amount;
                     }
                     // update amount with rest of the available amount
                     $amount = $amount - $splitPaymentAmount;
                 }
                 $modificationAmountObject = ['currency' => $currency, 'value' => $this->adyenHelper->formatAmount($modificationAmount, $currency)];
                 $result[] = ["modificationAmount" => $modificationAmountObject, "reference" => $payment->getOrder()->getIncrementId(), "originalReference" => $splitPayment->getPspreference(), "merchantAccount" => $merchantAccount];
             }
         }
     } else {
         //format the amount to minor units
         $amount = $this->adyenHelper->formatAmount($amount, $currency);
         $modificationAmount = ['currency' => $currency, 'value' => $amount];
         $result = [["modificationAmount" => $modificationAmount, "reference" => $payment->getOrder()->getIncrementId(), "originalReference" => $pspReference, "merchantAccount" => $merchantAccount]];
     }
     return $result;
 }