Advertisement
mahmudkuet

add item

Mar 14th, 2018
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.18 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Service\eBay;
  4.  
  5.  
  6. use App\Models\Store;
  7. use App\Service\Console;
  8. use App\Service\ListingDescService;
  9. use Illuminate\Http\Request;
  10. use Spatie\ArrayToXml\ArrayToXml;
  11.  
  12. class AddItemService extends EbayRequest
  13. {
  14.  
  15.     protected function getCallName()
  16.     {
  17.         return 'AddItem';
  18.     }
  19.  
  20.     public function addItem(Store $store, Request $request){
  21.         $specifics = $request->get('specifics');
  22.         $specificsArray = [];
  23.         foreach ($specifics as $specific){
  24.             $specificsArray['NameValueList'][] = [
  25.                 'Name'  =>  $specific['name'],
  26.                 'Value'  =>  $specific['values'],
  27.             ];
  28.         }
  29.  
  30.         $compatibilities = $request->get('compatibilities');
  31.         $compatibilitiesArray = [];
  32.         if($compatibilities){
  33.             $data = [];
  34.             foreach ($compatibilities as $compatibility){
  35.  
  36.                 $nameValue = [
  37.                     [
  38.                         'Name'  =>  'Year',
  39.                         'Value'  =>  $compatibility['year'],
  40.                     ],
  41.                     [
  42.                         'Name'  =>  'Make',
  43.                         'Value'  =>  $compatibility['make'],
  44.                     ],
  45.                     [
  46.                         'Name'  =>  'Model',
  47.                         'Value'  =>  $compatibility['model'],
  48.                     ],
  49.                 ];
  50.  
  51.                 if($compatibility['trim'] != 'All'){
  52.                     $nameValue[] = [
  53.                         'Name'  =>  'Trim',
  54.                         'Value'  =>  $compatibility['trim'],
  55.                     ];
  56.                 }
  57.  
  58.                 if($compatibility['engine'] != 'All'){
  59.                     $nameValue[] = [
  60.                         'Name'  =>  'Engine',
  61.                         'Value'  =>  $compatibility['engine'],
  62.                     ];
  63.                 }
  64.  
  65.                 $data[] = [
  66.                     'NameValueList' =>  $nameValue
  67.                 ];
  68.             }
  69.             $compatibilitiesArray['Type'] = 'NameValue';
  70.             $compatibilitiesArray['Compatibility'] = $data;
  71.         }
  72.  
  73.         $images = self::saveImagesToStorage($request->get('sku'), $request->get('images'), $request->get('listing_info'));
  74.  
  75.         $shippingServices = [];
  76.         $priority = 1;
  77.         foreach ($request->get('domestic_shipping_services') as $service){
  78.             if(array_key_exists('is_free', $service)){
  79.                 if($service['is_free'] == 'true'){
  80.                     $data = [
  81.                         'FreeShipping'  =>  'true',
  82.                         'ShippingService'   =>  $service['shipping_service'],
  83.                         'ShippingServicePriority'   =>  $priority,
  84.                     ];
  85.                     if($service['surcharge']){
  86.                         $data['ShippingSurcharge'] = (double)$service['surcharge'];
  87.                     }
  88.                     $shippingServices[] = $data;
  89.                 }else{
  90.  
  91.                     $data = [
  92.                         'FreeShipping'  =>  'false',
  93.                         'ShippingService'   =>  $service['shipping_service'],
  94.                         'ShippingServicePriority'   =>  $priority,
  95.                         'ShippingServiceCost'   =>  $service['cost'],
  96.                         'ShippingServiceAdditionalCost'   =>  (double)$service['additional_cost'],
  97.                     ];
  98.                     if($service['surcharge']){
  99.                         $data['ShippingSurcharge'] = (double)$service['surcharge'];
  100.                     }
  101.                     $shippingServices[] = $data;
  102.                 }
  103.             }else{
  104.                 $data = [
  105.                     'FreeShipping'  =>  'false',
  106.                     'ShippingService'   =>  $service['shipping_service'],
  107.                     'ShippingServicePriority'   =>  $priority,
  108.                     'ShippingServiceCost'   =>  $service['cost'],
  109.                     'ShippingServiceAdditionalCost'   =>  (double)$service['additional_cost'],
  110.                 ];
  111.                 if($service['surcharge']){
  112.                     $data['ShippingSurcharge'] = (double)$service['surcharge'];
  113.                 }
  114.                 $shippingServices[] = $data;
  115.             }
  116.  
  117.             $priority++;
  118.         }
  119.  
  120.         $listingDescService = new ListingDescService();
  121.  
  122.         $reqArray = [
  123.             'RequesterCredentials'  =>  [
  124.                 'eBayAuthToken' =>  $store->token
  125.             ],
  126.             'Item'  =>  [
  127.                 'AutoPay'   =>  $request->get('autopay'),
  128.                 'Currency'  =>  'USD',
  129.                 'Country'  =>  $request->get('country'),
  130.                 'Location'  =>  $request->get('location'),
  131.                 'PostalCode'    =>  $request->get('postal_code', '11106'),
  132.                 'DispatchTimeMax'  =>  $request->get('max_dispatch_time'),
  133.                 'ConditionID'  =>  $request->get('condition_id'),
  134.                 'Title'  =>  $request->get('title'),
  135.                 'SKU'  =>  $request->get('sku'),
  136.                 'PrimaryCategory'   =>  [
  137.                     'CategoryID'    =>  $request->get('primary_category_id')
  138.                 ],
  139.                 'Description'  =>  $listingDescService->prepareDescription($request, $images),
  140.                 'ProductListingDetails' =>  [
  141.                     'UPC'  =>  $request->get('upc'),
  142.                     'IncludeStockPhotoURL'  =>  'true',
  143.                     'UseStockPhotoURLAsGallery'  =>  'true',
  144.                     'ReturnSearchResultOnDuplicates'  =>  'true',
  145.                 ],
  146.                 'PictureDetails'    =>  [
  147.                     'GalleryType'   =>  'Gallery',
  148.                     'GalleryURL'  =>  $images[0],
  149.                     'PictureURL'  =>  $images,
  150.                 ],
  151.                 'ItemSpecifics' =>  $specificsArray,
  152.                 'ItemCompatibilityList' =>  $compatibilitiesArray,
  153.                 'ListingType'   =>  'FixedPriceItem',
  154.                 'ListingDuration'  =>  $request->get('duration'),
  155.                 'StartPrice'  =>  $request->get('price'),
  156.                 'Quantity'  =>  $request->get('quantity'),
  157.                 'PaymentMethods'  =>  $request->get('payment_method', 'PayPal'),
  158.                 'PayPalEmailAddress'  =>  $request->get('paypal_email'),
  159.                 'ReturnPolicy'  =>  [],
  160.                 'ShippingPackageDetails'    =>  [
  161.                     'PackageLength'  =>  $request->get('package_length'),
  162.                     'PackageWidth'  =>  $request->get('package_width'),
  163.                     'PackageDepth'  =>  $request->get('package_depth'),
  164.                     'ShippingPackage'  =>  $request->get('shipping_package_type'),
  165.                     'WeightMajor'  =>  $request->get('weight_major'),
  166.                     'WeightMinor'  =>  $request->get('weight_minor'),
  167.                 ],
  168.                 'ShippingDetails'   =>  [
  169.                     'ShippingType'  =>  'Flat',
  170.                     'ShippingServiceOptions'    =>  $shippingServices,
  171.                     'GlobalShipping'    =>  $request->get('global_shipping') == 'true' ? 'true' : 'false',
  172.                 ],
  173.                 'Site'  =>  'US',
  174.                 'UseTaxTable'  =>  $request->get('use_ebay_tax_table') == 'true' ? 'true' : 'false'
  175.             ]
  176.         ];
  177.  
  178.         if($request->get('return_option') == 'yes'){
  179.             $reqArray['Item']['ReturnPolicy'] = [
  180.                 'ReturnsAcceptedOption'  =>  'ReturnsAccepted',
  181.                 'RefundOption'  =>  $request->get('refund_option'),
  182.                 'ReturnsWithinOption'  =>  $request->get('returns_within'),
  183.                 'Description'  =>  $request->get('return_policy_desc', ''),
  184.                 'ShippingCostPaidByOption'  =>  $request->get('return_shipping_paid_by'),
  185.                 'RestockingFeeValueOption'  =>  $request->get('restocking_fee')
  186.             ];
  187.         }else{
  188.             $reqArray['Item']['ReturnPolicy'] = [
  189.                 'ReturnsAcceptedOption'  =>  'ReturnsNotAccepted',
  190.             ];
  191.         }
  192.  
  193.         if(count($request->get('excluded_shipping_location')) > 0){
  194.             $reqArray['Item']['ShippingDetails']['ExcludeShipToLocation'] = $request->get('excluded_shipping_location');
  195.         }
  196.  
  197.         if($request->get('store_category_id', 0) != 0){
  198.             $reqArray['Item']['Storefront']['StoreCategoryID'] = $request->get('store_category_id');
  199.         }
  200.         if($request->get('store_category2_id', 0) != 0){
  201.             $reqArray['Item']['Storefront']['StoreCategory2ID'] = $request->get('store_category2_id');
  202.         }
  203.  
  204.         $request_body = ArrayToXml::convert($reqArray, [
  205.             'rootElementName'   =>  'AddItemRequest',
  206.             '_attributes'   =>  [
  207.                 'xmlns' =>  'urn:ebay:apis:eBLBaseComponents'
  208.             ]
  209.         ], false, 'UTF-8');
  210.  
  211.         return $this->fetch($store, $request_body);
  212.     }
  213.  
  214.     public function saveImagesToStorage($sku, $images, $listing_info){
  215.         $links = [];
  216.         $sl = 1;
  217.         foreach ($images as $image){
  218.             $isExternalImage = self::isExternalImageLink($image);
  219.             if($isExternalImage){
  220.                 $links[] = self::storeExternalImage($image, $sl, $listing_info);
  221.             }else{
  222.                 $links[] = self::storeInternalImage($image, $sl, $listing_info);
  223.             }
  224.             $sl++;
  225.         }
  226.         return $links;
  227.     }
  228.  
  229.     private function isExternalImageLink($imageLink){
  230.         return ! preg_match('/'. preg_quote(url('/'), '/') .'\/storage\/.*/i', $imageLink);
  231.     }
  232.  
  233.     private function storeExternalImage($imageLink, $sl, $listing_info){
  234.  
  235.         $image = \Image::make($imageLink);
  236.         if($sl == 1){
  237.             $image = $this->addAdditionalImage($image, $listing_info);
  238.         }
  239.         $image = $this->addWatermark($image, $listing_info);
  240.         $mime = explode("/", $image->mime())[1];
  241.         $hash = md5($image->encode('data-url')->encoded);
  242.         $path = date('Y/m/') . $hash . '.' . $mime;
  243.         $image->save(storage_path("app/public/" . $path));
  244.         return $this->addOriginalImagePathAsQueryParam(url(\Storage::url($path)), $imageLink);
  245.     }
  246.  
  247.     public function storeInternalImage($imageLink, $sl, $listing_info){
  248.         preg_match_all("/.*\/storage(\/(\d{4})\/(\d{2})\/(.*)\..*)/", $imageLink, $matches);
  249.         $path = storage_path('app/public' . $matches[1][0]);
  250.         $image = \Image::make($path);
  251.         if($sl == 1){
  252.             $image = $this->addAdditionalImage($image, $listing_info);
  253.         }
  254.         $image = $this->addWatermark($image, $listing_info);
  255.         $path = "{$matches[2][0]}/{$matches[3][0]}/" . md5($matches[1][0] . time() . rand(1, 1000000)) . ".{$image->extension}";
  256.         $image->save(storage_path("app/public/" . $path));
  257.         return $this->addOriginalImagePathAsQueryParam(url(\Storage::url($path)), $imageLink);
  258.     }
  259.  
  260.     public function addOriginalImagePathAsQueryParam($url, $originalImageLink){
  261.         return addToURL('original', base64_encode($originalImageLink), $url, false);
  262.     }
  263.  
  264.     private function addAdditionalImage($image, $listingInfo){
  265.  
  266.         $image->resize(600, null, function ($constraint) {
  267.             $constraint->aspectRatio();
  268.         });
  269.  
  270.         $extras = [];
  271.  
  272.         if($listingInfo['is_capa_certified'] == 'true'){
  273.             $extras[] = [
  274.                 'image' =>  \Image::make(resource_path('assets/images/watermark/CAPA.png')),
  275.                 'position'  =>  'top-right'
  276.             ];
  277.         }
  278.         if($listingInfo['is_local_pickup'] == 'true'){
  279.             $extras[] = [
  280.                 'image' =>  \Image::make(resource_path('assets/images/watermark/local_pickup_bottom.png')),
  281.                 'position'  =>  'bottom'
  282.             ];
  283.             $extras[] = [
  284.                 'image' =>  \Image::make(resource_path('assets/images/watermark/local_pickup_top_left.png')),
  285.                 'position'  =>  'top-left'
  286.             ];
  287.         }
  288.         if($listingInfo['is_2_years_warranty'] == 'true'){
  289.             $extras[] = [
  290.                 'image' =>  \Image::make(resource_path('assets/images/watermark/2_years_warranty.png')),
  291.                 'position'  =>  'top-right'
  292.             ];
  293.         }
  294.  
  295.         foreach ($extras as $extra){
  296.             $image->insert($extra['image'], $extra['position']);
  297.         }
  298.  
  299.         return $image;
  300.     }
  301.  
  302.     public function addWatermark($image, $listingInfo){
  303.         $extras = [];
  304.  
  305.         if($listingInfo['store_short_code'] == 'APN'){
  306.             $extras[] = [
  307.                 'image' =>  \Image::make(resource_path('assets/images/watermark/APN.png')),
  308.                 'position'  =>  'center'
  309.             ];
  310.         }
  311.         if($listingInfo['store_short_code'] == 'PC'){
  312.             $extras[] = [
  313.                 'image' =>  \Image::make(resource_path('assets/images/watermark/PC.png')),
  314.                 'position'  =>  'center'
  315.             ];
  316.         }
  317.         if($listingInfo['store_short_code'] == 'FT'){
  318.             $extras[] = [
  319.                 'image' =>  \Image::make(resource_path('assets/images/watermark/FT.png')),
  320.                 'position'  =>  'center'
  321.             ];
  322.         }
  323.  
  324.         foreach ($extras as $extra){
  325.             $image->insert($extra['image'], $extra['position']);
  326.         }
  327.  
  328.         return $image;
  329.     }
  330.  
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement