public function createOrder($cwProductId, $item_qty) { $preOrdersPerItem = 0; $cwProduct = \CodesWholesale\Resource\Product::get($cwProductId); $codes = \CodesWholesale\Resource\Order::createBatchOrder($cwProduct, array('quantity' => $item_qty)); foreach ($codes as $code) { if ($code->isPreOrder()) { $preOrdersPerItem++; } $links[] = $code->getHref(); } $createdOrderArray = array('counted_pre_orders' => $preOrdersPerItem, 'links' => $links, 'codes' => $codes); return $createdOrderArray; }
public function orderProduct($productId, $qty = 1) { $client = $this->connectToCw(); $product = $this->getProductById($productId); //example $url = "https://sandbox.codeswholesale.com/v1/products/ffe2274d-5469-4b0f-b57b-f8d21b09c24c"; $url = "https://sandbox.codeswholesale.com/v1/products/04aeaf1e-f7b5-4ba9-ba19-91003a04db0a"; $url = "https://sandbox.codeswholesale.com/v1/products/6313677f-5219-47e4-a067-7401f55c5a3a"; //$product = \CodesWholesale\Resource\Product::get($url); try { $codes = \CodesWholesale\Resource\Order::createBatchOrder($product, array('quantity' => $qty)); return $codes; } catch (\CodesWholesale\Resource\ResourceError $e) { //Mage::log($e,null,"except.log"); Mage::helper("kontentaCw")->sentErrorEmail($e, $product, $qty); /*if($e->getStatus() == 400 && $e->getErrorCode() == 10002) { // send email // log it to database echo $e->getMessage(); } else // handle scenario when code details where not found if($e->getStatus() == 404 && $e->getErrorCode() == 50002) { // error when image binary data cannot be found within additional request // after order is made this shouldn't occurred echo $e->getMessage(); } else // handle scenario when product was not found in price list if($e->getStatus() == 404 && $e->getErrorCode() == 20001) { // error can occurred when you present e.g. some old products that are now excluded from price list. // redirect user to some error page } else // handle when quantity was less then 1 if($e->getStatus() == 400 && $e->getErrorCode() == 40002) { // the input data was not validated on your side and passed to CWS // quantity can't be <= 0 echo $e->getMessage(); } else { // handle general app error // Log it to database, and give us a shout if it's our false at devteam@codeswholesale.com echo $e->getCode(); echo $e->getErrorCode(); echo $e->getMoreInfo(); echo $e->getDeveloperMessage(); echo $e->getMessage(); }*/ return null; } }
public function purchase($cwProductId, $qty) { $cwProduct = \CodesWholesale\Resource\Product::get($cwProductId); $codes = \CodesWholesale\Resource\Order::createBatchOrder($cwProduct, array('quantity' => $qty)); $cwOrderId = $codes->getOrderId(); $preOrders = 0; $links = array(); foreach ($codes as $code) { if ($code->isPreOrder()) { $preOrders++; } $links[] = $code->getHref(); } $orderedCodesArray = array('cwOrderId' => $cwOrderId, 'links' => $links, 'preOrders' => $preOrders); return $orderedCodesArray; }
public function process() { $request = file_get_contents('php://input'); if (empty($request)) { die("No request data"); } try { $productOrdered = $this->connection->receiveProductOrdered(); $allCodesFromProduct = \CodesWholesale\Resource\Order::getCodes($productOrdered); $orderId = $productOrdered->getOrderId(); $item = $this->itemRetriever->retrieveItem($orderId); $params = array('item' => $item, 'allCodesFromProduct' => $allCodesFromProduct); $newKeys = $this->newKeysExtractor->extract($params); $this->eventDispatcher->dispatchEvent($newKeys); } catch (\Exception $e) { die('We found error. Probably this is the result of sending test POSTBACK. If your response status is: 200 OK it means that you are successfully connected. Error: ' . $e->getMessage()); } }
const SHOULD_FLUSH = 0; function flushToFile($buffer) { file_put_contents("generated_file.txt", $buffer); } if (SHOULD_FLUSH) { ob_start("flushToFile"); } $clientBuilder = new \CodesWholesale\ClientBuilder($params); $client = $clientBuilder->build(); try { // method will parse request and extract parameters // get order id, and id of ordered product from request params $productOrdered = $client->receiveProductOrdered(); // ask for bought codes $allCodesFromProduct = \CodesWholesale\Resource\Order::getCodes($productOrdered); /** * Go through bought codes */ foreach ($allCodesFromProduct as $code) { /** * There are 3 possible code types returned from CW. * * Pre Order (when codes are not in stock) */ if ($code->isPreOrder()) { // check if still some codes are in pre-order state echo "Pre-order <br />"; } /** * Code as a TEXT
* \CodesWholesale\Resource\Product::get($url); * */ /** * Sample products */ // $url = "https://sandbox.codeswholesale.com/v1/products/33e3e81d-2b78-475a-8886-9848116f5133"; // - pre order product // $url = "https://sandbox.codeswholesale.com/v1/products/04aeaf1e-f7b5-4ba9-ba19-91003a04db0a"; // - not enough balance // $url = "https://sandbox.codeswholesale.com/v1/products/6313677f-5219-47e4-a067-7401f55c5a3a"; // - image code $url = "https://sandbox.codeswholesale.com/v1/products/ffe2274d-5469-4b0f-b57b-f8d21b09c24c"; // - code text $product = \CodesWholesale\Resource\Product::get($url); /** * Make an order for this particular product for 15s codes */ $codes = \CodesWholesale\Resource\Order::createBatchOrder($product, array('quantity' => 10)); /** * Go through bought codes */ foreach ($codes as $code) { /** * There are 3 possible code types returned from CW. * * Pre Order (when codes are not in stock) */ if ($code->isPreOrder()) { // nothing much to do with PreOrdered code - we are working on Post Back functionality, // CW will send you a post back information // once the code is added to your order, post back will be send directly to your website. // For now you can send an notification email echo "Pre-order <br />";
* \CodesWholesale\Resource\Product::get($url); * */ /** * Sample products */ // $url = "https://sandbox.codeswholesale.com/v1/products/33e3e81d-2b78-475a-8886-9848116f5133"; // - pre order product // $url = "https://sandbox.codeswholesale.com/v1/products/04aeaf1e-f7b5-4ba9-ba19-91003a04db0a"; // - not enough balance // $url = "https://sandbox.codeswholesale.com/v1/products/6313677f-5219-47e4-a067-7401f55c5a3a"; // - image code $url = "https://sandbox.codeswholesale.com/v1/products/ffe2274d-5469-4b0f-b57b-f8d21b09c24c"; // - code text $product = \CodesWholesale\Resource\Product::get($url); /** * Make an order for this particular product */ $code = \CodesWholesale\Resource\Order::createOrder($product); /** * There are 3 possible code types returned from CW. * * Pre Order (when codes are not in stock) */ if ($code->isPreOrder()) { // nothing much to do with PreOrdered code - we are working on Post Back functionality, // CW will send you a post back information // once the code is added to your order, post back will be send directly to your website. // For now you can send an notification email echo "Pre-order"; } /** * Code as a TEXT */