mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-13 10:22:42 +01:00
7a5f622820
Summary: This has no real behavioral changes (except better error handling), it just factors things out to be a bit cleaner. In particular: - Move more shared form behaviors into the common JS form component. - Move more error handling into shared pathways. - Make the specialized Stripe / Balanced methods do less work. This needs some more polish for nontrival errors (especially on the Balanced side) but none of the error behavior is worse than it was and a lot of it is much better. Ref T2787. Test Plan: Hit all invalid form errors, added valid payment methods with Stripe and Balacned. Reviewers: btrahan, chad Reviewed By: btrahan CC: aran Maniphest Tasks: T2787 Differential Revision: https://secure.phabricator.com/D5771
175 lines
4.5 KiB
PHP
175 lines
4.5 KiB
PHP
<?php
|
|
|
|
final class PhortuneBalancedPaymentProvider extends PhortunePaymentProvider {
|
|
|
|
public function isEnabled() {
|
|
return $this->getMarketplaceURI() &&
|
|
$this->getSecretKey();
|
|
}
|
|
|
|
public function getProviderType() {
|
|
return 'balanced';
|
|
}
|
|
|
|
public function getProviderDomain() {
|
|
return 'balancedpayments.com';
|
|
}
|
|
|
|
public function getPaymentMethodDescription() {
|
|
return pht('Add Credit or Debit Card');
|
|
}
|
|
|
|
public function getPaymentMethodIcon() {
|
|
return celerity_get_resource_uri('/rsrc/image/phortune/balanced.png');
|
|
}
|
|
|
|
public function getPaymentMethodProviderDescription() {
|
|
return pht('Processed by Balanced');
|
|
}
|
|
|
|
|
|
public function canHandlePaymentMethod(PhortunePaymentMethod $method) {
|
|
$type = $method->getMetadataValue('type');
|
|
return ($type === 'balanced.account');
|
|
}
|
|
|
|
protected function executeCharge(
|
|
PhortunePaymentMethod $method,
|
|
PhortuneCharge $charge) {
|
|
throw new PhortuneNotImplementedException($this);
|
|
}
|
|
|
|
private function getMarketplaceURI() {
|
|
return PhabricatorEnv::getEnvConfig('phortune.balanced.marketplace-uri');
|
|
}
|
|
|
|
private function getSecretKey() {
|
|
return PhabricatorEnv::getEnvConfig('phortune.balanced.secret-key');
|
|
}
|
|
|
|
|
|
/* -( Adding Payment Methods )--------------------------------------------- */
|
|
|
|
|
|
public function canCreatePaymentMethods() {
|
|
return true;
|
|
}
|
|
|
|
public function validateCreatePaymentMethodToken(array $token) {
|
|
return isset($token['balancedMarketplaceURI']);
|
|
}
|
|
|
|
|
|
/**
|
|
* @phutil-external-symbol class Balanced\Card
|
|
* @phutil-external-symbol class Balanced\Settings
|
|
* @phutil-external-symbol class Balanced\Marketplace
|
|
* @phutil-external-symbol class RESTful\Exceptions\HTTPError
|
|
*/
|
|
public function createPaymentMethodFromRequest(
|
|
AphrontRequest $request,
|
|
PhortunePaymentMethod $method,
|
|
array $token) {
|
|
|
|
$errors = array();
|
|
|
|
$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';
|
|
|
|
$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']);
|
|
|
|
$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);
|
|
}
|
|
|
|
$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,
|
|
));
|
|
|
|
return $errors;
|
|
}
|
|
|
|
public function renderCreatePaymentMethodForm(
|
|
AphrontRequest $request,
|
|
array $errors) {
|
|
|
|
$ccform = id(new PhortuneCreditCardForm())
|
|
->setUser($request->getUser())
|
|
->setErrors($errors)
|
|
->addScript('https://js.balancedpayments.com/v1/balanced.js');
|
|
|
|
Javelin::initBehavior(
|
|
'balanced-payment-form',
|
|
array(
|
|
'balancedMarketplaceURI' => $this->getMarketplaceURI(),
|
|
'formID' => $ccform->getFormID(),
|
|
));
|
|
|
|
return $ccform->buildForm();
|
|
}
|
|
|
|
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(
|
|
);
|
|
|
|
if (isset($map[$short_code])) {
|
|
return $map[$short_code];
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|