/** * Create a new Order * * @param integer $productId productId * @param object $objUser \User object * @param string $transactionReference transactionReference * @param array $subscriptionOptions subscriptionOptions * * @return boolean * @throws OrderRepositoryException */ public function createOrder($productId, \Cx\Modules\Crm\Model\Entity\Currency $currency, \User $objUser, $transactionReference, $subscriptionOptions = array()) { if (\FWValidator::isEmpty($productId) || \FWValidator::isEmpty($subscriptionOptions) || \FWValidator::isEmpty($transactionReference) || \FWValidator::isEmpty($currency)) { return; } $contactId = $objUser->getCrmUserId(); if (\FWValidator::isEmpty($contactId)) { return; } try { $order = new \Cx\Modules\Order\Model\Entity\Order(); $order->setContactId($contactId); $order->setCurrency($currency); $productRepository = \Env::get('em')->getRepository('Cx\\Modules\\Pim\\Model\\Entity\\Product'); $product = $productRepository->findOneBy(array('id' => $productId)); //create subscription $subscription = $order->createSubscription($product, $subscriptionOptions); // set discount price for first payment period of subscription if (!empty($subscriptionOptions['oneTimeSalePrice'])) { $subscription->setPaymentAmount($subscriptionOptions['oneTimeSalePrice']); } $order->billSubscriptions(); $invoices = $order->getInvoices(); if (!empty($invoices)) { \DBG::msg(__METHOD__ . ": order has invoices"); $paymentRepo = \Env::get('em')->getRepository('\\Cx\\Modules\\Order\\Model\\Entity\\Payment'); foreach ($invoices as $invoice) { if (!$invoice->getPaid()) { \DBG::msg(__METHOD__ . ": lookup payment with transaction-reference {$transactionReference} and amount " . $invoice->getAmount()); $payment = $paymentRepo->findOneByCriteria(array('amount' => $invoice->getAmount(), 'transactionReference' => $transactionReference, 'invoice' => null)); if ($payment) { \DBG::msg(__METHOD__ . ": payment found"); //set subscription-id to Subscription::$externalSubscriptionId if ($subscription) { \DBG::msg(__METHOD__ . ": trying to link to new subscription to the external subscription ID"); $referenceArry = explode('|', $payment->getTransactionReference()); if (isset($referenceArry[4]) && !empty($referenceArry[4])) { $subscription->setExternalSubscriptionId($referenceArry[4]); } } $transactionData = $payment->getTransactionData(); if (!\FWValidator::isEmpty($transactionData) && isset($transactionData['contact']) && isset($transactionData['contact']['id'])) { \DBG::msg(__METHOD__ . ": set externalPaymentCustomerIdProfileAttributeId of user to " . $transactionData['contact']['id']); $objUser->setProfile(array(\Cx\Core\Setting\Controller\Setting::getValue('externalPaymentCustomerIdProfileAttributeId', 'MultiSite') => array(0 => $transactionData['contact']['id'])), true); if (!$objUser->store()) { \DBG::msg('Order::createOrder() Updating user failed: ' . $objUser->getErrorMsg()); } } $invoice->addPayment($payment); $payment->setInvoice($invoice); \Env::get('em')->persist($invoice); \Env::get('em')->persist($payment); break; } } } } \Env::get('em')->persist($order); \Env::get('em')->flush(); return $order; } catch (\Exception $e) { throw new OrderRepositoryException($e->getMessage()); } }