mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-23 22:10: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:
parent
0bbe3a6d28
commit
19c1c07d35
3 changed files with 45 additions and 2 deletions
|
@ -121,6 +121,13 @@ final class PhortuneCurrency extends Phobject {
|
||||||
return $this->currency;
|
return $this->currency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getValueInUSDCents() {
|
||||||
|
if ($this->currency !== 'USD') {
|
||||||
|
throw new Exception(pht('Unexpected currency!'));
|
||||||
|
}
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
private static function throwFormatException($string) {
|
private static function throwFormatException($string) {
|
||||||
throw new Exception("Invalid currency format ('{$string}').");
|
throw new Exception("Invalid currency format ('{$string}').");
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,43 @@ final class PhortuneBalancedPaymentProvider extends PhortunePaymentProvider {
|
||||||
protected function executeCharge(
|
protected function executeCharge(
|
||||||
PhortunePaymentMethod $method,
|
PhortunePaymentMethod $method,
|
||||||
PhortuneCharge $charge) {
|
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() {
|
private function getMarketplaceURI() {
|
||||||
|
|
|
@ -52,7 +52,7 @@ final class PhortuneStripePaymentProvider extends PhortunePaymentProvider {
|
||||||
|
|
||||||
$secret_key = $this->getSecretKey();
|
$secret_key = $this->getSecretKey();
|
||||||
$params = array(
|
$params = array(
|
||||||
'amount' => $price->getValue(),
|
'amount' => $price->getValueInUSDCents(),
|
||||||
'currency' => $price->getCurrency(),
|
'currency' => $price->getCurrency(),
|
||||||
'customer' => $method->getMetadataValue('stripe.customerID'),
|
'customer' => $method->getMetadataValue('stripe.customerID'),
|
||||||
'description' => $charge->getPHID(),
|
'description' => $charge->getPHID(),
|
||||||
|
|
Loading…
Reference in a new issue