public function saveAction()
 {
     if (null !== ($response = $this->checkAuth([AdminResources::MODULE], ["stripepayment"], AccessManager::UPDATE))) {
         return $response;
     }
     $baseForm = $this->createForm("stripepayment.configuration");
     $errorMessage = null;
     try {
         $form = $this->validateForm($baseForm);
         $data = $form->getData();
         StripePayment::setConfigValue(StripePaymentConfigValue::ENABLED, is_bool($data["enabled"]) ? (int) $data["enabled"] : $data["enabled"]);
         StripePayment::setConfigValue(StripePaymentConfigValue::SECRET_KEY, is_bool($data["secret_key"]) ? (int) $data["secret_key"] : $data["secret_key"]);
         StripePayment::setConfigValue(StripePaymentConfigValue::PUBLISHABLE_KEY, is_bool($data["publishable_key"]) ? (int) $data["publishable_key"] : $data["publishable_key"]);
     } catch (FormValidationException $ex) {
         // Invalid data entered
         $errorMessage = $this->createStandardFormValidationErrorMessage($ex);
     } catch (\Exception $ex) {
         // Any other error
         $errorMessage = $this->getTranslator()->trans('Sorry, an error occurred: %err', ['%err' => $ex->getMessage()], [], StripePayment::MESSAGE_DOMAIN);
     }
     if (null !== $errorMessage) {
         // Mark the form as with error
         $baseForm->setErrorMessage($errorMessage);
         // Send the form and the error to the parser
         $this->getParserContext()->addForm($baseForm)->setGeneralError($errorMessage);
     } else {
         $this->getParserContext()->set("success", true);
     }
     return $this->defaultAction();
 }
 /**
  * Send data to Stripe, save token, change order status & get response
  * @param OrderEvent $event
  * @throws \Exception
  * @throws \Propel\Runtime\Exception\PropelException
  */
 public function stripePayment(OrderEvent $event)
 {
     $order = $event->getPlacedOrder();
     $stripeModule = ModuleQuery::create()->findOneByCode($this->getStripeCode());
     if ($order->getPaymentModuleId() !== $stripeModule->getId()) {
         return;
     }
     $logMessage = null;
     $userMessage = null;
     \Stripe\Stripe::setApiKey(StripePayment::getConfigValue('secret_key'));
     try {
         // Check order amount
         $this->checkOrderAmount($order);
         // Create the charge on Stripe's servers - this will charge the user's card
         $this->stripeCharge($order);
         // Save Stripe token into order transaction reference
         $this->saveStripeToken($order);
         // Set 'paid' status to the order
         $this->changeOrderStatus($event);
         // Send payment confirmation mail
         $this->sendConfirmationMail($order);
     } catch (\Stripe\Error\Card $e) {
         // The card has been declined
         // FIXME Translate message here
         $logMessage = sprintf('Error paying order %d with Stripe. Card declined. Message: %s', $order->getId(), $e->getMessage());
         $userMessage = Translator::getInstance()->trans('Your card has been declined.', [], StripePayment::MESSAGE_DOMAIN);
     } catch (\Stripe\Error\RateLimit $e) {
         // Too many requests made to the API too quickly
         $logMessage = sprintf('Error paying order %d with Stripe. Too many requests. Message: %s', $order->getId(), $e->getMessage());
         $userMessage = Translator::getInstance()->trans('Too many requests too quickly.', [], StripePayment::MESSAGE_DOMAIN);
     } catch (\Stripe\Error\InvalidRequest $e) {
         // Invalid parameters were supplied to Stripe's API
         $logMessage = sprintf('Error paying order %d with Stripe. Invalid parameters. Message: %s', $order->getId(), $e->getMessage());
         $userMessage = Translator::getInstance()->trans('Invalid parameters were supplied to Stripe.', [], StripePayment::MESSAGE_DOMAIN);
     } catch (\Stripe\Error\Authentication $e) {
         // Authentication with Stripe's API failed
         // (maybe you changed API keys recently)
         $logMessage = sprintf('Error paying order %d with Stripe. Authentication failed: API key changed? Message: %s', $order->getId(), $e->getMessage());
         $userMessage = Translator::getInstance()->trans('Authentication with Stripe failed. Please contact administrators.', [], StripePayment::MESSAGE_DOMAIN);
     } catch (\Stripe\Error\ApiConnection $e) {
         // Network communication with Stripe failed
         $logMessage = sprintf('Error paying order %d with Stripe. Network communication failed. Message: %s', $order->getId(), $e->getMessage());
         $userMessage = Translator::getInstance()->trans('Network communication failed.', [], StripePayment::MESSAGE_DOMAIN);
     } catch (\Stripe\Error\Base $e) {
         // Display a very generic error to the user
         $logMessage = sprintf('Error paying order %d with Stripe. Message: %s', $order->getId(), $e->getMessage());
         $userMessage = Translator::getInstance()->trans('An error occurred with Stripe.', [], StripePayment::MESSAGE_DOMAIN);
     } catch (StripePaymentException $e) {
         // Amount shown to the user by Stripe & order amount are not equal
         $logMessage = sprintf('Error paying order %d with Stripe. Amounts are different. Message: %s', $order->getId(), $e->getMessage());
         $userMessage = $e->getMessage();
     } catch (\Exception $e) {
         // Something else happened, completely unrelated to Stripe
         $logMessage = sprintf('Error paying order %d with Stripe but maybe unrelated with it. Message: %s', $order->getId(), $e->getMessage());
         $userMessage = Translator::getInstance()->trans('An error occurred during payment.', [], StripePayment::MESSAGE_DOMAIN);
     }
     if ($logMessage !== NULL) {
         (new StripePaymentLog())->logText($logMessage);
         $this->redirectToFailurePage($order->getId(), $userMessage);
     }
 }
 protected function addPublishableKeyField(array $translationKeys, array $fieldsIdKeys)
 {
     $this->formBuilder->add("publishable_key", "text", array("label" => $this->readKey("publishable_key", $translationKeys), "label_attr" => ["for" => $this->readKey("publishable_key", $fieldsIdKeys), "help" => $this->readKey("help.publishable_key", $translationKeys)], "required" => true, "constraints" => array(new NotBlank()), "data" => StripePayment::getConfigValue(StripePaymentConfigValue::PUBLISHABLE_KEY)));
 }
 public function declareStripeOnClickEvent(HookRenderEvent $event)
 {
     $publicKey = StripePayment::getConfigValue('publishable_key');
     $event->add($this->render('assets/js/order-invoice-after-js-include.html', ['stripe_module_id' => $this->getModule()->getModuleId(), 'public_key' => $publicKey]));
 }