/** * @return JsonResponse|Response */ public function notifyAction() { $paymentController = $this->get('ant_qa.payu_bundle.controller.payment'); /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */ $dispatcher = $this->container->get('event_dispatcher'); try { $body = file_get_contents('php://input'); $data = stripslashes(trim($body)); /** @var \stdClass $result */ $result = \OpenPayU_Order::consumeNotification($data)->getResponse(); if ($result->order->orderId) { $order = \OpenPayU_Order::retrieve($result->order->orderId); /** @var Payment $payment */ $payment = $this->getDoctrine()->getManager()->getRepository($this->container->getParameter('payu_bundle.payment_class'))->find($result->order->orderId); if ($payment) { if ($payment->getStatus() !== Payment::STATUS_COMPLETED && $payment->getStatus() !== Payment::STATUS_CANCELED) { //update payment status $payment->setStatus($result->order->status); $this->getDoctrine()->getManager()->flush(); $event = new PaymentEvent($payment); $dispatcher->dispatch(AntQaPaymentEvents::PAYMENT_STATUS_UPDATE, $event); if ($result->order->status === Payment::STATUS_CANCELED) { //payment canceled - eg. notify user? $event = new PaymentEvent($payment); $dispatcher->dispatch(AntQaPaymentEvents::PAYMENT_CANCELED, $event); } if ($result->order->status === Payment::STATUS_COMPLETED) { //process payment action - eg. add user point? $event = new PaymentEvent($payment); $dispatcher->dispatch(AntQaPaymentEvents::PAYMENT_COMPLETED, $event); } } } $responseContent = \OpenPayU::buildOrderNotifyResponse($result->order->orderId); $response = new Response(); $response->setContent($responseContent); $response->headers->add(['Content-Type' => 'application/json']); return $response; } } catch (\Exception $e) { $this->get('logger')->addError($e->getMessage()); return new JsonResponse($e->getMessage()); } return new Response('thanks for notice'); }
/** * Function consume notification message * @access private * @param string $xml * @param boolean $response Show Response Xml * @param boolean $debug * @return object $result */ private static function consumeNotification($xml, $response = TRUE, $debug = TRUE) { if ($debug) { OpenPayU::addOutputConsole('OrderNotifyRequest message', $xml); } $xml = stripslashes(urldecode($xml)); $rq = OpenPayU::parseOpenPayUDocument($xml); $reqId = $rq['OpenPayU']['OrderDomainRequest']['OrderNotifyRequest']['ReqId']; $sessionId = $rq['OpenPayU']['OrderDomainRequest']['OrderNotifyRequest']['SessionId']; if ($debug) { OpenPayU::addOutputConsole('OrderNotifyRequest data, reqId', $reqId . ', sessionId: ' . $sessionId); } // response to payu service $rsp = OpenPayU::buildOrderNotifyResponse($reqId); if ($debug) { OpenPayU::addOutputConsole('OrderNotifyResponse message', $rsp); } // show response if ($response == TRUE) { header("Content-Type:text/xml"); echo $rsp; } // create OpenPayU Result object $result = new OpenPayU_Result(); $result->setSessionId($sessionId); $result->setSuccess(TRUE); $result->setRequest($rq); $result->setResponse($rsp); $result->setMessage('OrderNotifyRequest'); // if everything is alright return full data sent from payu service to client return $result; }