phorge/src/applications/phortune/storage/PhortunePurchase.php
epriestley e9615b74a5 Move Phortune product logic into applications
Summary: Ref T2787. `Product` is currently a fairly heavy object, but as Phortune develops it makes a lot of sense to make it a lighter object and put more product logic in applications. Convert it into a fairly lightweight reference to applications. The idea is that Phortune is mostly providing a cart flow, and applications manage the details of products.

Test Plan: Funded an initiative for $1.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T2787

Differential Revision: https://secure.phabricator.com/D10634
2014-10-06 10:30:06 -07:00

107 lines
2.8 KiB
PHP

<?php
/**
* A purchase represents a user buying something or a subscription to a plan.
*/
final class PhortunePurchase extends PhortuneDAO
implements PhabricatorPolicyInterface {
const STATUS_PENDING = 'purchase:pending';
const STATUS_PROCESSING = 'purchase:processing';
const STATUS_ACTIVE = 'purchase:active';
const STATUS_CANCELED = 'purchase:canceled';
const STATUS_DELIVERED = 'purchase:delivered';
const STATUS_FAILED = 'purchase:failed';
protected $productPHID;
protected $accountPHID;
protected $authorPHID;
protected $cartPHID;
protected $basePriceAsCurrency;
protected $quantity;
protected $status;
protected $metadata = array();
private $cart = self::ATTACHABLE;
public static function initializeNewPurchase(
PhabricatorUser $actor,
PhortuneProduct $product) {
return id(new PhortunePurchase())
->setAuthorPHID($actor->getPHID())
->setProductPHID($product->getPHID())
->setQuantity(1)
->setStatus(self::STATUS_PENDING)
->setBasePriceAsCurrency($product->getPriceAsCurrency());
}
public function getConfiguration() {
return array(
self::CONFIG_AUX_PHID => true,
self::CONFIG_SERIALIZATION => array(
'metadata' => self::SERIALIZATION_JSON,
),
self::CONFIG_APPLICATION_SERIALIZERS => array(
'basePriceAsCurrency' => new PhortuneCurrencySerializer(),
),
self::CONFIG_COLUMN_SCHEMA => array(
'cartPHID' => 'phid?',
'basePriceAsCurrency' => 'text64',
'quantity' => 'uint32',
'status' => 'text32',
),
self::CONFIG_KEY_SCHEMA => array(
'key_cart' => array(
'columns' => array('cartPHID'),
),
),
) + parent::getConfiguration();
}
public function generatePHID() {
return PhabricatorPHID::generateNewPHID(
PhabricatorPHIDConstants::PHID_TYPE_PRCH);
}
public function attachCart(PhortuneCart $cart) {
$this->cart = $cart;
return $this;
}
public function getCart() {
return $this->assertAttached($this->cart);
}
public function getFullDisplayName() {
return pht('Goods and/or Services');
}
public function getTotalPriceAsCurrency() {
return $this->getBasePriceAsCurrency();
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */
public function getCapabilities() {
return array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
);
}
public function getPolicy($capability) {
return $this->getCart()->getPolicy($capability);
}
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
return $this->getCart()->hasAutomaticCapability($capability, $viewer);
}
public function describeAutomaticCapability($capability) {
return pht('Purchases have the policies of their cart.');
}
}