$value) { $key = str_replace("%7E", "~", rawurlencode($key)); $value = str_replace("%7E", "~", rawurlencode($value)); $queryParamsUrl[] = $key."=".$value; } // glue all the "params=value"'s with an ampersand $signature['queryUrl']= implode("&", $queryParamsUrl); // we'll use this string to make the signature $StringToSign = $signature['method']."\n".$signature['host']."\n".$signature['uri']."\n".$signature['queryUrl']; // make signature $signature['string'] = str_replace("%7E", "~", rawurlencode( base64_encode( hash_hmac("sha256",$StringToSign,$secret,True ) ) ) ); return $signature; } function getSignedUrl($params, $key, $secret, $assoc) { // This function creates a complete Amazon request with the appropriate signature $signature = generateSignature($params, $key, $secret, $assoc); return $signedUrl= "http://".$signature['host'].$signature['uri'].'?'.$signature['queryUrl'].'&Signature='.$signature['string']; } function load_form($message='') { global $NYT_API; global $server_path; //Display form to allow user to select NYT Bestseller List to look at $nyt_bestseller_lists = 'http://api.nytimes.com/svc/books/v2/lists/names.xml?api-key=' . $NYT_API ; // go get the XML $xml = simplexml_load_file($nyt_bestseller_lists); $html = '
'; $html = $html . '

Order New York Times Bestsellers Your Library Does Not Have

'; $html = $html . '
'; if (isset($message)) { $html .= $message; } $html .= '
'; $html .= '

'; $html .= '

'; $html .= ''; $html .= ''; $html .= '

'; $html .= '
'; $html .= ''; return $html; } function load_list($listname, $oclcSym, $message='') { global $wskey; global $NYT_API; global $AWSAccessKeyId; global $SecretAccessKey; global $AssocTag; global $server_path; // build request $listURL = 'http://api.nytimes.com/svc/books/v2/lists/' . $listname . '.xml'; $listURL = $listURL . '?api-key=' . $NYT_API ; // go get the XML $xml = simplexml_load_file($listURL); $bookCount = count($xml->xpath('//results/book')); if ($bookCount > 0) { $listname_display = $xml->results->book[0]->list_name; $library_name_request = 'http://www.worldcat.org/webservices/catalog/content/libraries?oclcsymbol=' . $oclcSym . '&wskey=' . $wskey; $library_record = simplexml_load_file($library_name_request); $institution = $library_record->holding->physicalLocation; $html = '
'; $html .= '

Books from the ' . $listname_display . ' list not owned by ' . $institution . '

'; $html .= '
'; if (isset($message)) { $html .= $message; } $html .= '
'; $html .= '
'; $html .= ''; $html .= ''; $html .= ''; $outputrow = 0; foreach($xml->xpath('//results/book') as $book ) { $field = simplexml_load_string($book->asXML()); $isbn = $field->isbns->isbn[0]->isbn10; if (strlen($isbn) < 10) { $isbn = $field->isbns->isbn[0]->isbn13; } $request = 'http://www.worldcat.org/webservices/catalog/content/libraries/isbn/' . $isbn . '?oclcsymbol=' . $oclcSym . '&wskey=' . $wskey ; $xml = simplexml_load_file($request); if (count($xml->xpath('//holding')) > 0 ) { $library_has = TRUE; } else { $library_has = FALSE; } if ($library_has == FALSE) { // Go get data from MARC record $xml_record_request = 'http://www.worldcat.org/webservices/catalog/content/isbn/' . $isbn . '?recordSchema=info%3Asrw%2Fschema%2F1%2Fdc&wskey=' . $wskey; $xml_record = simplexml_load_file($xml_record_request); $xml_record->registerXPathNamespace("dc", "http://purl.org/dc/elements/1.1/"); $xml_record->registerXPathNamespace("oclcterms", "http://purl.org/dc/elements/1.1/"); $xml_record->registerXPathNamespace("xsi", "http://purl.org/dc/elements/1.1/"); $oclcnumber = $xml_record->xpath('//oclcterms:recordIdentifier[not(@xsi:type)]'); $title = $xml_record->xpath('//dc:title'); if (strlen($isbn) == 10 or strlen($isbn) == 13) { // Go get price and availability from Amazon $amazon = new AmazonECS($AWSAccessKeyId, $SecretAccessKey, 'com', $AssocTag); $response = $amazon->responseGroup('Medium,Offers')->optionalParameters(array( 'IdType' => 'ISBN', 'MerchantId' => 'Amazon', 'SearchIndex' => 'Books', 'Condition' => 'New')) ->lookup($isbn); $aitem = $response->Items->Item; if( $aitem->Offers->TotalOffers > 1 ) { $availability = $aitem->Offers->Offers[0]->OfferListing->Availability; $price = number_format((double)$aitem->Offers->Offers[0]->OfferListing->Price->Amount * .01, 2); } elseif( $aitem->Offers->TotalOffers == 1 ) { $availability = $aitem->Offers->Offer->OfferListing->Availability; $price = number_format((double)$aitem->Offers->Offer->OfferListing->Price->Amount * .01, 2); } else { $availability = 'Item not available from Amazon'; $price = ''; } } else { $availability = 'Item not available from Amazon'; $price = ''; } $book = array(); $book['oclcnumber'] = "$oclcnumber[0]"; $book['title'] = "$title[0]"; $book['price'] = "$price"; $book['availability'] = "$price"; $rowstyles = ($outputrow++ % 2) ? 'dt-odd' : 'dt-even'; if ($outputrow == 1) $rowstyles .= ' dt-first'; $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; if (isset($oclcnumber)) { $html .= ''; $html .= ''; } else { $html .= ''; } $html .= ''; } } $html .= ''; $html .= '
Title
OCLC Number
Price
Availability
Purchase?
Copies
' . $title[0]. '
' . $oclcnumber[0] . '
' . $price . '
' . $availability . '
Sorry we do no have a record of this title in WorldCat.
'; $html .= ''; $html .= ''; $html .= ''; $html .= '

'; $html .= '
'; return $html; } } function load_order($id, $edit, $message=''){ global $AWSAccessKeyId; global $SecretAccessKey; global $server_path; $order = new Order(); $order->getById($id); if ( $order->hasPO() ) { // Show Order Details $order_date = new Zend_Date(substr($order->content->insertTime, 0, 10), Zend_Date::TIMESTAMP); //if (isset($xml->orderDate)) { // $submitted_date = new Zend_Date(strtotime($xml->orderDate), Zend_Date::TIMESTAMP); //} $vendor_name = $order->content->vendor->vendorName; $currency = $order->content->currency; $order_total = $order->content->totalOrderPrice; $status = $order->content->purchaseOrderState; $number_items = $order->content->orderItemCount; $html = ''; $html .= '
'; $html .= '

View Order

'; $html .= '
'; if ($order->getStatus() == 'SUBMITTED' and $edit) { $edit = FALSE; $message = $message . '

Sorry you cannot send this order to Amazon as it has already been submitted.'; $message = $message . '

Search for another order number.

'; } if (isset($message)) { $html .= $message; } $html .= '
'; $html .= '

Order Number: ' . $id . '

'; $html .= '

Order Date: ' . $order_date->toString(' mm/dd/yyyy') . '

'; if (isset($submitted_date)) { $html .= '

Order Submitted Date: ' . $submitted_date->toString(' mm/dd/yyyy hh:mm:ss') . '

'; } $html .= '

Vendor: ' . $vendor_name . '

'; if ($order_total > 0) { $html .= '

Order Total: ' . $order_total . ' ' . $currency . '

'; } $html .= '
'; // Build a table that shows the item title, oclc number, # of copies, price and availability from Amazon, remove from order button $html .= '
'; if ($edit) { $html .= '
'; $html .= ''; $html .= ''; $html .= ''; $html .= '
'; $html .= '
'; $html .= ''; } $html .= ''; $html .= ''; if ($edit) { $html .= ''; } else { $html .= ''; } $html .= ''; $html .= ''; $outputrow = 0; foreach($order->getItems() as $item) { $title = $item->getTitle(); $oclcNumber = $item->getOCLC(); $item_id = $item->getOI(); $copies = $item->getCopyCount(); if ($edit) { /* if (count($item->resources->isbns) > 1) { foreach($item->resources->isbns as $isbn) { if (strpos($isbn, " ") > 0) { $isbn = substr($isbn, 0, strpos($isbn, " ")); } // Look up the item in Amazon and find price and availability $amazon = new Zend_Service_Amazon($AWSAccessKeyId, 'US', $SecretAccessKey); $aitem = $amazon->itemLookup($isbn, array( 'IdType' => 'ISBN', 'MerchantId' => 'Amazon', 'SearchIndex' => 'Books', 'ResponseGroup' => 'Medium,Offers', 'Condition' => 'New' )); if (isset($aitem->Offers->Offers)) { break; } } } else { $isbn = $item->resources->isbns[0]; if (strpos($isbn, " ") > 0) { $isbn = substr($isbn, 0, strpos($isbn, " ")); } // Look up the item in Amazon and find price and availability $amazon = new Zend_Service_Amazon($AWSAccessKeyId, 'US', $SecretAccessKey); $aitem = $amazon->itemLookup($isbn, array( 'IdType' => 'ISBN', 'MerchantId' => 'Amazon', 'SearchIndex' => 'Books', 'ResponseGroup' => 'Medium,Offers', 'Condition' => 'New' )); } if (isset($aitem->Offers->Offers)) { $availability = $aitem->Offers->Offers[0]->Availability; $price = number_format((double)$aitem->Offers->Offers[0]->Price * .01, 2); $a_currency = $aitem->Offers->Offers[0]->CurrencyCode; } else { $availability = 'Item not available from Amazon'; $price = ''; } */ } else { $price = $item->getOrderingPrice(); } $rowstyles = ($outputrow++ % 2) ? 'dt-odd' : 'dt-even'; if ($outputrow == 1) $rowstyles .= ' dt-first'; $html .= ''; $html .= ''; if ($edit) { $html .= ''; } else { $html .= ''; } $html .= ''; } $html .= ''; $html .= '
Item #
Title
OCLC Number
Number of Copies
Availability
Price (each)
Remove?
Price (each)
' . $item_id . '
' . $title . '
' . $oclcNumber . '
' . $copies . '
' . $availability . '
' . $price . ' ' . $a_currency . '
' . $price . ' ' . $currency . '
'; if ($edit) { $html .= ''; $html .= ''; $html .= ''; $html .= '
'; $html .= '
'; $html .= ''; $html .= ''; $html .= ''; $html .= '
'; } $html .= '
'; } else { $html = "

There was an error connecting to the server, please try again!

"; } return $html; } function create_order($books){ $order_date = Zend_Date::now(); $order = new Order(); $order->content->vendor->vendorId = '640c99b4-0b62-4c6b-9346-846fc9156da9'; $order->content->orderName = 'Amazon Order of NYT Items - No Shelving Location - '.time(); $order->content->orderNumberRange = 'default'; $order->content->currency = 'USD'; $order->content->exchangeRate = 1; $order->create(); if ( $order->hasPO() ): foreach ($books as $book): $item = new Item(); $resource->oclcNumber = $book['oclcnumber']; $item->content->resources->resource = array(array("worldcatResource" => $resource)); $copyConfig->copyCount = $book['copyCount']; $copyConfig->branchId = BRANCH_ID; $copyConfig->shelvingLocationId = 'MAIN-STACKS'; $copyConfig->orderStatus = 'OPEN'; $copyConfig->purchaseStatus = 'NO'; $item->content->copyConfigs->copyConfig = array($copyConfig); $item->content->quantity = $book['copyCount']; $item->content->orderingPrice = $book['price']; $order->addItem($item); endforeach; endif; $order_info['order_id'] = $order->getPO(); return $order_info; } function update_order_status($id, $status){ global $order_service_url; $request = $order_service_url . $id; $client = new Zend_Http_Client(); $client->setUri($request); $client->setConfig(array( 'maxredirects' => 2, 'strictredirects' => true)); $client->setHeaders('Accept: application/json'); $response = $client->request(); // have the JSON need to change the field and pass back $order = Zend_Json::decode($response); $order['status'] = $status; $updated_order = Zend_Json::encode($order); $update_request = $order_service_url . $id; $client->setUri($update_request); $response = $client->setRawData($updated_order, 'application/json')->request('PUT'); // check to make sure change made if ($response->isSuccessful()) { $message = '

Order has been moved to a SUBMITTED status is WMS.

'; } else { $message = '

The status of this order could not be updated.

'; //Debugging message info - uncomment to see what the server is doing //$message = $message . '

URL Requested: ' . $order_create_url . '
Status: ' . $response->getStatus() . '
Message: ' . $response->getMessage() . '

'; } return $message; } function place_order($id) { global $AWSAccessKeyId; global $SecretAccessKey; global $AssocTag; $order = new Order(); $order->getById($id); if ($order->getStatus() != 'SUBMITTED') { $items = $order->getItems(); // Send to Amazon and update order status $params = array( "region"=>"com", "Operation"=>"CartCreate" ); $i = 1; // Eventually need to deal with the fact I should probably only send 10 items at a time foreach( $items as $item ) { $isbns = $item->getISBN(); $copies = $item->getCopyCount(); $amazon = new AmazonECS($AWSAccessKeyId, $SecretAccessKey, 'com', $AssocTag); if (is_array($isbns)) { foreach($isbns as $isbn) { $response = $amazon->responseGroup('Medium,Offers')->optionalParameters(array( 'IdType' => 'ISBN', 'MerchantId' => 'Amazon', 'SearchIndex' => 'Books', 'Condition' => 'New')) ->lookup($isbn); $aitems = $response->Items->Item; if( is_array($aitems) ): foreach($aitems AS $record): if( $aitem->Offers->TotalOffers > 0 ): $aitem = $record; break; endif; endforeach; else: if( $aitems->Offers->TotalOffers > 0 ): $aitem = $aitems; break; endif; endif; } } else { $isbn = $ibns; $response = $amazon->responseGroup('Medium,Offers')->optionalParameters(array( 'IdType' => 'ISBN', 'MerchantId' => 'Amazon', 'SearchIndex' => 'Books', 'Condition' => 'New')) ->lookup($isbn); $aitem = $response->Items->Item; } $params['Item.' . $i . '.ASIN'] = $aitem->ASIN; $params['Item.' . $i . '.Quantity'] = $copies; $i++ ; if( $aitem->Offers->TotalOffers > 1 ) { $price = number_format((double)$aitem->Offers->Offers[0]->OfferListing->Price->Amount * .01, 2); } elseif( $aitem->Offers->TotalOffers == 1 ) { $price = number_format((double)$aitem->Offers->Offer->OfferListing->Price->Amount * .01, 2); } $item->updatePrice($price); } $request = getSignedURL($params, $AWSAccessKeyId, $SecretAccessKey, $AssocTag); $xml = simplexml_load_file($request); if (count($xml->xpath('/ErrorResponse')) == 0) { $xml->registerXPathNamespace("az", "http://webservices.amazon.com/AWSECommerceService/2010-10-01"); if (count($xml->xpath('//az:Cart/az:Request/az:Errors')) > 0) { $message = $message . '

Sorry not all these items could be added to the cart.

'; // List items that couldn't be added to the cart $message = $message . '

The following items were not added to the cart:

'; $message = $message . ''; $message = $message . '

Please update your order then recreate the cart

'; // Reload the order list and allow user to edit load_order($order_id, true, $message); } else { $cart_url = $xml->Cart->PurchaseURL; $subtotal = $xml->Cart->SubTotal->FormattedPrice; $message = '

Amazon Cart Successfully Created

'; $message = $message . '

View your cart

'; $message = $message . '

Subtotal: ' . $subtotal . '

'; $html = load_order($id, false, $message); } } } else { // Create a error message for order submitted $message = '

Sorry this order has already been submitted. You cannot resubmit it.

'; $html = load_order($id, false, $message); } return $html; } if (isset($_REQUEST['step'])) { $step = $_REQUEST['step']; } else { $step = ''; } switch ($step) { case "show_nyt_lists": if ((strlen($_REQUEST['list-name']) > 0) and (strlen($_REQUEST['oclcSym']) > 0)) { $list_id = $_REQUEST['list-name']; $oclcSym = urlencode($_REQUEST['oclcSym']); // Load the order list $html = load_list($list_id, $oclcSym); } else { $message = "

Please make sure that you've selected a list and input and OCLC Symbol before searching for a list.

"; $html = load_form($message); } break; case "order": if (isset($_REQUEST['books'])) { // Need to parse the form data and create a books array $to_order = $_REQUEST['books']; $books = array(); foreach ($to_order as $book) { if (isset($_REQUEST[$book . '_copies'])) { $copies = $_REQUEST[$book . '_copies']; } else { $copies = "1"; } $price = $_REQUEST[$book . '_price']; $b = array(); $b['oclcnumber'] = $book; $b['copyCount'] = $copies; $b['price'] = $price; $books[] = $b; } $response = create_order($books); if (isset($response['order_id'])) { $order_id = $response['order_id']; $html = place_order($order_id); } else { $html = $response['message']; } } elseif (isset($_REQUEST['list-name']) and isset($_REQUEST['oclcSym'])) { $list_id = $_REQUEST['list-name']; $oclcSym = $_REQUEST['oclcSym']; $message = '

Please select at least one book you want to order.

'; $html = load_list($list_id, $oclcSym, $message); } else { $html = "

Error

"; } break; case "update": if (isset($_REQUEST['order_id']) and isset($_REQUEST['item_id'])) { $order = new Order(); $order->getById(''); $failed_ids = array(); foreach($_REQUEST['item_id'] as $item_id) { $item = new Item(); $item->getById($order, $item_id); if( !$item->delete() ): $failed_ids[] = $item_id; endif; } if (sizeof($failed_ids) >= 1) { // Build message that shows all ids that couldn't be removed $message = '

The following items on the order could not be removed: ' . implode(", ", $failed_ids). '

'; } else { $message = '

Items successfully removed from the order.

'; } // Reload the order list $html = load_order($order_id, true, $message); } else { $html = '

Error. You cannot update an order without order ID and item id information.

'; } break; default: //Display form to allow user to select NYT Bestseller List to look at $html = load_form(); } ?> Order New York Times Bestsellers