Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Service\eBay;
- use App\Models\Store;
- use App\Service\Console;
- use App\Service\ListingDescService;
- use Illuminate\Http\Request;
- use Spatie\ArrayToXml\ArrayToXml;
- class AddItemService extends EbayRequest
- {
- protected function getCallName()
- {
- return 'AddItem';
- }
- public function addItem(Store $store, Request $request){
- $specifics = $request->get('specifics');
- $specificsArray = [];
- foreach ($specifics as $specific){
- $specificsArray['NameValueList'][] = [
- 'Name' => $specific['name'],
- 'Value' => $specific['values'],
- ];
- }
- $compatibilities = $request->get('compatibilities');
- $compatibilitiesArray = [];
- if($compatibilities){
- $data = [];
- foreach ($compatibilities as $compatibility){
- $nameValue = [
- [
- 'Name' => 'Year',
- 'Value' => $compatibility['year'],
- ],
- [
- 'Name' => 'Make',
- 'Value' => $compatibility['make'],
- ],
- [
- 'Name' => 'Model',
- 'Value' => $compatibility['model'],
- ],
- ];
- if($compatibility['trim'] != 'All'){
- $nameValue[] = [
- 'Name' => 'Trim',
- 'Value' => $compatibility['trim'],
- ];
- }
- if($compatibility['engine'] != 'All'){
- $nameValue[] = [
- 'Name' => 'Engine',
- 'Value' => $compatibility['engine'],
- ];
- }
- $data[] = [
- 'NameValueList' => $nameValue
- ];
- }
- $compatibilitiesArray['Type'] = 'NameValue';
- $compatibilitiesArray['Compatibility'] = $data;
- }
- $images = self::saveImagesToStorage($request->get('sku'), $request->get('images'), $request->get('listing_info'));
- $shippingServices = [];
- $priority = 1;
- foreach ($request->get('domestic_shipping_services') as $service){
- if(array_key_exists('is_free', $service)){
- if($service['is_free'] == 'true'){
- $data = [
- 'FreeShipping' => 'true',
- 'ShippingService' => $service['shipping_service'],
- 'ShippingServicePriority' => $priority,
- ];
- if($service['surcharge']){
- $data['ShippingSurcharge'] = (double)$service['surcharge'];
- }
- $shippingServices[] = $data;
- }else{
- $data = [
- 'FreeShipping' => 'false',
- 'ShippingService' => $service['shipping_service'],
- 'ShippingServicePriority' => $priority,
- 'ShippingServiceCost' => $service['cost'],
- 'ShippingServiceAdditionalCost' => (double)$service['additional_cost'],
- ];
- if($service['surcharge']){
- $data['ShippingSurcharge'] = (double)$service['surcharge'];
- }
- $shippingServices[] = $data;
- }
- }else{
- $data = [
- 'FreeShipping' => 'false',
- 'ShippingService' => $service['shipping_service'],
- 'ShippingServicePriority' => $priority,
- 'ShippingServiceCost' => $service['cost'],
- 'ShippingServiceAdditionalCost' => (double)$service['additional_cost'],
- ];
- if($service['surcharge']){
- $data['ShippingSurcharge'] = (double)$service['surcharge'];
- }
- $shippingServices[] = $data;
- }
- $priority++;
- }
- $listingDescService = new ListingDescService();
- $reqArray = [
- 'RequesterCredentials' => [
- 'eBayAuthToken' => $store->token
- ],
- 'Item' => [
- 'AutoPay' => $request->get('autopay'),
- 'Currency' => 'USD',
- 'Country' => $request->get('country'),
- 'Location' => $request->get('location'),
- 'PostalCode' => $request->get('postal_code', '11106'),
- 'DispatchTimeMax' => $request->get('max_dispatch_time'),
- 'ConditionID' => $request->get('condition_id'),
- 'Title' => $request->get('title'),
- 'SKU' => $request->get('sku'),
- 'PrimaryCategory' => [
- 'CategoryID' => $request->get('primary_category_id')
- ],
- 'Description' => $listingDescService->prepareDescription($request, $images),
- 'ProductListingDetails' => [
- 'UPC' => $request->get('upc'),
- 'IncludeStockPhotoURL' => 'true',
- 'UseStockPhotoURLAsGallery' => 'true',
- 'ReturnSearchResultOnDuplicates' => 'true',
- ],
- 'PictureDetails' => [
- 'GalleryType' => 'Gallery',
- 'GalleryURL' => $images[0],
- 'PictureURL' => $images,
- ],
- 'ItemSpecifics' => $specificsArray,
- 'ItemCompatibilityList' => $compatibilitiesArray,
- 'ListingType' => 'FixedPriceItem',
- 'ListingDuration' => $request->get('duration'),
- 'StartPrice' => $request->get('price'),
- 'Quantity' => $request->get('quantity'),
- 'PaymentMethods' => $request->get('payment_method', 'PayPal'),
- 'PayPalEmailAddress' => $request->get('paypal_email'),
- 'ReturnPolicy' => [],
- 'ShippingPackageDetails' => [
- 'PackageLength' => $request->get('package_length'),
- 'PackageWidth' => $request->get('package_width'),
- 'PackageDepth' => $request->get('package_depth'),
- 'ShippingPackage' => $request->get('shipping_package_type'),
- 'WeightMajor' => $request->get('weight_major'),
- 'WeightMinor' => $request->get('weight_minor'),
- ],
- 'ShippingDetails' => [
- 'ShippingType' => 'Flat',
- 'ShippingServiceOptions' => $shippingServices,
- 'GlobalShipping' => $request->get('global_shipping') == 'true' ? 'true' : 'false',
- ],
- 'Site' => 'US',
- 'UseTaxTable' => $request->get('use_ebay_tax_table') == 'true' ? 'true' : 'false'
- ]
- ];
- if($request->get('return_option') == 'yes'){
- $reqArray['Item']['ReturnPolicy'] = [
- 'ReturnsAcceptedOption' => 'ReturnsAccepted',
- 'RefundOption' => $request->get('refund_option'),
- 'ReturnsWithinOption' => $request->get('returns_within'),
- 'Description' => $request->get('return_policy_desc', ''),
- 'ShippingCostPaidByOption' => $request->get('return_shipping_paid_by'),
- 'RestockingFeeValueOption' => $request->get('restocking_fee')
- ];
- }else{
- $reqArray['Item']['ReturnPolicy'] = [
- 'ReturnsAcceptedOption' => 'ReturnsNotAccepted',
- ];
- }
- if(count($request->get('excluded_shipping_location')) > 0){
- $reqArray['Item']['ShippingDetails']['ExcludeShipToLocation'] = $request->get('excluded_shipping_location');
- }
- if($request->get('store_category_id', 0) != 0){
- $reqArray['Item']['Storefront']['StoreCategoryID'] = $request->get('store_category_id');
- }
- if($request->get('store_category2_id', 0) != 0){
- $reqArray['Item']['Storefront']['StoreCategory2ID'] = $request->get('store_category2_id');
- }
- $request_body = ArrayToXml::convert($reqArray, [
- 'rootElementName' => 'AddItemRequest',
- '_attributes' => [
- 'xmlns' => 'urn:ebay:apis:eBLBaseComponents'
- ]
- ], false, 'UTF-8');
- return $this->fetch($store, $request_body);
- }
- public function saveImagesToStorage($sku, $images, $listing_info){
- $links = [];
- $sl = 1;
- foreach ($images as $image){
- $isExternalImage = self::isExternalImageLink($image);
- if($isExternalImage){
- $links[] = self::storeExternalImage($image, $sl, $listing_info);
- }else{
- $links[] = self::storeInternalImage($image, $sl, $listing_info);
- }
- $sl++;
- }
- return $links;
- }
- private function isExternalImageLink($imageLink){
- return ! preg_match('/'. preg_quote(url('/'), '/') .'\/storage\/.*/i', $imageLink);
- }
- private function storeExternalImage($imageLink, $sl, $listing_info){
- $image = \Image::make($imageLink);
- if($sl == 1){
- $image = $this->addAdditionalImage($image, $listing_info);
- }
- $image = $this->addWatermark($image, $listing_info);
- $mime = explode("/", $image->mime())[1];
- $hash = md5($image->encode('data-url')->encoded);
- $path = date('Y/m/') . $hash . '.' . $mime;
- $image->save(storage_path("app/public/" . $path));
- return $this->addOriginalImagePathAsQueryParam(url(\Storage::url($path)), $imageLink);
- }
- public function storeInternalImage($imageLink, $sl, $listing_info){
- preg_match_all("/.*\/storage(\/(\d{4})\/(\d{2})\/(.*)\..*)/", $imageLink, $matches);
- $path = storage_path('app/public' . $matches[1][0]);
- $image = \Image::make($path);
- if($sl == 1){
- $image = $this->addAdditionalImage($image, $listing_info);
- }
- $image = $this->addWatermark($image, $listing_info);
- $path = "{$matches[2][0]}/{$matches[3][0]}/" . md5($matches[1][0] . time() . rand(1, 1000000)) . ".{$image->extension}";
- $image->save(storage_path("app/public/" . $path));
- return $this->addOriginalImagePathAsQueryParam(url(\Storage::url($path)), $imageLink);
- }
- public function addOriginalImagePathAsQueryParam($url, $originalImageLink){
- return addToURL('original', base64_encode($originalImageLink), $url, false);
- }
- private function addAdditionalImage($image, $listingInfo){
- $image->resize(600, null, function ($constraint) {
- $constraint->aspectRatio();
- });
- $extras = [];
- if($listingInfo['is_capa_certified'] == 'true'){
- $extras[] = [
- 'image' => \Image::make(resource_path('assets/images/watermark/CAPA.png')),
- 'position' => 'top-right'
- ];
- }
- if($listingInfo['is_local_pickup'] == 'true'){
- $extras[] = [
- 'image' => \Image::make(resource_path('assets/images/watermark/local_pickup_bottom.png')),
- 'position' => 'bottom'
- ];
- $extras[] = [
- 'image' => \Image::make(resource_path('assets/images/watermark/local_pickup_top_left.png')),
- 'position' => 'top-left'
- ];
- }
- if($listingInfo['is_2_years_warranty'] == 'true'){
- $extras[] = [
- 'image' => \Image::make(resource_path('assets/images/watermark/2_years_warranty.png')),
- 'position' => 'top-right'
- ];
- }
- foreach ($extras as $extra){
- $image->insert($extra['image'], $extra['position']);
- }
- return $image;
- }
- public function addWatermark($image, $listingInfo){
- $extras = [];
- if($listingInfo['store_short_code'] == 'APN'){
- $extras[] = [
- 'image' => \Image::make(resource_path('assets/images/watermark/APN.png')),
- 'position' => 'center'
- ];
- }
- if($listingInfo['store_short_code'] == 'PC'){
- $extras[] = [
- 'image' => \Image::make(resource_path('assets/images/watermark/PC.png')),
- 'position' => 'center'
- ];
- }
- if($listingInfo['store_short_code'] == 'FT'){
- $extras[] = [
- 'image' => \Image::make(resource_path('assets/images/watermark/FT.png')),
- 'position' => 'center'
- ];
- }
- foreach ($extras as $extra){
- $image->insert($extra['image'], $extra['position']);
- }
- return $image;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement