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