2013-04-25 18:45:43 +02:00
|
|
|
<?php
|
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
/**
|
|
|
|
* @task addmethod Adding Payment Methods
|
|
|
|
*/
|
2015-06-15 10:02:26 +02:00
|
|
|
abstract class PhortunePaymentProvider extends Phobject {
|
2013-04-25 18:45:43 +02:00
|
|
|
|
2014-10-07 23:41:41 +02:00
|
|
|
private $providerConfig;
|
|
|
|
|
|
|
|
public function setProviderConfig(
|
|
|
|
PhortunePaymentProviderConfig $provider_config) {
|
|
|
|
$this->providerConfig = $provider_config;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getProviderConfig() {
|
|
|
|
return $this->providerConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a short name which identifies this provider.
|
|
|
|
*/
|
|
|
|
abstract public function getName();
|
|
|
|
|
|
|
|
|
|
|
|
/* -( Configuring Providers )---------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a human-readable provider name for use on the merchant workflow
|
|
|
|
* where a merchant owner adds providers.
|
|
|
|
*/
|
|
|
|
abstract public function getConfigureName();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a human-readable provider description for use on the merchant
|
|
|
|
* workflow where a merchant owner adds providers.
|
|
|
|
*/
|
|
|
|
abstract public function getConfigureDescription();
|
|
|
|
|
|
|
|
abstract public function getConfigureInstructions();
|
|
|
|
|
2014-10-08 17:31:24 +02:00
|
|
|
abstract public function getConfigureProvidesDescription();
|
|
|
|
|
2014-10-07 23:41:41 +02:00
|
|
|
abstract public function getAllConfigurableProperties();
|
|
|
|
|
|
|
|
abstract public function getAllConfigurableSecretProperties();
|
|
|
|
/**
|
|
|
|
* Read a dictionary of properties from the provider's configuration for
|
|
|
|
* use when editing the provider.
|
|
|
|
*/
|
|
|
|
public function readEditFormValuesFromProviderConfig() {
|
|
|
|
$properties = $this->getAllConfigurableProperties();
|
|
|
|
$config = $this->getProviderConfig();
|
|
|
|
|
|
|
|
$secrets = $this->getAllConfigurableSecretProperties();
|
|
|
|
$secrets = array_fuse($secrets);
|
|
|
|
|
|
|
|
$map = array();
|
|
|
|
foreach ($properties as $property) {
|
|
|
|
$map[$property] = $config->getMetadataValue($property);
|
|
|
|
if (isset($secrets[$property])) {
|
|
|
|
$map[$property] = $this->renderConfigurationSecret($map[$property]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $map;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a dictionary of properties from a request for use when editing the
|
|
|
|
* provider.
|
|
|
|
*/
|
|
|
|
public function readEditFormValuesFromRequest(AphrontRequest $request) {
|
|
|
|
$properties = $this->getAllConfigurableProperties();
|
|
|
|
|
|
|
|
$map = array();
|
|
|
|
foreach ($properties as $property) {
|
|
|
|
$map[$property] = $request->getStr($property);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $map;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
abstract public function processEditForm(
|
|
|
|
AphrontRequest $request,
|
|
|
|
array $values);
|
|
|
|
|
|
|
|
abstract public function extendEditForm(
|
|
|
|
AphrontRequest $request,
|
|
|
|
AphrontFormView $form,
|
|
|
|
array $values,
|
|
|
|
array $issues);
|
|
|
|
|
|
|
|
protected function renderConfigurationSecret($value) {
|
|
|
|
if (strlen($value)) {
|
|
|
|
return str_repeat('*', strlen($value));
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isConfigurationSecret($value) {
|
|
|
|
return preg_match('/^\*+\z/', trim($value));
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract public function canRunConfigurationTest();
|
|
|
|
|
|
|
|
public function runConfigurationTest() {
|
|
|
|
throw new PhortuneNotImplementedException($this);
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
|
|
|
|
/* -( Selecting Providers )------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
|
|
public static function getAllProviders() {
|
2014-10-07 23:41:41 +02:00
|
|
|
return id(new PhutilSymbolLoader())
|
2015-05-13 22:50:28 +02:00
|
|
|
->setAncestorClass(__CLASS__)
|
2013-04-25 18:46:32 +02:00
|
|
|
->loadObjects();
|
|
|
|
}
|
|
|
|
|
2014-10-08 17:31:24 +02:00
|
|
|
public function isEnabled() {
|
|
|
|
return $this->getProviderConfig()->getIsEnabled();
|
|
|
|
}
|
2013-04-25 18:46:32 +02:00
|
|
|
|
2014-10-08 17:31:24 +02:00
|
|
|
abstract public function isAcceptingLivePayments();
|
2013-04-25 18:46:32 +02:00
|
|
|
abstract public function getPaymentMethodDescription();
|
|
|
|
abstract public function getPaymentMethodIcon();
|
|
|
|
abstract public function getPaymentMethodProviderDescription();
|
|
|
|
|
2014-07-23 19:36:12 +02:00
|
|
|
final public function applyCharge(
|
|
|
|
PhortunePaymentMethod $payment_method,
|
|
|
|
PhortuneCharge $charge) {
|
|
|
|
$this->executeCharge($payment_method, $charge);
|
|
|
|
}
|
|
|
|
|
2014-10-09 00:33:25 +02:00
|
|
|
final public function refundCharge(
|
|
|
|
PhortuneCharge $charge,
|
|
|
|
PhortuneCharge $refund) {
|
|
|
|
$this->executeRefund($charge, $refund);
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:45:43 +02:00
|
|
|
abstract protected function executeCharge(
|
|
|
|
PhortunePaymentMethod $payment_method,
|
|
|
|
PhortuneCharge $charge);
|
|
|
|
|
2014-10-09 00:33:25 +02:00
|
|
|
abstract protected function executeRefund(
|
|
|
|
PhortuneCharge $charge,
|
2014-10-10 01:57:52 +02:00
|
|
|
PhortuneCharge $refund);
|
|
|
|
|
|
|
|
abstract public function updateCharge(PhortuneCharge $charge);
|
2014-10-09 00:33:25 +02:00
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
|
|
|
|
/* -( Adding Payment Methods )--------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task addmethod
|
|
|
|
*/
|
|
|
|
public function canCreatePaymentMethods() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-25 18:49:32 +02:00
|
|
|
/**
|
|
|
|
* @task addmethod
|
|
|
|
*/
|
|
|
|
public function translateCreatePaymentMethodErrorCode($error_code) {
|
|
|
|
throw new PhortuneNotImplementedException($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task addmethod
|
|
|
|
*/
|
|
|
|
public function getCreatePaymentMethodErrorMessage($error_code) {
|
|
|
|
throw new PhortuneNotImplementedException($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task addmethod
|
|
|
|
*/
|
|
|
|
public function validateCreatePaymentMethodToken(array $token) {
|
|
|
|
throw new PhortuneNotImplementedException($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
/**
|
|
|
|
* @task addmethod
|
|
|
|
*/
|
|
|
|
public function createPaymentMethodFromRequest(
|
|
|
|
AphrontRequest $request,
|
2013-04-25 18:49:32 +02:00
|
|
|
PhortunePaymentMethod $method,
|
|
|
|
array $token) {
|
2013-04-25 18:46:32 +02:00
|
|
|
throw new PhortuneNotImplementedException($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task addmethod
|
|
|
|
*/
|
|
|
|
public function renderCreatePaymentMethodForm(
|
|
|
|
AphrontRequest $request,
|
|
|
|
array $errors) {
|
|
|
|
throw new PhortuneNotImplementedException($this);
|
|
|
|
}
|
|
|
|
|
2014-08-11 21:07:35 +02:00
|
|
|
public function getDefaultPaymentMethodDisplayName(
|
|
|
|
PhortunePaymentMethod $method) {
|
|
|
|
throw new PhortuneNotImplementedException($this);
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:46:59 +02:00
|
|
|
|
2013-05-06 20:44:24 +02:00
|
|
|
/* -( One-Time Payments )-------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function canProcessOneTimePayments() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function renderOneTimePaymentButton(
|
|
|
|
PhortuneAccount $account,
|
|
|
|
PhortuneCart $cart,
|
|
|
|
PhabricatorUser $user) {
|
2014-07-23 19:36:37 +02:00
|
|
|
|
|
|
|
require_celerity_resource('phortune-css');
|
|
|
|
|
|
|
|
$description = $this->getPaymentMethodProviderDescription();
|
|
|
|
$details = $this->getPaymentMethodDescription();
|
|
|
|
|
|
|
|
$icon = id(new PHUIIconView())
|
2014-10-08 00:07:01 +02:00
|
|
|
->setSpriteSheet(PHUIIconView::SPRITE_LOGIN)
|
|
|
|
->setSpriteIcon($this->getPaymentMethodIcon());
|
2014-07-23 19:36:37 +02:00
|
|
|
|
|
|
|
$button = id(new PHUIButtonView())
|
|
|
|
->setSize(PHUIButtonView::BIG)
|
|
|
|
->setColor(PHUIButtonView::GREY)
|
|
|
|
->setIcon($icon)
|
|
|
|
->setText($description)
|
|
|
|
->setSubtext($details);
|
|
|
|
|
2014-10-06 23:20:40 +02:00
|
|
|
// NOTE: We generate a local URI to make sure the form picks up CSRF tokens.
|
2014-07-23 19:36:37 +02:00
|
|
|
$uri = $this->getControllerURI(
|
|
|
|
'checkout',
|
|
|
|
array(
|
|
|
|
'cartID' => $cart->getID(),
|
2014-10-06 23:20:40 +02:00
|
|
|
),
|
|
|
|
$local = true);
|
2014-07-23 19:36:37 +02:00
|
|
|
|
|
|
|
return phabricator_form(
|
|
|
|
$user,
|
|
|
|
array(
|
|
|
|
'action' => $uri,
|
|
|
|
'method' => 'POST',
|
|
|
|
),
|
|
|
|
$button);
|
2013-05-06 20:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -( Controllers )-------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
final public function getControllerURI(
|
|
|
|
$action,
|
2014-10-06 23:20:40 +02:00
|
|
|
array $params = array(),
|
|
|
|
$local = false) {
|
2013-05-06 20:44:24 +02:00
|
|
|
|
2014-10-07 23:41:41 +02:00
|
|
|
$id = $this->getProviderConfig()->getID();
|
2014-07-23 02:03:09 +02:00
|
|
|
$app = PhabricatorApplication::getByClass('PhabricatorPhortuneApplication');
|
2014-10-07 23:41:41 +02:00
|
|
|
$path = $app->getBaseURI().'provider/'.$id.'/'.$action.'/';
|
2013-05-06 20:44:24 +02:00
|
|
|
|
|
|
|
$uri = new PhutilURI($path);
|
|
|
|
$uri->setQueryParams($params);
|
|
|
|
|
2014-10-06 23:20:40 +02:00
|
|
|
if ($local) {
|
|
|
|
return $uri;
|
|
|
|
} else {
|
|
|
|
return PhabricatorEnv::getURI((string)$uri);
|
|
|
|
}
|
2013-05-06 20:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function canRespondToControllerAction($action) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processControllerRequest(
|
2014-10-07 23:41:41 +02:00
|
|
|
PhortuneProviderActionController $controller,
|
2013-05-06 20:44:24 +02:00
|
|
|
AphrontRequest $request) {
|
|
|
|
throw new PhortuneNotImplementedException($this);
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:45:43 +02:00
|
|
|
}
|