0

I am working on REST Api for saving the data,

i am trying as like below data is saving but can't return the response

The below file V1.php (model/Api2/Customer/Rest/Admin/V1.php)

protected function _create($couponData)
{
    $websiteId = Mage::app()->getWebsite()->getId();
    $store = Mage::app()->getStore();

    Mage::log($websiteId,null,'website.log');
    Mage::log($store,null,'store.log');
    try {
          $customer = Mage::getModel("customer/customer");
          $customer->setWebsiteId($websiteId)
                   ->setStore($store)
                   ->setFirstname($couponData['name'])
                   ->setLastname($couponData['name'])
                   ->setEmail($couponData['email'])
                   ->setPassword($couponData['pwd']);
          $cid = $customer->save();
          $cid = "1232";
          echo "-------"; print_r($cid); echo "-------";

       // $this->_retrieveCollection($cid);
        //$this->_loadSalesRule($cid);


        return "testt";

     } catch(Exception $e) {
        echo "---------Going To Expection---------";
        print_r(json_encode($e->getMessage()));

     }

     return "testtt1";

}
/**
 * Retrieve list of coupon codes.
 *
 * @return array
 */
protected function _retrieveCollection($cid)
{
   /* $ruleId = $this->getRequest()->getParam('rule_id');
    $rule = $this->_loadSalesRule($ruleId);
    $collection = Mage::getResourceModel('salesrule/coupon_collection');
    $collection->addRuleToFilter($rule);
    $this->_applyCollectionModifiers($collection);
    $data = $collection->load()->toArray();
    //return $data['items'];*/
    return "----Recrive Collection Function---".$cid;
}
/**
 * Load sales rule by ID.
 *
 * @param int $ruleId
 * @return Mage_SalesRule_Model_Rule
 */
protected function _loadSalesRule($ruleId)
{
   /* 
   if (!$ruleId) {
        $this->_critical(Mage::helper('salesrule')
            ->__('Rule ID not specified.'), Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
    }
    $rule = Mage::getModel('salesrule/rule')->load($ruleId);
    if (!$rule->getId()) {
        $this->_critical(Mage::helper('salesrule')
            ->__('Rule was not found123.'), Mage_Api2_Model_Server::HTTP_NOT_FOUND);
    }
    return $rule;
    */
    return "Hello";
}

}

I am sending request with variable

try {
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();
if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
    $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
    $_SESSION['secret'] = $requestToken['oauth_token_secret'];
    $_SESSION['state'] = 1;
    header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
    exit;
} else if ($_SESSION['state'] == 1) {
    $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
    $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
    $_SESSION['state'] = 2;
    $_SESSION['token'] = $accessToken['oauth_token'];
    $_SESSION['secret'] = $accessToken['oauth_token_secret'];
    header('Location: ' . $callbackUrl);
    exit;
} else {
    $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
    $ruleId = "3";
    $couponGenerationData = array();
    $couponGenerationData['name'] = "ramesh";
    $couponGenerationData['email'] = "ramesh.Restapi421@test.com";
    $couponGenerationData['pwd'] = "test123";

    /* Send Throught Post*/
    $resourceUrl = $apiUrl."/coupondemo/{$ruleId}";
    //$resourceUrl = "$apiUrl/coupondemo/rules/{$ruleId}/codes";
    $oauthClient->fetch($resourceUrl, json_encode($couponGenerationData), OAUTH_HTTP_METHOD_POST, array(
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
    ));
    // Retrieve list of created coupons via GET
    $collectionFilters = array('limit' => $couponGenerationData['qty'], 'order' => 'coupon_id', 'dir' => 'dsc');
    $oauthClient->fetch($resourceUrl, $collectionFilters, OAUTH_HTTP_METHOD_GET, array(
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
    ));
    $coupons = json_decode($oauthClient->getLastResponse(), true);
    // Display the newly generated codes to demonstrate that the Coupon AutoGen API works
    // In reality, you might put these codes in emails to customers, store them in a database, etc.
    echo "New coupon codes:<br/>";
    foreach ($coupons as $coupon) {
        echo " --> " . $coupon['code'] . "<br/>";
    }
}} catch (OAuthException $e) {
print_r($e->getMessage());
echo "<br/>";
print_r($e->lastResponse);

}

it' returning only "Recrive Collection Function"

0

In my above script i am sending POST & GET requests at same time that's why i am getting response for GET because GET is the latest(last) request.

try {
   $authType = ($_SESSION['state'] == 2) ? 
      OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;

   $oauthClient = new OAuth($consumerKey, $consumerSecret, 
                    OAUTH_SIG_METHOD_HMACSHA1, $authType);

   $oauthClient->enableDebug();

   if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
        $requestToken = $oauthClient->
           getRequestToken($temporaryCredentialsRequestUrl);
        $_SESSION['secret'] = $requestToken['oauth_token_secret'];
        $_SESSION['state'] = 1;
        header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' 
            . $requestToken['oauth_token']);
        exit;
 } else if ($_SESSION['state'] == 1) {
        $oauthClient->setToken($_GET['oauth_token'], 
        $_SESSION['secret']);
        $accessToken = $oauthClient->      
           getAccessToken($accessTokenRequestUrl);
        $_SESSION['state'] = 2;
        $_SESSION['token'] = $accessToken['oauth_token'];
        $_SESSION['secret'] = $accessToken['oauth_token_secret'];
        header('Location: ' . $callbackUrl);
      exit;
     } else {
       $oauthClient->setToken($_SESSION['token'], 
       $_SESSION['secret']);
       $ruleId = "3";
       $couponGenerationData = array();
       $couponGenerationData['name'] = "ramesh";
       $couponGenerationData['email'] = "ramesh.Restapi421@test.com";
       $couponGenerationData['pwd'] = "test123";

/* Send Throught Post*/
$resourceUrl = $apiUrl."/coupondemo/{$ruleId}";
//$resourceUrl = "$apiUrl/coupondemo/rules/{$ruleId}/codes";
$oauthClient->fetch($resourceUrl, json_encode($couponGenerationData), OAUTH_HTTP_METHOD_POST, array(
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
));
$coupons = json_decode($oauthClient->getLastResponse(), true);
// Display the newly generated codes to demonstrate that the Coupon AutoGen API works
// In reality, you might put these codes in emails to customers, store them in a database, etc.
echo "New coupon codes:<br/>";
foreach ($coupons as $coupon) {
    echo " --> " . $coupon['code'] . "<br/>";
     }
    }
  } catch (OAuthException $e) {
  print_r($e->getMessage());
  echo "<br/>";
  print_r($e->lastResponse);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.