|
<?php |
|
|
|
abstract class Payment_Service_Command_Authorize implements Orderprocessing_Service_Interface_Command |
|
{ |
|
/** |
|
* @param Orderprocessing_Service_Interface_CommandParameter $parameter |
|
* |
|
* @see Orderprocessing_Service_Interface_Command::__invoke() |
|
*/ |
|
public function __invoke(Orderprocessing_Service_Interface_CommandParameter $parameter) |
|
{ |
|
$start = microtime(true); |
|
$workPackage = $parameter->getWorkPackage(); |
|
/* @var $context PaymentExt_Service_Context */ |
|
$context = $parameter->getContext('PaymentExt_Service_Context'); |
|
$processedItems = $parameter->getProcessedItems(); |
|
$orderCollection = $workPackage->getOrderCollection(); |
|
foreach ($processedItems->getAllOrdersWithoutFailedItems($orderCollection) as $order) { |
|
$paymentMethod = $context->getPaymentMethod($order); |
|
if ($paymentMethod->canAuthorize()) { |
|
$paymentResult = $paymentMethod->authorize($order); |
|
} else { |
|
$paymentResult = new PaymentExt_Service_Result_Successful(); |
|
} |
|
$context->setPaymentResult($order, $paymentResult); |
|
if ($paymentResult instanceof Bob_Api_Result_Successful) { |
|
$processedItems->setOrderSuccessful($order); |
|
} else { |
|
$processedItems->setOrderFailed($order); |
|
} |
|
} |
|
$this->publishPaymentAttemptEventAsynchronously($orderCollection); |
|
HF()->statsd()->timing('fraud-detection.payment.timing', microtime(true) - $start); |
|
} |
|
|
|
/** |
|
* @param Transfer_Sales_OrderCollection $orderCollection |
|
*/ |
|
private function publishPaymentAttemptEventAsynchronously(\Transfer_Sales_OrderCollection $orderCollection) |
|
{ |
|
HF()->log()->error('Starting asynchronous shit.'); |
|
\Icicle\Coroutine\create(function () use ($orderCollection) { |
|
HF()->log()->error('Created (and started) the co routine'); |
|
$fork = \Icicle\Concurrent\Forking\Fork::spawn(function () use ($orderCollection) { |
|
HF()->log()->error('Created a fork'); |
|
$start = microtime(); |
|
try { |
|
$publisher = new PaymentExt_Service_Event_Publisher( |
|
new Bi_Service_Adapter_RabbitMQProducer('OrdersExchange'), |
|
$orderCollection |
|
); |
|
$publisher->publishInBulk(); |
|
} catch (\Exception $e) { |
|
HF()->log()->exception($e); |
|
} |
|
HF()->statsd()->timing('fraud-detection.payment-async.timing', microtime(true) - $start); |
|
}); |
|
|
|
yield $fork->join(); |
|
}); |
|
HF()->log()->error('Run the loop.'); |
|
|
|
\Icicle\Loop\run(); |
|
} |
|
} |