public function test_charge_fee_to_customer()
 {
     $openpayCustomers = $this->customerAdapter->getList();
     $testCustomerId = $openpayCustomers[count($openpayCustomers) - 1]->getId();
     $mockCardInfo = json_decode($this->getMockCardInfo(), true);
     $openpayCardToken = $this->tokenAdapter->store($mockCardInfo);
     $parameters = $this->getCardMockRequest();
     $parametersArray = json_decode($parameters, true);
     $parametersArray['token_id'] = $openpayCardToken['token_id'];
     $openpayCard = $this->cardAdapter->store($testCustomerId, $parametersArray);
     $chargeMockRequest = $this->getChargeMockRequest();
     $cardId = $openpayCard->getId();
     $parameters = json_decode($chargeMockRequest, true);
     $parameters['source_id'] = $cardId;
     $openpayChargeTransaction = $this->chargeAdapter->chargeCustomerCard($testCustomerId, $parameters);
     $feeMockRequest = $this->getFeeMockRequest();
     $parameters = json_decode($feeMockRequest, true);
     $parameters['customer_id'] = $testCustomerId;
     $now = new \DateTime();
     $parameters['order_id'] = 'TEST_ORDER-' . $now->format(\DateTime::ISO8601);
     $openpayFeeTransaction = $this->feeAdapter->chargeFee($parameters);
     $this->assertInstanceOf('Openpay\\Client\\Type\\OpenpayTransactionType', $openpayFeeTransaction);
     $this->assertNotEmpty($openpayFeeTransaction->getId(), 'Id is not empty');
     $this->assertNull($openpayFeeTransaction->getAuthorization(), 'Authorization number is not empty');
     $this->assertEquals($openpayFeeTransaction->getStatus(), 'completed');
     $this->assertEquals($openpayFeeTransaction->getOperationType(), 'out');
     $this->assertEquals($openpayFeeTransaction->getTransactionType(), 'fee');
     $this->assertGreaterThan(0, $openpayFeeTransaction->getAmount());
 }
 /**
  * @param InfoInterface $payment
  * @param float $amount
  * @return $this
  * @throws LocalizedException
  */
 public function capture(InfoInterface $payment, $amount)
 {
     parent::capture($payment, $amount);
     $customerId = $payment->getOrder()->getCustomerId();
     $customer = $this->customerRepository->getById($customerId);
     $openpayCustomerId = $customer->getCustomAttribute('openpay_customer_id')->getValue();
     $order = $payment->getOrder();
     $useOrderId = $this->getConfigData('useOrderId');
     $paymentLeyend = $this->getConfigData('paymentLeyend');
     $orderId = $order->getIncrementId();
     $params = ['customer_id' => $openpayCustomerId, 'amount' => $amount, 'description' => __($paymentLeyend)->getText(), 'order_id' => $useOrderId ? $orderId : null];
     try {
         $transaction = $this->feeAdapter->chargeFee($params);
         $payment->setTransactionId($transaction->getId())->setIsTransactionClosed(0);
         $this->openpayCustomerRepository->clearCustomerCache($openpayCustomerId);
     } catch (OpenpayException $e) {
         $this->debugData(['request' => $params, 'exception' => $e->getMessage()]);
         $this->_logger->error(__('Payment capturing error.'));
         throw new LocalizedException(new Phrase('[' . $e->getErrorCode() . ']' . $e->getMessage()));
     }
     return $this;
 }