2013-04-25 18:48:04 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhortuneBalancedPaymentProvider extends PhortunePaymentProvider {
|
|
|
|
|
2014-10-07 23:41:41 +02:00
|
|
|
const BALANCED_MARKETPLACE_ID = 'balanced.marketplace-id';
|
|
|
|
const BALANCED_SECRET_KEY = 'balanced.secret-key';
|
|
|
|
|
2013-04-25 18:48:04 +02:00
|
|
|
public function isEnabled() {
|
|
|
|
return $this->getMarketplaceURI() &&
|
|
|
|
$this->getSecretKey();
|
|
|
|
}
|
|
|
|
|
2014-10-07 23:41:41 +02:00
|
|
|
public function getName() {
|
|
|
|
return pht('Balanced Payments');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfigureName() {
|
|
|
|
return pht('Add Balanced Payments Account');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfigureDescription() {
|
|
|
|
return pht(
|
|
|
|
'Allows you to accept credit or debit card payments with a '.
|
|
|
|
'balancedpayments.com account.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfigureInstructions() {
|
|
|
|
return pht(
|
|
|
|
"To configure Balacned, register or log in to an existing account on ".
|
|
|
|
"[[https://balancedpayments.com | balancedpayments.com]]. Once logged ".
|
|
|
|
"in:\n\n".
|
|
|
|
" - Choose a marketplace.\n".
|
|
|
|
" - Find the **Marketplace ID** in {nav My Marketplace > Settings} and ".
|
|
|
|
" copy it into the field above.\n".
|
|
|
|
" - On the same screen, under **API keys**, choose **Add a key**, then ".
|
|
|
|
" **Show key secret**. Copy the value into the field above.\n\n".
|
|
|
|
"You can either use a test marketplace to add this provider in test ".
|
|
|
|
"mode, or use a live marketplace to accept live payments.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllConfigurableProperties() {
|
|
|
|
return array(
|
|
|
|
self::BALANCED_MARKETPLACE_ID,
|
|
|
|
self::BALANCED_SECRET_KEY,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllConfigurableSecretProperties() {
|
|
|
|
return array(
|
|
|
|
self::BALANCED_SECRET_KEY,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processEditForm(
|
|
|
|
AphrontRequest $request,
|
|
|
|
array $values) {
|
|
|
|
|
|
|
|
$errors = array();
|
|
|
|
$issues = array();
|
|
|
|
|
|
|
|
if (!strlen($values[self::BALANCED_MARKETPLACE_ID])) {
|
|
|
|
$errors[] = pht('Balanced Marketplace ID is required.');
|
|
|
|
$issues[self::BALANCED_MARKETPLACE_ID] = pht('Required');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strlen($values[self::BALANCED_SECRET_KEY])) {
|
|
|
|
$errors[] = pht('Balanced Secret Key is required.');
|
|
|
|
$issues[self::BALANCED_SECRET_KEY] = pht('Required');
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($errors, $issues, $values);
|
2013-04-25 18:48:04 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 23:41:41 +02:00
|
|
|
public function extendEditForm(
|
|
|
|
AphrontRequest $request,
|
|
|
|
AphrontFormView $form,
|
|
|
|
array $values,
|
|
|
|
array $issues) {
|
|
|
|
|
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setName(self::BALANCED_MARKETPLACE_ID)
|
|
|
|
->setValue($values[self::BALANCED_MARKETPLACE_ID])
|
|
|
|
->setError(idx($issues, self::BALANCED_MARKETPLACE_ID, true))
|
|
|
|
->setLabel(pht('Balanced Marketplace ID')))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setName(self::BALANCED_SECRET_KEY)
|
|
|
|
->setValue($values[self::BALANCED_SECRET_KEY])
|
|
|
|
->setError(idx($issues, self::BALANCED_SECRET_KEY, true))
|
|
|
|
->setLabel(pht('Balanced Secret Key')));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canRunConfigurationTest() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function runConfigurationTest() {
|
|
|
|
$root = dirname(phutil_get_library_root('phabricator'));
|
|
|
|
require_once $root.'/externals/httpful/bootstrap.php';
|
|
|
|
require_once $root.'/externals/restful/bootstrap.php';
|
|
|
|
require_once $root.'/externals/balanced-php/bootstrap.php';
|
|
|
|
|
|
|
|
// TODO: This only tests that the secret key is correct. It's not clear
|
|
|
|
// how to test that the marketplace is correct.
|
|
|
|
|
|
|
|
try {
|
|
|
|
Balanced\Settings::$api_key = $this->getSecretKey();
|
|
|
|
Balanced\APIKey::query()->first();
|
|
|
|
} catch (RESTful\Exceptions\HTTPError $error) {
|
|
|
|
// NOTE: This exception doesn't print anything meaningful if it escapes
|
|
|
|
// to top level. Replace it with something slightly readable.
|
|
|
|
throw new Exception($error->response->body->description);
|
|
|
|
}
|
2013-04-25 18:48:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getPaymentMethodDescription() {
|
|
|
|
return pht('Add Credit or Debit Card');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPaymentMethodIcon() {
|
2014-10-08 00:07:01 +02:00
|
|
|
return 'Balanced';
|
2013-04-25 18:48:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getPaymentMethodProviderDescription() {
|
|
|
|
return pht('Processed by Balanced');
|
|
|
|
}
|
|
|
|
|
2014-08-11 21:07:35 +02:00
|
|
|
public function getDefaultPaymentMethodDisplayName(
|
|
|
|
PhortunePaymentMethod $method) {
|
|
|
|
return pht('Credit/Debit Card');
|
|
|
|
}
|
2013-04-25 18:48:04 +02:00
|
|
|
|
|
|
|
protected function executeCharge(
|
|
|
|
PhortunePaymentMethod $method,
|
|
|
|
PhortuneCharge $charge) {
|
2014-10-07 01:48:02 +02:00
|
|
|
|
|
|
|
$root = dirname(phutil_get_library_root('phabricator'));
|
|
|
|
require_once $root.'/externals/httpful/bootstrap.php';
|
|
|
|
require_once $root.'/externals/restful/bootstrap.php';
|
|
|
|
require_once $root.'/externals/balanced-php/bootstrap.php';
|
|
|
|
|
|
|
|
$price = $charge->getAmountAsCurrency();
|
|
|
|
|
|
|
|
// Build the string which will appear on the credit card statement.
|
|
|
|
$charge_as = new PhutilURI(PhabricatorEnv::getProductionURI('/'));
|
|
|
|
$charge_as = $charge_as->getDomain();
|
|
|
|
$charge_as = id(new PhutilUTF8StringTruncator())
|
|
|
|
->setMaximumBytes(22)
|
|
|
|
->setTerminator('')
|
|
|
|
->truncateString($charge_as);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Balanced\Settings::$api_key = $this->getSecretKey();
|
|
|
|
$card = Balanced\Card::get($method->getMetadataValue('balanced.cardURI'));
|
|
|
|
$debit = $card->debit($price->getValueInUSDCents(), $charge_as);
|
|
|
|
} catch (RESTful\Exceptions\HTTPError $error) {
|
|
|
|
// NOTE: This exception doesn't print anything meaningful if it escapes
|
|
|
|
// to top level. Replace it with something slightly readable.
|
|
|
|
throw new Exception($error->response->body->description);
|
|
|
|
}
|
|
|
|
|
|
|
|
$expect_status = 'succeeded';
|
|
|
|
if ($debit->status !== $expect_status) {
|
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'Debit failed, expected "%s", got "%s".',
|
|
|
|
$expect_status,
|
|
|
|
$debit->status));
|
|
|
|
}
|
|
|
|
|
|
|
|
$charge->setMetadataValue('balanced.debitURI', $debit->uri);
|
|
|
|
$charge->save();
|
2013-04-25 18:48:04 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 23:41:41 +02:00
|
|
|
private function getMarketplaceID() {
|
|
|
|
return $this
|
|
|
|
->getProviderConfig()
|
|
|
|
->getMetadataValue(self::BALANCED_MARKETPLACE_ID);
|
2013-04-25 18:48:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getSecretKey() {
|
2014-10-07 23:41:41 +02:00
|
|
|
return $this
|
|
|
|
->getProviderConfig()
|
|
|
|
->getMetadataValue(self::BALANCED_SECRET_KEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getMarketplaceURI() {
|
|
|
|
return '/v1/marketplace/'.$this->getMarketplaceID();
|
2013-04-25 18:48:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -( Adding Payment Methods )--------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function canCreatePaymentMethods() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:49:32 +02:00
|
|
|
public function validateCreatePaymentMethodToken(array $token) {
|
|
|
|
return isset($token['balancedMarketplaceURI']);
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:48:04 +02:00
|
|
|
|
|
|
|
/**
|
2013-04-25 18:49:32 +02:00
|
|
|
* @phutil-external-symbol class Balanced\Card
|
2013-04-25 18:48:04 +02:00
|
|
|
* @phutil-external-symbol class Balanced\Settings
|
|
|
|
* @phutil-external-symbol class Balanced\Marketplace
|
2014-10-07 23:41:41 +02:00
|
|
|
* @phutil-external-symbol class Balanced\APIKey
|
2013-04-25 18:48:04 +02:00
|
|
|
* @phutil-external-symbol class RESTful\Exceptions\HTTPError
|
|
|
|
*/
|
|
|
|
public function createPaymentMethodFromRequest(
|
|
|
|
AphrontRequest $request,
|
2013-04-25 18:49:32 +02:00
|
|
|
PhortunePaymentMethod $method,
|
|
|
|
array $token) {
|
2013-04-25 18:48:04 +02:00
|
|
|
|
|
|
|
$errors = array();
|
|
|
|
|
2013-04-25 18:49:32 +02:00
|
|
|
$root = dirname(phutil_get_library_root('phabricator'));
|
|
|
|
require_once $root.'/externals/httpful/bootstrap.php';
|
|
|
|
require_once $root.'/externals/restful/bootstrap.php';
|
|
|
|
require_once $root.'/externals/balanced-php/bootstrap.php';
|
2013-04-25 18:48:04 +02:00
|
|
|
|
2013-04-25 18:49:32 +02:00
|
|
|
$account_phid = $method->getAccountPHID();
|
|
|
|
$author_phid = $method->getAuthorPHID();
|
|
|
|
$description = $account_phid.':'.$author_phid;
|
|
|
|
|
|
|
|
try {
|
|
|
|
Balanced\Settings::$api_key = $this->getSecretKey();
|
|
|
|
|
|
|
|
$card = Balanced\Card::get($token['balancedMarketplaceURI']);
|
2013-04-25 18:48:04 +02:00
|
|
|
|
2013-04-25 18:49:32 +02:00
|
|
|
$buyer = Balanced\Marketplace::mine()->createBuyer(
|
|
|
|
null,
|
|
|
|
$card->uri,
|
|
|
|
array(
|
|
|
|
'description' => $description,
|
|
|
|
));
|
|
|
|
|
|
|
|
} catch (RESTful\Exceptions\HTTPError $error) {
|
|
|
|
// NOTE: This exception doesn't print anything meaningful if it escapes
|
|
|
|
// to top level. Replace it with something slightly readable.
|
|
|
|
throw new Exception($error->response->body->description);
|
2013-04-25 18:48:04 +02:00
|
|
|
}
|
|
|
|
|
2013-04-25 18:49:32 +02:00
|
|
|
$method
|
|
|
|
->setBrand($card->brand)
|
|
|
|
->setLastFourDigits($card->last_four)
|
|
|
|
->setExpires($card->expiration_year, $card->expiration_month)
|
|
|
|
->setMetadata(
|
|
|
|
array(
|
|
|
|
'type' => 'balanced.account',
|
|
|
|
'balanced.accountURI' => $buyer->uri,
|
|
|
|
'balanced.cardURI' => $card->uri,
|
|
|
|
));
|
|
|
|
|
2013-04-25 18:48:04 +02:00
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function renderCreatePaymentMethodForm(
|
|
|
|
AphrontRequest $request,
|
|
|
|
array $errors) {
|
|
|
|
|
|
|
|
$ccform = id(new PhortuneCreditCardForm())
|
|
|
|
->setUser($request->getUser())
|
2013-04-25 18:49:32 +02:00
|
|
|
->setErrors($errors)
|
2013-04-25 18:48:04 +02:00
|
|
|
->addScript('https://js.balancedpayments.com/v1/balanced.js');
|
|
|
|
|
|
|
|
Javelin::initBehavior(
|
|
|
|
'balanced-payment-form',
|
|
|
|
array(
|
|
|
|
'balancedMarketplaceURI' => $this->getMarketplaceURI(),
|
|
|
|
'formID' => $ccform->getFormID(),
|
|
|
|
));
|
|
|
|
|
|
|
|
return $ccform->buildForm();
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:49:32 +02:00
|
|
|
private function getBalancedShortErrorCode($error_code) {
|
|
|
|
$prefix = 'cc:balanced:';
|
|
|
|
if (strncmp($error_code, $prefix, strlen($prefix))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return substr($error_code, strlen($prefix));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function translateCreatePaymentMethodErrorCode($error_code) {
|
|
|
|
$short_code = $this->getBalancedShortErrorCode($error_code);
|
|
|
|
|
|
|
|
if ($short_code) {
|
|
|
|
static $map = array(
|
|
|
|
);
|
2013-04-25 18:48:04 +02:00
|
|
|
|
2013-04-25 18:49:32 +02:00
|
|
|
if (isset($map[$short_code])) {
|
|
|
|
return $map[$short_code];
|
2013-04-25 18:48:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:49:32 +02:00
|
|
|
return $error_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCreatePaymentMethodErrorMessage($error_code) {
|
|
|
|
$short_code = $this->getBalancedShortErrorCode($error_code);
|
|
|
|
if (!$short_code) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($short_code) {
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
2013-04-25 18:48:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|