/**
  * {@inheritDoc}
  */
 public function execute($request)
 {
     /** @var NotifyTokenizedDetailsRequest $request */
     $notification = new NotificationDetails();
     $notification->setPaymentName($request->getToken()->getPaymentName());
     $notification->setDetails($request->getNotification());
     $notification->setCreatedAt(new \DateTime());
     $this->doctrine->persist($notification);
     $this->doctrine->flush();
     $details = $notification->getDetails();
     if (is_array($details) && count($details) > 0 && isset($details['custom'])) {
         $custom = $details['custom'];
         $custom = json_decode($custom, true);
         #set vars
         $mcUser = $custom['name'];
         $userId = $custom['user_id'];
         $itemId = $custom['item_id'];
         $ip = $custom['ip'];
         $discount = $custom['discount'];
         $transaction = $details['txn_id'];
         $amountSupposed = $custom['amount'];
         $amountReceived = $details['mc_gross'];
         $user = $this->doctrine->getRepository('MaximCMSBundle:User')->findOneBy(array("id" => $userId));
         $item = $this->doctrine->getRepository('MaximCMSBundle:StoreItem')->findOneBy(array("id" => $itemId));
         # address
         $address['address_street'] = $details['address_street'];
         //address + house number
         $address['address_country_code'] = $details['address_country_code'];
         //country code
         $address['address_name'] = $details['address_name'];
         //address name (person)
         $address['address_country'] = $details['address_country'];
         //country
         $address['address_city'] = $details['address_city'];
         //address city
         $address['address_state'] = $details['address_state'];
         //address state
         $address['address_status'] = $details['address_status'];
         //addres status (confirmed ?)
         # create purchase
         //public function createPurchase($item, User $user, $amount, $discount, $status, $name, $ip, $transaction, $delivery) {
         $purchase = $this->purchaseHelper->createPurchase($item, $user, $amountReceived, $discount, Purchase::PURCHASE_PENDING, $mcUser, $ip, $transaction, Purchase::ITEM_DELIVERY_PENDING);
         $purchase->setMethod(Purchase::PAYMENT_METHOD_PAYPAL);
         if ($amountSupposed != $amountReceived) {
             $purchase->setStatus(Purchase::PURCHASE_INVALID_AMOUNT);
         }
         # deliver the item
         $succeeded = $this->deliverHelper->deliver($purchase);
         # Create notification
         $notification = new UserNotification();
         $notification->setType(UserNotification::TYPE_PURCHASE);
         $notification->setReceiver($purchase->getUser());
         $notification->setData(array('order' => $purchase->getId(), 'status' => $succeeded ? "successful" : "unsuccessful"));
         $this->doctrine->persist($notification);
         $this->doctrine->flush();
         $this->logger->info("Payment " . $succeeded ? "completed" : "failed" . " for user: "******"PAYUM: custom field not found, " . print_r($details, true));
     }
 }
 public function bitpayIpnAction()
 {
     $details = file_get_contents("php://input");
     $apiKey = "WV3IhdH0ysNsWSJtieYlLygwblFEJAXMo3tIWvH0I";
     $json = json_decode($details, true);
     if (is_string($json)) {
         return new Response($json);
     }
     // error
     if (!array_key_exists('posData', $json)) {
         return new Response('no posData');
     }
     $posData = json_decode($json['posData'], true);
     if ($posData['hash'] != $this->generateBitpayHash(serialize($posData['posData']), $apiKey)) {
         return new Response('authentication failed (bad hash)');
     }
     if (is_array($details) && count($details) > 0 && isset($json['posData'])) {
         $custom = $json['posData'];
         $custom = json_decode($custom, true);
         #set vars
         $mcUser = $custom['name'];
         $userId = $custom['user_id'];
         $itemId = $custom['item_id'];
         $ip = $custom['ip'];
         $discount = $custom['discount'];
         $transaction = $json['id'];
         $amountSupposed = $custom['price'];
         //$currency = $json['currency'];
         //$btcPrice = $json['btcPrice'];
         $amountReceived = $json['price'];
         $user = $this->doctrine->getRepository('MaximCMSBundle:User')->findOneBy(array("id" => $userId));
         $item = $this->doctrine->getRepository('MaximCMSBundle:StoreItem')->findOneBy(array("id" => $itemId));
         # address
         /*$address['address_street']       = $details['address_street']; //address + house number
           $address['address_country_code'] = $details['address_country_code'];  //country code
           $address['address_name']         = $details['address_name']; //address name (person)
           $address['address_country']      = $details['address_country']; //country
           $address['address_city']         = $details['address_city']; //address city
           $address['address_state']        = $details['address_state']; //address state
           $address['address_status']       = $details['address_status']; //addres status (confirmed ?)        */
         # create purchase
         $purchase = $this->purchaseHelper->createPurchase($item, $user, $amountReceived, $discount, Purchase::PURCHASE_PENDING, $mcUser, $ip, $transaction, Purchase::ITEM_DELIVERY_PENDING);
         $purchase->setMethod(Purchase::PAYMENT_METHOD_BITPAY);
         if ($amountSupposed != $amountReceived) {
             $purchase->setStatus(Purchase::PURCHASE_INVALID_AMOUNT);
         }
         $succeeded = false;
         try {
             switch ($item->getType()) {
                 case "COMMAND":
                     $this->eventDispatcher->dispatch("minecraft.send", new MinecraftSendEvent(array($purchase)));
                     break;
                 default:
                     $options = array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION);
                     $pdo = new \PDO('mysql:host=' . $this->config["host"] . ';dbname=' . $this->config["database"], $this->config["username"], $this->config["password"], $options);
                     $pdo->query($this->minecraft->parseCommand($item->getCommand(), array("USER" => $purchase->getName())));
                     $pdo = null;
                     $purchase->setStatus(Purchase::PURCHASE_COMPLETE);
                     $purchase->setItemDelivery(Purchase::ITEM_DELIVERY_SUCCESS);
                     $this->doctrine->flush();
             }
             $succeeded = true;
         } catch (\PDOException $ex) {
             $purchase->setStatus(Purchase::PURCHASE_ERROR_SQL);
             $purchase->setItemDelivery(Purchase::ITEM_DELIVERY_FAILED);
             $this->doctrine->flush();
         } catch (CommandExecutionException $ex) {
             $purchase->setStatus(Purchase::PURCHASE_ERROR_COMMAND);
             $purchase->setItemDelivery(Purchase::ITEM_DELIVERY_FAILED);
             $this->doctrine->flush();
         } catch (\Exception $ex) {
             $purchase->setStatus(Purchase::PURCHASE_ERROR_UNKNOWN);
             $purchase->setItemDelivery(Purchase::ITEM_DELIVERY_FAILED);
             $this->doctrine->flush();
         }
         # Create notification
         $notification = new UserNotification();
         $notification->setType(UserNotification::TYPE_PURCHASE);
         $notification->setReceiver($purchase->getUser());
         $notification->setData(array('order' => $purchase->getId(), 'status' => $succeeded ? "successful" : "unsuccessful"));
         $this->doctrine->persist($notification);
         $this->doctrine->flush();
         $this->logger->info("Payment " . $succeeded ? "completed" : "failed" . " for user: "******"PAYUM: custom field not found, " . print_r($details, true));
     }
 }
 public function friendRequestAction($type)
 {
     $request = Request::createFromGlobals();
     if (!$request->isXmlHttpRequest()) {
         throw new AccessDeniedException("Access denied!");
     }
     $em = $this->container->get('doctrine')->getManager();
     # get user to accept
     $userid = $request->request->get('_userid');
     $user = $em->getRepository('MaximCMSBundle:User')->findOneBy(array("id" => $userid));
     if (!$user) {
         throw new NotFoundHttpException("User could not be found.");
     }
     # get the friendrequest
     $frequest = $em->getRepository('MaximCMSBundle:FriendRequest')->findOneBy(array("recipient" => $this->container->get('security.context')->getToken()->getUser(), "user" => $user));
     if (!$frequest || in_array($frequest->getState(), array(FriendRequest::STATE_ACCEPT, FriendRequest::STATE_DENY))) {
         return new Response(json_encode(array("success" => false, "userid" => $user->getId(), "message" => "Could not find the request, possibly the friend has already been accepted or denied.")));
     }
     try {
         if (strtoupper($type) == "APPROVE") {
             $frequest->setState(FriendRequest::STATE_ACCEPT);
             $frequest->setChangedOn(new \DateTime("now"));
             # add the friend to the receiving user
             $friend = new UserFriend();
             $friend->setUser($this->container->get('security.context')->getToken()->getUser());
             $friend->setFriend($user);
             $em->persist($friend);
             # add the friend to the requesting user
             $friend = new UserFriend();
             $friend->setUser($user);
             $friend->setFriend($this->container->get('security.context')->getToken()->getUser());
             $em->persist($friend);
             # notify the user who sent the request
             $notification = new UserNotification();
             $notification->setReceiver($frequest->getUser());
             $notification->setUser($frequest->getRecipient());
             $notification->setType($notification::TYPE_FRIENDREQUEST);
             $notification->addData('accepted', true);
             $em->persist($notification);
         } else {
             $frequest->setState(FriendRequest::STATE_DENY);
             $frequest->setChangedOn(new \DateTime("now"));
         }
         $em->flush();
     } catch (\Exception $ex) {
         $this->container->get('logger')->error($ex->getMessage());
         return new Response(json_encode(array("success" => false, "userid" => $user->getId(), "message" => "An error occured, please try again later")));
     }
     return new Response(json_encode(array("success" => true, "userid" => $user->getId())));
 }