From f4604ae147beebe8f34239ded6bc7e792ca494c7 Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 8 Oct 2014 15:33:36 -0700 Subject: [PATCH] Implement refunds for Balanced in Phortune Summary: Ref T2787. Does refunds for Balanced. Test Plan: {F215205} Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T2787 Differential Revision: https://secure.phabricator.com/D10665 --- .../PhortuneBalancedPaymentProvider.php | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) 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