2013-04-12 17:10:22 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhortuneAccountBuyController
|
|
|
|
extends PhortuneController {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = $data['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
2014-07-23 19:36:12 +02:00
|
|
|
$viewer = $request->getUser();
|
2013-04-12 17:10:22 +02:00
|
|
|
|
2014-07-23 19:34:08 +02:00
|
|
|
$cart = id(new PhortuneCartQuery())
|
2014-07-23 19:36:12 +02:00
|
|
|
->setViewer($viewer)
|
2013-04-12 17:10:22 +02:00
|
|
|
->withIDs(array($this->id))
|
2014-07-23 19:34:08 +02:00
|
|
|
->needPurchases(true)
|
2013-04-12 17:10:22 +02:00
|
|
|
->executeOne();
|
2014-07-23 19:34:08 +02:00
|
|
|
if (!$cart) {
|
2013-04-12 17:10:22 +02:00
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2014-07-23 19:34:08 +02:00
|
|
|
$account = $cart->getAccount();
|
|
|
|
$account_uri = $this->getApplicationURI($account->getID().'/');
|
2013-04-25 18:45:07 +02:00
|
|
|
|
2014-07-23 19:36:12 +02:00
|
|
|
$methods = id(new PhortunePaymentMethodQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withAccountPHIDs(array($account->getPHID()))
|
|
|
|
->withStatus(PhortunePaymentMethodQuery::STATUS_OPEN)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$e_method = null;
|
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
if ($request->isFormPost()) {
|
|
|
|
|
|
|
|
// Require CAN_EDIT on the cart to actually make purchases.
|
|
|
|
|
|
|
|
PhabricatorPolicyFilter::requireCapability(
|
|
|
|
$viewer,
|
|
|
|
$cart,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT);
|
|
|
|
|
|
|
|
$method_id = $request->getInt('paymentMethodID');
|
|
|
|
$method = idx($methods, $method_id);
|
|
|
|
if (!$method) {
|
|
|
|
$e_method = pht('Required');
|
|
|
|
$errors[] = pht('You must choose a payment method.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$errors) {
|
|
|
|
$provider = $method->buildPaymentProvider();
|
|
|
|
|
|
|
|
$charge = id(new PhortuneCharge())
|
|
|
|
->setAccountPHID($account->getPHID())
|
|
|
|
->setCartPHID($cart->getPHID())
|
|
|
|
->setAuthorPHID($viewer->getPHID())
|
|
|
|
->setPaymentMethodPHID($method->getPHID())
|
|
|
|
->setAmountInCents($cart->getTotalPriceInCents())
|
|
|
|
->setStatus(PhortuneCharge::STATUS_PENDING);
|
|
|
|
|
|
|
|
$charge->openTransaction();
|
|
|
|
$charge->save();
|
|
|
|
|
|
|
|
// TODO: We should be setting some kind of status on the cart here.
|
|
|
|
$cart->save();
|
|
|
|
$charge->saveTransaction();
|
|
|
|
|
|
|
|
$provider->applyCharge($method, $charge);
|
|
|
|
|
|
|
|
throw new Exception('Executed a charge! Your money is gone forever!');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-25 18:45:07 +02:00
|
|
|
$rows = array();
|
|
|
|
$total = 0;
|
|
|
|
foreach ($cart->getPurchases() as $purchase) {
|
|
|
|
$rows[] = array(
|
2014-07-23 19:34:08 +02:00
|
|
|
pht('A Purchase'),
|
2013-05-07 03:04:45 +02:00
|
|
|
PhortuneCurrency::newFromUSDCents($purchase->getBasePriceInCents())
|
|
|
|
->formatForDisplay(),
|
2013-04-25 18:45:07 +02:00
|
|
|
$purchase->getQuantity(),
|
2013-05-07 03:04:45 +02:00
|
|
|
PhortuneCurrency::newFromUSDCents($purchase->getTotalPriceInCents())
|
|
|
|
->formatForDisplay(),
|
2013-04-25 18:45:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$total += $purchase->getTotalPriceInCents();
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows[] = array(
|
|
|
|
phutil_tag('strong', array(), pht('Total')),
|
|
|
|
'',
|
|
|
|
'',
|
2013-05-07 03:04:45 +02:00
|
|
|
phutil_tag('strong', array(),
|
|
|
|
PhortuneCurrency::newFromUSDCents($total)->formatForDisplay()),
|
2013-04-25 18:45:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$table = new AphrontTableView($rows);
|
|
|
|
$table->setHeaders(
|
|
|
|
array(
|
|
|
|
pht('Item'),
|
|
|
|
pht('Price'),
|
|
|
|
pht('Qty.'),
|
|
|
|
pht('Total'),
|
|
|
|
));
|
2014-07-13 18:18:50 +02:00
|
|
|
$table->setColumnClasses(
|
|
|
|
array(
|
|
|
|
'wide',
|
|
|
|
'right',
|
|
|
|
'right',
|
|
|
|
'right',
|
|
|
|
));
|
2013-04-25 18:45:07 +02:00
|
|
|
|
2014-07-13 18:18:50 +02:00
|
|
|
$cart_box = id(new PHUIObjectBoxView())
|
|
|
|
->setHeaderText(pht('Your Cart'))
|
2014-07-23 19:36:12 +02:00
|
|
|
->setFormErrors($errors)
|
2014-07-13 18:18:50 +02:00
|
|
|
->appendChild($table);
|
2013-04-25 18:45:07 +02:00
|
|
|
|
|
|
|
$title = pht('Buy Stuff');
|
|
|
|
|
|
|
|
if (!$methods) {
|
|
|
|
$method_control = id(new AphrontFormStaticControl())
|
|
|
|
->setLabel(pht('Payment Method'))
|
|
|
|
->setValue(
|
|
|
|
phutil_tag('em', array(), pht('No payment methods configured.')));
|
|
|
|
} else {
|
|
|
|
$method_control = id(new AphrontFormRadioButtonControl())
|
|
|
|
->setLabel(pht('Payment Method'))
|
|
|
|
->setName('paymentMethodID')
|
|
|
|
->setValue($request->getInt('paymentMethodID'));
|
|
|
|
foreach ($methods as $method) {
|
|
|
|
$method_control->addButton(
|
|
|
|
$method->getID(),
|
2013-05-06 20:44:24 +02:00
|
|
|
$method->getBrand().' / '.$method->getLastFourDigits(),
|
2013-04-25 18:45:07 +02:00
|
|
|
$method->getDescription());
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 17:10:22 +02:00
|
|
|
|
2014-07-23 19:36:12 +02:00
|
|
|
$method_control->setError($e_method);
|
|
|
|
|
2013-04-12 17:10:22 +02:00
|
|
|
$payment_method_uri = $this->getApplicationURI(
|
|
|
|
$account->getID().'/paymentmethod/edit/');
|
|
|
|
|
|
|
|
$form = id(new AphrontFormView())
|
2014-07-23 19:36:12 +02:00
|
|
|
->setUser($viewer)
|
2013-05-06 20:44:24 +02:00
|
|
|
->appendChild($method_control);
|
|
|
|
|
|
|
|
$add_providers = PhortunePaymentProvider::getProvidersForAddPaymentMethod();
|
|
|
|
if ($add_providers) {
|
|
|
|
$new_method = phutil_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'class' => 'button grey',
|
|
|
|
'href' => $payment_method_uri,
|
|
|
|
'sigil' => 'workflow',
|
|
|
|
),
|
|
|
|
pht('Add New Payment Method'));
|
|
|
|
$form->appendChild(
|
|
|
|
id(new AphrontFormMarkupControl())
|
|
|
|
->setValue($new_method));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($methods || $add_providers) {
|
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2014-06-09 20:36:49 +02:00
|
|
|
->setValue(pht('Submit Payment'))
|
2013-05-06 20:44:24 +02:00
|
|
|
->setDisabled(!$methods));
|
|
|
|
}
|
|
|
|
|
|
|
|
$provider_form = null;
|
|
|
|
|
|
|
|
$pay_providers = PhortunePaymentProvider::getProvidersForOneTimePayment();
|
|
|
|
if ($pay_providers) {
|
|
|
|
$one_time_options = array();
|
|
|
|
foreach ($pay_providers as $provider) {
|
|
|
|
$one_time_options[] = $provider->renderOneTimePaymentButton(
|
|
|
|
$account,
|
|
|
|
$cart,
|
2014-07-23 19:36:12 +02:00
|
|
|
$viewer);
|
2013-05-06 20:44:24 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 20:53:11 +02:00
|
|
|
$provider_form = new PHUIFormLayoutView();
|
2013-05-06 20:44:24 +02:00
|
|
|
$provider_form->appendChild(
|
2013-04-12 17:10:22 +02:00
|
|
|
id(new AphrontFormMarkupControl())
|
2013-05-06 20:44:24 +02:00
|
|
|
->setLabel('Pay With')
|
|
|
|
->setValue($one_time_options));
|
|
|
|
}
|
2013-04-12 17:10:22 +02:00
|
|
|
|
2014-07-13 18:18:50 +02:00
|
|
|
$payment_box = id(new PHUIObjectBoxView())
|
|
|
|
->setHeaderText(pht('Choose Payment Method'))
|
|
|
|
->appendChild($form)
|
|
|
|
->appendChild($provider_form);
|
|
|
|
|
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
|
|
|
$crumbs->addTextCrumb($title);
|
|
|
|
|
2013-04-12 17:10:22 +02:00
|
|
|
return $this->buildApplicationPage(
|
2013-04-25 18:45:07 +02:00
|
|
|
array(
|
2014-07-13 18:18:50 +02:00
|
|
|
$crumbs,
|
|
|
|
$cart_box,
|
|
|
|
$payment_box,
|
2013-04-25 18:45:07 +02:00
|
|
|
),
|
2013-04-12 17:10:22 +02:00
|
|
|
array(
|
|
|
|
'title' => $title,
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|