[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phortune/controller/ -> PhortuneCartCheckoutController.php (source)

   1  <?php
   2  
   3  final class PhortuneCartCheckoutController
   4    extends PhortuneCartController {
   5  
   6    private $id;
   7  
   8    public function willProcessRequest(array $data) {
   9      $this->id = $data['id'];
  10    }
  11  
  12    public function processRequest() {
  13      $request = $this->getRequest();
  14      $viewer = $request->getUser();
  15  
  16      $cart = id(new PhortuneCartQuery())
  17        ->setViewer($viewer)
  18        ->withIDs(array($this->id))
  19        ->needPurchases(true)
  20        ->executeOne();
  21      if (!$cart) {
  22        return new Aphront404Response();
  23      }
  24  
  25      $cancel_uri = $cart->getCancelURI();
  26      $merchant = $cart->getMerchant();
  27  
  28      switch ($cart->getStatus()) {
  29        case PhortuneCart::STATUS_BUILDING:
  30          return $this->newDialog()
  31            ->setTitle(pht('Incomplete Cart'))
  32            ->appendParagraph(
  33              pht(
  34                'The application that created this cart did not finish putting '.
  35                'products in it. You can not checkout with an incomplete '.
  36                'cart.'))
  37            ->addCancelButton($cancel_uri);
  38        case PhortuneCart::STATUS_READY:
  39          // This is the expected, normal state for a cart that's ready for
  40          // checkout.
  41          break;
  42        case PhortuneCart::STATUS_CHARGED:
  43        case PhortuneCart::STATUS_PURCHASING:
  44        case PhortuneCart::STATUS_HOLD:
  45        case PhortuneCart::STATUS_REVIEW:
  46        case PhortuneCart::STATUS_PURCHASED:
  47          // For these states, kick the user to the order page to give them
  48          // information and options.
  49          return id(new AphrontRedirectResponse())->setURI($cart->getDetailURI());
  50        default:
  51          throw new Exception(
  52            pht(
  53              'Unknown cart status "%s"!',
  54              $cart->getStatus()));
  55      }
  56  
  57      $account = $cart->getAccount();
  58      $account_uri = $this->getApplicationURI($account->getID().'/');
  59  
  60      $methods = id(new PhortunePaymentMethodQuery())
  61        ->setViewer($viewer)
  62        ->withAccountPHIDs(array($account->getPHID()))
  63        ->withMerchantPHIDs(array($merchant->getPHID()))
  64        ->withStatuses(array(PhortunePaymentMethod::STATUS_ACTIVE))
  65        ->execute();
  66  
  67      $e_method = null;
  68      $errors = array();
  69  
  70      if ($request->isFormPost()) {
  71  
  72        // Require CAN_EDIT on the cart to actually make purchases.
  73  
  74        PhabricatorPolicyFilter::requireCapability(
  75          $viewer,
  76          $cart,
  77          PhabricatorPolicyCapability::CAN_EDIT);
  78  
  79        $method_id = $request->getInt('paymentMethodID');
  80        $method = idx($methods, $method_id);
  81        if (!$method) {
  82          $e_method = pht('Required');
  83          $errors[] = pht('You must choose a payment method.');
  84        }
  85  
  86        if (!$errors) {
  87          $provider = $method->buildPaymentProvider();
  88  
  89          $charge = $cart->willApplyCharge($viewer, $provider, $method);
  90  
  91          try {
  92            $provider->applyCharge($method, $charge);
  93          } catch (Exception $ex) {
  94            $cart->didFailCharge($charge);
  95            return $this->newDialog()
  96              ->setTitle(pht('Charge Failed'))
  97              ->appendParagraph(
  98                pht(
  99                  'Unable to make payment: %s',
 100                  $ex->getMessage()))
 101              ->addCancelButton($cart->getCheckoutURI(), pht('Continue'));
 102          }
 103  
 104          $cart->didApplyCharge($charge);
 105  
 106          $done_uri = $cart->getCheckoutURI();
 107          return id(new AphrontRedirectResponse())->setURI($done_uri);
 108        }
 109      }
 110  
 111      $cart_table = $this->buildCartContentTable($cart);
 112  
 113      $cart_box = id(new PHUIObjectBoxView())
 114        ->setFormErrors($errors)
 115        ->setHeaderText(pht('Cart Contents'))
 116        ->appendChild($cart_table);
 117  
 118      $title = $cart->getName();
 119  
 120      if (!$methods) {
 121        $method_control = id(new AphrontFormStaticControl())
 122          ->setLabel(pht('Payment Method'))
 123          ->setValue(
 124            phutil_tag('em', array(), pht('No payment methods configured.')));
 125      } else {
 126        $method_control = id(new AphrontFormRadioButtonControl())
 127          ->setLabel(pht('Payment Method'))
 128          ->setName('paymentMethodID')
 129          ->setValue($request->getInt('paymentMethodID'));
 130        foreach ($methods as $method) {
 131          $method_control->addButton(
 132            $method->getID(),
 133            $method->getFullDisplayName(),
 134            $method->getDescription());
 135        }
 136      }
 137  
 138      $method_control->setError($e_method);
 139  
 140      $account_id = $account->getID();
 141  
 142      $payment_method_uri = $this->getApplicationURI("{$account_id}/card/new/");
 143      $payment_method_uri = new PhutilURI($payment_method_uri);
 144      $payment_method_uri->setQueryParams(
 145        array(
 146          'merchantID' => $merchant->getID(),
 147          'cartID' => $cart->getID(),
 148        ));
 149  
 150      $form = id(new AphrontFormView())
 151        ->setUser($viewer)
 152        ->appendChild($method_control);
 153  
 154      $add_providers = $this->loadCreatePaymentMethodProvidersForMerchant(
 155        $merchant);
 156      if ($add_providers) {
 157        $new_method = javelin_tag(
 158          'a',
 159          array(
 160            'class' => 'button grey',
 161            'href'  => $payment_method_uri,
 162            'sigil' => 'workflow',
 163          ),
 164          pht('Add New Payment Method'));
 165        $form->appendChild(
 166          id(new AphrontFormMarkupControl())
 167            ->setValue($new_method));
 168      }
 169  
 170      if ($methods || $add_providers) {
 171        $submit = id(new AphrontFormSubmitControl())
 172          ->setValue(pht('Submit Payment'))
 173          ->setDisabled(!$methods);
 174  
 175        if ($cart->getCancelURI() !== null) {
 176          $submit->addCancelButton($cart->getCancelURI());
 177        }
 178  
 179        $form->appendChild($submit);
 180      }
 181  
 182      $provider_form = null;
 183  
 184      $pay_providers = $this->loadOneTimePaymentProvidersForMerchant($merchant);
 185      if ($pay_providers) {
 186        $one_time_options = array();
 187        foreach ($pay_providers as $provider) {
 188          $one_time_options[] = $provider->renderOneTimePaymentButton(
 189            $account,
 190            $cart,
 191            $viewer);
 192        }
 193  
 194        $one_time_options = phutil_tag(
 195          'div',
 196          array(
 197            'class' => 'phortune-payment-onetime-list',
 198          ),
 199          $one_time_options);
 200  
 201        $provider_form = new PHUIFormLayoutView();
 202        $provider_form->appendChild(
 203          id(new AphrontFormMarkupControl())
 204            ->setLabel('Pay With')
 205            ->setValue($one_time_options));
 206      }
 207  
 208      $payment_box = id(new PHUIObjectBoxView())
 209        ->setHeaderText(pht('Choose Payment Method'))
 210        ->appendChild($form)
 211        ->appendChild($provider_form);
 212  
 213      $crumbs = $this->buildApplicationCrumbs();
 214      $crumbs->addTextCrumb(pht('Checkout'));
 215      $crumbs->addTextCrumb($title);
 216  
 217      return $this->buildApplicationPage(
 218        array(
 219          $crumbs,
 220          $cart_box,
 221          $payment_box,
 222        ),
 223        array(
 224          'title'   => $title,
 225        ));
 226  
 227    }
 228  }


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