Differential D20735 Diff 49444 src/applications/phortune/controller/cart/PhortuneCartViewController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/phortune/controller/cart/PhortuneCartViewController.php
<?php | <?php | ||||
final class PhortuneCartViewController | final class PhortuneCartViewController | ||||
extends PhortuneCartController { | extends PhortuneCartController { | ||||
private $action = null; | private $action = null; | ||||
public function handleRequest(AphrontRequest $request) { | protected function shouldRequireAccountAuthority() { | ||||
$viewer = $request->getViewer(); | return false; | ||||
$id = $request->getURIData('id'); | |||||
$this->action = $request->getURIData('action'); | |||||
$authority = $this->loadMerchantAuthority(); | |||||
require_celerity_resource('phortune-css'); | |||||
$query = id(new PhortuneCartQuery()) | |||||
->setViewer($viewer) | |||||
->withIDs(array($id)) | |||||
->needPurchases(true); | |||||
if ($authority) { | |||||
$query->withMerchantPHIDs(array($authority->getPHID())); | |||||
} | } | ||||
$cart = $query->executeOne(); | protected function shouldRequireMerchantAuthority() { | ||||
if (!$cart) { | return false; | ||||
return new Aphront404Response(); | |||||
} | } | ||||
$cart_table = $this->buildCartContentTable($cart); | protected function handleCartRequest(AphrontRequest $request) { | ||||
$viewer = $request->getViewer(); | |||||
$cart = $this->getCart(); | |||||
$authority = $this->getMerchantAuthority(); | |||||
$can_edit = $this->hasAccountAuthority(); | |||||
$can_edit = PhabricatorPolicyFilter::hasCapability( | $this->action = $request->getURIData('action'); | ||||
$viewer, | |||||
$cart, | $cart_table = $this->buildCartContentTable($cart); | ||||
PhabricatorPolicyCapability::CAN_EDIT); | |||||
$errors = array(); | $errors = array(); | ||||
$error_view = null; | $error_view = null; | ||||
$resume_uri = null; | $resume_uri = null; | ||||
switch ($cart->getStatus()) { | switch ($cart->getStatus()) { | ||||
case PhortuneCart::STATUS_READY: | case PhortuneCart::STATUS_READY: | ||||
if ($authority && $cart->getIsInvoice()) { | if ($cart->getIsInvoice()) { | ||||
// We arrived here by following the ad-hoc invoice workflow, and | |||||
// are acting with merchant authority. | |||||
$checkout_uri = PhabricatorEnv::getURI($cart->getCheckoutURI()); | |||||
$invoice_message = array( | |||||
pht( | |||||
'Manual invoices do not automatically notify recipients yet. '. | |||||
'Send the payer this checkout link:'), | |||||
' ', | |||||
phutil_tag( | |||||
'a', | |||||
array( | |||||
'href' => $checkout_uri, | |||||
), | |||||
$checkout_uri), | |||||
); | |||||
$error_view = id(new PHUIInfoView()) | $error_view = id(new PHUIInfoView()) | ||||
->setSeverity(PHUIInfoView::SEVERITY_WARNING) | ->setSeverity(PHUIInfoView::SEVERITY_NOTICE) | ||||
->setErrors(array($invoice_message)); | ->appendChild(pht('This invoice is ready for payment.')); | ||||
} | } | ||||
break; | break; | ||||
case PhortuneCart::STATUS_PURCHASING: | case PhortuneCart::STATUS_PURCHASING: | ||||
if ($can_edit) { | if ($can_edit) { | ||||
$resume_uri = $cart->getMetadataValue('provider.checkoutURI'); | $resume_uri = $cart->getMetadataValue('provider.checkoutURI'); | ||||
if ($resume_uri) { | if ($resume_uri) { | ||||
$errors[] = pht( | $errors[] = pht( | ||||
'The checkout process has been started, but not yet completed. '. | 'The checkout process has been started, but not yet completed. '. | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | $curtain = $this->buildCurtainView( | ||||
$cart, | $cart, | ||||
$can_edit, | $can_edit, | ||||
$authority, | $authority, | ||||
$resume_uri); | $resume_uri); | ||||
$header = id(new PHUIHeaderView()) | $header = id(new PHUIHeaderView()) | ||||
->setUser($viewer) | ->setUser($viewer) | ||||
->setHeader($cart->getName()) | ->setHeader($cart->getName()) | ||||
->setHeaderIcon('fa-shopping-cart'); | ->setHeaderIcon('fa-shopping-bag'); | ||||
if ($cart->getStatus() == PhortuneCart::STATUS_PURCHASED) { | if ($cart->getStatus() == PhortuneCart::STATUS_PURCHASED) { | ||||
$done_uri = $cart->getDoneURI(); | $done_uri = $cart->getDoneURI(); | ||||
if ($done_uri) { | if ($done_uri) { | ||||
$header->addActionLink( | $header->addActionLink( | ||||
id(new PHUIButtonView()) | id(new PHUIButtonView()) | ||||
->setTag('a') | ->setTag('a') | ||||
->setHref($done_uri) | ->setHref($done_uri) | ||||
Show All 10 Lines | protected function handleCartRequest(AphrontRequest $request) { | ||||
$description = $this->renderCartDescription($cart); | $description = $this->renderCartDescription($cart); | ||||
$charges = id(new PhortuneChargeQuery()) | $charges = id(new PhortuneChargeQuery()) | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->withCartPHIDs(array($cart->getPHID())) | ->withCartPHIDs(array($cart->getPHID())) | ||||
->needCarts(true) | ->needCarts(true) | ||||
->execute(); | ->execute(); | ||||
$phids = array(); | |||||
foreach ($charges as $charge) { | |||||
$phids[] = $charge->getProviderPHID(); | |||||
$phids[] = $charge->getCartPHID(); | |||||
$phids[] = $charge->getMerchantPHID(); | |||||
$phids[] = $charge->getPaymentMethodPHID(); | |||||
} | |||||
$handles = $this->loadViewerHandles($phids); | |||||
$charges_table = id(new PhortuneChargeTableView()) | $charges_table = id(new PhortuneChargeTableView()) | ||||
->setUser($viewer) | ->setUser($viewer) | ||||
->setHandles($handles) | |||||
->setCharges($charges) | ->setCharges($charges) | ||||
->setShowOrder(false); | ->setShowOrder(false); | ||||
$charges = id(new PHUIObjectBoxView()) | $charges = id(new PHUIObjectBoxView()) | ||||
->setHeaderText(pht('Charges')) | ->setHeaderText(pht('Charges')) | ||||
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) | ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) | ||||
->setTable($charges_table); | ->setTable($charges_table); | ||||
$account = $cart->getAccount(); | $account = $cart->getAccount(); | ||||
$crumbs = $this->buildApplicationCrumbs(); | $crumbs = $this->buildApplicationCrumbs() | ||||
if ($authority) { | ->addTextCrumb($account->getName(), $account->getURI()) | ||||
$this->addMerchantCrumb($crumbs, $authority); | ->addTextCrumb(pht('Orders'), $account->getOrdersURI()) | ||||
} else { | ->addTextCrumb(pht('Cart %d', $cart->getID())) | ||||
$this->addAccountCrumb($crumbs, $cart->getAccount()); | ->setBorder(true); | ||||
} | |||||
$crumbs->addTextCrumb(pht('Cart %d', $cart->getID())); | require_celerity_resource('phortune-css'); | ||||
$crumbs->setBorder(true); | |||||
if (!$this->action) { | if (!$this->action) { | ||||
$class = 'phortune-cart-page'; | $class = 'phortune-cart-page'; | ||||
$timeline = $this->buildTransactionTimeline( | $timeline = $this->buildTransactionTimeline( | ||||
$cart, | $cart, | ||||
new PhortuneCartTransactionQuery()); | new PhortuneCartTransactionQuery()); | ||||
$timeline | $timeline | ||||
->setShouldTerminate(true); | ->setShouldTerminate(true); | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | protected function handleCartRequest(AphrontRequest $request) { | ||||
$page = $this->newPage() | $page = $this->newPage() | ||||
->setTitle(pht('Cart %d', $cart->getID())) | ->setTitle(pht('Cart %d', $cart->getID())) | ||||
->addClass($class) | ->addClass($class) | ||||
->appendChild($view); | ->appendChild($view); | ||||
if ($crumbs) { | if ($crumbs) { | ||||
$page->setCrumbs($crumbs); | $page->setCrumbs($crumbs); | ||||
} | } | ||||
return $page; | return $page; | ||||
} | } | ||||
private function buildDetailsView(PhortuneCart $cart) { | private function buildDetailsView(PhortuneCart $cart) { | ||||
$viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
$view = id(new PHUIPropertyListView()) | $view = id(new PHUIPropertyListView()) | ||||
->setUser($viewer) | ->setUser($viewer) | ||||
->setObject($cart); | ->setObject($cart); | ||||
Show All 35 Lines | private function buildCurtainView( | ||||
PhortuneCart $cart, | PhortuneCart $cart, | ||||
$can_edit, | $can_edit, | ||||
$authority, | $authority, | ||||
$resume_uri) { | $resume_uri) { | ||||
$viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
$id = $cart->getID(); | $id = $cart->getID(); | ||||
$curtain = $this->newCurtainView($cart); | $curtain = $this->newCurtainView($cart); | ||||
$status = $cart->getStatus(); | |||||
$can_cancel = ($can_edit && $cart->canCancelOrder()); | $is_ready = ($status === PhortuneCart::STATUS_READY); | ||||
if ($authority) { | $can_cancel = ($can_edit && $cart->canCancelOrder()); | ||||
$prefix = 'merchant/'.$authority->getID().'/'; | $can_checkout = ($can_edit && $is_ready); | ||||
} else { | $can_accept = ($status === PhortuneCart::STATUS_REVIEW); | ||||
$prefix = ''; | $can_refund = ($authority && $cart->canRefundOrder()); | ||||
} | $can_void = ($authority && $cart->canVoidOrder()); | ||||
$cancel_uri = $this->getApplicationURI("cart/{$id}/cancel/"); | |||||
$refund_uri = $this->getApplicationURI("cart/{$id}/refund/"); | |||||
$update_uri = $this->getApplicationURI("cart/{$id}/update/"); | |||||
$accept_uri = $this->getApplicationURI("cart/{$id}/accept/"); | |||||
$print_uri = $this->getApplicationURI("cart/{$id}/print/"); | |||||
$checkout_uri = $cart->getCheckoutURI(); | |||||
$void_uri = $this->getApplicationURI("cart/{$id}/void/"); | |||||
$cancel_uri = $this->getApplicationURI("{$prefix}cart/{$id}/cancel/"); | $curtain->addAction( | ||||
$refund_uri = $this->getApplicationURI("{$prefix}cart/{$id}/refund/"); | id(new PhabricatorActionView()) | ||||
$update_uri = $this->getApplicationURI("{$prefix}cart/{$id}/update/"); | ->setName(pht('Pay Now')) | ||||
$accept_uri = $this->getApplicationURI("{$prefix}cart/{$id}/accept/"); | ->setIcon('fa-credit-card') | ||||
$print_uri = $this->getApplicationURI("{$prefix}cart/{$id}/print/"); | ->setDisabled(!$can_checkout) | ||||
->setWorkflow(!$can_checkout) | |||||
->setHref($checkout_uri)); | |||||
$curtain->addAction( | $curtain->addAction( | ||||
id(new PhabricatorActionView()) | id(new PhabricatorActionView()) | ||||
->setName(pht('Cancel Order')) | ->setName(pht('Cancel Order')) | ||||
->setIcon('fa-times') | ->setIcon('fa-times') | ||||
->setDisabled(!$can_cancel) | ->setDisabled(!$can_cancel) | ||||
->setWorkflow(true) | ->setWorkflow(true) | ||||
->setHref($cancel_uri)); | ->setHref($cancel_uri)); | ||||
if ($authority) { | |||||
if ($cart->getStatus() == PhortuneCart::STATUS_REVIEW) { | |||||
$curtain->addAction( | |||||
id(new PhabricatorActionView()) | |||||
->setName(pht('Accept Order')) | |||||
->setIcon('fa-check') | |||||
->setWorkflow(true) | |||||
->setHref($accept_uri)); | |||||
} | |||||
$curtain->addAction( | |||||
id(new PhabricatorActionView()) | |||||
->setName(pht('Refund Order')) | |||||
->setIcon('fa-reply') | |||||
->setWorkflow(true) | |||||
->setHref($refund_uri)); | |||||
} | |||||
$curtain->addAction( | $curtain->addAction( | ||||
id(new PhabricatorActionView()) | id(new PhabricatorActionView()) | ||||
->setName(pht('Update Status')) | ->setName(pht('Update Status')) | ||||
->setIcon('fa-refresh') | ->setIcon('fa-refresh') | ||||
->setHref($update_uri)); | ->setHref($update_uri)); | ||||
if ($can_edit && $resume_uri) { | if ($can_edit && $resume_uri) { | ||||
$curtain->addAction( | $curtain->addAction( | ||||
id(new PhabricatorActionView()) | id(new PhabricatorActionView()) | ||||
->setName(pht('Continue Checkout')) | ->setName(pht('Continue Checkout')) | ||||
->setIcon('fa-shopping-cart') | ->setIcon('fa-shopping-bag') | ||||
->setHref($resume_uri)); | ->setHref($resume_uri)); | ||||
} | } | ||||
$curtain->addAction( | $curtain->addAction( | ||||
id(new PhabricatorActionView()) | id(new PhabricatorActionView()) | ||||
->setName(pht('Printable Version')) | ->setName(pht('Printable Version')) | ||||
->setHref($print_uri) | ->setHref($print_uri) | ||||
->setOpenInNewWindow(true) | ->setOpenInNewWindow(true) | ||||
->setIcon('fa-print')); | ->setIcon('fa-print')); | ||||
if ($authority) { | |||||
$curtain->addAction( | |||||
id(new PhabricatorActionView()) | |||||
->setType(PhabricatorActionView::TYPE_DIVIDER)); | |||||
$curtain->addAction( | |||||
id(new PhabricatorActionView()) | |||||
->setName(pht('Accept Order')) | |||||
->setIcon('fa-check') | |||||
->setWorkflow(true) | |||||
->setDisabled(!$can_accept) | |||||
->setHref($accept_uri)); | |||||
$curtain->addAction( | |||||
id(new PhabricatorActionView()) | |||||
->setName(pht('Refund Order')) | |||||
->setIcon('fa-reply') | |||||
->setWorkflow(true) | |||||
->setDisabled(!$can_refund) | |||||
->setHref($refund_uri)); | |||||
$curtain->addAction( | |||||
id(new PhabricatorActionView()) | |||||
->setName(pht('Void Invoice')) | |||||
->setIcon('fa-times') | |||||
->setWorkflow(true) | |||||
->setDisabled(!$can_void) | |||||
->setHref($void_uri)); | |||||
} | |||||
return $curtain; | return $curtain; | ||||
} | } | ||||
} | } |