[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phortune/storage/ -> PhortuneCharge.php (source)

   1  <?php
   2  
   3  /**
   4   * A charge is a charge (or credit) against an account and represents an actual
   5   * transfer of funds. Each charge is normally associated with a cart, but a
   6   * cart may have multiple charges. For example, a product may have a failed
   7   * charge followed by a successful charge.
   8   */
   9  final class PhortuneCharge extends PhortuneDAO
  10    implements PhabricatorPolicyInterface {
  11  
  12    const STATUS_CHARGING   = 'charge:charging';
  13    const STATUS_CHARGED    = 'charge:charged';
  14    const STATUS_HOLD       = 'charge:hold';
  15    const STATUS_FAILED     = 'charge:failed';
  16  
  17    protected $accountPHID;
  18    protected $authorPHID;
  19    protected $cartPHID;
  20    protected $providerPHID;
  21    protected $merchantPHID;
  22    protected $paymentMethodPHID;
  23    protected $amountAsCurrency;
  24    protected $amountRefundedAsCurrency;
  25    protected $refundedChargePHID;
  26    protected $refundingPHID;
  27    protected $status;
  28    protected $metadata = array();
  29  
  30    private $account = self::ATTACHABLE;
  31    private $cart = self::ATTACHABLE;
  32  
  33    public static function initializeNewCharge() {
  34      return id(new PhortuneCharge())
  35        ->setStatus(self::STATUS_CHARGING)
  36        ->setAmountRefundedAsCurrency(PhortuneCurrency::newEmptyCurrency());
  37    }
  38  
  39    public function getConfiguration() {
  40      return array(
  41        self::CONFIG_AUX_PHID => true,
  42        self::CONFIG_SERIALIZATION => array(
  43          'metadata' => self::SERIALIZATION_JSON,
  44        ),
  45        self::CONFIG_APPLICATION_SERIALIZERS => array(
  46          'amountAsCurrency' => new PhortuneCurrencySerializer(),
  47          'amountRefundedAsCurrency' => new PhortuneCurrencySerializer(),
  48        ),
  49        self::CONFIG_COLUMN_SCHEMA => array(
  50          'paymentMethodPHID' => 'phid?',
  51          'refundedChargePHID' => 'phid?',
  52          'refundingPHID' => 'phid?',
  53          'amountAsCurrency' => 'text64',
  54          'amountRefundedAsCurrency' => 'text64',
  55          'status' => 'text32',
  56        ),
  57        self::CONFIG_KEY_SCHEMA => array(
  58          'key_cart' => array(
  59            'columns' => array('cartPHID'),
  60          ),
  61          'key_account' => array(
  62            'columns' => array('accountPHID'),
  63          ),
  64          'key_merchant' => array(
  65            'columns' => array('merchantPHID'),
  66          ),
  67          'key_provider' => array(
  68            'columns' => array('providerPHID'),
  69          ),
  70        ),
  71      ) + parent::getConfiguration();
  72    }
  73  
  74    public static function getStatusNameMap() {
  75      return array(
  76        self::STATUS_CHARGING => pht('Charging'),
  77        self::STATUS_CHARGED => pht('Charged'),
  78        self::STATUS_HOLD => pht('Hold'),
  79        self::STATUS_FAILED => pht('Failed'),
  80      );
  81    }
  82  
  83    public static function getNameForStatus($status) {
  84      return idx(self::getStatusNameMap(), $status, pht('Unknown'));
  85    }
  86  
  87    public function isRefund() {
  88      return $this->getAmountAsCurrency()->negate()->isPositive();
  89    }
  90  
  91    public function getStatusForDisplay() {
  92      if ($this->getStatus() == self::STATUS_CHARGED) {
  93        if ($this->getRefundedChargePHID()) {
  94          return pht('Refund');
  95        }
  96  
  97        $refunded = $this->getAmountRefundedAsCurrency();
  98  
  99        if ($refunded->isPositive()) {
 100          if ($refunded->isEqualTo($this->getAmountAsCurrency())) {
 101            return pht('Fully Refunded');
 102          } else {
 103            return pht('%s Refunded', $refunded->formatForDisplay());
 104          }
 105        }
 106      }
 107  
 108      return self::getNameForStatus($this->getStatus());
 109    }
 110  
 111    public function generatePHID() {
 112      return PhabricatorPHID::generateNewPHID(
 113        PhortuneChargePHIDType::TYPECONST);
 114    }
 115  
 116    public function getMetadataValue($key, $default = null) {
 117      return idx($this->metadata, $key, $default);
 118    }
 119  
 120    public function setMetadataValue($key, $value) {
 121      $this->metadata[$key] = $value;
 122      return $this;
 123    }
 124  
 125    public function getAccount() {
 126      return $this->assertAttached($this->account);
 127    }
 128  
 129    public function attachAccount(PhortuneAccount $account) {
 130      $this->account = $account;
 131      return $this;
 132    }
 133  
 134    public function getCart() {
 135      return $this->assertAttached($this->cart);
 136    }
 137  
 138    public function attachCart(PhortuneCart $cart = null) {
 139      $this->cart = $cart;
 140      return $this;
 141    }
 142  
 143    public function getAmountRefundableAsCurrency() {
 144      $amount = $this->getAmountAsCurrency();
 145      $refunded = $this->getAmountRefundedAsCurrency();
 146  
 147      // We can't refund negative amounts of money, since it does not make
 148      // sense and is not possible in the various payment APIs.
 149  
 150      $refundable = $amount->subtract($refunded);
 151      if ($refundable->isPositive()) {
 152        return $refundable;
 153      } else {
 154        return PhortuneCurrency::newEmptyCurrency();
 155      }
 156    }
 157  
 158  
 159  /* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 160  
 161  
 162    public function getCapabilities() {
 163      return array(
 164        PhabricatorPolicyCapability::CAN_VIEW,
 165      );
 166    }
 167  
 168    public function getPolicy($capability) {
 169      return $this->getAccount()->getPolicy($capability);
 170    }
 171  
 172    public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
 173      return $this->getAccount()->hasAutomaticCapability($capability, $viewer);
 174    }
 175  
 176    public function describeAutomaticCapability($capability) {
 177      return pht('Charges inherit the policies of the associated account.');
 178    }
 179  
 180  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1