PageRenderTime 61ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/applications/phortune/controller/PhortuneProviderEditController.php

http://github.com/facebook/phabricator
PHP | 302 lines | 248 code | 50 blank | 4 comment | 22 complexity | 5a8cadf87cff4f6335ab902f1f76e94d MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. final class PhortuneProviderEditController
  3. extends PhortuneMerchantController {
  4. public function handleRequest(AphrontRequest $request) {
  5. $viewer = $request->getViewer();
  6. $id = $request->getURIData('id');
  7. if ($id) {
  8. $provider_config = id(new PhortunePaymentProviderConfigQuery())
  9. ->setViewer($viewer)
  10. ->withIDs(array($id))
  11. ->requireCapabilities(
  12. array(
  13. PhabricatorPolicyCapability::CAN_VIEW,
  14. PhabricatorPolicyCapability::CAN_EDIT,
  15. ))
  16. ->executeOne();
  17. if (!$provider_config) {
  18. return new Aphront404Response();
  19. }
  20. $is_new = false;
  21. $is_choose_type = false;
  22. $merchant = $provider_config->getMerchant();
  23. $merchant_id = $merchant->getID();
  24. $cancel_uri = $this->getApplicationURI("merchant/{$merchant_id}/");
  25. } else {
  26. $merchant = id(new PhortuneMerchantQuery())
  27. ->setViewer($viewer)
  28. ->withIDs(array($request->getStr('merchantID')))
  29. ->requireCapabilities(
  30. array(
  31. PhabricatorPolicyCapability::CAN_VIEW,
  32. PhabricatorPolicyCapability::CAN_EDIT,
  33. ))
  34. ->executeOne();
  35. if (!$merchant) {
  36. return new Aphront404Response();
  37. }
  38. $merchant_id = $merchant->getID();
  39. $current_providers = id(new PhortunePaymentProviderConfigQuery())
  40. ->setViewer($viewer)
  41. ->withMerchantPHIDs(array($merchant->getPHID()))
  42. ->execute();
  43. $current_map = mgroup($current_providers, 'getProviderClass');
  44. $provider_config = PhortunePaymentProviderConfig::initializeNewProvider(
  45. $merchant);
  46. $is_new = true;
  47. $classes = PhortunePaymentProvider::getAllProviders();
  48. $class = $request->getStr('class');
  49. if (empty($classes[$class]) || isset($current_map[$class])) {
  50. return $this->processChooseClassRequest(
  51. $request,
  52. $merchant,
  53. $current_map);
  54. }
  55. $provider_config->setProviderClass($class);
  56. $cancel_uri = $this->getApplicationURI(
  57. 'provider/edit/?merchantID='.$merchant_id);
  58. }
  59. $provider = $provider_config->buildProvider();
  60. if ($is_new) {
  61. $title = pht('Create Payment Provider');
  62. $button_text = pht('Create Provider');
  63. } else {
  64. $title = pht(
  65. 'Edit Payment Provider %d %s',
  66. $provider_config->getID(),
  67. $provider->getName());
  68. $button_text = pht('Save Changes');
  69. }
  70. $errors = array();
  71. if ($request->isFormPost() && $request->getStr('edit')) {
  72. $form_values = $provider->readEditFormValuesFromRequest($request);
  73. list($errors, $issues, $xaction_values) = $provider->processEditForm(
  74. $request,
  75. $form_values);
  76. if (!$errors) {
  77. // Find any secret fields which we're about to set to "*******"
  78. // (indicating that the user did not edit the value) and remove them
  79. // from the list of properties to update (so we don't write "******"
  80. // to permanent configuration.
  81. $secrets = $provider->getAllConfigurableSecretProperties();
  82. $secrets = array_fuse($secrets);
  83. foreach ($xaction_values as $key => $value) {
  84. if ($provider->isConfigurationSecret($value)) {
  85. unset($xaction_values[$key]);
  86. }
  87. }
  88. if ($provider->canRunConfigurationTest()) {
  89. $proxy = clone $provider;
  90. $proxy_config = clone $provider_config;
  91. $proxy_config->setMetadata(
  92. $xaction_values + $provider_config->getMetadata());
  93. $proxy->setProviderConfig($proxy_config);
  94. try {
  95. $proxy->runConfigurationTest();
  96. } catch (Exception $ex) {
  97. $errors[] = pht('Unable to connect to payment provider:');
  98. $errors[] = $ex->getMessage();
  99. }
  100. }
  101. if (!$errors) {
  102. $template = id(new PhortunePaymentProviderConfigTransaction())
  103. ->setTransactionType(
  104. PhortunePaymentProviderConfigTransaction::TYPE_PROPERTY);
  105. $xactions = array();
  106. $xactions[] = id(new PhortunePaymentProviderConfigTransaction())
  107. ->setTransactionType(
  108. PhortunePaymentProviderConfigTransaction::TYPE_CREATE)
  109. ->setNewValue(true);
  110. foreach ($xaction_values as $key => $value) {
  111. $xactions[] = id(clone $template)
  112. ->setMetadataValue(
  113. PhortunePaymentProviderConfigTransaction::PROPERTY_KEY,
  114. $key)
  115. ->setNewValue($value);
  116. }
  117. $editor = id(new PhortunePaymentProviderConfigEditor())
  118. ->setActor($viewer)
  119. ->setContentSourceFromRequest($request)
  120. ->setContinueOnNoEffect(true);
  121. $editor->applyTransactions($provider_config, $xactions);
  122. $merchant_uri = $this->getApplicationURI(
  123. 'merchant/'.$merchant->getID().'/');
  124. return id(new AphrontRedirectResponse())->setURI($merchant_uri);
  125. }
  126. }
  127. } else {
  128. $form_values = $provider->readEditFormValuesFromProviderConfig();
  129. $issues = array();
  130. }
  131. $form = id(new AphrontFormView())
  132. ->setUser($viewer)
  133. ->addHiddenInput('merchantID', $merchant->getID())
  134. ->addHiddenInput('class', $provider_config->getProviderClass())
  135. ->addHiddenInput('edit', true)
  136. ->appendChild(
  137. id(new AphrontFormMarkupControl())
  138. ->setLabel(pht('Provider Type'))
  139. ->setValue($provider->getName()));
  140. $provider->extendEditForm($request, $form, $form_values, $issues);
  141. $form
  142. ->appendChild(
  143. id(new AphrontFormSubmitControl())
  144. ->setValue($button_text)
  145. ->addCancelButton($cancel_uri))
  146. ->appendChild(
  147. id(new AphrontFormDividerControl()))
  148. ->appendRemarkupInstructions(
  149. $provider->getConfigureInstructions());
  150. $crumbs = $this->buildApplicationCrumbs();
  151. $crumbs->addTextCrumb($merchant->getName(), $cancel_uri);
  152. $crumbs->setBorder(true);
  153. if ($is_new) {
  154. $crumbs->addTextCrumb(pht('Add Provider'));
  155. } else {
  156. $crumbs->addTextCrumb(
  157. pht('Edit Provider %d', $provider_config->getID()));
  158. }
  159. $header = id(new PHUIHeaderView())
  160. ->setHeader($title)
  161. ->setHeaderIcon('fa-pencil');
  162. $box = id(new PHUIObjectBoxView())
  163. ->setFormErrors($errors)
  164. ->setHeaderText(pht('Properties'))
  165. ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
  166. ->appendChild($form);
  167. $view = id(new PHUITwoColumnView())
  168. ->setHeader($header)
  169. ->setFooter(array(
  170. $box,
  171. ));
  172. return $this->newPage()
  173. ->setTitle($title)
  174. ->setCrumbs($crumbs)
  175. ->appendChild($view);
  176. }
  177. private function processChooseClassRequest(
  178. AphrontRequest $request,
  179. PhortuneMerchant $merchant,
  180. array $current_map) {
  181. $viewer = $request->getUser();
  182. $providers = PhortunePaymentProvider::getAllProviders();
  183. $v_class = null;
  184. $errors = array();
  185. if ($request->isFormPost()) {
  186. $v_class = $request->getStr('class');
  187. if (!isset($providers[$v_class])) {
  188. $errors[] = pht('You must select a valid provider type.');
  189. }
  190. }
  191. $merchant_id = $merchant->getID();
  192. $cancel_uri = $this->getApplicationURI("merchant/{$merchant_id}/");
  193. if (!$v_class) {
  194. $v_class = key($providers);
  195. }
  196. $panel_classes = id(new AphrontFormRadioButtonControl())
  197. ->setName('class')
  198. ->setValue($v_class);
  199. $providers = msort($providers, 'getConfigureName');
  200. foreach ($providers as $class => $provider) {
  201. $disabled = isset($current_map[$class]);
  202. if ($disabled) {
  203. $description = phutil_tag(
  204. 'em',
  205. array(),
  206. pht(
  207. 'This merchant already has a payment account configured '.
  208. 'with this provider.'));
  209. } else {
  210. $description = $provider->getConfigureDescription();
  211. }
  212. $panel_classes->addButton(
  213. $class,
  214. $provider->getConfigureName(),
  215. $description,
  216. null,
  217. $disabled);
  218. }
  219. $form = id(new AphrontFormView())
  220. ->setUser($viewer)
  221. ->addHiddenInput('merchantID', $merchant->getID())
  222. ->appendRemarkupInstructions(
  223. pht('Choose the type of payment provider to add:'))
  224. ->appendChild($panel_classes)
  225. ->appendChild(
  226. id(new AphrontFormSubmitControl())
  227. ->setValue(pht('Continue'))
  228. ->addCancelButton($cancel_uri));
  229. $title = pht('Add Payment Provider');
  230. $crumbs = $this->buildApplicationCrumbs();
  231. $crumbs->addTextCrumb($merchant->getName(), $cancel_uri);
  232. $crumbs->addTextCrumb($title);
  233. $crumbs->setBorder(true);
  234. $box = id(new PHUIObjectBoxView())
  235. ->setHeaderText(pht('Provider'))
  236. ->setFormErrors($errors)
  237. ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
  238. ->setForm($form);
  239. $header = id(new PHUIHeaderView())
  240. ->setHeader($title)
  241. ->setHeaderIcon('fa-plus-square');
  242. $view = id(new PHUITwoColumnView())
  243. ->setHeader($header)
  244. ->setFooter(array(
  245. $box,
  246. ));
  247. return $this->newPage()
  248. ->setTitle($title)
  249. ->setCrumbs($crumbs)
  250. ->appendChild($view);
  251. }
  252. }