diff --git a/src/applications/phortune/provider/PhortuneBalancedPaymentProvider.php b/src/applications/phortune/provider/PhortuneBalancedPaymentProvider.php index a79e5c28aa..92d1d64fb0 100644 --- a/src/applications/phortune/provider/PhortuneBalancedPaymentProvider.php +++ b/src/applications/phortune/provider/PhortuneBalancedPaymentProvider.php @@ -182,8 +182,36 @@ final class PhortuneBalancedPaymentProvider extends PhortunePaymentProvider { protected function executeRefund( PhortuneCharge $charge, PhortuneCharge $refund) { - // TODO: Implement. - 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'; + + $debit_uri = $charge->getMetadataValue('balanced.debitURI'); + if (!$debit_uri) { + throw new Exception(pht('No Balanced debit URI!')); + } + + $refund_cents = $refund + ->getAmountAsCurrency() + ->negate() + ->getValueInUSDCents(); + + $params = array( + 'amount' => $refund_cents, + ); + + try { + Balanced\Settings::$api_key = $this->getSecretKey(); + $balanced_debit = Balanced\Debit::get($debit_uri); + $balanced_refund = $balanced_debit->refunds->create($params); + } catch (RESTful\Exceptions\HTTPError $error) { + throw new Exception($error->response->body->description); + } + + $refund->setMetadataValue('balanced.refundURI', $balanced_refund->uri); + $refund->save(); } private function getMarketplaceID() { @@ -217,6 +245,7 @@ final class PhortuneBalancedPaymentProvider extends PhortunePaymentProvider { /** * @phutil-external-symbol class Balanced\Card + * @phutil-external-symbol class Balanced\Debit * @phutil-external-symbol class Balanced\Settings * @phutil-external-symbol class Balanced\Marketplace * @phutil-external-symbol class Balanced\APIKey