Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / phortune / provider / PhortunePaymentProvider.php
blob57b2956ecb357651965482852b06c5edeec74fb1
1 <?php
3 /**
4 * @task addmethod Adding Payment Methods
5 */
6 abstract class PhortunePaymentProvider extends Phobject {
8 private $providerConfig;
10 public function setProviderConfig(
11 PhortunePaymentProviderConfig $provider_config) {
12 $this->providerConfig = $provider_config;
13 return $this;
16 public function getProviderConfig() {
17 return $this->providerConfig;
20 /**
21 * Return a short name which identifies this provider.
23 abstract public function getName();
26 /* -( Configuring Providers )---------------------------------------------- */
29 /**
30 * Return a human-readable provider name for use on the merchant workflow
31 * where a merchant owner adds providers.
33 abstract public function getConfigureName();
36 /**
37 * Return a human-readable provider description for use on the merchant
38 * workflow where a merchant owner adds providers.
40 abstract public function getConfigureDescription();
42 abstract public function getConfigureInstructions();
44 abstract public function getConfigureProvidesDescription();
46 abstract public function getAllConfigurableProperties();
48 abstract public function getAllConfigurableSecretProperties();
49 /**
50 * Read a dictionary of properties from the provider's configuration for
51 * use when editing the provider.
53 public function readEditFormValuesFromProviderConfig() {
54 $properties = $this->getAllConfigurableProperties();
55 $config = $this->getProviderConfig();
57 $secrets = $this->getAllConfigurableSecretProperties();
58 $secrets = array_fuse($secrets);
60 $map = array();
61 foreach ($properties as $property) {
62 $map[$property] = $config->getMetadataValue($property);
63 if (isset($secrets[$property])) {
64 $map[$property] = $this->renderConfigurationSecret($map[$property]);
68 return $map;
72 /**
73 * Read a dictionary of properties from a request for use when editing the
74 * provider.
76 public function readEditFormValuesFromRequest(AphrontRequest $request) {
77 $properties = $this->getAllConfigurableProperties();
79 $map = array();
80 foreach ($properties as $property) {
81 $map[$property] = $request->getStr($property);
84 return $map;
88 abstract public function processEditForm(
89 AphrontRequest $request,
90 array $values);
92 abstract public function extendEditForm(
93 AphrontRequest $request,
94 AphrontFormView $form,
95 array $values,
96 array $issues);
98 protected function renderConfigurationSecret($value) {
99 if (strlen($value)) {
100 return str_repeat('*', strlen($value));
102 return '';
105 public function isConfigurationSecret($value) {
106 return preg_match('/^\*+\z/', trim($value));
109 abstract public function canRunConfigurationTest();
111 public function runConfigurationTest() {
112 throw new PhutilMethodNotImplementedException();
116 /* -( Selecting Providers )------------------------------------------------ */
119 public static function getAllProviders() {
120 return id(new PhutilClassMapQuery())
121 ->setAncestorClass(__CLASS__)
122 ->execute();
125 public function isEnabled() {
126 return $this->getProviderConfig()->getIsEnabled();
129 abstract public function isAcceptingLivePayments();
130 abstract public function getPaymentMethodDescription();
131 abstract public function getPaymentMethodIcon();
132 abstract public function getPaymentMethodProviderDescription();
134 final public function applyCharge(
135 PhortunePaymentMethod $payment_method,
136 PhortuneCharge $charge) {
137 $this->executeCharge($payment_method, $charge);
140 final public function refundCharge(
141 PhortuneCharge $charge,
142 PhortuneCharge $refund) {
143 $this->executeRefund($charge, $refund);
146 abstract protected function executeCharge(
147 PhortunePaymentMethod $payment_method,
148 PhortuneCharge $charge);
150 abstract protected function executeRefund(
151 PhortuneCharge $charge,
152 PhortuneCharge $refund);
154 abstract public function updateCharge(PhortuneCharge $charge);
157 /* -( Adding Payment Methods )--------------------------------------------- */
161 * @task addmethod
163 public function canCreatePaymentMethods() {
164 return false;
169 * @task addmethod
171 public function translateCreatePaymentMethodErrorCode($error_code) {
172 throw new PhutilMethodNotImplementedException();
177 * @task addmethod
179 public function getCreatePaymentMethodErrorMessage($error_code) {
180 throw new PhutilMethodNotImplementedException();
185 * @task addmethod
187 public function validateCreatePaymentMethodToken(array $token) {
188 throw new PhutilMethodNotImplementedException();
193 * @task addmethod
195 public function createPaymentMethodFromRequest(
196 AphrontRequest $request,
197 PhortunePaymentMethod $method,
198 array $token) {
199 throw new PhutilMethodNotImplementedException();
204 * @task addmethod
206 public function renderCreatePaymentMethodForm(
207 AphrontRequest $request,
208 array $errors) {
209 throw new PhutilMethodNotImplementedException();
212 public function getDefaultPaymentMethodDisplayName(
213 PhortunePaymentMethod $method) {
214 throw new PhutilMethodNotImplementedException();
218 /* -( One-Time Payments )-------------------------------------------------- */
221 public function canProcessOneTimePayments() {
222 return false;
225 public function renderOneTimePaymentButton(
226 PhortuneAccount $account,
227 PhortuneCart $cart,
228 PhabricatorUser $user) {
230 require_celerity_resource('phortune-css');
232 $description = $this->getPaymentMethodProviderDescription();
233 $details = $this->getPaymentMethodDescription();
235 $icon = id(new PHUIIconView())
236 ->setSpriteSheet(PHUIIconView::SPRITE_LOGIN)
237 ->setSpriteIcon($this->getPaymentMethodIcon());
239 $button = id(new PHUIButtonView())
240 ->setSize(PHUIButtonView::BIG)
241 ->setColor(PHUIButtonView::GREY)
242 ->setIcon($icon)
243 ->setText($description)
244 ->setSubtext($details);
246 // NOTE: We generate a local URI to make sure the form picks up CSRF tokens.
247 $uri = $this->getControllerURI(
248 'checkout',
249 array(
250 'cartID' => $cart->getID(),
252 $local = true);
254 return phabricator_form(
255 $user,
256 array(
257 'action' => $uri,
258 'method' => 'POST',
260 $button);
264 /* -( Controllers )-------------------------------------------------------- */
267 final public function getControllerURI(
268 $action,
269 array $params = array(),
270 $local = false) {
272 $id = $this->getProviderConfig()->getID();
273 $app = PhabricatorApplication::getByClass('PhabricatorPhortuneApplication');
274 $path = $app->getBaseURI().'provider/'.$id.'/'.$action.'/';
276 $uri = new PhutilURI($path, $params);
278 if ($local) {
279 return $uri;
280 } else {
281 return PhabricatorEnv::getURI((string)$uri);
285 public function canRespondToControllerAction($action) {
286 return false;
289 public function processControllerRequest(
290 PhortuneProviderActionController $controller,
291 AphrontRequest $request) {
292 throw new PhutilMethodNotImplementedException();