/**
  * Execute a command and return a response. Does not render
  * @param IRequest $Request
  * @throws \Exception
  * @return IResponse the execution response
  */
 function execute(IRequest $Request)
 {
     $SessionRequest = $Request;
     if (!$SessionRequest instanceof ISessionRequest) {
         throw new \Exception("Session required");
     }
     $Account = AbstractAccountType::loadFromSession($SessionRequest);
     if (!$Account instanceof AdministratorAccount) {
         throw new RequestException("Administrator account required");
     }
     $sourceOptions = array("Choose a Payment Source" => null);
     /** @var AbstractPaymentSource[] $SourceTypes */
     $SourceTypes = array();
     $SourceForms = array();
     foreach (AbstractPaymentSource::loadAllPaymentSourceTypes() as $SourceType) {
         $SourceTypes[$SourceType->getTypeName()] = $SourceType;
         $FieldSet = $SourceType->getFieldSet($Request);
         $FieldSet->setAttribute('data-' . self::PARAM_SOURCE_TYPE, $SourceType->getTypeName());
         $FieldSet->setAttribute('disabled', 'disabled');
         $SourceForms[] = $FieldSet;
         $sourceOptions[$SourceType->getDescription()] = $SourceType->getTypeName();
     }
     $Form = new HTMLForm(self::FORM_METHOD, $Request->getPath(), self::FORM_NAME, new HTMLMetaTag(HTMLMetaTag::META_TITLE, self::TITLE), new HTMLHeaderScript(__DIR__ . '/assets/payment-source.js'), new HTMLHeaderStyleSheet(__DIR__ . '/assets/payment-source.css'), new HTMLElement('fieldset', 'fieldset-create-payment-source', new HTMLElement('legend', 'legend-payment-source', self::TITLE), new HTMLElement('label', null, "Status<br/>", new HTMLSelectField(self::PARAM_SOURCE_STATUS, PaymentSourceEntry::$StatusOptions, new RequiredValidation())), "<br/><br/>", new HTMLElement('label', null, "Choose a Payment Source<br/>", new HTMLSelectField(self::PARAM_SOURCE_TYPE, $sourceOptions, new RequiredValidation())), "<br/><br/>", $SourceForms, "<br/>Submit:<br/>", new HTMLButton('submit', 'Submit', 'submit')), "<br/>");
     if (!$Request instanceof IFormRequest) {
         return $Form;
     }
     $status = $Form->validateField($Request, self::PARAM_SOURCE_STATUS);
     $sourceType = $Form->validateField($Request, self::PARAM_SOURCE_TYPE);
     $ChosenSource = $SourceTypes[$sourceType];
     $ChosenSource->validateRequest($Request, $Form);
     $id = PaymentSourceEntry::create($Request, $ChosenSource, $status);
     return new RedirectResponse(ManagePaymentSource::getRequestURL($id), "PaymentSource created successfully. Redirecting...", 5);
 }
 /**
  * Execute a command and return a response. Does not render
  * @param IRequest $Request
  * @return IResponse the execution response
  */
 function execute(IRequest $Request)
 {
     $Entry = PaymentSourceEntry::get($this->id);
     $Source = $Entry->getPaymentSource();
     $SourceForm = $Source->getFieldSet($Request);
     $SessionRequest = $Request;
     if (!$SessionRequest instanceof ISessionRequest) {
         throw new \Exception("Session required");
     }
     $Account = AbstractAccountType::loadFromSession($SessionRequest);
     if (!$Account instanceof AdministratorAccount) {
         throw new RequestException("Administrator account required");
     }
     $Form = new HTMLForm(self::FORM_METHOD, $Request->getPath(), self::FORM_NAME, new HTMLMetaTag(HTMLMetaTag::META_TITLE, self::TITLE), new HTMLElement('fieldset', new HTMLElement('legend', 'legend-submit', self::TITLE), new HTMLInputField(self::PARAM_ID, $this->id, 'hidden'), new HTMLElement('label', null, "Status<br/>", $Select = new HTMLSelectField(self::PARAM_SOURCE_STATUS, PaymentSourceEntry::$StatusOptions, new RequiredValidation())), "<br/><br/>", $SourceForm, "<br/>Submit:<br/>", new HTMLButton('submit', 'Submit', 'submit')), "<br/>");
     $Select->setInputValue($Entry->getStatus());
     if (!$Request instanceof IFormRequest) {
         return $Form;
     }
     $status = $Form->validateField($Request, self::PARAM_SOURCE_STATUS);
     $Source->validateRequest($Request, $Form);
     $Entry->update($Request, $Source, $status);
     return new RedirectResponse(ManagePaymentSource::getRequestURL($Entry->getID()), "Payment Source updated successfully. Redirecting...", 5);
 }
 /**
  * Execute a command and return a response. Does not render
  * @param IRequest $Request
  * @throws \Exception
  * @return IResponse the execution response
  */
 function execute(IRequest $Request)
 {
     $SessionRequest = $Request;
     if (!$SessionRequest instanceof ISessionRequest) {
         throw new \Exception("Session required");
     }
     $ProductForms = array();
     /** @var AbstractWallet[] $WalletTypes */
     $WalletTypes = array();
     $walletOptions = array('Choose a Wallet' => null);
     $WalletForms = array();
     $Products = ProductEntry::loadSessionProducts($SessionRequest);
     $productOptions = array('Choose a Product' => null);
     foreach ($Products as $ProductEntry) {
         $Product = $ProductEntry->getProduct();
         $productOptions[$Product->getTotalCost() . ' - ' . $Product->getProductTitle()] = $ProductEntry->getID();
         $Product = $ProductEntry->getProduct();
         $FieldSet = $Product->getOrderFieldSet($Request);
         $key = $ProductEntry->getID();
         $FieldSet->setAttribute('data-' . self::PARAM_PRODUCT_ID, $key);
         $ProductForms[] = $FieldSet;
         foreach ($Product->getWalletTypes() as $WalletType) {
             $key = $WalletType->getTypeName();
             $WalletTypes[$key] = $WalletType;
             $FieldSet = $WalletType->getFieldSet($Request);
             $FieldSet->setAttribute('data-' . self::PARAM_WALLET_ID, $key);
             $FieldSet->setAttribute('disabled', 'disabled');
             $WalletForms[] = $FieldSet;
             $walletOptions['New ' . $WalletType->getDescription()] = $key;
         }
     }
     $SessionWalletEntries = AbstractWallet::loadSessionWallets($SessionRequest);
     foreach ($SessionWalletEntries as $WalletEntry) {
         $Wallet = $WalletEntry->getWallet();
         $key = $WalletEntry->getID();
         $WalletTypes[$key] = $Wallet;
         $FieldSet = $Wallet->getFieldSet($Request);
         $FieldSet->setAttribute('data-' . self::PARAM_WALLET_ID, $key);
         $FieldSet->setAttribute('disabled', 'disabled');
         $WalletForms[] = $FieldSet;
         $walletOptions[$Wallet->getTitle() . ' - ' . $Wallet->getDescription()] = $key;
     }
     //		$walletTypes = Config::$AvailableWalletTypes;
     $Form = new HTMLForm(self::FORM_METHOD, self::FORM_ACTION, self::FORM_NAME, new HTMLMetaTag(HTMLMetaTag::META_TITLE, self::TITLE), new HTMLHeaderScript(__DIR__ . '/assets/transaction.js'), new HTMLHeaderStyleSheet(__DIR__ . '/assets/transaction.css'), new HTMLElement('fieldset', 'fieldset-create-transaction', new HTMLElement('legend', 'legend-wallet', self::TITLE), new HTMLElement('fieldset', 'fieldset-transaction', new HTMLElement('legend', 'legend-transaction', 'Transaction Details'), new HTMLElement('label', null, "Status<br/>", new HTMLSelectField(self::PARAM_TRANSACTION_STATUS, TransactionEntry::$StatusOptions, new RequiredValidation())), "<br/><br/>", new HTMLElement('label', null, "Product<br/>", new HTMLSelectField(self::PARAM_PRODUCT_ID, $productOptions, new RequiredValidation())), "<br/><br/>", $ProductForms), new HTMLElement('fieldset', 'fieldset-choose-wallet', new HTMLElement('legend', 'legend-wallet', 'Choose a Wallet'), new HTMLElement('label', null, new HTMLSelectField(self::PARAM_WALLET_ID, $walletOptions, new RequiredValidation())), "<br/><br/>", $WalletForms), "<br/><br/>Submit:<br/>", new HTMLButton('submit', 'Create Transaction', 'submit')), "<br/>");
     if (!$Request instanceof IFormRequest) {
         return $Form;
     }
     $Form->setFormValues($Request);
     //		$status = (int)$Form->validateField($Request, self::PARAM_TRANSACTION_STATUS);
     //		$email = $Form->validateField($Request, self::PARAM_TRANSACTION_EMAIL);
     $walletType = $Form->validateField($Request, self::PARAM_WALLET_ID);
     $ChosenWallet = $WalletTypes[$walletType];
     $ChosenWallet->validateRequest($Request, $Form);
     $productID = $Form->validateField($Request, self::PARAM_PRODUCT_ID);
     $ProductEntry = ProductEntry::get($productID);
     $Product = $ProductEntry->getProduct();
     $Invoice = $Product->createNewInvoice($Request, $ChosenWallet);
     $responses = array();
     foreach (PaymentSourceEntry::getActiveSources() as $PaymentSourceEntry) {
         $PaymentSource = $PaymentSourceEntry->getPaymentSource();
         if ($PaymentSource->supportsWalletType($ChosenWallet)) {
             $Response = $PaymentSource->executeWalletTransaction($ChosenWallet);
             $responses[] = $Response->getMessage();
             $paymentSourceID = $PaymentSourceEntry->getID();
             $walletID = WalletEntry::createOrUpdate($Request, $ChosenWallet);
             if ($Response->getCode() === TransactionEntry::STATUS_APPROVED) {
                 $status = TransactionEntry::STATUS_APPROVED;
                 $id = TransactionEntry::create($Request, $Invoice, $status, $walletID, $productID, $paymentSourceID);
                 ProfitEntry::update($Request, $id);
                 return new RedirectResponse(ManageTransaction::getRequestURL($id), "Transaction created successfully. Redirecting...", 5);
             } else {
                 $status = TransactionEntry::STATUS_DECLINED;
                 $id = TransactionEntry::create($Request, $Invoice, $status, $walletID, $productID, $paymentSourceID);
                 ProfitEntry::update($Request, $id);
             }
         }
     }
     throw new ValidationException($Form, "Transaction declined: \n\t" . implode("\n\t", $responses));
 }
 /**
  * Map data to the key map
  * @param IKeyMapper $Map the map inst to add data to
  * @internal param \CPath\Request\IRequest $Request
  * @internal param \CPath\Request\IRequest $Request
  * @return void
  */
 function mapKeys(IKeyMapper $Map)
 {
     $SourceEntry = PaymentSourceEntry::get($this->payment_source_id);
     // TODO: inefficient
     $Source = $SourceEntry->getPaymentSource();
     $Invoice = $this->getInvoice();
     $Product = $Invoice->getProduct();
     $Map->map('id', $this->getID());
     $Map->map('wallet', $this->getWalletID());
     $Map->map('email', $Invoice->getWallet()->getEmail());
     $Map->map('product', $this->getProductID(), $Product->getProductTitle());
     $Map->map('created', $this->getCreatedTimestamp());
     $Map->map('status', $this->getStatusText());
     $Map->map('amount', $this->getAmount());
     $Map->map('currency', $Source->getCurrency());
     //		$Map->map('description', $Product->getTypeDescription());
     //		$Map->map('payment-source', $Source->getTitle()); //todo: flags for mapping headers?
     //		$Map->map('product', $ProductEntry);
     //		$Map->map('product-title', $Product->getTitle());
     //		$Map->map('total', $Product->getTotalCost());
     //		$Map->map('type', $Product->getTypeName());
     //		$Map->map('invoice', $Invoice->getProduct()->);
 }