protected function loadPage() { $table = new PhortuneCart(); $conn = $table->establishConnection('r'); $rows = queryfx_all($conn, 'SELECT cart.* FROM %T cart %Q %Q %Q', $table->getTableName(), $this->buildWhereClause($conn), $this->buildOrderClause($conn), $this->buildLimitClause($conn)); return $table->loadAllFromArray($rows); }
public function willCreateCart(PhabricatorUser $viewer, PhortuneCart $cart) { $initiative = $this->getInitiative(); if (!$initiative) { throw new PhutilInvalidStateException('setInitiative'); } $cart->setMetadataValue('initiativePHID', $initiative->getPHID()); }
public function assertCanCancelOrder(PhortuneCart $cart) { switch ($cart->getStatus()) { case PhortuneCart::STATUS_PURCHASED: throw new Exception(pht('This order can not be cancelled because it has already been ' . 'completed.')); break; } }
public function willCreateCart(PhabricatorUser $viewer, PhortuneCart $cart) { $subscription = $this->getSubscription(); if (!$subscription) { throw new PhutilInvalidStateException('setSubscription'); } $cart->setMetadataValue('subscriptionPHID', $subscription->getPHID()); }
protected function renderCartDescription(PhortuneCart $cart) { $description = $cart->getDescription(); if (!strlen($description)) { return null; } $output = PhabricatorMarkupEngine::renderOneObject(id(new PhabricatorMarkupOneOff())->setPreserveLinebreaks(true)->setContent($description), 'default', $this->getViewer()); $box = id(new PHUIBoxView())->addMargin(PHUI::MARGIN_LARGE)->appendChild($output); return id(new PHUIObjectBoxView())->setHeaderText(pht('Description'))->appendChild($box); }
protected function renderCartDescription(PhortuneCart $cart) { $description = $cart->getDescription(); if (!strlen($description)) { return null; } $output = new PHUIRemarkupView($this->getUser(), $description); $box = id(new PHUIBoxView())->addMargin(PHUI::MARGIN_LARGE)->appendChild($output); return id(new PHUIObjectBoxView())->setHeaderText(pht('Description'))->appendChild($box); }
protected function buildCartContents(PhortuneCart $cart) { $rows = array(); $total = 0; foreach ($cart->getPurchases() as $purchase) { $rows[] = array($purchase->getFullDisplayName(), PhortuneCurrency::newFromUSDCents($purchase->getBasePriceInCents())->formatForDisplay(), $purchase->getQuantity(), PhortuneCurrency::newFromUSDCents($purchase->getTotalPriceInCents())->formatForDisplay()); $total += $purchase->getTotalPriceInCents(); } $rows[] = array(phutil_tag('strong', array(), pht('Total')), '', '', phutil_tag('strong', array(), PhortuneCurrency::newFromUSDCents($total)->formatForDisplay())); $table = new AphrontTableView($rows); $table->setHeaders(array(pht('Item'), pht('Price'), pht('Qty.'), pht('Total'))); $table->setColumnClasses(array('wide', 'right', 'right', 'right')); return id(new PHUIObjectBoxView())->setHeaderText(pht('Cart Contents'))->appendChild($table); }
public function newCart(PhabricatorUser $actor, PhortuneCartImplementation $implementation, PhortuneMerchant $merchant) { $cart = PhortuneCart::initializeNewCart($actor, $this, $merchant); $cart->setCartClass(get_class($implementation)); $cart->attachImplementation($implementation); $implementation->willCreateCart($actor, $cart); return $cart->save(); }
private function chargeSubscription(PhabricatorUser $viewer, PhortuneSubscription $subscription, PhortuneCart $cart) { $issues = array(); if (!$subscription->getDefaultPaymentMethodPHID()) { $issues[] = pht('There is no payment method associated with this subscription, so ' . 'it could not be billed automatically. Add a default payment method ' . 'to enable automatic billing.'); return $issues; } $method = id(new PhortunePaymentMethodQuery())->setViewer($viewer)->withPHIDs(array($subscription->getDefaultPaymentMethodPHID()))->executeOne(); if (!$method) { $issues[] = pht('The payment method associated with this subscription is invalid ' . 'or out of date, so it could not be automatically billed. Update ' . 'the default payment method to enable automatic billing.'); return $issues; } $provider = $method->buildPaymentProvider(); $charge = $cart->willApplyCharge($viewer, $provider, $method); try { $provider->applyCharge($method, $charge); } catch (Exception $ex) { $cart->didFailCharge($charge); $issues[] = pht('Automatic billing failed: %s', $ex->getMessage()); return $issues; } $cart->didApplyCharge($charge); }
public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $account = id(new PhortuneAccountQuery())->setViewer($user)->withIDs(array($this->accountID))->executeOne(); if (!$account) { return new Aphront404Response(); } $account_uri = $this->getApplicationURI($account->getID() . '/'); $product = id(new PhortuneProductQuery())->setViewer($user)->withIDs(array($this->productID))->executeOne(); if (!$product) { return new Aphront404Response(); } if ($request->isFormPost()) { // TODO: Use ApplicationTransations. $cart = new PhortuneCart(); $cart->openTransaction(); $cart->setStatus(PhortuneCart::STATUS_READY); $cart->setAccountPHID($account->getPHID()); $cart->setAuthorPHID($user->getPHID()); $cart->save(); $purchase = new PhortunePurchase(); $purchase->setProductPHID($product->getPHID()); $purchase->setAccountPHID($account->getPHID()); $purchase->setAuthorPHID($user->getPHID()); $purchase->setCartPHID($cart->getPHID()); $purchase->setBasePriceInCents($product->getPriceInCents()); $purchase->setQuantity(1); $purchase->setTotalPriceInCents($purchase->getBasePriceInCents() * $purchase->getQuantity()); $purchase->setStatus(PhortunePurchase::STATUS_PENDING); $purchase->save(); $cart->saveTransaction(); $cart_id = $cart->getID(); $cart_uri = $this->getApplicationURI('/cart/' . $cart_id . '/checkout/'); return id(new AphrontRedirectResponse())->setURI($cart_uri); } return $this->newDialog()->setTitle(pht('Purchase Product'))->appendParagraph(pht('Really purchase this stuff?'))->addSubmitButton(pht('Checkout'))->addCancelButton($account_uri); }
public function render() { $carts = $this->getCarts(); $handles = $this->getHandles(); $viewer = $this->getUser(); $is_invoices = $this->getIsInvoices(); $is_merchant = $this->getIsMerchantView(); $rows = array(); $rowc = array(); foreach ($carts as $cart) { $cart_link = $handles[$cart->getPHID()]->renderLink(); $purchases = $cart->getPurchases(); if (count($purchases) == 1) { $purchase = head($purchases); $purchase_name = $handles[$purchase->getPHID()]->renderLink(); $purchases = array(); } else { $purchase_name = ''; } if ($is_invoices) { $merchant_link = $handles[$cart->getMerchantPHID()]->renderLink(); } else { $merchant_link = null; } $rowc[] = ''; $rows[] = array($cart->getID(), $merchant_link, phutil_tag('strong', array(), $cart_link), $purchase_name, phutil_tag('strong', array(), $cart->getTotalPriceAsCurrency()->formatForDisplay()), PhortuneCart::getNameForStatus($cart->getStatus()), phabricator_datetime($cart->getDateModified(), $viewer), phabricator_datetime($cart->getDateCreated(), $viewer), phutil_tag('a', array('href' => $cart->getCheckoutURI(), 'class' => 'small green button'), pht('Pay Now'))); foreach ($purchases as $purchase) { $id = $purchase->getID(); $price = $purchase->getTotalPriceAsCurrency()->formatForDisplay(); $rowc[] = ''; $rows[] = array('', '', $handles[$purchase->getPHID()]->renderLink(), $price, '', '', '', ''); } } $table = id(new AphrontTableView($rows))->setNoDataString($this->getNoDataString())->setRowClasses($rowc)->setHeaders(array(pht('ID'), pht('Merchant'), $is_invoices ? pht('Invoice') : pht('Order'), pht('Purchase'), pht('Amount'), pht('Status'), pht('Updated'), pht('Invoice Date'), null))->setColumnClasses(array('', '', '', 'wide', 'right', '', 'right', 'right', 'action'))->setColumnVisibility(array(true, $is_invoices, true, true, true, !$is_invoices, !$is_invoices, $is_invoices, $is_invoices && !$is_merchant)); return $table; }
private function buildActionListView(PhortuneCart $cart, $can_edit, $authority, $resume_uri) { $viewer = $this->getRequest()->getUser(); $id = $cart->getID(); $view = id(new PhabricatorActionListView())->setUser($viewer)->setObject($cart); $can_cancel = $can_edit && $cart->canCancelOrder(); if ($authority) { $prefix = 'merchant/' . $authority->getID() . '/'; } else { $prefix = ''; } $cancel_uri = $this->getApplicationURI("{$prefix}cart/{$id}/cancel/"); $refund_uri = $this->getApplicationURI("{$prefix}cart/{$id}/refund/"); $update_uri = $this->getApplicationURI("{$prefix}cart/{$id}/update/"); $accept_uri = $this->getApplicationURI("{$prefix}cart/{$id}/accept/"); $view->addAction(id(new PhabricatorActionView())->setName(pht('Cancel Order'))->setIcon('fa-times')->setDisabled(!$can_cancel)->setWorkflow(true)->setHref($cancel_uri)); if ($authority) { if ($cart->getStatus() == PhortuneCart::STATUS_REVIEW) { $view->addAction(id(new PhabricatorActionView())->setName(pht('Accept Order'))->setIcon('fa-check')->setWorkflow(true)->setHref($accept_uri)); } $view->addAction(id(new PhabricatorActionView())->setName(pht('Refund Order'))->setIcon('fa-reply')->setWorkflow(true)->setHref($refund_uri)); } $view->addAction(id(new PhabricatorActionView())->setName(pht('Update Status'))->setIcon('fa-refresh')->setHref($update_uri)); if ($can_edit && $resume_uri) { $view->addAction(id(new PhabricatorActionView())->setName(pht('Continue Checkout'))->setIcon('fa-shopping-cart')->setHref($resume_uri)); } return $view; }
protected function renderResultList(array $carts, PhabricatorSavedQuery $query, array $handles) { assert_instances_of($carts, 'PhortuneCart'); $viewer = $this->requireViewer(); $rows = array(); foreach ($carts as $cart) { $merchant = $cart->getMerchant(); if ($this->getMerchant()) { $href = $this->getApplicationURI('merchant/' . $merchant->getID() . '/cart/' . $cart->getID() . '/'); } else { $href = $cart->getDetailURI(); } $rows[] = array($cart->getID(), $handles[$cart->getPHID()]->renderLink(), $handles[$merchant->getPHID()]->renderLink(), $handles[$cart->getAuthorPHID()]->renderLink(), $cart->getTotalPriceAsCurrency()->formatForDisplay(), PhortuneCart::getNameForStatus($cart->getStatus()), phabricator_datetime($cart->getDateModified(), $viewer)); } $table = id(new AphrontTableView($rows))->setNoDataString(pht('No orders match the query.'))->setHeaders(array(pht('ID'), pht('Order'), pht('Merchant'), pht('Authorized By'), pht('Amount'), pht('Status'), pht('Updated')))->setColumnClasses(array('', 'pri', '', '', 'wide right', '', 'right')); $merchant = $this->getMerchant(); if ($merchant) { $notice = pht('Orders for %s', $merchant->getName()); } else { $notice = pht('Your Orders'); } $table->setNotice($notice); $result = new PhabricatorApplicationSearchResultView(); $result->setTable($table); return $result; }
public function loadActiveCharge(PhortuneCart $cart) { $request = $this->getRequest(); $viewer = $request->getUser(); return id(new PhortuneChargeQuery())->setViewer($viewer)->withCartPHIDs(array($cart->getPHID()))->withStatuses(array(PhortuneCharge::STATUS_CHARGING))->executeOne(); }
public function renderOneTimePaymentButton(PhortuneAccount $account, PhortuneCart $cart, PhabricatorUser $user) { require_celerity_resource('phortune-css'); $icon_uri = $this->getPaymentMethodIcon(); $description = $this->getPaymentMethodProviderDescription(); $details = $this->getPaymentMethodDescription(); $icon = id(new PHUIIconView())->setImage($icon_uri)->addClass('phortune-payment-icon'); $button = id(new PHUIButtonView())->setSize(PHUIButtonView::BIG)->setColor(PHUIButtonView::GREY)->setIcon($icon)->setText($description)->setSubtext($details); $uri = $this->getControllerURI('checkout', array('cartID' => $cart->getID())); return phabricator_form($user, array('action' => $uri, 'method' => 'POST'), $button); }
public function getDescription(PhortuneCart $cart) { return $cart->getMetadataValue('adhoc.description'); }
public function renderOneTimePaymentButton(PhortuneAccount $account, PhortuneCart $cart, PhabricatorUser $user) { require_celerity_resource('phortune-css'); $description = $this->getPaymentMethodProviderDescription(); $details = $this->getPaymentMethodDescription(); $icon = id(new PHUIIconView())->setSpriteSheet(PHUIIconView::SPRITE_LOGIN)->setSpriteIcon($this->getPaymentMethodIcon()); $button = id(new PHUIButtonView())->setSize(PHUIButtonView::BIG)->setColor(PHUIButtonView::GREY)->setIcon($icon)->setText($description)->setSubtext($details); // NOTE: We generate a local URI to make sure the form picks up CSRF tokens. $uri = $this->getControllerURI('checkout', array('cartID' => $cart->getID()), $local = true); return phabricator_form($user, array('action' => $uri, 'method' => 'POST'), $button); }
private function buildCurtainView(PhortuneCart $cart, $can_edit, $authority, $resume_uri) { $viewer = $this->getViewer(); $id = $cart->getID(); $curtain = $this->newCurtainView($cart); $can_cancel = $can_edit && $cart->canCancelOrder(); if ($authority) { $prefix = 'merchant/' . $authority->getID() . '/'; } else { $prefix = ''; } $cancel_uri = $this->getApplicationURI("{$prefix}cart/{$id}/cancel/"); $refund_uri = $this->getApplicationURI("{$prefix}cart/{$id}/refund/"); $update_uri = $this->getApplicationURI("{$prefix}cart/{$id}/update/"); $accept_uri = $this->getApplicationURI("{$prefix}cart/{$id}/accept/"); $print_uri = $this->getApplicationURI("{$prefix}cart/{$id}/?__print__=1"); $curtain->addAction(id(new PhabricatorActionView())->setName(pht('Cancel Order'))->setIcon('fa-times')->setDisabled(!$can_cancel)->setWorkflow(true)->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(id(new PhabricatorActionView())->setName(pht('Update Status'))->setIcon('fa-refresh')->setHref($update_uri)); if ($can_edit && $resume_uri) { $curtain->addAction(id(new PhabricatorActionView())->setName(pht('Continue Checkout'))->setIcon('fa-shopping-cart')->setHref($resume_uri)); } $curtain->addAction(id(new PhabricatorActionView())->setName(pht('Printable Version'))->setHref($print_uri)->setOpenInNewWindow(true)->setIcon('fa-print')); return $curtain; }