2014-10-09 13:30:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhortuneCartUpdateController
|
|
|
|
extends PhortuneCartController {
|
|
|
|
|
2015-08-02 00:43:47 +02:00
|
|
|
public function handleRequest(AphrontRequest $request) {
|
|
|
|
$viewer = $request->getViewer();
|
|
|
|
$id = $request->getURIData('id');
|
2014-10-09 13:30:47 +02:00
|
|
|
|
2015-03-03 19:38:47 +01:00
|
|
|
$authority = $this->loadMerchantAuthority();
|
|
|
|
|
|
|
|
$cart_query = id(new PhortuneCartQuery())
|
2014-10-09 13:30:47 +02:00
|
|
|
->setViewer($viewer)
|
2015-08-02 00:43:47 +02:00
|
|
|
->withIDs(array($id))
|
2015-03-03 19:38:47 +01:00
|
|
|
->needPurchases(true);
|
|
|
|
|
|
|
|
if ($authority) {
|
|
|
|
$cart_query->withMerchantPHIDs(array($authority->getPHID()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$cart = $cart_query->executeOne();
|
2014-10-09 13:30:47 +02:00
|
|
|
if (!$cart) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2014-10-10 01:57:52 +02:00
|
|
|
$charges = id(new PhortuneChargeQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withCartPHIDs(array($cart->getPHID()))
|
|
|
|
->needCarts(true)
|
|
|
|
->withStatuses(
|
|
|
|
array(
|
|
|
|
PhortuneCharge::STATUS_HOLD,
|
|
|
|
PhortuneCharge::STATUS_CHARGED,
|
|
|
|
))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
if ($charges) {
|
|
|
|
$providers = id(new PhortunePaymentProviderConfigQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withPHIDs(mpull($charges, 'getProviderPHID'))
|
|
|
|
->execute();
|
|
|
|
$providers = mpull($providers, null, 'getPHID');
|
|
|
|
} else {
|
|
|
|
$providers = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($charges as $charge) {
|
|
|
|
if ($charge->isRefund()) {
|
|
|
|
// Don't update refunds.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$provider_config = idx($providers, $charge->getProviderPHID());
|
|
|
|
if (!$provider_config) {
|
|
|
|
throw new Exception(pht('Unable to load provider for charge!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$provider = $provider_config->buildProvider();
|
|
|
|
$provider->updateCharge($charge);
|
|
|
|
}
|
2014-10-09 13:30:47 +02:00
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())
|
2015-03-03 19:38:47 +01:00
|
|
|
->setURI($cart->getDetailURI($authority));
|
2014-10-09 13:30:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|