/** * Executes index action * * @param sfRequest $request A request object */ public function executeIndex(sfWebRequest $request) { $this->getResponse()->setContentType('application/json'); $this->setLayout(false); // first api only accepts posts if ($request->getMethod() != "POST") { $this->getResponse()->setStatusCode(405); return $this->renderPartial("wrong_method"); } $access_token = $request->getParameter("access_token"); // check access token if (!$access_token) { $this->getResponse()->setStatusCode(401); return $this->renderPartial("authentication_error"); } $user = Doctrine_Core::getTable('sfGuardUser')->createQuery('u')->where('u.access_token = ?', $access_token)->addWhere('u.is_active = ?', true)->fetchOne(); // check access token if (!$user) { $this->getResponse()->setStatusCode(401); return $this->renderPartial("authentication_error"); } // check if account was setup properly // @todo add publisher commission if (!$user->getApiPriceLike() || !$user->getApiPriceMediaPenetration() || !$user->getApiPaymentMethod() || !$user->getApiCommissionPercentage()) { $this->getResponse()->setStatusCode(403); return $this->renderPartial("setup_failure"); } // get json post $json_content = file_get_contents("php://input"); sfContext::getInstance()->getLogger()->notice($json_content); $deal = new Deal(); $deal->setSfGuardUser($user); $deal->setPaymentMethod($user->getApiPaymentMethod()); $data = json_decode($json_content, true); // check if data is valid json if (!$data) { $this->getResponse()->setStatusCode(406); return $this->renderPartial("wrong_mimetype"); } $deal->fromApiArray($data); // set commission values $deal->setCommissionPercentage($user->getApiCommissionPercentage()); $pot = round($deal->getPrice() * $user->getApiCommissionPercentage() / 100, 2, PHP_ROUND_HALF_UP); $deal->setCommissionPot($pot); $unit = round($pot / $deal->getTargetQuantity(), 2, PHP_ROUND_HALF_UP); $deal->setCommissionPerUnit($unit); // validate request $validate = $deal->validate(); if ($validate !== true) { $this->getResponse()->setStatusCode(406); return $this->renderPartial("deals/wrong_fields", array("errors" => $validate)); } else { $deal->save(); $deal->complete_campaign(); $deal->complete_share(); $deal->complete_coupon(); $deal->complete_billing(); $deal->submit(); if (array_key_exists('activate', $data) && $data['activate'] === true) { $deal->approve(); } } }
public function testDealFromApiArray() { $d = new Deal(); $data = json_decode(self::$VALID_TEST_JSON, true); $d->fromApiArray($data); $this->assertEquals($data['name'], $d->getName()); $this->assertEquals($data['motivation']['title'], $d->getMotivationTitle()); $this->assertEquals($data['motivation']['text'], $d->getMotivationText()); $this->assertEquals($data['spread']['title'], $d->getSpreadTitle()); $this->assertEquals($data['spread']['text'], $d->getSpreadText()); $this->assertEquals($data['spread']['url'], $d->getSpreadUrl()); $this->assertEquals($data['spread']['img'], $d->getSpreadImg()); $this->assertEquals($data['spread']['tos'], $d->getSpreadTos()); $this->assertEquals($data['coupon']['type'], $d->getCouponType()); $this->assertEquals($data['coupon']['title'], $d->getCouponTitle()); $this->assertEquals($data['coupon']['text'], $d->getCouponText()); $this->assertEquals($data['coupon']['code'], $d->getCouponCode()); $this->assertEquals($data['coupon']['url'], $d->getCouponUrl()); $this->assertEquals($data['coupon']['webhook_url'], $d->getCouponWebhookUrl()); $this->assertEquals($data['coupon']['redeem_url'], $d->getCouponRedeemUrl()); $this->assertEquals($data['billing']['type'], $d->getBillingType()); $this->assertEquals($data['billing']['target_quantity'], $d->getTargetQuantity()); }