public function accountSavedCards()
 {
     if (!Securionpay4WC::allowSavedCards()) {
         return;
     }
     $customer = new SecurionpayCustomer(get_current_user_id());
     if (!$customer || !$customer->getCards()) {
         return;
     }
     $this->handleDeleteCard($customer);
     $data = array('cards' => $customer->getCards());
     Securionpay4WC::getTemplate('saved-cards.php', $data);
 }
 public function process_payment($order_id)
 {
     $order = new WC_Order($order_id);
     try {
         $customer = null;
         $cardId = null;
         $newCard = false;
         if ($this->allowSavedCards()) {
             $customer = new SecurionpayCustomer(get_current_user_id());
             if (!$customer->getCustomerId()) {
                 $customerResponse = $this->api->createCustomer();
                 $customer->setCustomerId($customerResponse->getId());
                 $customer->save();
             }
             $cardIndex = isset($_POST['securionpay4wc-card']) ? $_POST['securionpay4wc-card'] : '';
             $card = $customer->getCard($cardIndex);
             if ($card) {
                 $cardId = $card['id'];
             }
         }
         if (!$cardId) {
             $newCard = true;
             $cardId = isset($_POST['securionpay4wc-token']) ? $_POST['securionpay4wc-token'] : '';
             if (!$cardId) {
                 $field = __('Credit Card Number', 'securionpay-for-woocommerce');
                 wc_add_notice($this->getValidationErrorMessage($field), 'error');
                 return;
             }
         }
         $charge = $this->api->createCharge($customer, $cardId, $order);
         if ($customer) {
             $card = $charge->getCard();
             if ($newCard) {
                 $customer->addCard($card->getId(), $card->getLast4(), $card->getExpMonth(), $card->getExpYear(), $card->getBrand());
             }
             $customer->setDefaultCardId($card->getId());
             $customer->save();
         }
         $this->completePayment($order, $charge->getId());
         return array('result' => 'success', 'redirect' => $this->get_return_url($order));
     } catch (Exception $e) {
         $this->api->handleException($e, '<span class="securionpay4wc-token-consumed"></span>');
     }
 }
 /**
  * @param SecurionpayCustomer $customer
  */
 public function deleteCard($customer, $cardId)
 {
     $this->gateway()->deleteCard($customer->getCustomerId(), $cardId);
 }