config_loader = new Config_Loader(); } /** * Returns the item lookup URL for asins * * @param string $asin Asin value. * @param string $marketplaces Marketplace to search the products. * @param string $tracking_id Associate tag. * * @return string Signed URL for item lookup. */ function get_item_lookup_url( $asin_array, $marketplace, $tracking_id ) { $marketplace_endpoint = $this->get_marketplace_endpoint( $marketplace ); $asin = implode( ",", $asin_array ); $params = array( "Operation" => "ItemLookup", "ItemId" => "$asin", "IdType" => "ASIN", "ResponseGroup" => "Images,ItemAttributes,OfferFull", "AssociateTag" => "$tracking_id", ); $url = $this->aws_signed_url( $params, $marketplace_endpoint ); return $url; } /** * Returns signed URL for Paapi request * * @since 1.0.0 * * @param array $params Paapi parameters. * @param string $marketplace Marketplace to search the product. * * @return string Signed URL. */ function aws_signed_url( $params, $marketplace ) { $access_key_id = openssl_decrypt( base64_decode( get_option( Db_Constants::AWS_ACCESS_KEY ) ), Plugin_Constants::ENCRYPTION_ALGORITHM, Plugin_Constants::ENCRYPTION_KEY, 0, Plugin_Constants::ENCRYPTION_IV ); $secret_key = openssl_decrypt( base64_decode( get_option( Db_Constants::AWS_SECRET_KEY ) ), Plugin_Constants::ENCRYPTION_ALGORITHM, Plugin_Constants::ENCRYPTION_KEY, 0, Plugin_Constants::ENCRYPTION_IV ); $host = $marketplace; $method = 'GET'; $uri = Paapi_Constants::URI; $params["Service"] = Paapi_Constants::SERVICE; $params["AWSAccessKeyId"] = $access_key_id; $params["Timestamp"] = gmdate( 'Y-m-d\TH:i:s\Z' ); $params["Version"] = Paapi_Constants::VERSION; ksort( $params ); $canonicalized_query = array(); foreach ( $params as $param => $value ) { $param = str_replace( "%7E", "~", rawurlencode( $param ) ); $value = str_replace( "%7E", "~", rawurlencode( $value ) ); $canonicalized_query[] = $param . "=" . $value; } $canonicalized_query = implode( "&", $canonicalized_query ); $string_to_sign = $method . "\n" . $host . "\n" . $uri . "\n" . $canonicalized_query; $signature = base64_encode( hash_hmac( "sha256", $string_to_sign, $secret_key, true ) ); $signature = str_replace( "%7E", "~", rawurlencode( $signature ) ); $signed_url = Paapi_Constants::TRANSFER_PROTOCOL . $host . $uri . Paapi_Constants::URL_QUERY_SEPARATOR . $canonicalized_query . "&Signature=" . $signature; return $signed_url; } /** * Returns the item search URL for search keywords * * @param string $search_keywords Search keywords of the products. * @param string $marketplaces Marketplace to search the products. * @param string $tracking_id Associate tag. * * @return string Signed URL for item search. */ function get_item_search_url( $search_keywords, $marketplace, $tracking_id ) { $marketplace_endpoint = $this->get_marketplace_endpoint( $marketplace ); $params = array( "Operation" => "ItemSearch", "SearchIndex" => "All", "Keywords" => "$search_keywords", "ResponseGroup" => "Images,ItemAttributes,Offers", "AssociateTag" => "$tracking_id", ); $url = $this->aws_signed_url( $params, $marketplace_endpoint ); return $url; } /** * PA-API error messages to display in case of request errors * * @param string $error code Error code of the request. * * @return string PA-API error message. */ function get_error_message( $error ) { switch ( $error ) { case HTTP_Constants::BAD_REQUEST: /* translators: 1: URL of Associate sign-up page 2: _blank 3:URL of adding secondary user page 4: _blank */ return '

' . sprintf( __( "Your AWS Access Key Id is not registered as an Amazon Associate. Please verify that you are registered as an Amazon Associate in respective locale and you added the email address registered for the Product Advertising API as a secondary email address in your Amazon Associates account.", 'amazon-associates-link-builder' ), Plugin_Urls::ASSOCIATE_SIGN_UP_URL, Plugin_Urls::NEW_PAGE_TARGET, Plugin_Urls::ADDING_SECONDARY_USER_AC_URL, Plugin_Urls::NEW_PAGE_TARGET ) . '

'; case HTTP_Constants::FORBIDDEN: /* translators: 1: URL of PA-API sign-up page 2: _blank */ return '

' . sprintf( __( "Your AccessKey Id is not registered for Product Advertising API. Please sign up for Product Advertising API by following these guidelines.", 'amazon-associates-link-builder' ), Paapi_Constants::SIGN_UP_URL, Plugin_Urls::NEW_PAGE_TARGET ) . '

'; case HTTP_Constants::REQUEST_URI_TOO_LONG: return '

' . sprintf( __( "Your AccessKey Id is not registered for Product Advertising API. Please sign up for Product Advertising API by following these guidelines.", 'amazon-associates-link-builder' ), Paapi_Constants::SIGN_UP_URL, Plugin_Urls::NEW_PAGE_TARGET ) . '

'; case HTTP_Constants::INTERNAL_SERVER_ERROR: return '

' . esc_html__( "Internal server error", 'amazon-associates-link-builder' ) . '

'; case HTTP_Constants::THROTTLE: /* translators: 1: URL of PA-API efficiency guidelines page 2: _blank */ return '

' . sprintf( __( "You are submitting requests too quickly. Please retry your requests at a slower rate. For more information, see Efficiency Guidelines.", 'amazon-associates-link-builder' ), Paapi_Constants::EFFICIENCY_GUIDELINES_URL, Plugin_Urls::NEW_PAGE_TARGET ) . '

'; case HTTP_Constants::TIME_OUT: /* translators: %s: Email-id of the support */ return '

' . sprintf( __( "Request timed out. Try again after some time. Please check you network and firewall settings. If the error still persists, write to us at %s.", 'amazon-associates-link-builder' ), Plugin_Constants::SUPPORT_EMAIL_ID ) . '

'; default: /** *

tag ensures that the message is treated as HTML element in jQuery.find in aalb_admin.js. * Otherwise due to the error message string's characters like "!,:,etc", string is parsed as * if it contains partial css classes and later given syntax error */ return '

' . $error . '

'; } } /** * Get marketplace endpoint for marketplace abbreviation * * @since 1.8.0 * * @param string $marketplace_abbr Marketplace Abbreviation from shortcode * * @return string $marketplace_endpoint Marketplace endpoint */ private function get_marketplace_endpoint( $marketplace_abbr ) { $aalb_marketplace_names = $this->config_loader->fetch_marketplaces(); $marketplace_endpoint = array_search( $marketplace_abbr, $aalb_marketplace_names ); return $marketplace_endpoint; } /** * Returns default store_id for given marketplace. * * @param $marketplace * * @return string store_id for given marketplace */ public function get_store_id_for_marketplace( $marketplace ){ try{ $store_ids_list = json_decode( get_option( Db_Constants::STORE_IDS )); $store_ids = $store_ids_list->{$marketplace}; $store_id = $store_ids[0]; } catch( \Exception $e ){ error_log("No store_id found for marketplace {$marketplace}"); $store_id = get_option( Db_Constants::DEFAULT_STORE_ID ); } return $store_id; } } ?>