1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-19 12:00:55 +01:00

Give Balanced provider complete workflow logic in Phortune

Summary: Ref T2787. Like Stripe, this one is pretty easy to get working correctly on the "good" path and fataling out in a safe way on bad paths.

Test Plan: Funded an initiative with Balanced.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T2787

Differential Revision: https://secure.phabricator.com/D10645
This commit is contained in:
epriestley 2014-10-06 16:48:02 -07:00
parent 0bbe3a6d28
commit 19c1c07d35
3 changed files with 45 additions and 2 deletions

View file

@ -121,6 +121,13 @@ final class PhortuneCurrency extends Phobject {
return $this->currency;
}
public function getValueInUSDCents() {
if ($this->currency !== 'USD') {
throw new Exception(pht('Unexpected currency!'));
}
return $this->value;
}
private static function throwFormatException($string) {
throw new Exception("Invalid currency format ('{$string}').");
}

View file

@ -40,7 +40,43 @@ final class PhortuneBalancedPaymentProvider extends PhortunePaymentProvider {
protected function executeCharge(
PhortunePaymentMethod $method,
PhortuneCharge $charge) {
throw new PhortuneNotImplementedException($this);
$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();
}
private function getMarketplaceURI() {

View file

@ -52,7 +52,7 @@ final class PhortuneStripePaymentProvider extends PhortunePaymentProvider {
$secret_key = $this->getSecretKey();
$params = array(
'amount' => $price->getValue(),
'amount' => $price->getValueInUSDCents(),
'currency' => $price->getCurrency(),
'customer' => $method->getMetadataValue('stripe.customerID'),
'description' => $charge->getPHID(),